Paste: Remove ".." in path

Author: Sam
Mode: factor
Date: Mon, 25 May 2009 12:28:29
Plain Text |
USING: kernel sequences splitting ;

: remove-.. ( seq -- newseq )
    dup empty?
    [ unclip dup ".." =
      [
          drop dup empty? [ drop { ".." } ] [ rest remove-.. ] if
      ]
      [
          [ remove-.. ] dip prefix
      ]	if
    ] unless ;

: simplify ( path -- newpath )
    "/" split reverse
    remove-.. reverse "/" join ;

Annotation: Note

Author: Sam
Mode: text
Date: Mon, 25 May 2009 12:30:04
Plain Text |
This handles ".." at the beginning of the path, but mishandles "../.." at the beginning, so needs to be reworked.

Annotation: cleaner

Author: randy7
Mode: factor
Date: Sat, 30 May 2009 20:19:00
Plain Text |
hi Sam, what about something like this:

"/path/to/../from/./your/code/to/mine"
"/" split harvest
V{ } clone tuck dup '[
dup "." = [ drop ] [
dup ".." = [ drop _ pop* ] [ _ push ] if  ] if
] each 

=> V{ "path" "from" "your" "code" "to" "mine" }

(ofcourse later:   "/" join "/" prepend  )

I tried to make it into a cond, but it didn't work for some reason.
another way is to filter out '.' later.
( [ CHAR: . = not ] filter )

New Annotation

Summary:
Author:
Mode:
Body: