about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-10-28 23:23:51 -0700
committerBrian Anderson <banderson@mozilla.com>2011-10-28 23:34:01 -0700
commit802deac3233d91bcd6ba5a87c6a4de8b3e12425e (patch)
tree29843187afb551550e9804df7408a0028ee97b03 /src/lib
parenta2377ccf91158ce5c9da2e5d0cc1f56fa562b2fe (diff)
downloadrust-802deac3233d91bcd6ba5a87c6a4de8b3e12425e.tar.gz
rust-802deac3233d91bcd6ba5a87c6a4de8b3e12425e.zip
stdlib: Add fs::splitext
Splits a path into the filename + extension
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/fs.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/fs.rs b/src/lib/fs.rs
index 5787e9c510c..057f933d0a5 100644
--- a/src/lib/fs.rs
+++ b/src/lib/fs.rs
@@ -177,6 +177,55 @@ fn split(p: path) -> [path] {
 }
 
 /*
+Function: splitext
+
+Split a path into a pair of strings with the first element being the filename
+without the extension and the second being either empty or the file extension
+including the period. Leading periods in the basename are ignored.  If the
+path includes directory components then they are included in the filename part
+of the result pair.
+*/
+fn splitext(p: path) -> (str, str) {
+    if str::is_empty(p) { ("", "") }
+    else {
+        let parts = str::split(p, '.' as u8);
+        if vec::len(parts) > 1u {
+            let base = str::connect(vec::init(parts), ".");
+            let ext = "." + option::get(vec::last(parts));
+
+            fn is_dotfile(base: str) -> bool {
+                str::is_empty(base)
+                    || str::ends_with(
+                        base, str::from_char(os_fs::path_sep))
+                    || str::ends_with(
+                        base, str::from_char(os_fs::alt_path_sep))
+            }
+
+            fn ext_contains_sep(ext: str) -> bool {
+                vec::len(split(ext)) > 1u
+            }
+
+            fn no_basename(ext: str) -> bool {
+                str::ends_with(
+                    ext, str::from_char(os_fs::path_sep))
+                    || str::ends_with(
+                        ext, str::from_char(os_fs::alt_path_sep))
+            }
+
+            if is_dotfile(base)
+                || ext_contains_sep(ext)
+                || no_basename(ext) {
+                (p, "")
+            } else {
+                (base, ext)
+            }
+        } else {
+            (p, "")
+        }
+    }
+}
+
+/*
 Function: normalize
 
 Removes extra "." and ".." entries from paths.