diff options
| author | Johannes Hoff <johshoff@gmail.com> | 2014-12-24 13:22:11 +0100 |
|---|---|---|
| committer | Johannes Hoff <johshoff@gmail.com> | 2014-12-24 13:22:11 +0100 |
| commit | 0128159c95d0544e0c30b8b52ce3e7ce348fc114 (patch) | |
| tree | 8af4db0f2758f86434b895169122a9962fb79b21 /src/libstd/path | |
| parent | 8f827d33cab1be648120fc8ac34651d9cc079b5e (diff) | |
| parent | e64a8193b02ce72ef183274994a25eae281cb89c (diff) | |
| download | rust-0128159c95d0544e0c30b8b52ce3e7ce348fc114.tar.gz rust-0128159c95d0544e0c30b8b52ce3e7ce348fc114.zip | |
Merge branch 'master' into cfg_tmp_dir
Conflicts: src/etc/rustup.sh
Diffstat (limited to 'src/libstd/path')
| -rw-r--r-- | src/libstd/path/mod.rs | 142 | ||||
| -rw-r--r-- | src/libstd/path/posix.rs | 235 | ||||
| -rw-r--r-- | src/libstd/path/windows.rs | 377 |
3 files changed, 370 insertions, 384 deletions
diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index a185a29a700..30f3f56bc1c 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -8,62 +8,56 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -/*! - -Cross-platform path support - -This module implements support for two flavors of paths. `PosixPath` represents -a path on any unix-like system, whereas `WindowsPath` represents a path on -Windows. This module also exposes a typedef `Path` which is equal to the -appropriate platform-specific path variant. - -Both `PosixPath` and `WindowsPath` implement a trait `GenericPath`, which -contains the set of methods that behave the same for both paths. They each also -implement some methods that could not be expressed in `GenericPath`, yet behave -identically for both path flavors, such as `.components()`. - -The three main design goals of this module are 1) to avoid unnecessary -allocation, 2) to behave the same regardless of which flavor of path is being -used, and 3) to support paths that cannot be represented in UTF-8 (as Linux has -no restriction on paths beyond disallowing NUL). - -## Usage - -Usage of this module is fairly straightforward. Unless writing platform-specific -code, `Path` should be used to refer to the platform-native path. - -Creation of a path is typically done with either `Path::new(some_str)` or -`Path::new(some_vec)`. This path can be modified with `.push()` and -`.pop()` (and other setters). The resulting Path can either be passed to another -API that expects a path, or can be turned into a `&[u8]` with `.as_vec()` or a -`Option<&str>` with `.as_str()`. Similarly, attributes of the path can be queried -with methods such as `.filename()`. There are also methods that return a new -path instead of modifying the receiver, such as `.join()` or `.dir_path()`. - -Paths are always kept in normalized form. This means that creating the path -`Path::new("a/b/../c")` will return the path `a/c`. Similarly any attempt -to mutate the path will always leave it in normalized form. - -When rendering a path to some form of output, there is a method `.display()` -which is compatible with the `format!()` parameter `{}`. This will render the -path as a string, replacing all non-utf8 sequences with the Replacement -Character (U+FFFD). As such it is not suitable for passing to any API that -actually operates on the path; it is only intended for display. - -## Example - -```rust -use std::io::fs::PathExtensions; - -let mut path = Path::new("/tmp/path"); -println!("path: {}", path.display()); -path.set_filename("foo"); -path.push("bar"); -println!("new path: {}", path.display()); -println!("path exists: {}", path.exists()); -``` - -*/ +//! Cross-platform path support +//! +//! This module implements support for two flavors of paths. `PosixPath` represents a path on any +//! unix-like system, whereas `WindowsPath` represents a path on Windows. This module also exposes +//! a typedef `Path` which is equal to the appropriate platform-specific path variant. +//! +//! Both `PosixPath` and `WindowsPath` implement a trait `GenericPath`, which contains the set of +//! methods that behave the same for both paths. They each also implement some methods that could +//! not be expressed in `GenericPath`, yet behave identically for both path flavors, such as +//! `.components()`. +//! +//! The three main design goals of this module are 1) to avoid unnecessary allocation, 2) to behave +//! the same regardless of which flavor of path is being used, and 3) to support paths that cannot +//! be represented in UTF-8 (as Linux has no restriction on paths beyond disallowing NUL). +//! +//! ## Usage +//! +//! Usage of this module is fairly straightforward. Unless writing platform-specific code, `Path` +//! should be used to refer to the platform-native path. +//! +//! Creation of a path is typically done with either `Path::new(some_str)` or +//! `Path::new(some_vec)`. This path can be modified with `.push()` and `.pop()` (and other +//! setters). The resulting Path can either be passed to another API that expects a path, or can be +//! turned into a `&[u8]` with `.as_vec()` or a `Option<&str>` with `.as_str()`. Similarly, +//! attributes of the path can be queried with methods such as `.filename()`. There are also +//! methods that return a new path instead of modifying the receiver, such as `.join()` or +//! `.dir_path()`. +//! +//! Paths are always kept in normalized form. This means that creating the path +//! `Path::new("a/b/../c")` will return the path `a/c`. Similarly any attempt to mutate the path +//! will always leave it in normalized form. +//! +//! When rendering a path to some form of output, there is a method `.display()` which is +//! compatible with the `format!()` parameter `{}`. This will render the path as a string, +//! replacing all non-utf8 sequences with the Replacement Character (U+FFFD). As such it is not +//! suitable for passing to any API that actually operates on the path; it is only intended for +//! display. +//! +//! ## Example +//! +//! ```rust +//! use std::io::fs::PathExtensions; +//! +//! let mut path = Path::new("/tmp/path"); +//! println!("path: {}", path.display()); +//! path.set_filename("foo"); +//! path.push("bar"); +//! println!("new path: {}", path.display()); +//! println!("path exists: {}", path.exists()); +//! ``` #![experimental] @@ -71,13 +65,14 @@ use core::kinds::Sized; use c_str::CString; use clone::Clone; use fmt; -use iter::Iterator; -use option::{Option, None, Some}; +use iter::IteratorExt; +use option::Option; +use option::Option::{None, Some}; use str; -use str::{MaybeOwned, Str, StrPrelude}; +use str::{CowString, MaybeOwned, Str, StrExt}; use string::String; -use slice::{AsSlice, CloneSliceAllocPrelude}; -use slice::{PartialEqSlicePrelude, SlicePrelude}; +use slice::{AsSlice, CloneSliceExt}; +use slice::{PartialEqSliceExt, SliceExt}; use vec::Vec; /// Typedef for POSIX file paths. @@ -202,7 +197,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// ``` #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.as_vec()) + str::from_utf8(self.as_vec()).ok() } /// Returns the path as a byte vector @@ -298,7 +293,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// ``` #[inline] fn dirname_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.dirname()) + str::from_utf8(self.dirname()).ok() } /// Returns the file component of `self`, as a byte vector. @@ -332,7 +327,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// ``` #[inline] fn filename_str<'a>(&'a self) -> Option<&'a str> { - self.filename().and_then(str::from_utf8) + self.filename().and_then(|s| str::from_utf8(s).ok()) } /// Returns the stem of the filename of `self`, as a byte vector. @@ -378,7 +373,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// ``` #[inline] fn filestem_str<'a>(&'a self) -> Option<&'a str> { - self.filestem().and_then(str::from_utf8) + self.filestem().and_then(|s| str::from_utf8(s).ok()) } /// Returns the extension of the filename of `self`, as an optional byte vector. @@ -425,7 +420,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// ``` #[inline] fn extension_str<'a>(&'a self) -> Option<&'a str> { - self.extension().and_then(str::from_utf8) + self.extension().and_then(|s| str::from_utf8(s).ok()) } /// Replaces the filename portion of the path with the given byte vector or string. @@ -798,7 +793,7 @@ pub trait BytesContainer for Sized? { /// Returns the receiver interpreted as a utf-8 string, if possible #[inline] fn container_as_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.container_as_bytes()) + str::from_utf8(self.container_as_bytes()).ok() } /// Returns whether .container_as_str() is guaranteed to not fail // FIXME (#8888): Remove unused arg once ::<for T> works @@ -830,7 +825,7 @@ pub struct Display<'a, P:'a> { impl<'a, P: GenericPath> fmt::Show for Display<'a, P> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_maybe_owned().as_slice().fmt(f) + self.as_cow().fmt(f) } } @@ -840,7 +835,7 @@ impl<'a, P: GenericPath> Display<'a, P> { /// If the path is not UTF-8, invalid sequences will be replaced with the /// Unicode replacement char. This involves allocation. #[inline] - pub fn as_maybe_owned(&self) -> MaybeOwned<'a> { + pub fn as_cow(&self) -> CowString<'a> { String::from_utf8_lossy(if self.filename { match self.path.filename() { None => { @@ -875,7 +870,7 @@ impl BytesContainer for String { } #[inline] fn container_as_str(&self) -> Option<&str> { - Some(self.as_slice()) + Some(self[]) } #[inline] fn is_str(_: Option<&String>) -> bool { true } @@ -891,7 +886,7 @@ impl BytesContainer for [u8] { impl BytesContainer for Vec<u8> { #[inline] fn container_as_bytes(&self) -> &[u8] { - self.as_slice() + self[] } } @@ -902,6 +897,7 @@ impl BytesContainer for CString { } } +#[allow(deprecated)] impl<'a> BytesContainer for str::MaybeOwned<'a> { #[inline] fn container_as_bytes<'b>(&'b self) -> &'b [u8] { @@ -936,8 +932,6 @@ fn contains_nul<T: BytesContainer>(v: &T) -> bool { #[cfg(test)] mod tests { use prelude::*; - use super::{GenericPath, PosixPath, WindowsPath}; - use c_str::ToCStr; #[test] fn test_cstring() { @@ -947,6 +941,6 @@ mod tests { let input = r"\foo\bar\baz"; let path: WindowsPath = WindowsPath::new(input.to_c_str()); - assert_eq!(path.as_str().unwrap(), input.as_slice()); + assert_eq!(path.as_str().unwrap(), input); } } diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 2b444fdc32b..f0a00b421c3 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -15,23 +15,25 @@ use clone::Clone; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use hash; use io::Writer; -use iter::{DoubleEndedIterator, AdditiveIterator, Extend, Iterator, Map}; +use iter::{DoubleEndedIteratorExt, AdditiveIterator, Extend}; +use iter::{Iterator, IteratorExt, Map}; +use option::Option; +use option::Option::{None, Some}; use kinds::Sized; -use option::{Option, None, Some}; use str::{FromStr, Str}; use str; -use slice::{CloneSliceAllocPrelude, Splits, AsSlice, VectorVector, - PartialEqSlicePrelude, SlicePrelude}; +use slice::{CloneSliceExt, Splits, AsSlice, VectorVector, + PartialEqSliceExt, SliceExt}; use vec::Vec; use super::{BytesContainer, GenericPath, GenericPathUnsafe}; /// Iterator that yields successive components of a Path as &[u8] -pub type Components<'a> = Splits<'a, u8>; +pub type Components<'a> = Splits<'a, u8, fn(&u8) -> bool>; /// Iterator that yields successive components of a Path as Option<&str> -pub type StrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>, - Components<'a>>; +pub type StrComponents<'a> = + Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>; /// Represents a POSIX file path #[deriving(Clone)] @@ -130,7 +132,7 @@ impl GenericPathUnsafe for Path { unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { let filename = filename.container_as_bytes(); match self.sepidx { - None if b".." == self.repr.as_slice() => { + None if b".." == self.repr => { let mut v = Vec::with_capacity(3 + filename.len()); v.push_all(dot_dot_static); v.push(SEP_BYTE); @@ -157,7 +159,7 @@ impl GenericPathUnsafe for Path { self.repr = Path::normalize(v.as_slice()); } } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); } unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) { @@ -173,7 +175,7 @@ impl GenericPathUnsafe for Path { // FIXME: this is slow self.repr = Path::normalize(v.as_slice()); } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); } } } @@ -190,7 +192,7 @@ impl GenericPath for Path { fn dirname<'a>(&'a self) -> &'a [u8] { match self.sepidx { - None if b".." == self.repr.as_slice() => self.repr.as_slice(), + 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(), @@ -200,8 +202,8 @@ impl GenericPath for Path { fn filename<'a>(&'a self) -> Option<&'a [u8]> { match self.sepidx { - None if b"." == self.repr.as_slice() || - b".." == self.repr.as_slice() => None, + 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, @@ -211,20 +213,20 @@ impl GenericPath for Path { fn pop(&mut self) -> bool { match self.sepidx { - None if b"." == self.repr.as_slice() => false, + None if b"." == self.repr => false, None => { self.repr = vec![b'.']; self.sepidx = None; true } - Some(0) if b"/" == self.repr.as_slice() => false, + Some(0) if b"/" == self.repr => false, Some(idx) => { if idx == 0 { self.repr.truncate(idx+1); } else { self.repr.truncate(idx); } - self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE); + self.sepidx = self.repr.rposition_elem(&SEP_BYTE); true } } @@ -249,7 +251,7 @@ impl GenericPath for Path { } else { let mut ita = self.components(); let mut itb = other.components(); - if b"." == self.repr.as_slice() { + if b"." == self.repr { return match itb.next() { None => true, Some(b) => b != b".." @@ -304,7 +306,7 @@ impl GenericPath for Path { } } } - Some(Path::new(comps.as_slice().connect_vec(&SEP_BYTE))) + Some(Path::new(comps.connect_vec(&SEP_BYTE))) } } @@ -388,6 +390,7 @@ impl Path { let v = if self.repr[0] == SEP_BYTE { self.repr[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); if v.is_empty() { // consume the empty "" component @@ -399,13 +402,17 @@ impl Path { /// Returns an iterator that yields each component of the path as Option<&str>. /// See components() for details. pub fn str_components<'a>(&'a self) -> StrComponents<'a> { - self.components().map(str::from_utf8) + fn from_utf8(s: &[u8]) -> Option<&str> { + str::from_utf8(s).ok() + } + let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr + self.components().map(f) } } // None result means the byte vector didn't need normalizing fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> { - if is_abs && v.as_slice().is_empty() { + if is_abs && v.is_empty() { return None; } let mut comps: Vec<&'a [u8]> = vec![]; @@ -442,11 +449,9 @@ static dot_dot_static: &'static [u8] = b".."; mod tests { use prelude::*; use super::*; - use mem; use str; - use str::StrPrelude; - macro_rules! t( + macro_rules! t { (s: $path:expr, $exp:expr) => ( { let path = $path; @@ -459,7 +464,7 @@ mod tests { assert!(path.as_vec() == $exp); } ) - ) + } #[test] fn test_paths() { @@ -495,8 +500,8 @@ mod tests { t!(s: Path::new("foo/../../.."), "../.."); t!(s: Path::new("foo/../../bar"), "../bar"); - assert_eq!(Path::new(b"foo/bar").into_vec().as_slice(), b"foo/bar"); - assert_eq!(Path::new(b"/foo/../../bar").into_vec().as_slice(), + assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar"); + assert_eq!(Path::new(b"/foo/../../bar").into_vec(), b"/bar"); let p = Path::new(b"foo/bar\x80"); @@ -513,62 +518,62 @@ mod tests { #[test] fn test_null_byte() { - use task; - let result = task::try(proc() { + use thread::Thread; + let result = Thread::spawn(move|| { Path::new(b"foo/bar\0") - }); + }).join(); assert!(result.is_err()); - let result = task::try(proc() { + let result = Thread::spawn(move|| { Path::new("test").set_filename(b"f\0o") - }); + }).join(); assert!(result.is_err()); - let result = task::try(proc() { + let result = Thread::spawn(move|| { Path::new("test").push(b"f\0o"); - }); + }).join(); assert!(result.is_err()); } #[test] fn test_display_str() { - macro_rules! t( + macro_rules! t { ($path:expr, $disp:ident, $exp:expr) => ( { let path = Path::new($path); - assert!(path.$disp().to_string().as_slice() == $exp); + assert!(path.$disp().to_string() == $exp); } ) - ) + } t!("foo", display, "foo"); - t!(b"foo\x80", display, "foo\uFFFD"); - t!(b"foo\xFFbar", display, "foo\uFFFDbar"); + t!(b"foo\x80", display, "foo\u{FFFD}"); + t!(b"foo\xFFbar", display, "foo\u{FFFD}bar"); t!(b"foo\xFF/bar", filename_display, "bar"); - t!(b"foo/\xFFbar", filename_display, "\uFFFDbar"); + t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar"); t!(b"/", filename_display, ""); macro_rules! t( ($path:expr, $exp:expr) => ( { let path = Path::new($path); - let mo = path.display().as_maybe_owned(); + let mo = path.display().as_cow(); assert!(mo.as_slice() == $exp); } ); ($path:expr, $exp:expr, filename) => ( { let path = Path::new($path); - let mo = path.filename_display().as_maybe_owned(); + let mo = path.filename_display().as_cow(); assert!(mo.as_slice() == $exp); } ) - ) + ); t!("foo", "foo"); - t!(b"foo\x80", "foo\uFFFD"); - t!(b"foo\xFFbar", "foo\uFFFDbar"); + t!(b"foo\x80", "foo\u{FFFD}"); + t!(b"foo\xFFbar", "foo\u{FFFD}bar"); t!(b"foo\xFF/bar", "bar", filename); - t!(b"foo/\xFFbar", "\uFFFDbar", filename); + t!(b"foo/\xFFbar", "\u{FFFD}bar", filename); t!(b"/", "", filename); } @@ -579,20 +584,20 @@ mod tests { { let path = Path::new($path); let f = format!("{}", path.display()); - assert!(f.as_slice() == $exp); + assert!(f == $exp); let f = format!("{}", path.filename_display()); - assert!(f.as_slice() == $expf); + assert!(f == $expf); } ) - ) + ); t!(b"foo", "foo", "foo"); t!(b"foo/bar", "foo/bar", "bar"); t!(b"/", "/", ""); - t!(b"foo\xFF", "foo\uFFFD", "foo\uFFFD"); - t!(b"foo\xFF/bar", "foo\uFFFD/bar", "bar"); - t!(b"foo/\xFFbar", "foo/\uFFFDbar", "\uFFFDbar"); - t!(b"\xFFfoo/bar\xFF", "\uFFFDfoo/bar\uFFFD", "bar\uFFFD"); + t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}"); + t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar"); + t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar"); + t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}"); } #[test] @@ -600,10 +605,8 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - unsafe { - let path = Path::new($path); - assert!(path.$op() == mem::transmute(($exp).as_bytes())); - } + let path = Path::new($path); + assert!(path.$op() == ($exp).as_bytes()); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( @@ -615,14 +618,12 @@ mod tests { ); (v: $path:expr, $op:ident, $exp:expr) => ( { - unsafe { - let arg = $path; - let path = Path::new(arg); - assert!(path.$op() == mem::transmute($exp)); - } + let arg = $path; + let path = Path::new(arg); + assert!(path.$op() == $exp); } ); - ) + ); t!(v: b"a/b/c", filename, Some(b"c")); t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF")); @@ -667,9 +668,8 @@ mod tests { t!(v: b"hi/there.txt", extension, Some(b"txt")); t!(v: b"hi/there\x80.txt", extension, Some(b"txt")); t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt")); - let no: Option<&'static [u8]> = None; - t!(v: b"hi/there", extension, no); - t!(v: b"hi/there\x80", extension, no); + t!(v: b"hi/there", extension, None); + t!(v: b"hi/there\x80", extension, None); t!(s: "hi/there.txt", extension, Some("txt"), opt); t!(s: "hi/there", extension, None, opt); t!(s: "there.txt", extension, Some("txt"), opt); @@ -697,7 +697,7 @@ mod tests { assert!(p1 == p2.join(join)); } ) - ) + ); t!(s: "a/b/c", ".."); t!(s: "/a/b/c", "d"); @@ -716,7 +716,7 @@ mod tests { assert!(p.as_str() == Some($exp)); } ) - ) + ); t!(s: "a/b/c", "d", "a/b/c/d"); t!(s: "/a/b/c", "d", "/a/b/c/d"); @@ -743,7 +743,7 @@ mod tests { assert!(p.as_vec() == $exp); } ) - ) + ); t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e"); t!(s: "a/b/c", ["d", "/e"], "/e"); @@ -773,7 +773,7 @@ mod tests { assert!(result == $right); } ) - ) + ); t!(b: b"a/b/c", b"a/b", true); t!(b: b"a", b".", true); @@ -821,7 +821,7 @@ mod tests { assert!(res.as_str() == Some($exp)); } ) - ) + ); t!(s: "a/b/c", "..", "a/b"); t!(s: "/a/b/c", "d", "/a/b/c/d"); @@ -848,7 +848,7 @@ mod tests { assert!(res.as_vec() == $exp); } ) - ) + ); t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e"); t!(s: "a/b/c", ["..", "d"], "a/b/d"); @@ -932,7 +932,7 @@ mod tests { assert!(p1 == p2.$with(arg)); } ) - ) + ); t!(v: b"a/b/c", set_filename, with_filename, b"d"); t!(v: b"/", set_filename, with_filename, b"foo"); @@ -958,62 +958,57 @@ mod tests { macro_rules! t( (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - unsafe { - 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() == mem::transmute(ext), - "`{}`.extension_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), ext, path.extension_str()); - } + 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()); } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - unsafe { - let path = $path; - assert!(path.filename() == mem::transmute($filename)); - assert!(path.dirname() == mem::transmute($dirname)); - assert!(path.filestem() == mem::transmute($filestem)); - assert!(path.extension() == mem::transmute($ext)); - } + let path = $path; + assert!(path.filename() == $filename); + assert!(path.dirname() == $dirname); + assert!(path.filestem() == $filestem); + assert!(path.extension() == $ext); } ) - ) + ); - let no: Option<&'static str> = None; - t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), no); - t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), no); + t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None); + t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None); t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi", Some(b"there"), Some(b"\xFF")); - t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), no); - t!(s: Path::new("."), None, Some("."), None, no); - t!(s: Path::new("/"), None, Some("/"), None, no); - t!(s: Path::new(".."), None, Some(".."), None, no); - t!(s: Path::new("../.."), None, Some("../.."), None, no); + t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None); + t!(s: Path::new("."), None, Some("."), None, None); + t!(s: Path::new("/"), None, Some("/"), None, None); + t!(s: Path::new(".."), None, Some(".."), None, None); + t!(s: Path::new("../.."), None, Some("../.."), None, None); t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"), Some("there"), Some("txt")); - t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), no); + t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None); t!(s: Path::new("hi/there."), Some("there."), Some("hi"), Some("there"), Some("")); - t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), no); + t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None); t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"), Some("."), Some("there")); - t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, no); + t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None); t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt")); - t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), no); - t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), no); + t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None); + t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None); } #[test] @@ -1038,7 +1033,7 @@ mod tests { assert_eq!(path.is_relative(), $rel); } ) - ) + ); t!(s: "a/b/c", false, true); t!(s: "/a/b/c", true, false); t!(s: "a", false, true); @@ -1059,7 +1054,7 @@ mod tests { assert_eq!(path.is_ancestor_of(&dest), $exp); } ) - ) + ); t!(s: "a/b/c", "a/b/c/d", true); t!(s: "a/b/c", "a/b/c", true); @@ -1100,7 +1095,7 @@ mod tests { assert_eq!(path.ends_with_path(&child), $exp); } ) - ) + ); t!(s: "a/b/c", "c", true); t!(s: "a/b/c", "d", false); @@ -1133,7 +1128,7 @@ mod tests { assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp); } ) - ) + ); t!(s: "a/b/c", "a/b", Some("c")); t!(s: "a/b/c", "a/b/d", Some("../c")); @@ -1189,13 +1184,13 @@ mod tests { let path = Path::new($arg); let comps = path.components().collect::<Vec<&[u8]>>(); let exp: &[&[u8]] = &[$($exp),*]; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.components().rev().collect::<Vec<&[u8]>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>(); assert_eq!(comps, exp) } ) - ) + ); t!(b: b"a/b/c", [b"a", b"b", b"c"]); t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]); @@ -1221,13 +1216,13 @@ mod tests { let path = Path::new($arg); let comps = path.str_components().collect::<Vec<Option<&str>>>(); let exp: &[Option<&str>] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.str_components().rev().collect::<Vec<Option<&str>>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>(); assert_eq!(comps, exp); } ) - ) + ); t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]); t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]); diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index f31ffdab17b..7d10188c437 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -12,7 +12,7 @@ //! Windows file path handling -pub use self::PathPrefix::*; +use self::PathPrefix::*; use ascii::AsciiCast; use c_str::{CString, ToCStr}; @@ -20,12 +20,14 @@ use clone::Clone; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use hash; use io::Writer; -use iter::{AdditiveIterator, DoubleEndedIterator, Extend, Iterator, Map}; +use iter::{AdditiveIterator, DoubleEndedIteratorExt, Extend}; +use iter::{Iterator, IteratorExt, Map}; use mem; -use option::{Option, Some, None}; -use slice::{AsSlice, SlicePrelude}; -use str::{CharSplits, FromStr, Str, StrAllocating, StrVector, StrPrelude}; -use string::String; +use option::Option; +use option::Option::{Some, None}; +use slice::SliceExt; +use str::{CharSplits, FromStr, StrVector, StrExt}; +use string::{String, ToString}; use unicode::char::UnicodeChar; use vec::Vec; @@ -35,12 +37,12 @@ use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe}; /// /// Each component is yielded as Option<&str> for compatibility with PosixPath, but /// every component in WindowsPath is guaranteed to be Some. -pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>, - CharSplits<'a, char>>; +pub type StrComponents<'a> = + Map<&'a str, Option<&'a str>, CharSplits<'a, char>, fn(&'a str) -> Option<&'a str>>; /// Iterator that yields successive components of a Path as &[u8] -pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8], - StrComponents<'a>>; +pub type Components<'a> = + Map<Option<&'a str>, &'a [u8], StrComponents<'a>, fn(Option<&str>) -> &[u8]>; /// Represents a Windows path // Notes for Windows path impl: @@ -180,35 +182,35 @@ impl GenericPathUnsafe for Path { unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { let filename = filename.container_as_str().unwrap(); match self.sepidx_or_prefix_len() { - None if ".." == self.repr.as_slice() => { + None if ".." == self.repr => { let mut s = String::with_capacity(3 + filename.len()); s.push_str(".."); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } None => { self.update_normalized(filename); } - Some((_,idxa,end)) if self.repr.as_slice().slice(idxa,end) == ".." => { + Some((_,idxa,end)) if self.repr[idxa..end] == ".." => { let mut s = String::with_capacity(end + 1 + filename.len()); - s.push_str(self.repr.as_slice().slice_to(end)); + s.push_str(self.repr[0..end]); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } 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.as_slice().slice_to(idxb)); + s.push_str(self.repr[0..idxb]); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } Some((idxb,_,_)) => { let mut s = String::with_capacity(idxb + 1 + filename.len()); - s.push_str(self.repr.as_slice().slice_to(idxb)); + s.push_str(self.repr[0..idxb]); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } } } @@ -227,18 +229,18 @@ 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.slice_from(prefix_len(prefix)); + let rest = path[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.as_slice(); + let repr = me.repr[]; match me.prefix { Some(DiskPrefix) => { - repr.as_bytes()[0] == path.as_bytes()[0].to_ascii().to_uppercase().to_byte() + repr.as_bytes()[0] == path.as_bytes()[0].to_ascii().to_uppercase().as_byte() } Some(VerbatimDiskPrefix) => { - repr.as_bytes()[4] == path.as_bytes()[0].to_ascii().to_uppercase().to_byte() + repr.as_bytes()[4] == path.as_bytes()[0].to_ascii().to_uppercase().as_byte() } _ => false } @@ -264,7 +266,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.as_slice()); + s.push_str(me.repr[]); let plen = me.prefix_len(); // if me is "C:" we don't want to add a path separator match me.prefix { @@ -276,9 +278,9 @@ impl GenericPathUnsafe for Path { } match path_ { None => s.push_str(path), - Some(p) => s.push_str(p.as_slice()) + Some(p) => s.push_str(p[]), }; - me.update_normalized(s) + me.update_normalized(s[]) } if !path.is_empty() { @@ -286,7 +288,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.slice_from(prefix_len(prefix))); + append_path(self, path[prefix_len(prefix)..]); } Some(_) => { // absolute path, or cwd-relative and self is not same volume @@ -332,7 +334,7 @@ impl GenericPath for Path { /// Always returns a `Some` value. #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - Some(self.repr.as_slice()) + Some(self.repr[]) } #[inline] @@ -354,21 +356,17 @@ 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.as_slice() => self.repr.as_slice(), + None if ".." == self.repr => self.repr[], None => ".", - Some((_,idxa,end)) if self.repr.as_slice().slice(idxa, end) == ".." => { - self.repr.as_slice() - } - Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => { - self.repr.as_slice() - } - Some((0,idxa,_)) => self.repr.as_slice().slice_to(idxa), + 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((idxb,idxa,_)) => { match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => { - self.repr.as_slice().slice_to(idxa) + self.repr[0..idxa] } - _ => self.repr.as_slice().slice_to(idxb) + _ => self.repr[0..idxb] } } }) @@ -382,13 +380,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.as_slice(); + let repr = self.repr[]; match self.sepidx_or_prefix_len() { None if "." == repr || ".." == repr => None, None => Some(repr), - Some((_,idxa,end)) if repr.slice(idxa, end) == ".." => None, + Some((_,idxa,end)) if repr[idxa..end] == ".." => None, Some((_,idxa,end)) if idxa == end => None, - Some((_,idxa,end)) => Some(repr.slice(idxa, end)) + Some((_,idxa,end)) => Some(repr[idxa..end]) } } @@ -413,14 +411,14 @@ impl GenericPath for Path { #[inline] fn pop(&mut self) -> bool { match self.sepidx_or_prefix_len() { - None if "." == self.repr.as_slice() => false, + None if "." == self.repr => false, None => { self.repr = String::from_str("."); self.sepidx = None; true } Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false, - Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => false, + Some((idxb,_,end)) if self.repr[idxb..end] == "\\" => false, Some((idxb,idxa,_)) => { let trunc = match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => { @@ -440,15 +438,15 @@ impl GenericPath for Path { if self.prefix.is_some() { Some(Path::new(match self.prefix { Some(DiskPrefix) if self.is_absolute() => { - self.repr.as_slice().slice_to(self.prefix_len()+1) + self.repr[0..self.prefix_len()+1] } Some(VerbatimDiskPrefix) => { - self.repr.as_slice().slice_to(self.prefix_len()+1) + self.repr[0..self.prefix_len()+1] } - _ => self.repr.as_slice().slice_to(self.prefix_len()) + _ => self.repr[0..self.prefix_len()] })) } else if is_vol_relative(self) { - Some(Path::new(self.repr.as_slice().slice_to(1))) + Some(Path::new(self.repr[0..1])) } else { None } @@ -467,7 +465,7 @@ impl GenericPath for Path { fn is_absolute(&self) -> bool { match self.prefix { Some(DiskPrefix) => { - let rest = self.repr.as_slice().slice_from(self.prefix_len()); + let rest = self.repr[self.prefix_len()..]; rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE } Some(_) => true, @@ -489,7 +487,7 @@ impl GenericPath for Path { } else { let mut ita = self.str_components().map(|x|x.unwrap()); let mut itb = other.str_components().map(|x|x.unwrap()); - if "." == self.repr.as_slice() { + if "." == self.repr { return itb.next() != Some(".."); } loop { @@ -642,18 +640,19 @@ 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.as_slice(); + let repr = self.repr[]; let s = match self.prefix { Some(_) => { let plen = self.prefix_len(); if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE { - repr.slice_from(plen+1) - } else { repr.slice_from(plen) } + repr[plen+1..] + } else { repr[plen..] } } - None if repr.as_bytes()[0] == SEP_BYTE => repr.slice_from(1), + None if repr.as_bytes()[0] == SEP_BYTE => repr[1..], None => repr }; - let ret = s.split_terminator(SEP).map(Some); + let some: fn(&'a str) -> Option<&'a str> = Some; // coerce to fn ptr + let ret = s.split_terminator(SEP).map(some); ret } @@ -664,47 +663,51 @@ impl Path { #![inline] x.unwrap().as_bytes() } + let convert: for<'b> fn(Option<&'b str>) -> &'b [u8] = convert; // coerce to fn ptr self.str_components().map(convert) } fn equiv_prefix(&self, other: &Path) -> bool { - let s_repr = self.repr.as_slice(); - let o_repr = other.repr.as_slice(); + let s_repr = self.repr[]; + let o_repr = other.repr[]; match (self.prefix, other.prefix) { (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => { self.is_absolute() && - s_repr.as_bytes()[0].to_ascii().eq_ignore_case(o_repr.as_bytes()[4].to_ascii()) + s_repr.as_bytes()[0].to_ascii().to_lowercase() == + o_repr.as_bytes()[4].to_ascii().to_lowercase() } (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => { other.is_absolute() && - s_repr.as_bytes()[4].to_ascii().eq_ignore_case(o_repr.as_bytes()[0].to_ascii()) + s_repr.as_bytes()[4].to_ascii().to_lowercase() == + o_repr.as_bytes()[0].to_ascii().to_lowercase() } (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => { - s_repr.as_bytes()[4].to_ascii().eq_ignore_case(o_repr.as_bytes()[4].to_ascii()) + s_repr.as_bytes()[4].to_ascii().to_lowercase() == + o_repr.as_bytes()[4].to_ascii().to_lowercase() } (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => { - s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len()) + s_repr[2..self.prefix_len()] == o_repr[8..other.prefix_len()] } (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => { - s_repr.slice(8, self.prefix_len()) == o_repr.slice(2, other.prefix_len()) + s_repr[8..self.prefix_len()] == o_repr[2..other.prefix_len()] } (None, None) => true, (a, b) if a == b => { - s_repr.slice_to(self.prefix_len()) == o_repr.slice_to(other.prefix_len()) + s_repr[0..self.prefix_len()] == o_repr[0..other.prefix_len()] } _ => false } } - fn normalize_<S: StrAllocating>(s: S) -> (Option<PathPrefix>, String) { + fn normalize_(s: &str) -> (Option<PathPrefix>, String) { // make borrowck happy let (prefix, val) = { - let prefix = parse_prefix(s.as_slice()); - let path = Path::normalize__(s.as_slice(), prefix); + let prefix = parse_prefix(s); + let path = Path::normalize__(s, prefix); (prefix, path) }; (prefix, match val { - None => s.into_string(), + None => s.to_string(), Some(val) => val }) } @@ -744,13 +747,10 @@ impl Path { match prefix.unwrap() { DiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s.slice_to(len)); + let mut s = String::from_str(s[0..len]); unsafe { let v = s.as_mut_vec(); - v[0] = (*v)[0] - .to_ascii() - .to_uppercase() - .to_byte(); + v[0] = (*v)[0].to_ascii().to_uppercase().as_byte(); } if is_abs { // normalize C:/ to C:\ @@ -762,24 +762,24 @@ impl Path { } VerbatimDiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s.slice_to(len)); + let mut s = String::from_str(s[0..len]); unsafe { let v = s.as_mut_vec(); - v[4] = (*v)[4].to_ascii().to_uppercase().to_byte(); + v[4] = (*v)[4].to_ascii().to_uppercase().as_byte(); } Some(s) } _ => { let plen = prefix_len(prefix); if s.len() > plen { - Some(String::from_str(s.slice_to(plen))) + Some(String::from_str(s[0..plen])) } else { None } } } } else if is_abs && comps.is_empty() { Some(String::from_char(1, SEP)) } else { - let prefix_ = s.slice_to(prefix_len(prefix)); + let prefix_ = s[0..prefix_len(prefix)]; let n = prefix_.len() + if is_abs { comps.len() } else { comps.len() - 1} + comps.iter().map(|v| v.len()).sum(); @@ -787,20 +787,20 @@ impl Path { match prefix { Some(DiskPrefix) => { s.push(prefix_.as_bytes()[0].to_ascii() - .to_uppercase().to_char()); + .to_uppercase().as_char()); s.push(':'); } Some(VerbatimDiskPrefix) => { - s.push_str(prefix_.slice_to(4)); + s.push_str(prefix_[0..4]); s.push(prefix_.as_bytes()[4].to_ascii() - .to_uppercase().to_char()); - s.push_str(prefix_.slice_from(5)); + .to_uppercase().as_char()); + s.push_str(prefix_[5..]); } Some(UNCPrefix(a,b)) => { s.push_str("\\\\"); - s.push_str(prefix_.slice(2, a+2)); + s.push_str(prefix_[2..a+2]); s.push(SEP); - s.push_str(prefix_.slice(3+a, 3+a+b)); + s.push_str(prefix_[3+a..3+a+b]); } Some(_) => s.push_str(prefix_), None => () @@ -825,10 +825,14 @@ impl Path { fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { - self.repr.as_slice().slice_to(self.repr.len()-1) - } else { self.repr.as_slice() }; - let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } - else { is_sep_verbatim }); + self.repr[0..self.repr.len()-1] + } else { self.repr[] }; + let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) { + is_sep + } else { + is_sep_verbatim + }; + let idx = s.rfind(sep_test); let prefixlen = self.prefix_len(); self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) }); } @@ -858,8 +862,8 @@ impl Path { self.repr.as_bytes()[self.repr.len()-1] == SEP_BYTE } - fn update_normalized<S: Str>(&mut self, s: S) { - let (prefix, path) = Path::normalize_(s.as_slice()); + fn update_normalized(&mut self, s: &str) { + let (prefix, path) = Path::normalize_(s); self.repr = path; self.prefix = prefix; self.update_sepidx(); @@ -901,17 +905,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.as_slice(); + let repr = path.repr[]; 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.slice_from(4)) + Path::new(repr[4..]) } Some(VerbatimUNCPrefix(_,_)) => { // \\?\UNC\server\share - Path::new(format!(r"\{}", repr.slice_from(7))) + Path::new(format!(r"\{}", repr[7..])) } }; if new_path.prefix.is_none() { @@ -920,8 +924,8 @@ pub fn make_non_verbatim(path: &Path) -> Option<Path> { return None; } // now ensure normalization didn't change anything - if repr.slice_from(path.prefix_len()) == - new_path.repr.as_slice().slice_from(new_path.prefix_len()) { + if repr[path.prefix_len()..] == + new_path.repr[new_path.prefix_len()..] { Some(new_path) } else { None @@ -967,7 +971,7 @@ pub fn is_sep_byte_verbatim(u: &u8) -> bool { } /// Prefix types for Path -#[deriving(PartialEq, Clone, Show)] +#[deriving(Copy, PartialEq, Clone, Show)] pub enum PathPrefix { /// Prefix `\\?\`, uint is the length of the following component VerbatimPrefix(uint), @@ -986,13 +990,13 @@ pub enum PathPrefix { fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { if path.starts_with("\\\\") { // \\ - path = path.slice_from(2); + path = path[2..]; if path.starts_with("?\\") { // \\?\ - path = path.slice_from(2); + path = path[2..]; if path.starts_with("UNC\\") { // \\?\UNC\server\share - path = path.slice_from(4); + path = path[4..]; let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) { Some(x) => x, None => (path.len(), 0) @@ -1013,7 +1017,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { } } else if path.starts_with(".\\") { // \\.\path - path = path.slice_from(2); + path = path[2..]; let idx = path.find('\\').unwrap_or(path.len()); return Some(DeviceNSPrefix(idx)); } @@ -1033,13 +1037,12 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { } return None; - fn parse_two_comps<'a>(mut path: &'a str, f: |char| -> bool) - -> Option<(uint, uint)> { - let idx_a = match path.find(|x| f(x)) { + fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> { + let idx_a = match path.find(f) { None => return None, Some(x) => x }; - path = path.slice_from(idx_a+1); + path = path[idx_a+1..]; let idx_b = path.find(f).unwrap_or(path.len()); Some((idx_a, idx_b)) } @@ -1047,10 +1050,14 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> { // None result means the string didn't need normalizing fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) { - let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim }; + let f: fn(char) -> bool = if !prefix_is_verbatim(prefix) { + is_sep + } else { + is_sep_verbatim + }; let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix))); - let s_ = s.slice_from(prefix_len(prefix)); - let s_ = if is_abs { s_.slice_from(1) } else { s_ }; + let s_ = s[prefix_len(prefix)..]; + let s_ = if is_abs { s_[1..] } else { s_ }; if is_abs && s_.is_empty() { return (is_abs, match prefix { @@ -1114,12 +1121,12 @@ fn prefix_len(p: Option<PathPrefix>) -> uint { #[cfg(test)] mod tests { - use mem; use prelude::*; use super::*; + use super::PathPrefix::*; use super::parse_prefix; - macro_rules! t( + macro_rules! t { (s: $path:expr, $exp:expr) => ( { let path = $path; @@ -1132,7 +1139,7 @@ mod tests { assert!(path.as_vec() == $exp); } ) - ) + } #[test] fn test_parse_prefix() { @@ -1146,7 +1153,7 @@ mod tests { "parse_prefix(\"{}\"): expected {}, found {}", path, exp, res); } ) - ) + ); t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5))); t!("\\\\", None); @@ -1232,8 +1239,8 @@ mod tests { t!(s: Path::new("foo\\..\\..\\.."), "..\\.."); t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar"); - assert_eq!(Path::new(b"foo\\bar").into_vec().as_slice(), b"foo\\bar"); - assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec().as_slice(), b"\\bar"); + assert_eq!(Path::new(b"foo\\bar").into_vec(), b"foo\\bar"); + assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec(), b"\\bar"); t!(s: Path::new("\\\\a"), "\\a"); t!(s: Path::new("\\\\a\\"), "\\a"); @@ -1295,20 +1302,20 @@ mod tests { #[test] fn test_null_byte() { - use task; - let result = task::try(proc() { + use thread::Thread; + let result = Thread::spawn(move|| { Path::new(b"foo/bar\0") - }); + }).join(); assert!(result.is_err()); - let result = task::try(proc() { + let result = Thread::spawn(move|| { Path::new("test").set_filename(b"f\0o") - }); + }).join(); assert!(result.is_err()); - let result = task::try(proc() { + let result = Thread::spawn(move || { Path::new("test").push(b"f\0o"); - }); + }).join(); assert!(result.is_err()); } @@ -1321,15 +1328,15 @@ mod tests { #[test] fn test_display_str() { let path = Path::new("foo"); - assert_eq!(path.display().to_string(), "foo".to_string()); + assert_eq!(path.display().to_string(), "foo"); let path = Path::new(b"\\"); - assert_eq!(path.filename_display().to_string(), "".to_string()); + assert_eq!(path.filename_display().to_string(), ""); let path = Path::new("foo"); - let mo = path.display().as_maybe_owned(); + let mo = path.display().as_cow(); assert_eq!(mo.as_slice(), "foo"); let path = Path::new(b"\\"); - let mo = path.filename_display().as_maybe_owned(); + let mo = path.filename_display().as_cow(); assert_eq!(mo.as_slice(), ""); } @@ -1340,12 +1347,12 @@ mod tests { { let path = Path::new($path); let f = format!("{}", path.display()); - assert_eq!(f.as_slice(), $exp); + assert_eq!(f, $exp); let f = format!("{}", path.filename_display()); - assert_eq!(f.as_slice(), $expf); + assert_eq!(f, $expf); } ) - ) + ); t!("foo", "foo", "foo"); t!("foo\\bar", "foo\\bar", "bar"); @@ -1357,11 +1364,9 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - unsafe { - let path = $path; - let path = Path::new(path); - assert!(path.$op() == Some(mem::transmute($exp))); - } + let path = $path; + let path = Path::new(path); + assert!(path.$op() == Some($exp)); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( @@ -1374,14 +1379,12 @@ mod tests { ); (v: $path:expr, $op:ident, $exp:expr) => ( { - unsafe { - let path = $path; - let path = Path::new(path); - assert!(path.$op() == mem::transmute($exp)); - } + let path = $path; + let path = Path::new(path); + assert!(path.$op() == $exp); } ) - ) + ); t!(v: b"a\\b\\c", filename, Some(b"c")); t!(s: "a\\b\\c", filename_str, "c"); @@ -1463,8 +1466,7 @@ mod tests { // filestem is based on filename, so we don't need the full set of prefix tests t!(v: b"hi\\there.txt", extension, Some(b"txt")); - let no: Option<&'static [u8]> = None; - t!(v: b"hi\\there", extension, no); + t!(v: b"hi\\there", extension, None); t!(s: "hi\\there.txt", extension_str, Some("txt"), opt); t!(s: "hi\\there", extension_str, None, opt); t!(s: "there.txt", extension_str, Some("txt"), opt); @@ -1493,7 +1495,7 @@ mod tests { assert!(p1 == p2.join(join)); } ) - ) + ); t!(s: "a\\b\\c", ".."); t!(s: "\\a\\b\\c", "d"); @@ -1526,7 +1528,7 @@ mod tests { assert_eq!(p.as_str(), Some($exp)); } ) - ) + ); t!(s: "a\\b\\c", "d", "a\\b\\c\\d"); t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d"); @@ -1584,7 +1586,7 @@ mod tests { assert_eq!(p.as_vec(), $exp); } ) - ) + ); t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e"); t!(s: "a\\b\\c", ["d", "\\e"], "\\e"); @@ -1619,7 +1621,7 @@ mod tests { assert!(result == $right); } ) - ) + ); t!(s: "a\\b\\c", "a\\b", true); t!(s: "a", ".", true); @@ -1696,7 +1698,7 @@ mod tests { assert_eq!(res.as_str(), Some($exp)); } ) - ) + ); t!(s: "a\\b\\c", "..", "a\\b"); t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d"); @@ -1725,7 +1727,7 @@ mod tests { assert_eq!(res.as_vec(), $exp); } ) - ) + ); t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e"); t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d"); @@ -1751,7 +1753,7 @@ mod tests { pstr, stringify!($op), arg, exp, res.as_str().unwrap()); } ) - ) + ); t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d"); t!(s: ".", with_filename, "foo", "foo"); @@ -1844,7 +1846,7 @@ mod tests { assert!(p1 == p2.$with(arg)); } ) - ) + ); t!(v: b"a\\b\\c", set_filename, with_filename, b"d"); t!(v: b"\\", set_filename, with_filename, b"foo"); @@ -1871,53 +1873,48 @@ mod tests { macro_rules! t( (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - unsafe { - 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() == mem::transmute(ext), - "`{}`.extension_str(): Expected `{}`, found `{}`", - path.as_str().unwrap(), ext, path.extension_str()); - } + 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()); } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - unsafe { - let path = $path; - assert!(path.filename() == mem::transmute($filename)); - assert!(path.dirname() == mem::transmute($dirname)); - assert!(path.filestem() == mem::transmute($filestem)); - assert!(path.extension() == mem::transmute($ext)); - } + let path = $path; + assert!(path.filename() == $filename); + assert!(path.dirname() == $dirname); + assert!(path.filestem() == $filestem); + assert!(path.extension() == $ext); } ) - ) + ); - let no: Option<&'static str> = None; - t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), no); - t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), no); - t!(s: Path::new("."), None, Some("."), None, no); - t!(s: Path::new("\\"), None, Some("\\"), None, no); - t!(s: Path::new(".."), None, Some(".."), None, no); - t!(s: Path::new("..\\.."), None, Some("..\\.."), None, no); + t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None); + t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None); + t!(s: Path::new("."), None, Some("."), None, None); + t!(s: Path::new("\\"), None, Some("\\"), None, None); + t!(s: Path::new(".."), None, Some(".."), None, None); + t!(s: Path::new("..\\.."), None, Some("..\\.."), None, None); t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"), Some("there"), Some("txt")); - t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), no); + t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None); t!(s: Path::new("hi\\there."), Some("there."), Some("hi"), Some("there"), Some("")); - t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), no); + t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None); t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"), Some("."), Some("there")); @@ -1958,7 +1955,7 @@ mod tests { path.as_str().unwrap(), rel, b); } ) - ) + ); t!("a\\b\\c", false, false, false, true); t!("\\a\\b\\c", false, true, false, false); t!("a", false, false, false, true); @@ -1991,7 +1988,7 @@ mod tests { path.as_str().unwrap(), dest.as_str().unwrap(), exp, res); } ) - ) + ); t!(s: "a\\b\\c", "a\\b\\c\\d", true); t!(s: "a\\b\\c", "a\\b\\c", true); @@ -2090,7 +2087,7 @@ mod tests { assert_eq!(path.ends_with_path(&child), $exp); } ); - ) + ); t!(s: "a\\b\\c", "c", true); t!(s: "a\\b\\c", "d", false); @@ -2127,7 +2124,7 @@ mod tests { res.as_ref().and_then(|x| x.as_str())); } ) - ) + ); t!(s: "a\\b\\c", "a\\b", Some("c")); t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c")); @@ -2255,14 +2252,14 @@ mod tests { let comps = path.str_components().map(|x|x.unwrap()) .collect::<Vec<&str>>(); let exp: &[&str] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.str_components().rev().map(|x|x.unwrap()) .collect::<Vec<&str>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>(); assert_eq!(comps, exp); } ); - ) + ); t!(s: b"a\\b\\c", ["a", "b", "c"]); t!(s: "a\\b\\c", ["a", "b", "c"]); @@ -2312,13 +2309,13 @@ mod tests { let path = Path::new($path); let comps = path.components().collect::<Vec<&[u8]>>(); let exp: &[&[u8]] = &$exp; - assert_eq!(comps.as_slice(), exp); + assert_eq!(comps, exp); let comps = path.components().rev().collect::<Vec<&[u8]>>(); let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>(); assert_eq!(comps, exp); } ) - ) + ); t!(s: "a\\b\\c", [b"a", b"b", b"c"]); t!(s: ".", [b"."]); @@ -2336,7 +2333,7 @@ mod tests { assert!(make_non_verbatim(&path) == exp); } ) - ) + ); t!(r"\a\b\c", Some(r"\a\b\c")); t!(r"a\b\c", Some(r"a\b\c")); |
