Skip to content
This repository

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

Merged
merged 1 commit into from over 2 years ago

2 participants

Steve Miner Anthony Grimes
Steve Miner

No description provided.

Anthony Grimes Raynes merged commit 42d1858 into from January 05, 2012
Anthony Grimes Raynes closed this January 05, 2012
Anthony Grimes
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jan 05, 2012
Steve Miner Split needs to handle the unix root as a special case cc5f059
This page is out of date. Refresh to see the latest.
14  src/fs/core.clj
@@ -164,10 +164,20 @@
164 164
   (.mkdirs (file path))
165 165
   path)
166 166
 
  167
+(def unix-root "The root of a unix system is /, nil on Windows"
  168
+  (when (= File/separator "/") File/separator))
  169
+
  170
+
167 171
 (defn split
168  
-  "Split path to componenets."
  172
+  "Split path to components."
169 173
   [path]
170  
-  (seq (.split (str path) (str "\\Q" File/separator "\\E"))))
  174
+  (let [pathstr (str path)
  175
+        jregx (str "\\Q" File/separator "\\E")]
  176
+    (cond (= pathstr unix-root) (list unix-root)
  177
+          (and unix-root (.startsWith pathstr unix-root))
  178
+          ;; unix absolute path
  179
+            (cons unix-root (seq (.split (subs pathstr 1) jregx)))
  180
+          :else (seq (.split pathstr jregx)))))
171 181
 
172 182
 (defn rename
173 183
   "Rename old-path to new-path. Only works on files."
9  test/fs/core_test.clj
@@ -131,6 +131,13 @@
131 131
 (fact
132 132
   (split (file "test/fs")) => (has-suffix ["test" "fs"]))
133 133
 
  134
+(when unix-root
  135
+  (fact
  136
+   (split (file "/tmp/foo/bar.txt")) => '("/" "tmp" "foo" "bar.txt")
  137
+   (split (file "/")) => '("/")
  138
+   (split "/") => '("/")
  139
+   (split "") => '("")))
  140
+
134 141
 (fact
135 142
   (let [f (temp-file)
136 143
         new-f (str f "-new")]
@@ -271,4 +278,4 @@
271 278
   (path-ns "foo/bar/baz_quux.clj") => 'foo.bar.baz-quux)
272 279
 
273 280
 (fact
274  
-  (str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))
  281
+  (str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.