From 5c00c4a8df5e70b7db950e64f4bafffa4a9f58ef Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Mon, 10 Feb 2014 21:44:40 +0900 Subject: [PATCH] Normalizing trailing /. in paths. --- lib/chibi/pathname.scm | 34 ++++++++++++++++++---------------- tests/path-tests.scm | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/chibi/pathname.scm b/lib/chibi/pathname.scm index 7703c586..7da032f7 100644 --- a/lib/chibi/pathname.scm +++ b/lib/chibi/pathname.scm @@ -153,25 +153,27 @@ (boundary i (+ j 1) res) (inside i (+ j 1) res)))) (define (boundary i j res) - (if (>= j len-1) + (if (>= j len) (finish i res) (case (string-ref path j) ((#\.) - (case (string-ref path (+ j 1)) - ((#\.) - (if (or (>= j (- len 2)) (eqv? #\/ (string-ref path (+ j 2)))) - (if (>= i (- j 1)) - (if (null? res) - (backup j "" '()) - (backup j (car res) (cdr res))) - (backup j (substring path i j) res)) - (inside i (+ j 2) res))) - ((#\/) - (if (= i j) - (boundary (+ j 2) (+ j 2) res) - (let ((s (substring path i j))) - (boundary (+ j 2) (+ j 2) (cons s res))))) - (else (inside i (+ j 1) res)))) + (cond + ((or (= j len-1) (eqv? #\/ (string-ref path (+ j 1)))) + (if (= i j) + (boundary (+ j 2) (+ j 2) res) + (let ((s (substring path i j))) + (boundary (+ j 2) (+ j 2) (cons s res))))) + ((eqv? #\. (string-ref path (+ j 1))) + (if (or (>= j (- len 2)) + (eqv? #\/ (string-ref path (+ j 2)))) + (if (>= i (- j 1)) + (if (null? res) + (backup j "" '()) + (backup j (car res) (cdr res))) + (backup j (substring path i j) res)) + (inside i (+ j 2) res))) + (else + (inside i (+ j 1) res)))) ((#\/) (boundary (+ j 1) (+ j 1) (collect i j res))) (else (inside i (+ j 1) res))))) (define (backup j s res) diff --git a/tests/path-tests.scm b/tests/path-tests.scm index 182f8702..25b98961 100644 --- a/tests/path-tests.scm +++ b/tests/path-tests.scm @@ -162,10 +162,12 @@ (test "/a/c/e" (path-normalize "/a/b//..///c//d/../e")) (test "/a/b/c/d/e/" (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 ".")) (test "/" (path-normalize "/")) +(test "/" (path-normalize "/.")) (test "path-normalize:overflow" "/" (path-normalize "/a/b/c/../../../../.."))