From 20063a926b0b4daf96c06b31b11e5d028225e6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 1 Feb 2022 21:32:51 +0100 Subject: Add path-split. --- module/hnh/util/path.scm | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'module/hnh') diff --git a/module/hnh/util/path.scm b/module/hnh/util/path.scm index 41b2e1fc..49338d01 100644 --- a/module/hnh/util/path.scm +++ b/module/hnh/util/path.scm @@ -20,3 +20,24 @@ (cdr strings))) (define-public (path-join lst) (apply path-append lst)) + +;; @example +;; (path-split "usr/lib/test") +;; ⇒ ("usr" "lib" "test") +;; (path-split "/usr/lib/test") +;; ⇒ ("" "usr" "lib" "test") +;; (path-split "//usr////lib/test") +;; ⇒ ("" "usr" "lib" "test") +;; @end example +(define-public (path-split path) + (let* ((head tail + (car+cdr + (reverse + (map reverse-list->string + (fold (lambda (c done) + (if (file-name-separator? c) + (cons '() done) + (cons (cons c (car done)) (cdr done)))) + '(()) + (string->list path))))))) + (cons head (remove string-null? tail)))) -- cgit v1.2.3