Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ variable we use on this OS.")
(def sep-string "\\")
(def escapes? false))
(posix-only
(defn absolute? [p] (String.starts-with? p "/"))
(defn absolute? [p] (and (not (String.empty? p)) (= \/ (String.head p))))
(def separator \/)
(def separators [\/])
(def search-path-separator \:)
Expand Down Expand Up @@ -165,7 +165,9 @@ Examples on POSIX:
(let [i (Pattern.find extension-pat p)]
(if (= -1 i)
(Maybe.Nothing)
(Maybe.Just (Pair.init (prefix p i) (suffix p (inc i)))))))
(Maybe.Just
(Pair.init (String.byte-slice p 0 i)
(String.byte-slice p (inc i) (String.length p)))))))

(doc extension "gets the extension of a file as a `Maybe`.")
(defn extension [p] (Maybe.apply (split-extension p) &(fn [p] @(Pair.b &p))))
Expand Down
18 changes: 18 additions & 0 deletions test/path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
(assert-false test (absolute? "path") "absolute? works on relative paths")
(assert-false test (relative? "/path") "relative? works on absolute paths")
(assert-true test (relative? "path") "relative? works on relative paths")
(assert-false test
(absolute? &(String.from-bytes &[187b]))
"absolute? works on a path that is a lone UTF-8 continuation byte")
(assert-equal test
"file.ext"
&(add-extension "file" "ext")
Expand Down Expand Up @@ -163,6 +166,21 @@
&(Maybe.Just (Pair.init @"file/path.txt.bob" @"fred"))
&(split-extension "file/path.txt.bob.fred")
"split-extension gets last extension")
(assert-equal test
&(Maybe.Just (Pair.init @"ä" @"txt"))
&(split-extension "ä.txt")
"split-extension splits at the dot in a multi-byte filename")
(assert-equal test
&(Maybe.Just (Pair.init @"grüße" @"c"))
&(split-extension "grüße.c")
"split-extension keeps the extension of a multi-byte filename")
(assert-true test
(is-extension? "ä.txt" "txt")
"is-extension? works on a multi-byte filename")
(assert-equal test
&(Maybe.Just (Pair.init (String.from-bytes &[187b 187b]) @""))
&(split-extension &(String.from-bytes &[187b 187b 46b]))
"split-extension works on a path of UTF-8 continuation bytes")
(assert-equal test
"file.ext"
&(replace-extension "file.txt" "ext")
Expand Down