diff options
Diffstat (limited to 'src/libstd/path')
| -rw-r--r-- | src/libstd/path/mod.rs | 46 | ||||
| -rw-r--r-- | src/libstd/path/posix.rs | 130 | ||||
| -rw-r--r-- | src/libstd/path/windows.rs | 255 |
3 files changed, 206 insertions, 225 deletions
diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index b020164158c..581969e98fb 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -61,13 +61,14 @@ #![experimental] -use core::kinds::Sized; +use core::marker::Sized; use ffi::CString; use clone::Clone; use fmt; use iter::IteratorExt; use option::Option; use option::Option::{None, Some}; +use ops::{FullRange, Index}; use str; use str::StrExt; use string::{String, CowString}; @@ -351,7 +352,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { match name.rposition_elem(&dot) { None | Some(0) => name, Some(1) if name == b".." => name, - Some(pos) => name[..pos] + Some(pos) => name.index(&(0..pos)) } }) } @@ -398,7 +399,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { match name.rposition_elem(&dot) { None | Some(0) => None, Some(1) if name == b".." => None, - Some(pos) => Some(name[pos+1..]) + Some(pos) => Some(name.index(&((pos+1)..))) } } } @@ -459,7 +460,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("abc/def.txt"); /// p.set_extension("csv"); - /// assert!(p == Path::new("abc/def.csv")); + /// assert_eq!(p, Path::new("abc/def.csv")); /// # } /// ``` /// @@ -474,7 +475,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { let extlen = extension.container_as_bytes().len(); match (name.rposition_elem(&dot), extlen) { (None, 0) | (Some(0), 0) => None, - (Some(idx), 0) => Some(name[..idx].to_vec()), + (Some(idx), 0) => Some(name.index(&(0..idx)).to_vec()), (idx, extlen) => { let idx = match idx { None | Some(0) => name.len(), @@ -483,7 +484,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { let mut v; v = Vec::with_capacity(idx + extlen + 1); - v.push_all(name[..idx]); + v.push_all(name.index(&(0..idx))); v.push(dot); v.push_all(extension.container_as_bytes()); Some(v) @@ -508,7 +509,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(windows)] fn foo() {} /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("abc/def.txt"); - /// assert!(p.with_filename("foo.dat") == Path::new("abc/foo.dat")); + /// assert_eq!(p.with_filename("foo.dat"), Path::new("abc/foo.dat")); /// # } /// ``` /// @@ -533,7 +534,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(windows)] fn foo() {} /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("abc/def.txt"); - /// assert!(p.with_extension("csv") == Path::new("abc/def.csv")); + /// assert_eq!(p.with_extension("csv"), Path::new("abc/def.csv")); /// # } /// ``` /// @@ -557,7 +558,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(windows)] fn foo() {} /// # #[cfg(unix)] fn foo() { /// let p = Path::new("abc/def/ghi"); - /// assert!(p.dir_path() == Path::new("abc/def")); + /// assert_eq!(p.dir_path(), Path::new("abc/def")); /// # } /// ``` fn dir_path(&self) -> Self { @@ -575,8 +576,8 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # foo(); /// # #[cfg(windows)] fn foo() {} /// # #[cfg(unix)] fn foo() { - /// assert!(Path::new("abc/def").root_path() == None); - /// assert!(Path::new("/abc/def").root_path() == Some(Path::new("/"))); + /// assert_eq!(Path::new("abc/def").root_path(), None); + /// assert_eq!(Path::new("/abc/def").root_path(), Some(Path::new("/"))); /// # } /// ``` fn root_path(&self) -> Option<Self>; @@ -592,7 +593,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("foo/bar"); /// p.push("baz.txt"); - /// assert!(p == Path::new("foo/bar/baz.txt")); + /// assert_eq!(p, Path::new("foo/bar/baz.txt")); /// # } /// ``` /// @@ -616,7 +617,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("foo"); /// p.push_many(&["bar", "baz.txt"]); - /// assert!(p == Path::new("foo/bar/baz.txt")); + /// assert_eq!(p, Path::new("foo/bar/baz.txt")); /// # } /// ``` #[inline] @@ -645,7 +646,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(unix)] fn foo() { /// let mut p = Path::new("foo/bar/baz.txt"); /// p.pop(); - /// assert!(p == Path::new("foo/bar")); + /// assert_eq!(p, Path::new("foo/bar")); /// # } /// ``` fn pop(&mut self) -> bool; @@ -661,7 +662,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(windows)] fn foo() {} /// # #[cfg(unix)] fn foo() { /// let p = Path::new("/foo"); - /// assert!(p.join("bar.txt") == Path::new("/foo/bar.txt")); + /// assert_eq!(p.join("bar.txt"), Path::new("/foo/bar.txt")); /// # } /// ``` /// @@ -687,7 +688,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// # #[cfg(unix)] fn foo() { /// let p = Path::new("foo"); /// let fbbq = Path::new("foo/bar/baz/quux.txt"); - /// assert!(p.join_many(&["bar", "baz", "quux.txt"]) == fbbq); + /// assert_eq!(p.join_many(&["bar", "baz", "quux.txt"]), fbbq); /// # } /// ``` #[inline] @@ -764,7 +765,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// let p = Path::new("foo/bar/baz/quux.txt"); /// let fb = Path::new("foo/bar"); /// let bq = Path::new("baz/quux.txt"); - /// assert!(p.path_relative_from(&fb) == Some(bq)); + /// assert_eq!(p.path_relative_from(&fb), Some(bq)); /// # } /// ``` fn path_relative_from(&self, base: &Self) -> Option<Self>; @@ -822,8 +823,15 @@ pub struct Display<'a, P:'a> { filename: bool } +//NOTE(stage0): replace with deriving(Show) after snapshot impl<'a, P: GenericPath> fmt::Show for Display<'a, P> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::String::fmt(self, f) + } +} + +impl<'a, P: GenericPath> fmt::String for Display<'a, P> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.as_cow().fmt(f) } } @@ -869,7 +877,7 @@ impl BytesContainer for String { } #[inline] fn container_as_str(&self) -> Option<&str> { - Some(self[]) + Some(self.index(&FullRange)) } #[inline] fn is_str(_: Option<&String>) -> bool { true } @@ -885,7 +893,7 @@ impl BytesContainer for [u8] { impl BytesContainer for Vec<u8> { #[inline] fn container_as_bytes(&self) -> &[u8] { - self[] + self.index(&FullRange) } } diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index d9981ace030..0b7dc19fcab 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -12,11 +12,13 @@ use clone::Clone; use cmp::{Ordering, Eq, Ord, PartialEq, PartialOrd}; +use fmt; use hash; use io::Writer; use iter::{AdditiveIterator, Extend}; use iter::{Iterator, IteratorExt, Map}; -use kinds::Sized; +use ops::Index; +use marker::Sized; use option::Option::{self, Some, None}; use slice::{AsSlice, Split, SliceExt, SliceConcatExt}; use str::{self, FromStr, StrExt}; @@ -56,6 +58,12 @@ pub fn is_sep(c: char) -> bool { c == SEP } +impl fmt::Show for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Path {{ {} }}", self.display()) + } +} + impl PartialEq for Path { #[inline] fn eq(&self, other: &Path) -> bool { @@ -119,7 +127,7 @@ impl GenericPathUnsafe for Path { None => { self.repr = Path::normalize(filename); } - Some(idx) if self.repr[idx+1..] == b".." => { + Some(idx) if self.repr.index(&((idx+1)..)) == b".." => { let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len()); v.push_all(self.repr.as_slice()); v.push(SEP_BYTE); @@ -129,7 +137,7 @@ impl GenericPathUnsafe for Path { } Some(idx) => { let mut v = Vec::with_capacity(idx + 1 + filename.len()); - v.push_all(self.repr[..idx+1]); + v.push_all(self.repr.index(&(0..(idx+1)))); v.push_all(filename); // FIXME: this is slow self.repr = Path::normalize(v.as_slice()); @@ -170,9 +178,9 @@ impl GenericPath for Path { match self.sepidx { None if b".." == self.repr => self.repr.as_slice(), None => dot_static, - Some(0) => self.repr[..1], - Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(), - Some(idx) => self.repr[..idx] + Some(0) => self.repr.index(&(0..1)), + Some(idx) if self.repr.index(&((idx+1)..)) == b".." => self.repr.as_slice(), + Some(idx) => self.repr.index(&(0..idx)) } } @@ -181,9 +189,9 @@ impl GenericPath for Path { None if b"." == self.repr || b".." == self.repr => None, None => Some(self.repr.as_slice()), - Some(idx) if self.repr[idx+1..] == b".." => None, - Some(0) if self.repr[1..].is_empty() => None, - Some(idx) => Some(self.repr[idx+1..]) + Some(idx) if self.repr.index(&((idx+1)..)) == b".." => None, + Some(0) if self.repr.index(&(1..)).is_empty() => None, + Some(idx) => Some(self.repr.index(&((idx+1)..))) } } @@ -325,7 +333,7 @@ impl Path { // borrowck is being very picky let val = { let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE; - let v_ = if is_abs { v.as_slice()[1..] } else { v.as_slice() }; + let v_ = if is_abs { v.as_slice().index(&(1..)) } else { v.as_slice() }; let comps = normalize_helper(v_, is_abs); match comps { None => None, @@ -364,7 +372,7 @@ impl Path { /// A path of "/" yields no components. A path of "." yields one component. pub fn components<'a>(&'a self) -> Components<'a> { let v = if self.repr[0] == SEP_BYTE { - self.repr[1..] + self.repr.index(&(1..)) } else { self.repr.as_slice() }; let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr let mut ret = v.split(is_sep_byte); @@ -438,13 +446,13 @@ mod tests { (s: $path:expr, $exp:expr) => ( { let path = $path; - assert!(path.as_str() == Some($exp)); + assert_eq!(path.as_str(), Some($exp)); } ); (v: $path:expr, $exp:expr) => ( { let path = $path; - assert!(path.as_vec() == $exp); + assert_eq!(path.as_vec(), $exp); } ) } @@ -458,7 +466,7 @@ mod tests { t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF"); t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80"); let p = Path::new(b"a/b/c\xFF"); - assert!(p.as_str() == None); + assert!(p.as_str().is_none()); t!(s: Path::new(""), "."); t!(s: Path::new("/"), "/"); @@ -488,31 +496,31 @@ mod tests { b"/bar"); let p = Path::new(b"foo/bar\x80"); - assert!(p.as_str() == None); + assert!(p.as_str().is_none()); } #[test] fn test_opt_paths() { - assert!(Path::new_opt(b"foo/bar\0") == None); + assert!(Path::new_opt(b"foo/bar\0").is_none()); t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar"); - assert!(Path::new_opt("foo/bar\0") == None); + assert!(Path::new_opt("foo/bar\0").is_none()); t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar"); } #[test] fn test_null_byte() { use thread::Thread; - let result = Thread::spawn(move|| { + let result = Thread::scoped(move|| { Path::new(b"foo/bar\0") }).join(); assert!(result.is_err()); - let result = Thread::spawn(move|| { + let result = Thread::scoped(move|| { Path::new("test").set_filename(b"f\0o") }).join(); assert!(result.is_err()); - let result = Thread::spawn(move|| { + let result = Thread::scoped(move|| { Path::new("test").push(b"f\0o"); }).join(); assert!(result.is_err()); @@ -524,7 +532,7 @@ mod tests { ($path:expr, $disp:ident, $exp:expr) => ( { let path = Path::new($path); - assert!(path.$disp().to_string() == $exp); + assert_eq!(path.$disp().to_string(), $exp); } ) } @@ -540,14 +548,14 @@ mod tests { { let path = Path::new($path); let mo = path.display().as_cow(); - assert!(mo.as_slice() == $exp); + assert_eq!(mo.as_slice(), $exp); } ); ($path:expr, $exp:expr, filename) => ( { let path = Path::new($path); let mo = path.filename_display().as_cow(); - assert!(mo.as_slice() == $exp); + assert_eq!(mo.as_slice(), $exp); } ) } @@ -567,9 +575,9 @@ mod tests { { let path = Path::new($path); let f = format!("{}", path.display()); - assert!(f == $exp); + assert_eq!(f, $exp); let f = format!("{}", path.filename_display()); - assert!(f == $expf); + assert_eq!(f, $expf); } ) } @@ -589,21 +597,21 @@ mod tests { (s: $path:expr, $op:ident, $exp:expr) => ( { let path = Path::new($path); - assert!(path.$op() == ($exp).as_bytes()); + assert_eq!(path.$op(), ($exp).as_bytes()); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( { let path = Path::new($path); let left = path.$op().map(|x| str::from_utf8(x).unwrap()); - assert!(left == $exp); + assert_eq!(left, $exp); } ); (v: $path:expr, $op:ident, $exp:expr) => ( { let arg = $path; let path = Path::new(arg); - assert!(path.$op() == $exp); + assert_eq!(path.$op(), $exp); } ); } @@ -677,7 +685,7 @@ mod tests { let mut p1 = Path::new(path); let p2 = p1.clone(); p1.push(join); - assert!(p1 == p2.join(join)); + assert_eq!(p1, p2.join(join)); } ) } @@ -696,7 +704,7 @@ mod tests { let mut p = Path::new($path); let push = Path::new($push); p.push(&push); - assert!(p.as_str() == Some($exp)); + assert_eq!(p.as_str(), Some($exp)); } ) } @@ -716,14 +724,14 @@ mod tests { { let mut p = Path::new($path); p.push_many(&$push); - assert!(p.as_str() == Some($exp)); + assert_eq!(p.as_str(), Some($exp)); } ); (v: $path:expr, $push:expr, $exp:expr) => ( { let mut p = Path::new($path); p.push_many(&$push); - assert!(p.as_vec() == $exp); + assert_eq!(p.as_vec(), $exp); } ) } @@ -744,16 +752,16 @@ mod tests { { let mut p = Path::new($path); let result = p.pop(); - assert!(p.as_str() == Some($left)); - assert!(result == $right); + assert_eq!(p.as_str(), Some($left)); + assert_eq!(result, $right); } ); (b: $path:expr, $left:expr, $right:expr) => ( { let mut p = Path::new($path); let result = p.pop(); - assert!(p.as_vec() == $left); - assert!(result == $right); + assert_eq!(p.as_vec(), $left); + assert_eq!(result, $right); } ) } @@ -776,8 +784,8 @@ mod tests { #[test] fn test_root_path() { - assert!(Path::new(b"a/b/c").root_path() == None); - assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/"))); + assert_eq!(Path::new(b"a/b/c").root_path(), None); + assert_eq!(Path::new(b"/a/b/c").root_path(), Some(Path::new("/"))); } #[test] @@ -801,7 +809,7 @@ mod tests { let path = Path::new($path); let join = Path::new($join); let res = path.join(&join); - assert!(res.as_str() == Some($exp)); + assert_eq!(res.as_str(), Some($exp)); } ) } @@ -821,14 +829,14 @@ mod tests { { let path = Path::new($path); let res = path.join_many(&$join); - assert!(res.as_str() == Some($exp)); + assert_eq!(res.as_str(), Some($exp)); } ); (v: $path:expr, $join:expr, $exp:expr) => ( { let path = Path::new($path); let res = path.join_many(&$join); - assert!(res.as_vec() == $exp); + assert_eq!(res.as_vec(), $exp); } ) } @@ -902,7 +910,7 @@ mod tests { let mut p1 = Path::new(path); p1.$set(arg); let p2 = Path::new(path); - assert!(p1 == p2.$with(arg)); + assert_eq!(p1, p2.$with(arg)); } ); (v: $path:expr, $set:ident, $with:ident, $arg:expr) => ( @@ -912,7 +920,7 @@ mod tests { let mut p1 = Path::new(path); p1.$set(arg); let p2 = Path::new(path); - assert!(p1 == p2.$with(arg)); + assert_eq!(p1, p2.$with(arg)); } ) } @@ -942,31 +950,19 @@ mod tests { (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { let path = $path; - let filename = $filename; - assert!(path.filename_str() == filename, - "{}.filename_str(): Expected `{}`, found {}", - path.as_str().unwrap(), filename, path.filename_str()); - let dirname = $dirname; - assert!(path.dirname_str() == dirname, - "`{}`.dirname_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), dirname, path.dirname_str()); - let filestem = $filestem; - assert!(path.filestem_str() == filestem, - "`{}`.filestem_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), filestem, path.filestem_str()); - let ext = $ext; - assert!(path.extension_str() == ext, - "`{}`.extension_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), ext, path.extension_str()); - } + assert_eq!(path.filename_str(), $filename); + assert_eq!(path.dirname_str(), $dirname); + assert_eq!(path.filestem_str(), $filestem); + assert_eq!(path.extension_str(), $ext); + } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { let path = $path; - assert!(path.filename() == $filename); - assert!(path.dirname() == $dirname); - assert!(path.filestem() == $filestem); - assert!(path.extension() == $ext); + assert_eq!(path.filename(), $filename); + assert_eq!(path.dirname(), $dirname); + assert_eq!(path.filestem(), $filestem); + assert_eq!(path.extension(), $ext); } ) } @@ -1154,12 +1150,10 @@ mod tests { let comps = path.components().collect::<Vec<&[u8]>>(); let exp: &[&str] = &$exp; let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>(); - assert!(comps == exps, "components: Expected {}, found {}", - comps, exps); + assert_eq!(comps, exps); let comps = path.components().rev().collect::<Vec<&[u8]>>(); let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>(); - assert!(comps == exps, "rev_components: Expected {}, found {}", - comps, exps); + assert_eq!(comps, exps); } ); (b: $arg:expr, [$($exp:expr),*]) => ( diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 4b5d793355b..5c4e7aa9ac2 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -18,12 +18,14 @@ use ascii::AsciiExt; use char::CharExt; use clone::Clone; use cmp::{Ordering, Eq, Ord, PartialEq, PartialOrd}; +use fmt; use hash; use io::Writer; use iter::{AdditiveIterator, Extend}; use iter::{Iterator, IteratorExt, Map, repeat}; use mem; use option::Option::{self, Some, None}; +use ops::{FullRange, Index}; use slice::{SliceExt, SliceConcatExt}; use str::{SplitTerminator, FromStr, StrExt}; use string::{String, ToString}; @@ -83,6 +85,12 @@ pub struct Path { sepidx: Option<uint> // index of the final separator in the non-prefix portion of repr } +impl fmt::Show for Path { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Path {{ {} }}", self.display()) + } +} + impl PartialEq for Path { #[inline] fn eq(&self, other: &Path) -> bool { @@ -165,30 +173,30 @@ impl GenericPathUnsafe for Path { s.push_str(".."); s.push(SEP); s.push_str(filename); - self.update_normalized(s[]); + self.update_normalized(s.index(&FullRange)); } None => { self.update_normalized(filename); } - Some((_,idxa,end)) if self.repr[idxa..end] == ".." => { + Some((_,idxa,end)) if self.repr.index(&(idxa..end)) == ".." => { let mut s = String::with_capacity(end + 1 + filename.len()); - s.push_str(self.repr[0..end]); + s.push_str(self.repr.index(&(0..end))); s.push(SEP); s.push_str(filename); - self.update_normalized(s[]); + self.update_normalized(s.index(&FullRange)); } Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => { let mut s = String::with_capacity(idxb + filename.len()); - s.push_str(self.repr[0..idxb]); + s.push_str(self.repr.index(&(0..idxb))); s.push_str(filename); - self.update_normalized(s[]); + self.update_normalized(s.index(&FullRange)); } Some((idxb,_,_)) => { let mut s = String::with_capacity(idxb + 1 + filename.len()); - s.push_str(self.repr[0..idxb]); + s.push_str(self.repr.index(&(0..idxb))); s.push(SEP); s.push_str(filename); - self.update_normalized(s[]); + self.update_normalized(s.index(&FullRange)); } } } @@ -207,12 +215,12 @@ impl GenericPathUnsafe for Path { let path = path.container_as_str().unwrap(); fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool { // assume prefix is Some(DiskPrefix) - let rest = path[prefix_len(prefix)..]; + let rest = path.index(&(prefix_len(prefix)..)); !rest.is_empty() && rest.as_bytes()[0].is_ascii() && is_sep(rest.as_bytes()[0] as char) } fn shares_volume(me: &Path, path: &str) -> bool { // path is assumed to have a prefix of Some(DiskPrefix) - let repr = me.repr[]; + let repr = me.repr.index(&FullRange); match me.prefix { Some(DiskPrefix) => { repr.as_bytes()[0] == path.as_bytes()[0].to_ascii_uppercase() @@ -244,7 +252,7 @@ impl GenericPathUnsafe for Path { else { None }; let pathlen = path_.as_ref().map_or(path.len(), |p| p.len()); let mut s = String::with_capacity(me.repr.len() + 1 + pathlen); - s.push_str(me.repr[]); + s.push_str(me.repr.index(&FullRange)); let plen = me.prefix_len(); // if me is "C:" we don't want to add a path separator match me.prefix { @@ -256,9 +264,9 @@ impl GenericPathUnsafe for Path { } match path_ { None => s.push_str(path), - Some(p) => s.push_str(p[]), + Some(p) => s.push_str(p.index(&FullRange)), }; - me.update_normalized(s[]) + me.update_normalized(s.index(&FullRange)) } if !path.is_empty() { @@ -266,7 +274,7 @@ impl GenericPathUnsafe for Path { match prefix { Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => { // cwd-relative path, self is on the same volume - append_path(self, path[prefix_len(prefix)..]); + append_path(self, path.index(&(prefix_len(prefix)..))); } Some(_) => { // absolute path, or cwd-relative and self is not same volume @@ -312,7 +320,7 @@ impl GenericPath for Path { /// Always returns a `Some` value. #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - Some(self.repr[]) + Some(self.repr.index(&FullRange)) } #[inline] @@ -334,17 +342,21 @@ impl GenericPath for Path { /// Always returns a `Some` value. fn dirname_str<'a>(&'a self) -> Option<&'a str> { Some(match self.sepidx_or_prefix_len() { - None if ".." == self.repr => self.repr[], + None if ".." == self.repr => self.repr.index(&FullRange), None => ".", - Some((_,idxa,end)) if self.repr[idxa..end] == ".." => self.repr[], - Some((idxb,_,end)) if self.repr[idxb..end] == "\\" => self.repr[], - Some((0,idxa,_)) => self.repr[0..idxa], + Some((_,idxa,end)) if self.repr.index(&(idxa..end)) == ".." => { + self.repr.index(&FullRange) + } + Some((idxb,_,end)) if self.repr.index(&(idxb..end)) == "\\" => { + self.repr.index(&FullRange) + } + Some((0,idxa,_)) => self.repr.index(&(0..idxa)), Some((idxb,idxa,_)) => { match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => { - self.repr[0..idxa] + self.repr.index(&(0..idxa)) } - _ => self.repr[0..idxb] + _ => self.repr.index(&(0..idxb)) } } }) @@ -358,13 +370,13 @@ impl GenericPath for Path { /// See `GenericPath::filename_str` for info. /// Always returns a `Some` value if `filename` returns a `Some` value. fn filename_str<'a>(&'a self) -> Option<&'a str> { - let repr = self.repr[]; + let repr = self.repr.index(&FullRange); match self.sepidx_or_prefix_len() { None if "." == repr || ".." == repr => None, None => Some(repr), - Some((_,idxa,end)) if repr[idxa..end] == ".." => None, + Some((_,idxa,end)) if repr.index(&(idxa..end)) == ".." => None, Some((_,idxa,end)) if idxa == end => None, - Some((_,idxa,end)) => Some(repr[idxa..end]) + Some((_,idxa,end)) => Some(repr.index(&(idxa..end))) } } @@ -396,7 +408,7 @@ impl GenericPath for Path { true } Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false, - Some((idxb,_,end)) if self.repr[idxb..end] == "\\" => false, + Some((idxb,_,end)) if self.repr.index(&(idxb..end)) == "\\" => false, Some((idxb,idxa,_)) => { let trunc = match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => { @@ -416,15 +428,15 @@ impl GenericPath for Path { if self.prefix.is_some() { Some(Path::new(match self.prefix { Some(DiskPrefix) if self.is_absolute() => { - self.repr[0..self.prefix_len()+1] + self.repr.index(&(0..(self.prefix_len()+1))) } Some(VerbatimDiskPrefix) => { - self.repr[0..self.prefix_len()+1] + self.repr.index(&(0..(self.prefix_len()+1))) } - _ => self.repr[0..self.prefix_len()] + _ => self.repr.index(&(0..self.prefix_len())) })) } else if is_vol_relative(self) { - Some(Path::new(self.repr[0..1])) + Some(Path::new(self.repr.index(&(0..1)))) } else { None } @@ -443,7 +455,7 @@ impl GenericPath for Path { fn is_absolute(&self) -> bool { match self.prefix { Some(DiskPrefix) => { - let rest = self.repr[self.prefix_len()..]; + let rest = self.repr.index(&(self.prefix_len()..)); rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE } Some(_) => true, @@ -618,15 +630,15 @@ impl Path { /// Does not distinguish between absolute and cwd-relative paths, e.g. /// C:\foo and C:foo. pub fn str_components<'a>(&'a self) -> StrComponents<'a> { - let repr = self.repr[]; + let repr = self.repr.index(&FullRange); let s = match self.prefix { Some(_) => { let plen = self.prefix_len(); if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE { - repr[plen+1..] - } else { repr[plen..] } + repr.index(&((plen+1)..)) + } else { repr.index(&(plen..)) } } - None if repr.as_bytes()[0] == SEP_BYTE => repr[1..], + None if repr.as_bytes()[0] == SEP_BYTE => repr.index(&(1..)), None => repr }; let some: fn(&'a str) -> Option<&'a str> = Some; // coerce to fn ptr @@ -646,8 +658,8 @@ impl Path { } fn equiv_prefix(&self, other: &Path) -> bool { - let s_repr = self.repr[]; - let o_repr = other.repr[]; + let s_repr = self.repr.index(&FullRange); + let o_repr = other.repr.index(&FullRange); match (self.prefix, other.prefix) { (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => { self.is_absolute() && @@ -664,14 +676,14 @@ impl Path { o_repr.as_bytes()[4].to_ascii_lowercase() } (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => { - s_repr[2..self.prefix_len()] == o_repr[8..other.prefix_len()] + s_repr.index(&(2..self.prefix_len())) == o_repr.index(&(8..other.prefix_len())) } (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => { - s_repr[8..self.prefix_len()] == o_repr[2..other.prefix_len()] + s_repr.index(&(8..self.prefix_len())) == o_repr.index(&(2..other.prefix_len())) } (None, None) => true, (a, b) if a == b => { - s_repr[0..self.prefix_len()] == o_repr[0..other.prefix_len()] + s_repr.index(&(0..self.prefix_len())) == o_repr.index(&(0..other.prefix_len())) } _ => false } @@ -725,7 +737,7 @@ impl Path { match prefix.unwrap() { DiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s[0..len]); + let mut s = String::from_str(s.index(&(0..len))); unsafe { let v = s.as_mut_vec(); v[0] = (*v)[0].to_ascii_uppercase(); @@ -740,7 +752,7 @@ impl Path { } VerbatimDiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s[0..len]); + let mut s = String::from_str(s.index(&(0..len))); unsafe { let v = s.as_mut_vec(); v[4] = (*v)[4].to_ascii_uppercase(); @@ -750,14 +762,14 @@ impl Path { _ => { let plen = prefix_len(prefix); if s.len() > plen { - Some(String::from_str(s[0..plen])) + Some(String::from_str(s.index(&(0..plen)))) } else { None } } } } else if is_abs && comps.is_empty() { Some(repeat(SEP).take(1).collect()) } else { - let prefix_ = s[0..prefix_len(prefix)]; + let prefix_ = s.index(&(0..prefix_len(prefix))); let n = prefix_.len() + if is_abs { comps.len() } else { comps.len() - 1} + comps.iter().map(|v| v.len()).sum(); @@ -768,15 +780,15 @@ impl Path { s.push(':'); } Some(VerbatimDiskPrefix) => { - s.push_str(prefix_[0..4]); + s.push_str(prefix_.index(&(0..4))); s.push(prefix_.as_bytes()[4].to_ascii_uppercase() as char); - s.push_str(prefix_[5..]); + s.push_str(prefix_.index(&(5..))); } Some(UNCPrefix(a,b)) => { s.push_str("\\\\"); - s.push_str(prefix_[2..a+2]); + s.push_str(prefix_.index(&(2..(a+2)))); s.push(SEP); - s.push_str(prefix_[3+a..3+a+b]); + s.push_str(prefix_.index(&((3+a)..(3+a+b)))); } Some(_) => s.push_str(prefix_), None => () @@ -801,8 +813,8 @@ impl Path { fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { - self.repr[0..self.repr.len()-1] - } else { self.repr[] }; + self.repr.index(&(0..(self.repr.len()-1))) + } else { self.repr.index(&FullRange) }; let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) { is_sep } else { @@ -881,17 +893,17 @@ pub fn is_verbatim(path: &Path) -> bool { /// non-verbatim, the non-verbatim version is returned. /// Otherwise, None is returned. pub fn make_non_verbatim(path: &Path) -> Option<Path> { - let repr = path.repr[]; + let repr = path.repr.index(&FullRange); let new_path = match path.prefix { Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None, Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()), Some(VerbatimDiskPrefix) => { // \\?\D:\ - Path::new(repr[4..]) + Path::new(repr.index(&(4..))) } Some(VerbatimUNCPrefix(_,_)) => { // \\?\UNC\server\share - Path::new(format!(r"\{}", repr[7..])) + Path::new(format!(r"\{}", repr.index(&(7..)))) } }; if new_path.prefix.is_none() { @@ -900,8 +912,8 @@ pub fn make_non_verbatim(path: &Path) -> Option<Path> { return None; } // now ensure normalization didn't change anything - if repr[path.prefix_len()..] == - new_path.repr[new_path.prefix_len()..] { + if repr.index(&(path.prefix_len()..)) == + new_path.repr.index(&(new_path.prefix_len()..)) { Some(new_path) } else { None @@ -966,13 +978,13 @@ pub enum PathPrefix { fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { if path.starts_with("\\\\") { // \\ - path = path[2..]; + path = path.index(&(2..)); if path.starts_with("?\\") { // \\?\ - path = path[2..]; + path = path.index(&(2..)); if path.starts_with("UNC\\") { // \\?\UNC\server\share - path = path[4..]; + path = path.index(&(4..)); let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) { Some(x) => x, None => (path.len(), 0) @@ -993,7 +1005,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { } } else if path.starts_with(".\\") { // \\.\path - path = path[2..]; + path = path.index(&(2..)); let idx = path.find('\\').unwrap_or(path.len()); return Some(DeviceNSPrefix(idx)); } @@ -1018,7 +1030,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { None => return None, Some(x) => x }; - path = path[idx_a+1..]; + path = path.index(&((idx_a+1)..)); let idx_b = path.find(f).unwrap_or(path.len()); Some((idx_a, idx_b)) } @@ -1032,8 +1044,8 @@ fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option is_sep_verbatim }; let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix))); - let s_ = s[prefix_len(prefix)..]; - let s_ = if is_abs { s_[1..] } else { s_ }; + let s_ = s.index(&(prefix_len(prefix)..)); + let s_ = if is_abs { s_.index(&(1..)) } else { s_ }; if is_abs && s_.is_empty() { return (is_abs, match prefix { @@ -1114,13 +1126,13 @@ mod tests { (s: $path:expr, $exp:expr) => ( { let path = $path; - assert!(path.as_str() == Some($exp)); + assert_eq!(path.as_str(), Some($exp)); } ); (v: $path:expr, $exp:expr) => ( { let path = $path; - assert!(path.as_vec() == $exp); + assert_eq!(path.as_vec(), $exp); } ) } @@ -1133,8 +1145,7 @@ mod tests { let path = $path; let exp = $exp; let res = parse_prefix(path); - assert!(res == exp, - "parse_prefix(\"{}\"): expected {}, found {}", path, exp, res); + assert_eq!(res, exp); } ) } @@ -1287,17 +1298,17 @@ mod tests { #[test] fn test_null_byte() { use thread::Thread; - let result = Thread::spawn(move|| { + let result = Thread::scoped(move|| { Path::new(b"foo/bar\0") }).join(); assert!(result.is_err()); - let result = Thread::spawn(move|| { + let result = Thread::scoped(move|| { Path::new("test").set_filename(b"f\0o") }).join(); assert!(result.is_err()); - let result = Thread::spawn(move || { + let result = Thread::scoped(move || { Path::new("test").push(b"f\0o"); }).join(); assert!(result.is_err()); @@ -1350,7 +1361,7 @@ mod tests { { let path = $path; let path = Path::new(path); - assert!(path.$op() == Some($exp)); + assert_eq!(path.$op(), Some($exp)); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( @@ -1358,14 +1369,14 @@ mod tests { let path = $path; let path = Path::new(path); let left = path.$op(); - assert!(left == $exp); + assert_eq!(left, $exp); } ); (v: $path:expr, $op:ident, $exp:expr) => ( { let path = $path; let path = Path::new(path); - assert!(path.$op() == $exp); + assert_eq!(path.$op(), $exp); } ) } @@ -1476,7 +1487,7 @@ mod tests { let mut p1 = Path::new(path); let p2 = p1.clone(); p1.push(join); - assert!(p1 == p2.join(join)); + assert_eq!(p1, p2.join(join)); } ) } @@ -1490,9 +1501,9 @@ mod tests { // we do want to check one odd case though to ensure the prefix is re-parsed let mut p = Path::new("\\\\?\\C:"); - assert!(prefix(&p) == Some(VerbatimPrefix(2))); + assert_eq!(prefix(&p), Some(VerbatimPrefix(2))); p.push("foo"); - assert!(prefix(&p) == Some(VerbatimDiskPrefix)); + assert_eq!(prefix(&p), Some(VerbatimDiskPrefix)); assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo")); // and another with verbatim non-normalized paths @@ -1591,10 +1602,8 @@ mod tests { let mut p = Path::new(pstr); let result = p.pop(); let left = $left; - assert!(p.as_str() == Some(left), - "`{}`.pop() failed; expected remainder `{}`, found `{}`", - pstr, left, p.as_str().unwrap()); - assert!(result == $right); + assert_eq!(p.as_str(), Some(left)); + assert_eq!(result, $right); } ); (b: $path:expr, $left:expr, $right:expr) => ( @@ -1602,7 +1611,7 @@ mod tests { let mut p = Path::new($path); let result = p.pop(); assert_eq!(p.as_vec(), $left); - assert!(result == $right); + assert_eq!(result, $right); } ) } @@ -1645,16 +1654,16 @@ mod tests { #[test] fn test_root_path() { - assert!(Path::new("a\\b\\c").root_path() == None); - assert!(Path::new("\\a\\b\\c").root_path() == Some(Path::new("\\"))); - assert!(Path::new("C:a").root_path() == Some(Path::new("C:"))); - assert!(Path::new("C:\\a").root_path() == Some(Path::new("C:\\"))); - assert!(Path::new("\\\\a\\b\\c").root_path() == Some(Path::new("\\\\a\\b"))); - assert!(Path::new("\\\\?\\a\\b").root_path() == Some(Path::new("\\\\?\\a"))); - assert!(Path::new("\\\\?\\C:\\a").root_path() == Some(Path::new("\\\\?\\C:\\"))); - assert!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path() == + assert_eq!(Path::new("a\\b\\c").root_path(), None); + assert_eq!(Path::new("\\a\\b\\c").root_path(), Some(Path::new("\\"))); + assert_eq!(Path::new("C:a").root_path(), Some(Path::new("C:"))); + assert_eq!(Path::new("C:\\a").root_path(), Some(Path::new("C:\\"))); + assert_eq!(Path::new("\\\\a\\b\\c").root_path(), Some(Path::new("\\\\a\\b"))); + assert_eq!(Path::new("\\\\?\\a\\b").root_path(), Some(Path::new("\\\\?\\a"))); + assert_eq!(Path::new("\\\\?\\C:\\a").root_path(), Some(Path::new("\\\\?\\C:\\"))); + assert_eq!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path(), Some(Path::new("\\\\?\\UNC\\a\\b"))); - assert!(Path::new("\\\\.\\a\\b").root_path() == Some(Path::new("\\\\.\\a"))); + assert_eq!(Path::new("\\\\.\\a\\b").root_path(), Some(Path::new("\\\\.\\a"))); } #[test] @@ -1731,10 +1740,8 @@ mod tests { let path = Path::new(pstr); let arg = $arg; let res = path.$op(arg); - let exp = $res; - assert!(res.as_str() == Some(exp), - "`{}`.{}(\"{}\"): Expected `{}`, found `{}`", - pstr, stringify!($op), arg, exp, res.as_str().unwrap()); + let exp = Path::new($res); + assert_eq!(res, exp); } ) } @@ -1817,7 +1824,7 @@ mod tests { let mut p1 = Path::new(path); p1.$set(arg); let p2 = Path::new(path); - assert!(p1 == p2.$with(arg)); + assert_eq!(p1, p2.$with(arg)); } ); (v: $path:expr, $set:ident, $with:ident, $arg:expr) => ( @@ -1827,7 +1834,7 @@ mod tests { let mut p1 = Path::new(path); p1.$set(arg); let p2 = Path::new(path); - assert!(p1 == p2.$with(arg)); + assert_eq!(p1, p2.$with(arg)); } ) } @@ -1858,31 +1865,19 @@ mod tests { (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { let path = $path; - let filename = $filename; - assert!(path.filename_str() == filename, - "`{}`.filename_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), filename, path.filename_str()); - let dirname = $dirname; - assert!(path.dirname_str() == dirname, - "`{}`.dirname_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), dirname, path.dirname_str()); - let filestem = $filestem; - assert!(path.filestem_str() == filestem, - "`{}`.filestem_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), filestem, path.filestem_str()); - let ext = $ext; - assert!(path.extension_str() == ext, - "`{}`.extension_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), ext, path.extension_str()); + assert_eq!(path.filename_str(), $filename); + assert_eq!(path.dirname_str(), $dirname); + assert_eq!(path.filestem_str(), $filestem); + assert_eq!(path.extension_str(), $ext); } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { let path = $path; - assert!(path.filename() == $filename); - assert!(path.dirname() == $dirname); - assert!(path.filestem() == $filestem); - assert!(path.extension() == $ext); + assert_eq!(path.filename(), $filename); + assert_eq!(path.dirname(), $dirname); + assert_eq!(path.filestem(), $filestem); + assert_eq!(path.extension(), $ext); } ) } @@ -1925,18 +1920,10 @@ mod tests { { let path = Path::new($path); let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel); - let b = path.is_absolute(); - assert!(b == abs, "Path '{}'.is_absolute(): expected {}, found {}", - path.as_str().unwrap(), abs, b); - let b = is_vol_relative(&path); - assert!(b == vol, "is_vol_relative('{}'): expected {}, found {}", - path.as_str().unwrap(), vol, b); - let b = is_cwd_relative(&path); - assert!(b == cwd, "is_cwd_relative('{}'): expected {}, found {}", - path.as_str().unwrap(), cwd, b); - let b = path.is_relative(); - assert!(b == rel, "Path '{}'.is_relativf(): expected {}, found {}", - path.as_str().unwrap(), rel, b); + assert_eq!(path.is_absolute(), abs); + assert_eq!(is_vol_relative(&path), vol); + assert_eq!(is_cwd_relative(&path), cwd); + assert_eq!(path.is_relative(), rel); } ) } @@ -1967,9 +1954,7 @@ mod tests { let dest = Path::new($dest); let exp = $exp; let res = path.is_ancestor_of(&dest); - assert!(res == exp, - "`{}`.is_ancestor_of(`{}`): Expected {}, found {}", - path.as_str().unwrap(), dest.as_str().unwrap(), exp, res); + assert_eq!(res, exp); } ) } @@ -2098,14 +2083,8 @@ mod tests { macro_rules! t { (s: $path:expr, $other:expr, $exp:expr) => ( { - let path = Path::new($path); - let other = Path::new($other); - let res = path.path_relative_from(&other); - let exp = $exp; - assert!(res.as_ref().and_then(|x| x.as_str()) == exp, - "`{}`.path_relative_from(`{}`): Expected {}, got {}", - path.as_str().unwrap(), other.as_str().unwrap(), exp, - res.as_ref().and_then(|x| x.as_str())); + assert_eq!(Path::new($path).path_relative_from(&Path::new($other)) + .as_ref().and_then(|x| x.as_str()), $exp); } ) } @@ -2314,7 +2293,7 @@ mod tests { let path = Path::new($path); let exp: Option<&str> = $exp; let exp = exp.map(|s| Path::new(s)); - assert!(make_non_verbatim(&path) == exp); + assert_eq!(make_non_verbatim(&path), exp); } ) } |
