about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorKevin Ballard <kevin@sb.org>2013-09-26 00:54:30 -0700
committerKevin Ballard <kevin@sb.org>2013-10-15 20:10:11 -0700
commit1dfe5088d85df13a172810d0df92c25dbcc37e98 (patch)
tree8578c8be4bdadf0b04aa232ac66c4e0503c6bbe8 /src/libstd
parentc16d7a439441ecfb8ac53deee070bb616fdc759e (diff)
downloadrust-1dfe5088d85df13a172810d0df92c25dbcc37e98.tar.gz
rust-1dfe5088d85df13a172810d0df92c25dbcc37e98.zip
path2: Add opt variants for from_vec/from_str
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/path2/mod.rs23
-rw-r--r--src/libstd/path2/posix.rs20
-rw-r--r--src/libstd/path2/windows.rs30
3 files changed, 73 insertions, 0 deletions
diff --git a/src/libstd/path2/mod.rs b/src/libstd/path2/mod.rs
index 183086a785b..054a8a36eb5 100644
--- a/src/libstd/path2/mod.rs
+++ b/src/libstd/path2/mod.rs
@@ -81,6 +81,17 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
         }
     }
 
+    /// Creates a new Path from a byte vector, if possible.
+    /// The resulting Path will always be normalized.
+    #[inline]
+    fn from_vec_opt(path: &[u8]) -> Option<Self> {
+        if contains_nul(path) {
+            None
+        } else {
+            Some(unsafe { GenericPathUnsafe::from_vec_unchecked(path) })
+        }
+    }
+
     /// Creates a new Path from a string.
     /// The resulting Path will always be normalized.
     ///
@@ -97,6 +108,18 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
         }
     }
 
+    /// Creates a new Path from a string, if possible.
+    /// The resulting Path will always be normalized.
+    #[inline]
+    fn from_str_opt(path: &str) -> Option<Self> {
+        let v = path.as_bytes();
+        if contains_nul(v) {
+            None
+        } else {
+            Some(unsafe { GenericPathUnsafe::from_str_unchecked(path) })
+        }
+    }
+
     /// Creates a new Path from a CString.
     /// The resulting Path will always be normalized.
     ///
diff --git a/src/libstd/path2/posix.rs b/src/libstd/path2/posix.rs
index 66b4a6fb326..fb39be7b6b1 100644
--- a/src/libstd/path2/posix.rs
+++ b/src/libstd/path2/posix.rs
@@ -287,6 +287,12 @@ impl Path {
         GenericPath::from_vec(v)
     }
 
+    /// Returns a new Path from a byte vector, if possible
+    #[inline]
+    pub fn from_vec_opt(v: &[u8]) -> Option<Path> {
+        GenericPath::from_vec_opt(v)
+    }
+
     /// Returns a new Path from a string
     ///
     /// # Failure
@@ -297,6 +303,12 @@ impl Path {
         GenericPath::from_str(s)
     }
 
+    /// Returns a new Path from a string, if possible
+    #[inline]
+    pub fn from_str_opt(s: &str) -> Option<Path> {
+        GenericPath::from_str_opt(s)
+    }
+
     /// Converts the Path into an owned byte vector
     pub fn into_vec(self) -> ~[u8] {
         self.repr
@@ -476,6 +488,14 @@ mod tests {
     }
 
     #[test]
+    fn test_opt_paths() {
+        assert_eq!(Path::from_vec_opt(b!("foo/bar", 0)), None);
+        t!(v: Path::from_vec_opt(b!("foo/bar")).unwrap(), b!("foo/bar"));
+        assert_eq!(Path::from_str_opt("foo/bar\0"), None);
+        t!(s: Path::from_str_opt("foo/bar").unwrap(), "foo/bar");
+    }
+
+    #[test]
     fn test_null_byte() {
         use path2::null_byte::cond;
 
diff --git a/src/libstd/path2/windows.rs b/src/libstd/path2/windows.rs
index e4c194ba6ca..747f803f08c 100644
--- a/src/libstd/path2/windows.rs
+++ b/src/libstd/path2/windows.rs
@@ -328,6 +328,15 @@ impl GenericPathUnsafe for Path {
 }
 
 impl GenericPath for Path {
+    #[inline]
+    fn from_vec_opt(v: &[u8]) -> Option<Path> {
+        if contains_nul(v) || !str::is_utf8(v) {
+            None
+        } else {
+            Some(unsafe { GenericPathUnsafe::from_vec_unchecked(v) })
+        }
+    }
+
     /// See `GenericPath::as_str` for info.
     /// Always returns a `Some` value.
     #[inline]
@@ -583,6 +592,12 @@ impl Path {
         GenericPath::from_vec(v)
     }
 
+    /// Returns a new Path from a byte vector, if possible
+    #[inline]
+    pub fn from_vec_opt(v: &[u8]) -> Option<Path> {
+        GenericPath::from_vec_opt(v)
+    }
+
     /// Returns a new Path from a string
     ///
     /// # Failure
@@ -593,6 +608,12 @@ impl Path {
         GenericPath::from_str(s)
     }
 
+    /// Returns a new Path from a string, if possible
+    #[inline]
+    pub fn from_str_opt(s: &str) -> Option<Path> {
+        GenericPath::from_str_opt(s)
+    }
+
     /// Converts the Path into an owned byte vector
     pub fn into_vec(self) -> ~[u8] {
         self.repr.into_bytes()
@@ -1202,6 +1223,15 @@ mod tests {
     }
 
     #[test]
+    fn test_opt_paths() {
+        assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0)), None);
+        assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0x80)), None);
+        t!(v: Path::from_vec_opt(b!("foo\\bar")).unwrap(), b!("foo\\bar"));
+        assert_eq!(Path::from_str_opt("foo\\bar\0"), None);
+        t!(s: Path::from_str_opt("foo\\bar").unwrap(), "foo\\bar");
+    }
+
+    #[test]
     fn test_null_byte() {
         use path2::null_byte::cond;