Normalizing trailing /. in paths.

This commit is contained in:
Alex Shinn 2014-02-10 21:44:40 +09:00
parent be778009e2
commit 5c00c4a8df
2 changed files with 20 additions and 16 deletions

View file

@ -153,25 +153,27 @@
(boundary i (+ j 1) res) (boundary i (+ j 1) res)
(inside i (+ j 1) res)))) (inside i (+ j 1) res))))
(define (boundary i j res) (define (boundary i j res)
(if (>= j len-1) (if (>= j len)
(finish i res) (finish i res)
(case (string-ref path j) (case (string-ref path j)
((#\.) ((#\.)
(case (string-ref path (+ j 1)) (cond
((#\.) ((or (= j len-1) (eqv? #\/ (string-ref path (+ j 1))))
(if (or (>= j (- len 2)) (eqv? #\/ (string-ref path (+ j 2)))) (if (= i j)
(if (>= i (- j 1)) (boundary (+ j 2) (+ j 2) res)
(if (null? res) (let ((s (substring path i j)))
(backup j "" '()) (boundary (+ j 2) (+ j 2) (cons s res)))))
(backup j (car res) (cdr res))) ((eqv? #\. (string-ref path (+ j 1)))
(backup j (substring path i j) res)) (if (or (>= j (- len 2))
(inside i (+ j 2) res))) (eqv? #\/ (string-ref path (+ j 2))))
((#\/) (if (>= i (- j 1))
(if (= i j) (if (null? res)
(boundary (+ j 2) (+ j 2) res) (backup j "" '())
(let ((s (substring path i j))) (backup j (car res) (cdr res)))
(boundary (+ j 2) (+ j 2) (cons s res))))) (backup j (substring path i j) res))
(else (inside i (+ j 1) res)))) (inside i (+ j 2) res)))
(else
(inside i (+ j 1) res))))
((#\/) (boundary (+ j 1) (+ j 1) (collect i j res))) ((#\/) (boundary (+ j 1) (+ j 1) (collect i j res)))
(else (inside i (+ j 1) res))))) (else (inside i (+ j 1) res)))))
(define (backup j s res) (define (backup j s res)

View file

@ -162,10 +162,12 @@
(test "/a/c/e" (path-normalize "/a/b//..///c//d/../e")) (test "/a/c/e" (path-normalize "/a/b//..///c//d/../e"))
(test "/a/b/c/d/e/" (test "/a/b/c/d/e/"
(path-normalize "/a/b//./../c/d/../../b//c/d/e/f/..")) (path-normalize "/a/b//./../c/d/../../b//c/d/e/f/.."))
(test "/a/b/c/" (path-normalize "/a/b/c/."))
(test "path-normalize:border" "" (path-normalize "")) (test "path-normalize:border" "" (path-normalize ""))
(test "." (path-normalize ".")) (test "." (path-normalize "."))
(test "/" (path-normalize "/")) (test "/" (path-normalize "/"))
(test "/" (path-normalize "/."))
(test "path-normalize:overflow" (test "path-normalize:overflow"
"/" (path-normalize "/a/b/c/../../../../..")) "/" (path-normalize "/a/b/c/../../../../.."))