diff options
author | Wojtek Kosior <koszko@koszko.org> | 2024-01-26 11:30:32 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2024-01-26 11:30:32 +0100 |
commit | 4343f40aa77904ff26a5425ed41211d94573002a (patch) | |
tree | d0946211cedb6f6194cef8f5b5ba7e1ec85032a5 /src/guile | |
parent | 62f82cd792753aa5b6cd9dce9a0e723e60bbd9fd (diff) | |
download | cantius-4343f40aa77904ff26a5425ed41211d94573002a.tar.gz cantius-4343f40aa77904ff26a5425ed41211d94573002a.zip |
Add `normalize-path` function.
Diffstat (limited to 'src/guile')
-rw-r--r-- | src/guile/cantius.scm | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/guile/cantius.scm b/src/guile/cantius.scm index 601427e..648a5a2 100644 --- a/src/guile/cantius.scm +++ b/src/guile/cantius.scm @@ -41,6 +41,31 @@ (s35:define-condition-type &forbidden s35:&condition forbidden-condition?) +(define-public (normalize-path path) + (define absolute? + (eqv? #\/ (string-ref path 0))) + + (let loop ((parent-walks 0) + (processed '()) + (to-process (string-split path #\/))) + (match (list parent-walks processed to-process) + ((0 () ()) + (if absolute? "/" ".")) + ((_ _ ()) + (string-join (append (map (const "..") (iota parent-walks)) + (reverse processed)) + "/" + (if absolute? 'prefix 'infix))) + ((_ _ ((? (cut member <> '("" "."))) + . rest)) + (loop parent-walks processed rest)) + ((_ () (".." . rest)) + (loop (1+ parent-walks) '() rest)) + ((_ (_ . processed-rest) (".." . rest)) + (loop parent-walks processed-rest rest)) + ((_ _ (segment . rest)) + (loop parent-walks (cons segment processed) rest))))) + (export find-resource-file) (define* (find-resource-file file #:optional (root-path (%resource-root-path))) (unless (legal-path? file) |