split should handle the unix root "/" specially #18

Merged
merged 1 commit into from Jan 5, 2012
Jump to file or symbol
Failed to load files and symbols.
+20 −3
Split
View
@@ -164,10 +164,20 @@
(.mkdirs (file path))
path)
+(def unix-root "The root of a unix system is /, nil on Windows"
+ (when (= File/separator "/") File/separator))
+
+
(defn split
- "Split path to componenets."
+ "Split path to components."
[path]
- (seq (.split (str path) (str "\\Q" File/separator "\\E"))))
+ (let [pathstr (str path)
+ jregx (str "\\Q" File/separator "\\E")]
+ (cond (= pathstr unix-root) (list unix-root)
+ (and unix-root (.startsWith pathstr unix-root))
+ ;; unix absolute path
+ (cons unix-root (seq (.split (subs pathstr 1) jregx)))
+ :else (seq (.split pathstr jregx)))))
(defn rename
"Rename old-path to new-path. Only works on files."
View
@@ -131,6 +131,13 @@
(fact
(split (file "test/fs")) => (has-suffix ["test" "fs"]))
+(when unix-root
+ (fact
+ (split (file "/tmp/foo/bar.txt")) => '("/" "tmp" "foo" "bar.txt")
+ (split (file "/")) => '("/")
+ (split "/") => '("/")
+ (split "") => '("")))
+
(fact
(let [f (temp-file)
new-f (str f "-new")]
@@ -271,4 +278,4 @@
(path-ns "foo/bar/baz_quux.clj") => 'foo.bar.baz-quux)
(fact
- (str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))
+ (str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))