diff options
| author | Kevin Ballard <kevin@sb.org> | 2013-10-05 19:49:32 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2013-10-15 22:18:30 -0700 |
| commit | d6d9b926836b1f1c2b8b3fe4ab35dc63bec7ffcd (patch) | |
| tree | e197783b86700e71d94c9bc6d0254eb25b16cc0c /src/libstd | |
| parent | ed539e14712539473c3e89604cb69e2307110772 (diff) | |
| download | rust-d6d9b926836b1f1c2b8b3fe4ab35dc63bec7ffcd.tar.gz rust-d6d9b926836b1f1c2b8b3fe4ab35dc63bec7ffcd.zip | |
path2: Adjust the API to remove all the _str mutation methods
Add a new trait BytesContainer that is implemented for both byte vectors and strings. Convert Path::from_vec and ::from_str to one function, Path::new(). Remove all the _str-suffixed mutation methods (push, join, with_*, set_*) and modify the non-suffixed versions to use BytesContainer.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 24 | ||||
| -rw-r--r-- | src/libstd/os.rs | 62 | ||||
| -rw-r--r-- | src/libstd/path/mod.rs | 469 | ||||
| -rw-r--r-- | src/libstd/path/posix.rs | 618 | ||||
| -rw-r--r-- | src/libstd/path/windows.rs | 848 | ||||
| -rw-r--r-- | src/libstd/rt/io/file.rs | 28 | ||||
| -rw-r--r-- | src/libstd/rt/io/support.rs | 2 | ||||
| -rw-r--r-- | src/libstd/rt/uv/file.rs | 12 | ||||
| -rw-r--r-- | src/libstd/rt/uv/uvio.rs | 13 | ||||
| -rw-r--r-- | src/libstd/run.rs | 4 | ||||
| -rw-r--r-- | src/libstd/unstable/dynamic_lib.rs | 2 |
11 files changed, 992 insertions, 1090 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 434d781805b..c03fc6d7ffb 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1893,7 +1893,7 @@ mod tests { #[test] fn test_simple() { - let tmpfile = &Path::from_str("tmp/lib-io-test-simple.tmp"); + let tmpfile = &Path::new("tmp/lib-io-test-simple.tmp"); debug2!("{}", tmpfile.display()); let frood: ~str = ~"A hoopy frood who really knows where his towel is."; @@ -1911,7 +1911,7 @@ mod tests { #[test] fn test_each_byte_each_char_file() { // Issue #5056 -- shouldn't include trailing EOF. - let path = Path::from_str("tmp/lib-io-test-each-byte-each-char-file.tmp"); + let path = Path::new("tmp/lib-io-test-each-byte-each-char-file.tmp"); { // create empty, enough to reproduce a problem @@ -2011,7 +2011,7 @@ mod tests { #[test] fn file_reader_not_exist() { - match io::file_reader(&Path::from_str("not a file")) { + match io::file_reader(&Path::new("not a file")) { Err(e) => { assert_eq!(e, ~"error opening not a file"); } @@ -2022,7 +2022,7 @@ mod tests { #[test] #[should_fail] fn test_read_buffer_too_small() { - let path = &Path::from_str("tmp/lib-io-test-read-buffer-too-small.tmp"); + let path = &Path::new("tmp/lib-io-test-read-buffer-too-small.tmp"); // ensure the file exists io::file_writer(path, [io::Create]).unwrap(); @@ -2033,7 +2033,7 @@ mod tests { #[test] fn test_read_buffer_big_enough() { - let path = &Path::from_str("tmp/lib-io-test-read-buffer-big-enough.tmp"); + let path = &Path::new("tmp/lib-io-test-read-buffer-big-enough.tmp"); // ensure the file exists io::file_writer(path, [io::Create]).unwrap(); @@ -2044,14 +2044,14 @@ mod tests { #[test] fn test_write_empty() { - let file = io::file_writer(&Path::from_str("tmp/lib-io-test-write-empty.tmp"), + let file = io::file_writer(&Path::new("tmp/lib-io-test-write-empty.tmp"), [io::Create]).unwrap(); file.write([]); } #[test] fn file_writer_bad_name() { - match io::file_writer(&Path("?/?"), []) { + match io::file_writer(&Path::new("?/?"), []) { Err(e) => { assert!(e.starts_with("error opening")); } @@ -2076,7 +2076,7 @@ mod tests { #[test] fn test_read_write_le() { - let path = Path::from_str("tmp/lib-io-test-read-write-le.tmp"); + let path = Path::new("tmp/lib-io-test-read-write-le.tmp"); let uints = [0, 1, 2, 42, 10_123, 100_123_456, u64::max_value]; // write the ints to the file @@ -2098,7 +2098,7 @@ mod tests { #[test] fn test_read_write_be() { - let path = Path::from_str("tmp/lib-io-test-read-write-be.tmp"); + let path = Path::new("tmp/lib-io-test-read-write-be.tmp"); let uints = [0, 1, 2, 42, 10_123, 100_123_456, u64::max_value]; // write the ints to the file @@ -2120,7 +2120,7 @@ mod tests { #[test] fn test_read_be_int_n() { - let path = Path::from_str("tmp/lib-io-test-read-be-int-n.tmp"); + let path = Path::new("tmp/lib-io-test-read-be-int-n.tmp"); let ints = [i32::min_value, -123456, -42, -5, 0, 1, i32::max_value]; // write the ints to the file @@ -2144,7 +2144,7 @@ mod tests { #[test] fn test_read_f32() { - let path = Path::from_str("tmp/lib-io-test-read-f32.tmp"); + let path = Path::new("tmp/lib-io-test-read-f32.tmp"); //big-endian floating-point 8.1250 let buf = ~[0x41, 0x02, 0x00, 0x00]; @@ -2162,7 +2162,7 @@ mod tests { #[test] fn test_read_write_f32() { - let path = Path::from_str("tmp/lib-io-test-read-write-f32.tmp"); + let path = Path::new("tmp/lib-io-test-read-write-f32.tmp"); let f:f32 = 8.1250; { diff --git a/src/libstd/os.rs b/src/libstd/os.rs index bfde1a86771..449888b4e2a 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -513,7 +513,7 @@ pub fn self_exe_path() -> Option<Path> { } } - load_self().and_then(|path| Path::from_vec_opt(path).map(|p| p.dir_path())) + load_self().and_then(|path| Path::new_opt(path).map(|p| { p.pop(); p })) } @@ -538,7 +538,7 @@ pub fn self_exe_path() -> Option<Path> { pub fn homedir() -> Option<Path> { // FIXME (#7188): getenv needs a ~[u8] variant return match getenv("HOME") { - Some(ref p) if !p.is_empty() => Path::from_str_opt(*p), + Some(ref p) if !p.is_empty() => Path::new_opt(p.as_slice()), _ => secondary() }; @@ -551,7 +551,7 @@ pub fn homedir() -> Option<Path> { fn secondary() -> Option<Path> { do getenv("USERPROFILE").and_then |p| { if !p.is_empty() { - Path::from_str_opt(p) + Path::new_opt(p) } else { None } @@ -580,7 +580,7 @@ pub fn tmpdir() -> Path { if x.is_empty() { None } else { - Path::from_str_opt(x) + Path::new_opt(x) }, _ => None } @@ -589,9 +589,9 @@ pub fn tmpdir() -> Path { #[cfg(unix)] fn lookup() -> Path { if cfg!(target_os = "android") { - Path::from_str("/data/tmp") + Path::new("/data/tmp") } else { - getenv_nonempty("TMPDIR").unwrap_or(Path::from_str("/tmp")) + getenv_nonempty("TMPDIR").unwrap_or(Path::new("/tmp")) } } @@ -600,7 +600,7 @@ pub fn tmpdir() -> Path { getenv_nonempty("TMP").or( getenv_nonempty("TEMP").or( getenv_nonempty("USERPROFILE").or( - getenv_nonempty("WINDIR")))).unwrap_or(Path::from_str("C:\\Windows")) + getenv_nonempty("WINDIR")))).unwrap_or(Path::new("C:\\Windows")) } } @@ -762,7 +762,7 @@ pub fn list_dir(p: &Path) -> ~[Path] { fn rust_list_dir_wfd_size() -> libc::size_t; fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void) -> *u16; } - let star = p.join_str("*"); + let star = p.join("*"); do as_utf16_p(star.as_str().unwrap()) |path_ptr| { let mut paths = ~[]; let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint); @@ -778,7 +778,7 @@ pub fn list_dir(p: &Path) -> ~[Path] { let fp_vec = vec::from_buf( fp_buf, wcslen(fp_buf) as uint); let fp_str = str::from_utf16(fp_vec); - paths.push(Path::from_str(fp_str)); + paths.push(Path::new(fp_str)); } more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE); } @@ -1830,13 +1830,13 @@ mod tests { #[test] fn test() { - assert!((!Path::from_str("test-path").is_absolute())); + assert!((!Path::new("test-path").is_absolute())); let cwd = getcwd(); debug2!("Current working directory: {}", cwd.display()); - debug2!("{:?}", make_absolute(&Path::from_str("test-path"))); - debug2!("{:?}", make_absolute(&Path::from_str("/usr/bin"))); + debug2!("{:?}", make_absolute(&Path::new("test-path"))); + debug2!("{:?}", make_absolute(&Path::new("/usr/bin"))); } #[test] @@ -1845,7 +1845,7 @@ mod tests { let oldhome = getenv("HOME"); setenv("HOME", "/home/MountainView"); - assert_eq!(os::homedir(), Some(Path::from_str("/home/MountainView"))); + assert_eq!(os::homedir(), Some(Path::new("/home/MountainView"))); setenv("HOME", ""); assert!(os::homedir().is_none()); @@ -1866,16 +1866,16 @@ mod tests { assert!(os::homedir().is_none()); setenv("HOME", "/home/MountainView"); - assert_eq!(os::homedir(), Some(Path::from_str("/home/MountainView"))); + assert_eq!(os::homedir(), Some(Path::new("/home/MountainView"))); setenv("HOME", ""); setenv("USERPROFILE", "/home/MountainView"); - assert_eq!(os::homedir(), Some(Path::from_str("/home/MountainView"))); + assert_eq!(os::homedir(), Some(Path::new("/home/MountainView"))); setenv("HOME", "/home/MountainView"); setenv("USERPROFILE", "/home/PaloAlto"); - assert_eq!(os::homedir(), Some(Path::from_str("/home/MountainView"))); + assert_eq!(os::homedir(), Some(Path::new("/home/MountainView"))); for s in oldhome.iter() { setenv("HOME", *s) } for s in olduserprofile.iter() { setenv("USERPROFILE", *s) } @@ -1891,12 +1891,12 @@ mod tests { // Issue #712 #[test] fn test_list_dir_no_invalid_memory_access() { - os::list_dir(&Path::from_str(".")); + os::list_dir(&Path::new(".")); } #[test] fn list_dir() { - let dirs = os::list_dir(&Path::from_str(".")); + let dirs = os::list_dir(&Path::new(".")); // Just assuming that we've got some contents in the current directory assert!(dirs.len() > 0u); @@ -1908,35 +1908,35 @@ mod tests { #[test] #[cfg(not(windows))] fn list_dir_root() { - let dirs = os::list_dir(&Path::from_str("/")); + let dirs = os::list_dir(&Path::new("/")); assert!(dirs.len() > 1); } #[test] #[cfg(windows)] fn list_dir_root() { - let dirs = os::list_dir(&Path::from_str("C:\\")); + let dirs = os::list_dir(&Path::new("C:\\")); assert!(dirs.len() > 1); } #[test] fn path_is_dir() { - assert!((os::path_is_dir(&Path::from_str(".")))); - assert!((!os::path_is_dir(&Path::from_str("test/stdtest/fs.rs")))); + assert!((os::path_is_dir(&Path::new(".")))); + assert!((!os::path_is_dir(&Path::new("test/stdtest/fs.rs")))); } #[test] fn path_exists() { - assert!((os::path_exists(&Path::from_str(".")))); - assert!((!os::path_exists(&Path::from_str( + assert!((os::path_exists(&Path::new(".")))); + assert!((!os::path_exists(&Path::new( "test/nonexistent-bogus-path")))); } #[test] fn copy_file_does_not_exist() { - assert!(!os::copy_file(&Path::from_str("test/nonexistent-bogus-path"), - &Path::from_str("test/other-bogus-path"))); - assert!(!os::path_exists(&Path::from_str("test/other-bogus-path"))); + assert!(!os::copy_file(&Path::new("test/nonexistent-bogus-path"), + &Path::new("test/other-bogus-path"))); + assert!(!os::path_exists(&Path::new("test/other-bogus-path"))); } #[test] @@ -1946,8 +1946,8 @@ mod tests { unsafe { let tempdir = getcwd(); // would like to use $TMPDIR, // doesn't seem to work on Linux - let input = tempdir.join_str("in.txt"); - let out = tempdir.join_str("out.txt"); + let input = tempdir.join("in.txt"); + let out = tempdir.join("out.txt"); /* Write the temp input file */ let ostream = do input.with_c_str |fromp| { @@ -1983,7 +1983,7 @@ mod tests { #[test] fn recursive_mkdir_slash() { - let path = Path::from_str("/"); + let path = Path::new("/"); assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32)); } @@ -2032,7 +2032,7 @@ mod tests { } let mut path = tmpdir(); - path.push_str("mmap_file.tmp"); + path.push("mmap_file.tmp"); let size = MemoryMap::granularity() * 2; remove_file(&path); diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 1ecb31a2a87..561155f2258 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -20,8 +20,7 @@ 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 `::from_str()` or -`.component_iter()`. +identically for both path flavors, such as `.component_iter()`. 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 @@ -35,8 +34,8 @@ code, `Path` should be used to refer to the platform-native path, and methods used should be restricted to those defined in `GenericPath`, and those methods that are declared identically on both `PosixPath` and `WindowsPath`. -Creation of a path is typically done with either `Path::from_str(some_str)` or -`Path::from_vec(some_vec)`. This path can be modified with `.push()` and +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 @@ -44,10 +43,10 @@ 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::from_str("a/b/../c")` will return the path `a/c`. Similarly any attempt +`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 display, there is a method `.display()` +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 @@ -56,10 +55,10 @@ actually operates on the path; it is only intended for display. ## Example ```rust -let mut path = Path::from_str("/tmp/path"); +let mut path = Path::new("/tmp/path"); debug2!("path: {}", path.display()); -path.set_filename_str("foo"); -path.push_str("bar"); +path.set_filename("foo"); +path.push("bar"); debug2!("new path: {}", path.display()); let b = std::os::path_exists(&path); debug2!("path exists: {}", b); @@ -70,6 +69,7 @@ debug2!("path exists: {}", b); use container::Container; use c_str::CString; use clone::Clone; +use either::{Left, Right}; use fmt; use iter::Iterator; use option::{Option, None, Some}; @@ -131,7 +131,7 @@ condition! { /// A trait that represents the generic operations available on paths pub trait GenericPath: Clone + GenericPathUnsafe { - /// Creates a new Path from a byte vector. + /// Creates a new Path from a byte vector or string. /// The resulting Path will always be normalized. /// /// # Failure @@ -140,52 +140,24 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// /// See individual Path impls for additional restrictions. #[inline] - fn from_vec(path: &[u8]) -> Self { - if contains_nul(path) { - let path = self::null_byte::cond.raise(path.to_owned()); + fn new<T: BytesContainer>(path: T) -> Self { + if contains_nul(path.container_as_bytes()) { + let path = self::null_byte::cond.raise(path.container_into_owned_bytes()); assert!(!contains_nul(path)); - unsafe { GenericPathUnsafe::from_vec_unchecked(path) } + unsafe { GenericPathUnsafe::new_unchecked(path) } } else { - unsafe { GenericPathUnsafe::from_vec_unchecked(path) } + unsafe { GenericPathUnsafe::new_unchecked(path) } } } - /// Creates a new Path from a byte vector, if possible. + /// Creates a new Path from a byte vector or string, if possible. /// The resulting Path will always be normalized. #[inline] - fn from_vec_opt(path: &[u8]) -> Option<Self> { - if contains_nul(path) { + fn new_opt<T: BytesContainer>(path: T) -> Option<Self> { + if contains_nul(path.container_as_bytes()) { None } else { - Some(unsafe { GenericPathUnsafe::from_vec_unchecked(path) }) - } - } - - /// Creates a new Path from a string. - /// The resulting Path will always be normalized. - /// - /// # Failure - /// - /// Raises the `null_byte` condition if the path contains a NUL. - #[inline] - fn from_str(path: &str) -> Self { - let v = path.as_bytes(); - if contains_nul(v) { - GenericPath::from_vec(path.as_bytes()) // let from_vec handle the condition - } else { - unsafe { GenericPathUnsafe::from_str_unchecked(path) } - } - } - - /// Creates a new Path from a string, if possible. - /// The resulting Path will always be normalized. - #[inline] - fn from_str_opt(path: &str) -> Option<Self> { - let v = path.as_bytes(); - if contains_nul(v) { - None - } else { - Some(unsafe { GenericPathUnsafe::from_str_unchecked(path) }) + Some(unsafe { GenericPathUnsafe::new_unchecked(path) }) } } @@ -199,7 +171,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { let v = path.as_bytes(); // v is NUL-terminated. Strip it off let v = v.slice_to(v.len()-1); - unsafe { GenericPathUnsafe::from_vec_unchecked(v) } + unsafe { GenericPathUnsafe::new_unchecked(v) } } /// Returns the path as a string, if possible. @@ -209,9 +181,15 @@ pub trait GenericPath: Clone + GenericPathUnsafe { str::from_utf8_slice_opt(self.as_vec()) } + /// Converts the Path into an owned string, if possible + fn into_str(self) -> Option<~str>; + /// Returns the path as a byte vector fn as_vec<'a>(&'a self) -> &'a [u8]; + /// Converts the Path into an owned byte vector + fn into_vec(self) -> ~[u8]; + /// Provides the path as a string /// /// If the path is not UTF-8, invalid sequences will be replaced with the unicode @@ -345,115 +323,91 @@ pub trait GenericPath: Clone + GenericPathUnsafe { self.extension().and_then(str::from_utf8_slice_opt) } - /// Replaces the directory portion of the path with the given byte vector. + /// Replaces the directory portion of the path with the given byte vector or string. /// If `self` represents the root of the filesystem hierarchy, the last path component - /// of the given byte vector becomes the filename. + /// of the argument becomes the filename. /// /// # Failure /// /// Raises the `null_byte` condition if the dirname contains a NUL. #[inline] - fn set_dirname(&mut self, dirname: &[u8]) { - if contains_nul(dirname) { - let dirname = self::null_byte::cond.raise(dirname.to_owned()); + fn set_dirname<T: BytesContainer>(&mut self, dirname: T) { + if contains_nul(dirname.container_as_bytes()) { + let dirname = self::null_byte::cond.raise(dirname.container_into_owned_bytes()); assert!(!contains_nul(dirname)); unsafe { self.set_dirname_unchecked(dirname) } } else { unsafe { self.set_dirname_unchecked(dirname) } } } - /// Replaces the directory portion of the path with the given string. - /// See `set_dirname` for details. - #[inline] - fn set_dirname_str(&mut self, dirname: &str) { - if contains_nul(dirname.as_bytes()) { - self.set_dirname(dirname.as_bytes()) // triggers null_byte condition - } else { - unsafe { self.set_dirname_str_unchecked(dirname) } - } - } - /// Replaces the filename portion of the path with the given byte vector. + /// Replaces the filename portion of the path with the given byte vector or string. /// If the replacement name is [], this is equivalent to popping the path. /// /// # Failure /// /// Raises the `null_byte` condition if the filename contains a NUL. #[inline] - fn set_filename(&mut self, filename: &[u8]) { - if contains_nul(filename) { - let filename = self::null_byte::cond.raise(filename.to_owned()); + fn set_filename<T: BytesContainer>(&mut self, filename: T) { + if contains_nul(filename.container_as_bytes()) { + let filename = self::null_byte::cond.raise(filename.container_into_owned_bytes()); assert!(!contains_nul(filename)); unsafe { self.set_filename_unchecked(filename) } } else { unsafe { self.set_filename_unchecked(filename) } } } - /// Replaces the filename portion of the path with the given string. - /// See `set_filename` for details. - #[inline] - fn set_filename_str(&mut self, filename: &str) { - if contains_nul(filename.as_bytes()) { - self.set_filename(filename.as_bytes()) // triggers null_byte condition - } else { - unsafe { self.set_filename_str_unchecked(filename) } - } - } - /// Replaces the filestem with the given byte vector. + /// Replaces the filestem with the given byte vector or string. /// If there is no extension in `self` (or `self` has no filename), this is equivalent - /// to `set_filename`. Otherwise, if the given byte vector is [], the extension (including + /// to `set_filename`. Otherwise, if the argument is [] or "", the extension (including /// the preceding '.') becomes the new filename. /// /// # Failure /// /// Raises the `null_byte` condition if the filestem contains a NUL. - fn set_filestem(&mut self, filestem: &[u8]) { + fn set_filestem<T: BytesContainer>(&mut self, filestem: T) { // borrowck is being a pain here let val = { match self.filename() { - None => None, + None => Left(filestem), Some(name) => { let dot = '.' as u8; match name.rposition_elem(&dot) { - None | Some(0) => None, + None | Some(0) => Left(filestem), Some(idx) => { let mut v; - if contains_nul(filestem) { - let filestem = self::null_byte::cond.raise(filestem.to_owned()); + if contains_nul(filestem.container_as_bytes()) { + let filestem = filestem.container_into_owned_bytes(); + let filestem = self::null_byte::cond.raise(filestem); assert!(!contains_nul(filestem)); v = filestem; let n = v.len(); v.reserve(n + name.len() - idx); } else { + let filestem = filestem.container_as_bytes(); v = vec::with_capacity(filestem.len() + name.len() - idx); v.push_all(filestem); } v.push_all(name.slice_from(idx)); - Some(v) + Right(v) } } } } }; match val { - None => self.set_filename(filestem), - Some(v) => unsafe { self.set_filename_unchecked(v) } + Left(v) => self.set_filename(v), + Right(v) => unsafe { self.set_filename_unchecked(v) } } } - /// Replaces the filestem with the given string. - /// See `set_filestem` for details. - #[inline] - fn set_filestem_str(&mut self, filestem: &str) { - self.set_filestem(filestem.as_bytes()) - } - /// Replaces the extension with the given byte vector. + /// Replaces the extension with the given byte vector or string. /// If there is no extension in `self`, this adds one. - /// If the given byte vector is [], this removes the extension. + /// If the argument is [] or "", this removes the extension. /// If `self` has no filename, this is a no-op. /// /// # Failure /// /// Raises the `null_byte` condition if the extension contains a NUL. - fn set_extension(&mut self, extension: &[u8]) { + fn set_extension<T: BytesContainer>(&mut self, extension: T) { // borrowck causes problems here too let val = { match self.filename() { @@ -462,12 +416,12 @@ pub trait GenericPath: Clone + GenericPathUnsafe { let dot = '.' as u8; match name.rposition_elem(&dot) { None | Some(0) => { - if extension.is_empty() { + if extension.container_as_bytes().is_empty() { None } else { let mut v; - if contains_nul(extension) { - let ext = extension.to_owned(); + if contains_nul(extension.container_as_bytes()) { + let ext = extension.container_into_owned_bytes(); let extension = self::null_byte::cond.raise(ext); assert!(!contains_nul(extension)); v = vec::with_capacity(name.len() + extension.len() + 1); @@ -475,6 +429,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { v.push(dot); v.push_all(extension); } else { + let extension = extension.container_as_bytes(); v = vec::with_capacity(name.len() + extension.len() + 1); v.push_all(name); v.push(dot); @@ -484,18 +439,19 @@ pub trait GenericPath: Clone + GenericPathUnsafe { } } Some(idx) => { - if extension.is_empty() { + if extension.container_as_bytes().is_empty() { Some(name.slice_to(idx).to_owned()) } else { let mut v; - if contains_nul(extension) { - let ext = extension.to_owned(); + if contains_nul(extension.container_as_bytes()) { + let ext = extension.container_into_owned_bytes(); let extension = self::null_byte::cond.raise(ext); assert!(!contains_nul(extension)); v = vec::with_capacity(idx + extension.len() + 1); v.push_all(name.slice_to(idx+1)); v.push_all(extension); } else { + let extension = extension.container_as_bytes(); v = vec::with_capacity(idx + extension.len() + 1); v.push_all(name.slice_to(idx+1)); v.push_all(extension); @@ -512,31 +468,25 @@ pub trait GenericPath: Clone + GenericPathUnsafe { Some(v) => unsafe { self.set_filename_unchecked(v) } } } - /// Replaces the extension with the given string. - /// See `set_extension` for details. - #[inline] - fn set_extension_str(&mut self, extension: &str) { - self.set_extension(extension.as_bytes()) - } - /// Adds the given extension (as a byte vector) to the file. + /// Adds the given extension (as a byte vector or string) to the file. /// This does not remove any existing extension. /// `foo.bar`.add_extension(`baz`) becomes `foo.bar.baz`. /// If `self` has no filename, this is a no-op. - /// If the given byte vector is [], this is a no-op. + /// If the argument is [] or "", this is a no-op. /// /// # Failure /// /// Raises the `null_byte` condition if the extension contains a NUL. - fn add_extension(&mut self, extension: &[u8]) { - if extension.is_empty() { return; } + fn add_extension<T: BytesContainer>(&mut self, extension: T) { + if extension.container_as_bytes().is_empty() { return; } // appease borrowck let val = { match self.filename() { None => None, Some(name) => { let mut v; - if contains_nul(extension) { - let ext = extension.to_owned(); + if contains_nul(extension.container_as_bytes()) { + let ext = extension.container_into_owned_bytes(); let extension = self::null_byte::cond.raise(ext); assert!(!contains_nul(extension)); v = vec::with_capacity(name.len() + 1 + extension.len()); @@ -544,6 +494,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { v.push('.' as u8); v.push_all(extension); } else { + let extension = extension.container_as_bytes(); v = vec::with_capacity(name.len() + 1 + extension.len()); v.push_all(name); v.push('.' as u8); @@ -558,105 +509,71 @@ pub trait GenericPath: Clone + GenericPathUnsafe { Some(v) => unsafe { self.set_filename_unchecked(v) } } } - /// Adds the given extension (as a string) to the file. - /// See `add_extension` for details. - #[inline] - fn add_extension_str(&mut self, extension: &str) { - self.add_extension(extension.as_bytes()) - } - /// Returns a new Path constructed by replacing the dirname with the given byte vector. + /// Returns a new Path constructed by replacing the dirname with the given + /// byte vector or string. /// See `set_dirname` for details. /// /// # Failure /// /// Raises the `null_byte` condition if the dirname contains a NUL. #[inline] - fn with_dirname(&self, dirname: &[u8]) -> Self { + fn with_dirname<T: BytesContainer>(&self, dirname: T) -> Self { let mut p = self.clone(); p.set_dirname(dirname); p } - /// Returns a new Path constructed by replacing the dirname with the given string. - /// See `set_dirname` for details. - #[inline] - fn with_dirname_str(&self, dirname: &str) -> Self { - let mut p = self.clone(); - p.set_dirname_str(dirname); - p - } - /// Returns a new Path constructed by replacing the filename with the given byte vector. + /// Returns a new Path constructed by replacing the filename with the given + /// byte vector or string. /// See `set_filename` for details. /// /// # Failure /// /// Raises the `null_byte` condition if the filename contains a NUL. #[inline] - fn with_filename(&self, filename: &[u8]) -> Self { + fn with_filename<T: BytesContainer>(&self, filename: T) -> Self { let mut p = self.clone(); p.set_filename(filename); p } - /// Returns a new Path constructed by replacing the filename with the given string. - /// See `set_filename` for details. - #[inline] - fn with_filename_str(&self, filename: &str) -> Self { - let mut p = self.clone(); - p.set_filename_str(filename); - p - } - /// Returns a new Path constructed by setting the filestem to the given byte vector. + /// Returns a new Path constructed by setting the filestem to the given + /// byte vector or string. /// See `set_filestem` for details. /// /// # Failure /// /// Raises the `null_byte` condition if the filestem contains a NUL. #[inline] - fn with_filestem(&self, filestem: &[u8]) -> Self { + fn with_filestem<T: BytesContainer>(&self, filestem: T) -> Self { let mut p = self.clone(); p.set_filestem(filestem); p } - /// Returns a new Path constructed by setting the filestem to the given string. - /// See `set_filestem` for details. - #[inline] - fn with_filestem_str(&self, filestem: &str) -> Self { - let mut p = self.clone(); - p.set_filestem_str(filestem); - p - } - /// Returns a new Path constructed by setting the extension to the given byte vector. + /// Returns a new Path constructed by setting the extension to the given + /// byte vector or string. /// See `set_extension` for details. /// /// # Failure /// /// Raises the `null_byte` condition if the extension contains a NUL. #[inline] - fn with_extension(&self, extension: &[u8]) -> Self { + fn with_extension<T: BytesContainer>(&self, extension: T) -> Self { let mut p = self.clone(); p.set_extension(extension); p } - /// Returns a new Path constructed by setting the extension to the given string. - /// See `set_extension` for details. - #[inline] - fn with_extension_str(&self, extension: &str) -> Self { - let mut p = self.clone(); - p.set_extension_str(extension); - p - } /// Returns the directory component of `self`, as a Path. /// If `self` represents the root of the filesystem hierarchy, returns `self`. fn dir_path(&self) -> Self { // self.dirname() returns a NUL-free vector - unsafe { GenericPathUnsafe::from_vec_unchecked(self.dirname()) } + unsafe { GenericPathUnsafe::new_unchecked(self.dirname()) } } /// Returns the file component of `self`, as a relative Path. /// If `self` represents the root of the filesystem hierarchy, returns None. fn file_path(&self) -> Option<Self> { // self.filename() returns a NUL-free vector - self.filename().map_move(|v| unsafe { GenericPathUnsafe::from_vec_unchecked(v) }) + self.filename().map_move(|v| unsafe { GenericPathUnsafe::new_unchecked(v) }) } /// Returns a Path that represents the filesystem root that `self` is rooted in. @@ -664,51 +581,41 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// If `self` is not absolute, or vol-relative in the case of Windows, this returns None. fn root_path(&self) -> Option<Self>; - /// Pushes a path (as a byte vector) onto `self`. + /// Pushes a path (as a byte vector or string) onto `self`. /// If the argument represents an absolute path, it replaces `self`. /// /// # Failure /// /// Raises the `null_byte` condition if the path contains a NUL. #[inline] - fn push(&mut self, path: &[u8]) { - if contains_nul(path) { - let path = self::null_byte::cond.raise(path.to_owned()); + fn push<T: BytesContainer>(&mut self, path: T) { + if contains_nul(path.container_as_bytes()) { + let path = self::null_byte::cond.raise(path.container_into_owned_bytes()); assert!(!contains_nul(path)); unsafe { self.push_unchecked(path) } } else { unsafe { self.push_unchecked(path) } } } - /// Pushes a path (as a string) onto `self. - /// See `push` for details. - #[inline] - fn push_str(&mut self, path: &str) { - if contains_nul(path.as_bytes()) { - self.push(path.as_bytes()) // triggers null_byte condition - } else { - unsafe { self.push_str_unchecked(path) } - } - } /// Pushes a Path onto `self`. /// If the argument represents an absolute path, it replaces `self`. #[inline] fn push_path(&mut self, path: &Self) { self.push(path.as_vec()) } - /// Pushes multiple paths (as byte vectors) onto `self`. + /// Pushes multiple paths (as byte vectors or strings) onto `self`. /// See `push` for details. #[inline] - fn push_many<V: Vector<u8>>(&mut self, paths: &[V]) { - for p in paths.iter() { - self.push(p.as_slice()); - } - } - /// Pushes multiple paths (as strings) onto `self`. - #[inline] - fn push_many_str<S: Str>(&mut self, paths: &[S]) { - for p in paths.iter() { - self.push_str(p.as_slice()); + fn push_many<T: BytesContainer>(&mut self, paths: &[T]) { + let t: Option<T> = None; + if BytesContainer::is_str(t) { + for p in paths.iter() { + self.push(p.container_as_str()) + } + } else { + for p in paths.iter() { + self.push(p.container_as_bytes()) + } } } /// Pops the last path component off of `self` and returns it. @@ -722,26 +629,19 @@ pub trait GenericPath: Clone + GenericPathUnsafe { self.pop().and_then(|v| str::from_utf8_owned_opt(v)) } - /// Returns a new Path constructed by joining `self` with the given path (as a byte vector). + /// Returns a new Path constructed by joining `self` with the given path + /// (as a byte vector or string). /// If the given path is absolute, the new Path will represent just that. /// /// # Failure /// /// Raises the `null_byte` condition if the path contains a NUL. #[inline] - fn join(&self, path: &[u8]) -> Self { + fn join<T: BytesContainer>(&self, path: T) -> Self { let mut p = self.clone(); p.push(path); p } - /// Returns a new Path constructed by joining `self` with the given path (as a string). - /// See `join` for details. - #[inline] - fn join_str(&self, path: &str) -> Self { - let mut p = self.clone(); - p.push_str(path); - p - } /// Returns a new Path constructed by joining `self` with the given path. /// If the given path is absolute, the new Path will represent just that. #[inline] @@ -750,22 +650,15 @@ pub trait GenericPath: Clone + GenericPathUnsafe { p.push_path(path); p } - /// Returns a new Path constructed by joining `self` with the given paths (as byte vectors). + /// Returns a new Path constructed by joining `self` with the given paths + /// (as byte vectors or strings). /// See `join` for details. #[inline] - fn join_many<V: Vector<u8>>(&self, paths: &[V]) -> Self { + fn join_many<T: BytesContainer>(&self, paths: &[T]) -> Self { let mut p = self.clone(); p.push_many(paths); p } - /// Returns a new Path constructed by joining `self` with the given paths (as strings). - /// See `join` for details. - #[inline] - fn join_many_str<S: Str>(&self, paths: &[S]) -> Self { - let mut p = self.clone(); - p.push_many_str(paths); - p - } /// Returns whether `self` represents an absolute path. /// An absolute path is defined as one that, when joined to another path, will @@ -805,57 +698,59 @@ pub trait GenericPath: Clone + GenericPathUnsafe { } true } + + /// Returns whether the relative path `child` is a suffix of `self`. + fn ends_with_path(&self, child: &Self) -> bool; +} + +/// A trait that represents something bytes-like (e.g. a &[u8] or a &str) +pub trait BytesContainer { + /// Returns a &[u8] representing the receiver + fn container_as_bytes<'a>(&'a self) -> &'a [u8]; + /// Consumes the receiver and converts it into ~[u8] + #[inline] + fn container_into_owned_bytes(self) -> ~[u8] { + self.container_as_bytes().to_owned() + } + /// Returns the receiver interpreted as a utf-8 string + /// + /// # Failure + /// + /// Raises `str::null_byte` if not utf-8 + #[inline] + fn container_as_str<'a>(&'a self) -> &'a str { + str::from_utf8_slice(self.container_as_bytes()) + } + /// Returns the receiver interpreted as a utf-8 string, if possible + #[inline] + fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> { + str::from_utf8_slice_opt(self.container_as_bytes()) + } + /// Returns whether the concrete receiver is a string type + // FIXME (#8888): Remove unused arg once ::<for T> works + #[inline] + fn is_str(_: Option<Self>) -> bool { false } } /// A trait that represents the unsafe operations on GenericPaths pub trait GenericPathUnsafe { - /// Creates a new Path from a byte vector without checking for null bytes. + /// Creates a new Path without checking for null bytes. /// The resulting Path will always be normalized. - unsafe fn from_vec_unchecked(path: &[u8]) -> Self; + unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Self; - /// Creates a new Path from a str without checking for null bytes. - /// The resulting Path will always be normalized. - #[inline] - unsafe fn from_str_unchecked(path: &str) -> Self { - GenericPathUnsafe::from_vec_unchecked(path.as_bytes()) - } - - /// Replaces the directory portion of the path with the given byte vector without - /// checking for null bytes. + /// Replaces the directory portion of the path without checking for null + /// bytes. /// See `set_dirname` for details. - unsafe fn set_dirname_unchecked(&mut self, dirname: &[u8]); - - /// Replaces the directory portion of the path with the given str without - /// checking for null bytes. - /// See `set_dirname_str` for details. - #[inline] - unsafe fn set_dirname_str_unchecked(&mut self, dirname: &str) { - self.set_dirname_unchecked(dirname.as_bytes()) - } + unsafe fn set_dirname_unchecked<T: BytesContainer>(&mut self, dirname: T); - /// Replaces the filename portion of the path with the given byte vector without - /// checking for null bytes. + /// Replaces the filename portion of the path without checking for null + /// bytes. /// See `set_filename` for details. - unsafe fn set_filename_unchecked(&mut self, filename: &[u8]); - - /// Replaces the filename portion of the path with the given str without - /// checking for null bytes. - /// See `set_filename_str` for details. - #[inline] - unsafe fn set_filename_str_unchecked(&mut self, filename: &str) { - self.set_filename_unchecked(filename.as_bytes()) - } + unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T); - /// Pushes a byte vector onto `self` without checking for null bytes. + /// Pushes a path onto `self` without checking for null bytes. /// See `push` for details. - unsafe fn push_unchecked(&mut self, path: &[u8]); - - /// Pushes a str onto `self` without checking for null bytes. - /// See `push_str` for details. - #[inline] - unsafe fn push_str_unchecked(&mut self, path: &str) { - self.push_unchecked(path.as_bytes()) - } + unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T); } /// Helper struct for printing paths with format!() @@ -883,6 +778,86 @@ impl<'self, P: GenericPath> fmt::Default for FilenameDisplay<'self, P> { } } +impl<'self> BytesContainer for &'self str { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_bytes() + } + #[inline] + fn container_as_str<'a>(&'a self) -> &'a str { + *self + } + #[inline] + fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> { + Some(*self) + } + #[inline] + fn is_str(_: Option<&'self str>) -> bool { true } +} + +impl BytesContainer for ~str { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_bytes() + } + #[inline] + fn container_into_owned_bytes(self) -> ~[u8] { + self.into_bytes() + } + #[inline] + fn container_as_str<'a>(&'a self) -> &'a str { + self.as_slice() + } + #[inline] + fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> { + Some(self.as_slice()) + } + #[inline] + fn is_str(_: Option<~str>) -> bool { true } +} + +impl BytesContainer for @str { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_bytes() + } + #[inline] + fn container_as_str<'a>(&'a self) -> &'a str { + self.as_slice() + } + #[inline] + fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> { + Some(self.as_slice()) + } + #[inline] + fn is_str(_: Option<@str>) -> bool { true } +} + +impl<'self> BytesContainer for &'self [u8] { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + *self + } +} + +impl BytesContainer for ~[u8] { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_slice() + } + #[inline] + fn container_into_owned_bytes(self) -> ~[u8] { + self + } +} + +impl BytesContainer for @[u8] { + #[inline] + fn container_as_bytes<'a>(&'a self) -> &'a [u8] { + self.as_slice() + } +} + #[inline(always)] fn contains_nul(v: &[u8]) -> bool { v.iter().any(|&x| x == 0) diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index f39062e66cb..3400b673c30 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -23,7 +23,7 @@ use to_bytes::IterBytes; use util; use vec; use vec::{CopyableVector, RSplitIterator, SplitIterator, Vector, VectorVector}; -use super::{GenericPath, GenericPathUnsafe}; +use super::{BytesContainer, GenericPath, GenericPathUnsafe}; #[cfg(not(target_os = "win32"))] use libc; @@ -65,12 +65,7 @@ impl Eq for Path { impl FromStr for Path { fn from_str(s: &str) -> Option<Path> { - let v = s.as_bytes(); - if contains_nul(v) { - None - } else { - Some(unsafe { GenericPathUnsafe::from_vec_unchecked(v) }) - } + Path::new_opt(s) } } @@ -95,14 +90,15 @@ impl IterBytes for Path { } impl GenericPathUnsafe for Path { - unsafe fn from_vec_unchecked(path: &[u8]) -> Path { - let path = Path::normalize(path); + unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path { + let path = Path::normalize(path.container_as_bytes()); assert!(!path.is_empty()); let idx = path.rposition_elem(&sep); Path{ repr: path, sepidx: idx } } - unsafe fn set_dirname_unchecked(&mut self, dirname: &[u8]) { + unsafe fn set_dirname_unchecked<T: BytesContainer>(&mut self, dirname: T) { + let dirname = dirname.container_as_bytes(); match self.sepidx { None if bytes!(".") == self.repr || bytes!("..") == self.repr => { self.repr = Path::normalize(dirname); @@ -134,7 +130,8 @@ impl GenericPathUnsafe for Path { self.sepidx = self.repr.rposition_elem(&sep); } - unsafe fn set_filename_unchecked(&mut self, filename: &[u8]) { + unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { + let filename = filename.container_as_bytes(); match self.sepidx { None if bytes!("..") == self.repr => { let mut v = vec::with_capacity(3 + filename.len()); @@ -163,7 +160,8 @@ impl GenericPathUnsafe for Path { self.sepidx = self.repr.rposition_elem(&sep); } - unsafe fn push_unchecked(&mut self, path: &[u8]) { + unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) { + let path = path.container_as_bytes(); if !path.is_empty() { if path[0] == sep { self.repr = Path::normalize(path); @@ -185,6 +183,14 @@ impl GenericPath for Path { self.repr.as_slice() } + fn into_vec(self) -> ~[u8] { + self.repr + } + + fn into_str(self) -> Option<~str> { + str::from_utf8_owned_opt(self.repr) + } + fn dirname<'a>(&'a self) -> &'a [u8] { match self.sepidx { None if bytes!("..") == self.repr => self.repr.as_slice(), @@ -230,7 +236,7 @@ impl GenericPath for Path { fn root_path(&self) -> Option<Path> { if self.is_absolute() { - Some(Path::from_str("/")) + Some(Path::new("/")) } else { None } @@ -299,52 +305,41 @@ impl GenericPath for Path { } } } - Some(Path::from_vec(comps.connect_vec(&sep))) + Some(Path::new(comps.connect_vec(&sep))) + } + } + + fn ends_with_path(&self, child: &Path) -> bool { + if !child.is_relative() { return false; } + let mut selfit = self.rev_component_iter(); + let mut childit = child.rev_component_iter(); + loop { + match (selfit.next(), childit.next()) { + (Some(a), Some(b)) => if a != b { return false; }, + (Some(_), None) => break, + (None, Some(_)) => return false, + (None, None) => break + } } + true } } impl Path { - /// Returns a new Path from a byte vector + /// Returns a new Path from a byte vector or string /// /// # Failure /// /// Raises the `null_byte` condition if the vector contains a NUL. #[inline] - pub fn from_vec(v: &[u8]) -> Path { - GenericPath::from_vec(v) - } - - /// Returns a new Path from a byte vector, if possible - #[inline] - pub fn from_vec_opt(v: &[u8]) -> Option<Path> { - GenericPath::from_vec_opt(v) - } - - /// Returns a new Path from a string - /// - /// # Failure - /// - /// Raises the `null_byte` condition if the str contains a NUL. - #[inline] - pub fn from_str(s: &str) -> Path { - GenericPath::from_str(s) + pub fn new<T: BytesContainer>(path: T) -> Path { + GenericPath::new(path) } - /// Returns a new Path from a string, if possible + /// Returns a new Path from a byte vector or string, if possible #[inline] - pub fn from_str_opt(s: &str) -> Option<Path> { - GenericPath::from_str_opt(s) - } - - /// Converts the Path into an owned byte vector - pub fn into_vec(self) -> ~[u8] { - self.repr - } - - /// Converts the Path into an owned string, if possible - pub fn into_str(self) -> Option<~str> { - str::from_utf8_owned_opt(self.repr) + pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> { + GenericPath::new_opt(path) } /// Returns a normalized byte vector representation of a path, by removing all empty @@ -427,22 +422,6 @@ impl Path { pub fn rev_str_component_iter<'a>(&'a self) -> RevStrComponentIter<'a> { self.rev_component_iter().map(str::from_utf8_slice_opt) } - - /// Returns whether the relative path `child` is a suffix of `self`. - pub fn ends_with_path(&self, child: &Path) -> bool { - if !child.is_relative() { return false; } - let mut selfit = self.rev_component_iter(); - let mut childit = child.rev_component_iter(); - loop { - match (selfit.next(), childit.next()) { - (Some(a), Some(b)) => if a != b { return false; }, - (Some(_), None) => break, - (None, Some(_)) => return false, - (None, None) => break - } - } - true - } } // None result means the byte vector didn't need normalizing @@ -611,54 +590,55 @@ mod tests { #[test] fn test_paths() { - t!(v: Path::from_vec([]), b!(".")); - t!(v: Path::from_vec(b!("/")), b!("/")); - t!(v: Path::from_vec(b!("a/b/c")), b!("a/b/c")); - t!(v: Path::from_vec(b!("a/b/c", 0xff)), b!("a/b/c", 0xff)); - t!(v: Path::from_vec(b!(0xff, "/../foo", 0x80)), b!("foo", 0x80)); - let p = Path::from_vec(b!("a/b/c", 0xff)); + let empty: &[u8] = []; + t!(v: Path::new(empty), b!(".")); + t!(v: Path::new(b!("/")), b!("/")); + t!(v: Path::new(b!("a/b/c")), b!("a/b/c")); + t!(v: Path::new(b!("a/b/c", 0xff)), b!("a/b/c", 0xff)); + t!(v: Path::new(b!(0xff, "/../foo", 0x80)), b!("foo", 0x80)); + let p = Path::new(b!("a/b/c", 0xff)); assert_eq!(p.as_str(), None); - t!(s: Path::from_str(""), "."); - t!(s: Path::from_str("/"), "/"); - t!(s: Path::from_str("hi"), "hi"); - t!(s: Path::from_str("hi/"), "hi"); - t!(s: Path::from_str("/lib"), "/lib"); - t!(s: Path::from_str("/lib/"), "/lib"); - t!(s: Path::from_str("hi/there"), "hi/there"); - t!(s: Path::from_str("hi/there.txt"), "hi/there.txt"); - - t!(s: Path::from_str("hi/there/"), "hi/there"); - t!(s: Path::from_str("hi/../there"), "there"); - t!(s: Path::from_str("../hi/there"), "../hi/there"); - t!(s: Path::from_str("/../hi/there"), "/hi/there"); - t!(s: Path::from_str("foo/.."), "."); - t!(s: Path::from_str("/foo/.."), "/"); - t!(s: Path::from_str("/foo/../.."), "/"); - t!(s: Path::from_str("/foo/../../bar"), "/bar"); - t!(s: Path::from_str("/./hi/./there/."), "/hi/there"); - t!(s: Path::from_str("/./hi/./there/./.."), "/hi"); - t!(s: Path::from_str("foo/../.."), ".."); - t!(s: Path::from_str("foo/../../.."), "../.."); - t!(s: Path::from_str("foo/../../bar"), "../bar"); - - assert_eq!(Path::from_vec(b!("foo/bar")).into_vec(), b!("foo/bar").to_owned()); - assert_eq!(Path::from_vec(b!("/foo/../../bar")).into_vec(), + t!(s: Path::new(""), "."); + t!(s: Path::new("/"), "/"); + t!(s: Path::new("hi"), "hi"); + t!(s: Path::new("hi/"), "hi"); + t!(s: Path::new("/lib"), "/lib"); + t!(s: Path::new("/lib/"), "/lib"); + t!(s: Path::new("hi/there"), "hi/there"); + t!(s: Path::new("hi/there.txt"), "hi/there.txt"); + + t!(s: Path::new("hi/there/"), "hi/there"); + t!(s: Path::new("hi/../there"), "there"); + t!(s: Path::new("../hi/there"), "../hi/there"); + t!(s: Path::new("/../hi/there"), "/hi/there"); + t!(s: Path::new("foo/.."), "."); + t!(s: Path::new("/foo/.."), "/"); + t!(s: Path::new("/foo/../.."), "/"); + t!(s: Path::new("/foo/../../bar"), "/bar"); + t!(s: Path::new("/./hi/./there/."), "/hi/there"); + t!(s: Path::new("/./hi/./there/./.."), "/hi"); + t!(s: Path::new("foo/../.."), ".."); + t!(s: Path::new("foo/../../.."), "../.."); + t!(s: Path::new("foo/../../bar"), "../bar"); + + assert_eq!(Path::new(b!("foo/bar")).into_vec(), b!("foo/bar").to_owned()); + assert_eq!(Path::new(b!("/foo/../../bar")).into_vec(), b!("/bar").to_owned()); - assert_eq!(Path::from_str("foo/bar").into_str(), Some(~"foo/bar")); - assert_eq!(Path::from_str("/foo/../../bar").into_str(), Some(~"/bar")); + assert_eq!(Path::new("foo/bar").into_str(), Some(~"foo/bar")); + assert_eq!(Path::new("/foo/../../bar").into_str(), Some(~"/bar")); - let p = Path::from_vec(b!("foo/bar", 0x80)); + let p = Path::new(b!("foo/bar", 0x80)); assert_eq!(p.as_str(), None); - assert_eq!(Path::from_vec(b!("foo", 0xff, "/bar")).into_str(), None); + assert_eq!(Path::new(b!("foo", 0xff, "/bar")).into_str(), None); } #[test] fn test_opt_paths() { - assert_eq!(Path::from_vec_opt(b!("foo/bar", 0)), None); - t!(v: Path::from_vec_opt(b!("foo/bar")).unwrap(), b!("foo/bar")); - assert_eq!(Path::from_str_opt("foo/bar\0"), None); - t!(s: Path::from_str_opt("foo/bar").unwrap(), "foo/bar"); + assert_eq!(Path::new_opt(b!("foo/bar", 0)), None); + t!(v: Path::new_opt(b!("foo/bar")).unwrap(), b!("foo/bar")); + assert_eq!(Path::new_opt("foo/bar\0"), None); + t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar"); } #[test] @@ -671,7 +651,7 @@ mod tests { assert_eq!(v.as_slice(), b!("foo/bar", 0)); (b!("/bar").to_owned()) }).inside { - Path::from_vec(b!("foo/bar", 0)) + Path::new(b!("foo/bar", 0)) }; assert!(handled); assert_eq!(p.as_vec(), b!("/bar")); @@ -727,16 +707,16 @@ mod tests { ) ) - t!(~"from_vec() w/nul" => { + t!(~"new() w/nul" => { do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { - Path::from_vec(b!("foo/bar", 0)) + Path::new(b!("foo/bar", 0)) }; }) t!(~"set_filename w/nul" => { - let mut p = Path::from_vec(b!("foo/bar")); + let mut p = Path::new(b!("foo/bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -745,7 +725,7 @@ mod tests { }) t!(~"set_dirname w/nul" => { - let mut p = Path::from_vec(b!("foo/bar")); + let mut p = Path::new(b!("foo/bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -754,7 +734,7 @@ mod tests { }) t!(~"push w/nul" => { - let mut p = Path::from_vec(b!("foo/bar")); + let mut p = Path::new(b!("foo/bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -765,46 +745,46 @@ mod tests { #[test] fn test_display_str() { - assert_eq!(Path::from_str("foo").to_display_str(), ~"foo"); - assert_eq!(Path::from_vec(b!("foo", 0x80)).to_display_str(), ~"foo\uFFFD"); - assert_eq!(Path::from_vec(b!("foo", 0xff, "bar")).to_display_str(), ~"foo\uFFFDbar"); - assert_eq!(Path::from_vec(b!("foo", 0xff, "/bar")).to_filename_display_str(), Some(~"bar")); - assert_eq!(Path::from_vec(b!("foo/", 0xff, "bar")).to_filename_display_str(), + assert_eq!(Path::new("foo").to_display_str(), ~"foo"); + assert_eq!(Path::new(b!("foo", 0x80)).to_display_str(), ~"foo\uFFFD"); + assert_eq!(Path::new(b!("foo", 0xff, "bar")).to_display_str(), ~"foo\uFFFDbar"); + assert_eq!(Path::new(b!("foo", 0xff, "/bar")).to_filename_display_str(), Some(~"bar")); + assert_eq!(Path::new(b!("foo/", 0xff, "bar")).to_filename_display_str(), Some(~"\uFFFDbar")); - assert_eq!(Path::from_vec(b!("/")).to_filename_display_str(), None); + assert_eq!(Path::new(b!("/")).to_filename_display_str(), None); let mut called = false; - do Path::from_str("foo").with_display_str |s| { + do Path::new("foo").with_display_str |s| { assert_eq!(s, "foo"); called = true; }; assert!(called); called = false; - do Path::from_vec(b!("foo", 0x80)).with_display_str |s| { + do Path::new(b!("foo", 0x80)).with_display_str |s| { assert_eq!(s, "foo\uFFFD"); called = true; }; assert!(called); called = false; - do Path::from_vec(b!("foo", 0xff, "bar")).with_display_str |s| { + do Path::new(b!("foo", 0xff, "bar")).with_display_str |s| { assert_eq!(s, "foo\uFFFDbar"); called = true; }; assert!(called); called = false; - do Path::from_vec(b!("foo", 0xff, "/bar")).with_filename_display_str |s| { + do Path::new(b!("foo", 0xff, "/bar")).with_filename_display_str |s| { assert_eq!(s, Some("bar")); called = true; } assert!(called); called = false; - do Path::from_vec(b!("foo/", 0xff, "bar")).with_filename_display_str |s| { + do Path::new(b!("foo/", 0xff, "bar")).with_filename_display_str |s| { assert_eq!(s, Some("\uFFFDbar")); called = true; } assert!(called); called = false; - do Path::from_vec(b!("/")).with_filename_display_str |s| { + do Path::new(b!("/")).with_filename_display_str |s| { assert!(s.is_none()); called = true; } @@ -816,7 +796,7 @@ mod tests { macro_rules! t( ($path:expr, $exp:expr, $expf:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); let f = format!("{}", path.display()); assert_eq!(f.as_slice(), $exp); let f = format!("{}", path.filename_display()); @@ -839,20 +819,20 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); assert_eq!(path.$op(), ($exp).as_bytes()); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let left = path.$op().map(|&x| str::from_utf8_slice(x)); assert_eq!(left, $exp); } ); (v: $path:expr, $op:ident, $exp:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); assert_eq!(path.$op(), $exp); } ); @@ -924,10 +904,10 @@ mod tests { { let path = ($path); let join = ($join); - let mut p1 = Path::from_str(path); + let mut p1 = Path::new(path); let p2 = p1.clone(); - p1.push_str(join); - assert_eq!(p1, p2.join_str(join)); + p1.push(join); + assert_eq!(p1, p2.join(join)); } ) ) @@ -943,8 +923,8 @@ mod tests { macro_rules! t( (s: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_str($path); - let push = Path::from_str($push); + let mut p = Path::new($path); + let push = Path::new($push); p.push_path(&push); assert_eq!(p.as_str(), Some($exp)); } @@ -966,14 +946,14 @@ mod tests { macro_rules! t( (s: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_str($path); - p.push_many_str($push); + let mut p = Path::new($path); + p.push_many($push); assert_eq!(p.as_str(), Some($exp)); } ); (v: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_vec($path); + let mut p = Path::new($path); p.push_many($push); assert_eq!(p.as_vec(), $exp); } @@ -997,7 +977,7 @@ mod tests { macro_rules! t( (s: $path:expr, $left:expr, $right:expr) => ( { - let mut p = Path::from_str($path); + let mut p = Path::new($path); let file = p.pop_str(); assert_eq!(p.as_str(), Some($left)); assert_eq!(file.map(|s| s.as_slice()), $right); @@ -1005,7 +985,7 @@ mod tests { ); (v: [$($path:expr),+], [$($left:expr),+], Some($($right:expr),+)) => ( { - let mut p = Path::from_vec(b!($($path),+)); + let mut p = Path::new(b!($($path),+)); let file = p.pop(); assert_eq!(p.as_vec(), b!($($left),+)); assert_eq!(file.map(|v| v.as_slice()), Some(b!($($right),+))); @@ -1013,7 +993,7 @@ mod tests { ); (v: [$($path:expr),+], [$($left:expr),+], None) => ( { - let mut p = Path::from_vec(b!($($path),+)); + let mut p = Path::new(b!($($path),+)); let file = p.pop(); assert_eq!(p.as_vec(), b!($($left),+)); assert_eq!(file, None); @@ -1036,27 +1016,27 @@ mod tests { t!(s: "/a", "/", Some("a")); t!(s: "/", "/", None); - assert_eq!(Path::from_vec(b!("foo/bar", 0x80)).pop_str(), None); - assert_eq!(Path::from_vec(b!("foo", 0x80, "/bar")).pop_str(), Some(~"bar")); + assert_eq!(Path::new(b!("foo/bar", 0x80)).pop_str(), None); + assert_eq!(Path::new(b!("foo", 0x80, "/bar")).pop_str(), Some(~"bar")); } #[test] fn test_root_path() { - assert_eq!(Path::from_vec(b!("a/b/c")).root_path(), None); - assert_eq!(Path::from_vec(b!("/a/b/c")).root_path(), Some(Path::from_str("/"))); + 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] fn test_join() { - t!(v: Path::from_vec(b!("a/b/c")).join(b!("..")), b!("a/b")); - t!(v: Path::from_vec(b!("/a/b/c")).join(b!("d")), b!("/a/b/c/d")); - t!(v: Path::from_vec(b!("a/", 0x80, "/c")).join(b!(0xff)), b!("a/", 0x80, "/c/", 0xff)); - t!(s: Path::from_str("a/b/c").join_str(".."), "a/b"); - t!(s: Path::from_str("/a/b/c").join_str("d"), "/a/b/c/d"); - t!(s: Path::from_str("a/b").join_str("c/d"), "a/b/c/d"); - t!(s: Path::from_str("a/b").join_str("/c/d"), "/c/d"); - t!(s: Path::from_str(".").join_str("a/b"), "a/b"); - t!(s: Path::from_str("/").join_str("a/b"), "/a/b"); + t!(v: Path::new(b!("a/b/c")).join(b!("..")), b!("a/b")); + t!(v: Path::new(b!("/a/b/c")).join(b!("d")), b!("/a/b/c/d")); + t!(v: Path::new(b!("a/", 0x80, "/c")).join(b!(0xff)), b!("a/", 0x80, "/c/", 0xff)); + t!(s: Path::new("a/b/c").join(".."), "a/b"); + t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d"); + t!(s: Path::new("a/b").join("c/d"), "a/b/c/d"); + t!(s: Path::new("a/b").join("/c/d"), "/c/d"); + t!(s: Path::new(".").join("a/b"), "a/b"); + t!(s: Path::new("/").join("a/b"), "/a/b"); } #[test] @@ -1064,8 +1044,8 @@ mod tests { macro_rules! t( (s: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let join = Path::from_str($join); + let path = Path::new($path); + let join = Path::new($join); let res = path.join_path(&join); assert_eq!(res.as_str(), Some($exp)); } @@ -1087,14 +1067,14 @@ mod tests { macro_rules! t( (s: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let res = path.join_many_str($join); + let path = Path::new($path); + let res = path.join_many($join); assert_eq!(res.as_str(), Some($exp)); } ); (v: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); let res = path.join_many($join); assert_eq!(res.as_vec(), $exp); } @@ -1114,100 +1094,102 @@ mod tests { #[test] fn test_with_helpers() { - t!(v: Path::from_vec(b!("a/b/c")).with_dirname(b!("d")), b!("d/c")); - t!(v: Path::from_vec(b!("a/b/c")).with_dirname(b!("d/e")), b!("d/e/c")); - t!(v: Path::from_vec(b!("a/", 0x80, "b/c")).with_dirname(b!(0xff)), b!(0xff, "/c")); - t!(v: Path::from_vec(b!("a/b/", 0x80)).with_dirname(b!("/", 0xcd)), + let empty: &[u8] = []; + + t!(v: Path::new(b!("a/b/c")).with_dirname(b!("d")), b!("d/c")); + t!(v: Path::new(b!("a/b/c")).with_dirname(b!("d/e")), b!("d/e/c")); + t!(v: Path::new(b!("a/", 0x80, "b/c")).with_dirname(b!(0xff)), b!(0xff, "/c")); + t!(v: Path::new(b!("a/b/", 0x80)).with_dirname(b!("/", 0xcd)), b!("/", 0xcd, "/", 0x80)); - t!(s: Path::from_str("a/b/c").with_dirname_str("d"), "d/c"); - t!(s: Path::from_str("a/b/c").with_dirname_str("d/e"), "d/e/c"); - t!(s: Path::from_str("a/b/c").with_dirname_str(""), "c"); - t!(s: Path::from_str("a/b/c").with_dirname_str("/"), "/c"); - t!(s: Path::from_str("a/b/c").with_dirname_str("."), "c"); - t!(s: Path::from_str("a/b/c").with_dirname_str(".."), "../c"); - t!(s: Path::from_str("/").with_dirname_str("foo"), "foo"); - t!(s: Path::from_str("/").with_dirname_str(""), "."); - t!(s: Path::from_str("/foo").with_dirname_str("bar"), "bar/foo"); - t!(s: Path::from_str("..").with_dirname_str("foo"), "foo"); - t!(s: Path::from_str("../..").with_dirname_str("foo"), "foo"); - t!(s: Path::from_str("..").with_dirname_str(""), "."); - t!(s: Path::from_str("../..").with_dirname_str(""), "."); - t!(s: Path::from_str("foo").with_dirname_str(".."), "../foo"); - t!(s: Path::from_str("foo").with_dirname_str("../.."), "../../foo"); - - t!(v: Path::from_vec(b!("a/b/c")).with_filename(b!("d")), b!("a/b/d")); - t!(v: Path::from_vec(b!("a/b/c", 0xff)).with_filename(b!(0x80)), b!("a/b/", 0x80)); - t!(v: Path::from_vec(b!("/", 0xff, "/foo")).with_filename(b!(0xcd)), + t!(s: Path::new("a/b/c").with_dirname("d"), "d/c"); + t!(s: Path::new("a/b/c").with_dirname("d/e"), "d/e/c"); + t!(s: Path::new("a/b/c").with_dirname(""), "c"); + t!(s: Path::new("a/b/c").with_dirname("/"), "/c"); + t!(s: Path::new("a/b/c").with_dirname("."), "c"); + t!(s: Path::new("a/b/c").with_dirname(".."), "../c"); + t!(s: Path::new("/").with_dirname("foo"), "foo"); + t!(s: Path::new("/").with_dirname(""), "."); + t!(s: Path::new("/foo").with_dirname("bar"), "bar/foo"); + t!(s: Path::new("..").with_dirname("foo"), "foo"); + t!(s: Path::new("../..").with_dirname("foo"), "foo"); + t!(s: Path::new("..").with_dirname(""), "."); + t!(s: Path::new("../..").with_dirname(""), "."); + t!(s: Path::new("foo").with_dirname(".."), "../foo"); + t!(s: Path::new("foo").with_dirname("../.."), "../../foo"); + + t!(v: Path::new(b!("a/b/c")).with_filename(b!("d")), b!("a/b/d")); + t!(v: Path::new(b!("a/b/c", 0xff)).with_filename(b!(0x80)), b!("a/b/", 0x80)); + t!(v: Path::new(b!("/", 0xff, "/foo")).with_filename(b!(0xcd)), b!("/", 0xff, "/", 0xcd)); - t!(s: Path::from_str("a/b/c").with_filename_str("d"), "a/b/d"); - t!(s: Path::from_str(".").with_filename_str("foo"), "foo"); - t!(s: Path::from_str("/a/b/c").with_filename_str("d"), "/a/b/d"); - t!(s: Path::from_str("/").with_filename_str("foo"), "/foo"); - t!(s: Path::from_str("/a").with_filename_str("foo"), "/foo"); - t!(s: Path::from_str("foo").with_filename_str("bar"), "bar"); - t!(s: Path::from_str("/").with_filename_str("foo/"), "/foo"); - t!(s: Path::from_str("/a").with_filename_str("foo/"), "/foo"); - t!(s: Path::from_str("a/b/c").with_filename_str(""), "a/b"); - t!(s: Path::from_str("a/b/c").with_filename_str("."), "a/b"); - t!(s: Path::from_str("a/b/c").with_filename_str(".."), "a"); - t!(s: Path::from_str("/a").with_filename_str(""), "/"); - t!(s: Path::from_str("foo").with_filename_str(""), "."); - t!(s: Path::from_str("a/b/c").with_filename_str("d/e"), "a/b/d/e"); - t!(s: Path::from_str("a/b/c").with_filename_str("/d"), "a/b/d"); - t!(s: Path::from_str("..").with_filename_str("foo"), "../foo"); - t!(s: Path::from_str("../..").with_filename_str("foo"), "../../foo"); - t!(s: Path::from_str("..").with_filename_str(""), ".."); - t!(s: Path::from_str("../..").with_filename_str(""), "../.."); - - t!(v: Path::from_vec(b!("hi/there", 0x80, ".txt")).with_filestem(b!(0xff)), + t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d"); + t!(s: Path::new(".").with_filename("foo"), "foo"); + t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d"); + t!(s: Path::new("/").with_filename("foo"), "/foo"); + t!(s: Path::new("/a").with_filename("foo"), "/foo"); + t!(s: Path::new("foo").with_filename("bar"), "bar"); + t!(s: Path::new("/").with_filename("foo/"), "/foo"); + t!(s: Path::new("/a").with_filename("foo/"), "/foo"); + t!(s: Path::new("a/b/c").with_filename(""), "a/b"); + t!(s: Path::new("a/b/c").with_filename("."), "a/b"); + t!(s: Path::new("a/b/c").with_filename(".."), "a"); + t!(s: Path::new("/a").with_filename(""), "/"); + t!(s: Path::new("foo").with_filename(""), "."); + t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e"); + t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d"); + t!(s: Path::new("..").with_filename("foo"), "../foo"); + t!(s: Path::new("../..").with_filename("foo"), "../../foo"); + t!(s: Path::new("..").with_filename(""), ".."); + t!(s: Path::new("../..").with_filename(""), "../.."); + + t!(v: Path::new(b!("hi/there", 0x80, ".txt")).with_filestem(b!(0xff)), b!("hi/", 0xff, ".txt")); - t!(v: Path::from_vec(b!("hi/there.txt", 0x80)).with_filestem(b!(0xff)), + t!(v: Path::new(b!("hi/there.txt", 0x80)).with_filestem(b!(0xff)), b!("hi/", 0xff, ".txt", 0x80)); - t!(v: Path::from_vec(b!("hi/there", 0xff)).with_filestem(b!(0x80)), b!("hi/", 0x80)); - t!(v: Path::from_vec(b!("hi", 0x80, "/there")).with_filestem([]), b!("hi", 0x80)); - t!(s: Path::from_str("hi/there.txt").with_filestem_str("here"), "hi/here.txt"); - t!(s: Path::from_str("hi/there.txt").with_filestem_str(""), "hi/.txt"); - t!(s: Path::from_str("hi/there.txt").with_filestem_str("."), "hi/..txt"); - t!(s: Path::from_str("hi/there.txt").with_filestem_str(".."), "hi/...txt"); - t!(s: Path::from_str("hi/there.txt").with_filestem_str("/"), "hi/.txt"); - t!(s: Path::from_str("hi/there.txt").with_filestem_str("foo/bar"), "hi/foo/bar.txt"); - t!(s: Path::from_str("hi/there.foo.txt").with_filestem_str("here"), "hi/here.txt"); - t!(s: Path::from_str("hi/there").with_filestem_str("here"), "hi/here"); - t!(s: Path::from_str("hi/there").with_filestem_str(""), "hi"); - t!(s: Path::from_str("hi").with_filestem_str(""), "."); - t!(s: Path::from_str("/hi").with_filestem_str(""), "/"); - t!(s: Path::from_str("hi/there").with_filestem_str(".."), "."); - t!(s: Path::from_str("hi/there").with_filestem_str("."), "hi"); - t!(s: Path::from_str("hi/there.").with_filestem_str("foo"), "hi/foo."); - t!(s: Path::from_str("hi/there.").with_filestem_str(""), "hi"); - t!(s: Path::from_str("hi/there.").with_filestem_str("."), "."); - t!(s: Path::from_str("hi/there.").with_filestem_str(".."), "hi/..."); - t!(s: Path::from_str("/").with_filestem_str("foo"), "/foo"); - t!(s: Path::from_str(".").with_filestem_str("foo"), "foo"); - t!(s: Path::from_str("hi/there..").with_filestem_str("here"), "hi/here."); - t!(s: Path::from_str("hi/there..").with_filestem_str(""), "hi"); - - t!(v: Path::from_vec(b!("hi/there", 0x80, ".txt")).with_extension(b!("exe")), + t!(v: Path::new(b!("hi/there", 0xff)).with_filestem(b!(0x80)), b!("hi/", 0x80)); + t!(v: Path::new(b!("hi", 0x80, "/there")).with_filestem(empty), b!("hi", 0x80)); + t!(s: Path::new("hi/there.txt").with_filestem("here"), "hi/here.txt"); + t!(s: Path::new("hi/there.txt").with_filestem(""), "hi/.txt"); + t!(s: Path::new("hi/there.txt").with_filestem("."), "hi/..txt"); + t!(s: Path::new("hi/there.txt").with_filestem(".."), "hi/...txt"); + t!(s: Path::new("hi/there.txt").with_filestem("/"), "hi/.txt"); + t!(s: Path::new("hi/there.txt").with_filestem("foo/bar"), "hi/foo/bar.txt"); + t!(s: Path::new("hi/there.foo.txt").with_filestem("here"), "hi/here.txt"); + t!(s: Path::new("hi/there").with_filestem("here"), "hi/here"); + t!(s: Path::new("hi/there").with_filestem(""), "hi"); + t!(s: Path::new("hi").with_filestem(""), "."); + t!(s: Path::new("/hi").with_filestem(""), "/"); + t!(s: Path::new("hi/there").with_filestem(".."), "."); + t!(s: Path::new("hi/there").with_filestem("."), "hi"); + t!(s: Path::new("hi/there.").with_filestem("foo"), "hi/foo."); + t!(s: Path::new("hi/there.").with_filestem(""), "hi"); + t!(s: Path::new("hi/there.").with_filestem("."), "."); + t!(s: Path::new("hi/there.").with_filestem(".."), "hi/..."); + t!(s: Path::new("/").with_filestem("foo"), "/foo"); + t!(s: Path::new(".").with_filestem("foo"), "foo"); + t!(s: Path::new("hi/there..").with_filestem("here"), "hi/here."); + t!(s: Path::new("hi/there..").with_filestem(""), "hi"); + + t!(v: Path::new(b!("hi/there", 0x80, ".txt")).with_extension(b!("exe")), b!("hi/there", 0x80, ".exe")); - t!(v: Path::from_vec(b!("hi/there.txt", 0x80)).with_extension(b!(0xff)), + t!(v: Path::new(b!("hi/there.txt", 0x80)).with_extension(b!(0xff)), b!("hi/there.", 0xff)); - t!(v: Path::from_vec(b!("hi/there", 0x80)).with_extension(b!(0xff)), + t!(v: Path::new(b!("hi/there", 0x80)).with_extension(b!(0xff)), b!("hi/there", 0x80, ".", 0xff)); - t!(v: Path::from_vec(b!("hi/there.", 0xff)).with_extension([]), b!("hi/there")); - t!(s: Path::from_str("hi/there.txt").with_extension_str("exe"), "hi/there.exe"); - t!(s: Path::from_str("hi/there.txt").with_extension_str(""), "hi/there"); - t!(s: Path::from_str("hi/there.txt").with_extension_str("."), "hi/there.."); - t!(s: Path::from_str("hi/there.txt").with_extension_str(".."), "hi/there..."); - t!(s: Path::from_str("hi/there").with_extension_str("txt"), "hi/there.txt"); - t!(s: Path::from_str("hi/there").with_extension_str("."), "hi/there.."); - t!(s: Path::from_str("hi/there").with_extension_str(".."), "hi/there..."); - t!(s: Path::from_str("hi/there.").with_extension_str("txt"), "hi/there.txt"); - t!(s: Path::from_str("hi/.foo").with_extension_str("txt"), "hi/.foo.txt"); - t!(s: Path::from_str("hi/there.txt").with_extension_str(".foo"), "hi/there..foo"); - t!(s: Path::from_str("/").with_extension_str("txt"), "/"); - t!(s: Path::from_str("/").with_extension_str("."), "/"); - t!(s: Path::from_str("/").with_extension_str(".."), "/"); - t!(s: Path::from_str(".").with_extension_str("txt"), "."); + t!(v: Path::new(b!("hi/there.", 0xff)).with_extension(empty), b!("hi/there")); + t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe"); + t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there"); + t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there.."); + t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there..."); + t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt"); + t!(s: Path::new("hi/there").with_extension("."), "hi/there.."); + t!(s: Path::new("hi/there").with_extension(".."), "hi/there..."); + t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt"); + t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt"); + t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo"); + t!(s: Path::new("/").with_extension("txt"), "/"); + t!(s: Path::new("/").with_extension("."), "/"); + t!(s: Path::new("/").with_extension(".."), "/"); + t!(s: Path::new(".").with_extension("txt"), "."); } #[test] @@ -1217,9 +1199,9 @@ mod tests { { let path = $path; let arg = $arg; - let mut p1 = Path::from_str(path); + let mut p1 = Path::new(path); p1.$set(arg); - let p2 = Path::from_str(path); + let p2 = Path::new(path); assert_eq!(p1, p2.$with(arg)); } ); @@ -1227,9 +1209,9 @@ mod tests { { let path = $path; let arg = $arg; - let mut p1 = Path::from_vec(path); + let mut p1 = Path::new(path); p1.$set(arg); - let p2 = Path::from_vec(path); + let p2 = Path::new(path); assert_eq!(p1, p2.$with(arg)); } ) @@ -1238,39 +1220,39 @@ mod tests { t!(v: b!("a/b/c"), set_dirname, with_dirname, b!("d")); t!(v: b!("a/b/c"), set_dirname, with_dirname, b!("d/e")); t!(v: b!("a/", 0x80, "/c"), set_dirname, with_dirname, b!(0xff)); - t!(s: "a/b/c", set_dirname_str, with_dirname_str, "d"); - t!(s: "a/b/c", set_dirname_str, with_dirname_str, "d/e"); - t!(s: "/", set_dirname_str, with_dirname_str, "foo"); - t!(s: "/foo", set_dirname_str, with_dirname_str, "bar"); - t!(s: "a/b/c", set_dirname_str, with_dirname_str, ""); - t!(s: "../..", set_dirname_str, with_dirname_str, "x"); - t!(s: "foo", set_dirname_str, with_dirname_str, "../.."); + t!(s: "a/b/c", set_dirname, with_dirname, "d"); + t!(s: "a/b/c", set_dirname, with_dirname, "d/e"); + t!(s: "/", set_dirname, with_dirname, "foo"); + t!(s: "/foo", set_dirname, with_dirname, "bar"); + t!(s: "a/b/c", set_dirname, with_dirname, ""); + t!(s: "../..", set_dirname, with_dirname, "x"); + t!(s: "foo", set_dirname, with_dirname, "../.."); t!(v: b!("a/b/c"), set_filename, with_filename, b!("d")); t!(v: b!("/"), set_filename, with_filename, b!("foo")); t!(v: b!(0x80), set_filename, with_filename, b!(0xff)); - t!(s: "a/b/c", set_filename_str, with_filename_str, "d"); - t!(s: "/", set_filename_str, with_filename_str, "foo"); - t!(s: ".", set_filename_str, with_filename_str, "foo"); - t!(s: "a/b", set_filename_str, with_filename_str, ""); - t!(s: "a", set_filename_str, with_filename_str, ""); + t!(s: "a/b/c", set_filename, with_filename, "d"); + t!(s: "/", set_filename, with_filename, "foo"); + t!(s: ".", set_filename, with_filename, "foo"); + t!(s: "a/b", set_filename, with_filename, ""); + t!(s: "a", set_filename, with_filename, ""); t!(v: b!("hi/there.txt"), set_filestem, with_filestem, b!("here")); t!(v: b!("hi/there", 0x80, ".txt"), set_filestem, with_filestem, b!("here", 0xff)); - t!(s: "hi/there.txt", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi/there.", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi/there", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi/there.txt", set_filestem_str, with_filestem_str, ""); - t!(s: "hi/there", set_filestem_str, with_filestem_str, ""); + t!(s: "hi/there.txt", set_filestem, with_filestem, "here"); + t!(s: "hi/there.", set_filestem, with_filestem, "here"); + t!(s: "hi/there", set_filestem, with_filestem, "here"); + t!(s: "hi/there.txt", set_filestem, with_filestem, ""); + t!(s: "hi/there", set_filestem, with_filestem, ""); t!(v: b!("hi/there.txt"), set_extension, with_extension, b!("exe")); t!(v: b!("hi/there.t", 0x80, "xt"), set_extension, with_extension, b!("exe", 0xff)); - t!(s: "hi/there.txt", set_extension_str, with_extension_str, "exe"); - t!(s: "hi/there.", set_extension_str, with_extension_str, "txt"); - t!(s: "hi/there", set_extension_str, with_extension_str, "txt"); - t!(s: "hi/there.txt", set_extension_str, with_extension_str, ""); - t!(s: "hi/there", set_extension_str, with_extension_str, ""); - t!(s: ".", set_extension_str, with_extension_str, "txt"); + t!(s: "hi/there.txt", set_extension, with_extension, "exe"); + t!(s: "hi/there.", set_extension, with_extension, "txt"); + t!(s: "hi/there", set_extension, with_extension, "txt"); + t!(s: "hi/there.txt", set_extension, with_extension, ""); + t!(s: "hi/there", set_extension, with_extension, ""); + t!(s: ".", set_extension, with_extension, "txt"); } #[test] @@ -1278,14 +1260,14 @@ mod tests { macro_rules! t( (s: $path:expr, $ext:expr, $exp:expr) => ( { - let mut path = Path::from_str($path); - path.add_extension_str($ext); + let mut path = Path::new($path); + path.add_extension($ext); assert_eq!(path.as_str(), Some($exp)); } ); (v: $path:expr, $ext:expr, $exp:expr) => ( { - let mut path = Path::from_vec($path); + let mut path = Path::new($path); path.add_extension($ext); assert_eq!(path.as_vec(), $exp); } @@ -1338,39 +1320,39 @@ mod tests { ) ) - t!(v: Path::from_vec(b!("a/b/c")), Some(b!("c")), b!("a/b"), Some(b!("c")), None); - t!(v: Path::from_vec(b!("a/b/", 0xff)), Some(b!(0xff)), b!("a/b"), Some(b!(0xff)), None); - t!(v: Path::from_vec(b!("hi/there.", 0xff)), Some(b!("there.", 0xff)), b!("hi"), + 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/", 0xff)), Some(b!(0xff)), b!("a/b"), Some(b!(0xff)), None); + t!(v: Path::new(b!("hi/there.", 0xff)), Some(b!("there.", 0xff)), b!("hi"), Some(b!("there")), Some(b!(0xff))); - t!(s: Path::from_str("a/b/c"), Some("c"), Some("a/b"), Some("c"), None); - t!(s: Path::from_str("."), None, Some("."), None, None); - t!(s: Path::from_str("/"), None, Some("/"), None, None); - t!(s: Path::from_str(".."), None, Some(".."), None, None); - t!(s: Path::from_str("../.."), None, Some("../.."), None, None); - t!(s: Path::from_str("hi/there.txt"), Some("there.txt"), Some("hi"), + 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::from_str("hi/there"), Some("there"), Some("hi"), Some("there"), None); - t!(s: Path::from_str("hi/there."), Some("there."), Some("hi"), + 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::from_str("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None); - t!(s: Path::from_str("hi/..there"), Some("..there"), Some("hi"), + 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::from_vec(b!("a/b/", 0xff)), None, Some("a/b"), None, None); - t!(s: Path::from_vec(b!("a/b/", 0xff, ".txt")), None, Some("a/b"), None, Some("txt")); - t!(s: Path::from_vec(b!("a/b/c.", 0x80)), None, Some("a/b"), Some("c"), None); - t!(s: Path::from_vec(b!(0xff, "/b")), Some("b"), None, Some("b"), None); + t!(s: Path::new(b!("a/b/", 0xff)), None, Some("a/b"), None, None); + t!(s: Path::new(b!("a/b/", 0xff, ".txt")), None, Some("a/b"), None, Some("txt")); + t!(s: Path::new(b!("a/b/c.", 0x80)), None, Some("a/b"), Some("c"), None); + t!(s: Path::new(b!(0xff, "/b")), Some("b"), None, Some("b"), None); } #[test] fn test_dir_file_path() { - t!(v: Path::from_vec(b!("hi/there", 0x80)).dir_path(), b!("hi")); - t!(v: Path::from_vec(b!("hi", 0xff, "/there")).dir_path(), b!("hi", 0xff)); - t!(s: Path::from_str("hi/there").dir_path(), "hi"); - t!(s: Path::from_str("hi").dir_path(), "."); - t!(s: Path::from_str("/hi").dir_path(), "/"); - t!(s: Path::from_str("/").dir_path(), "/"); - t!(s: Path::from_str("..").dir_path(), ".."); - t!(s: Path::from_str("../..").dir_path(), "../.."); + t!(v: Path::new(b!("hi/there", 0x80)).dir_path(), b!("hi")); + t!(v: Path::new(b!("hi", 0xff, "/there")).dir_path(), b!("hi", 0xff)); + t!(s: Path::new("hi/there").dir_path(), "hi"); + t!(s: Path::new("hi").dir_path(), "."); + t!(s: Path::new("/hi").dir_path(), "/"); + t!(s: Path::new("/").dir_path(), "/"); + t!(s: Path::new("..").dir_path(), ".."); + t!(s: Path::new("../..").dir_path(), "../.."); macro_rules! t( (s: $path:expr, $exp:expr) => ( @@ -1389,14 +1371,14 @@ mod tests { ) ) - t!(v: Path::from_vec(b!("hi/there", 0x80)).file_path(), Some(b!("there", 0x80))); - t!(v: Path::from_vec(b!("hi", 0xff, "/there")).file_path(), Some(b!("there"))); - t!(s: Path::from_str("hi/there").file_path(), Some("there")); - t!(s: Path::from_str("hi").file_path(), Some("hi")); - t!(s: Path::from_str(".").file_path(), None); - t!(s: Path::from_str("/").file_path(), None); - t!(s: Path::from_str("..").file_path(), None); - t!(s: Path::from_str("../..").file_path(), None); + t!(v: Path::new(b!("hi/there", 0x80)).file_path(), Some(b!("there", 0x80))); + t!(v: Path::new(b!("hi", 0xff, "/there")).file_path(), Some(b!("there"))); + t!(s: Path::new("hi/there").file_path(), Some("there")); + t!(s: Path::new("hi").file_path(), Some("hi")); + t!(s: Path::new(".").file_path(), None); + t!(s: Path::new("/").file_path(), None); + t!(s: Path::new("..").file_path(), None); + t!(s: Path::new("../..").file_path(), None); } #[test] @@ -1404,7 +1386,7 @@ mod tests { macro_rules! t( (s: $path:expr, $abs:expr, $rel:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); assert_eq!(path.is_absolute(), $abs); assert_eq!(path.is_relative(), $rel); } @@ -1425,8 +1407,8 @@ mod tests { macro_rules! t( (s: $path:expr, $dest:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let dest = Path::from_str($dest); + let path = Path::new($path); + let dest = Path::new($dest); assert_eq!(path.is_ancestor_of(&dest), $exp); } ) @@ -1459,15 +1441,15 @@ mod tests { macro_rules! t( (s: $path:expr, $child:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let child = Path::from_str($child); + let path = Path::new($path); + let child = Path::new($child); assert_eq!(path.ends_with_path(&child), $exp); } ); (v: $path:expr, $child:expr, $exp:expr) => ( { - let path = Path::from_vec($path); - let child = Path::from_vec($child); + let path = Path::new($path); + let child = Path::new($child); assert_eq!(path.ends_with_path(&child), $exp); } ) @@ -1498,8 +1480,8 @@ mod tests { macro_rules! t( (s: $path:expr, $other:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let other = Path::from_str($other); + let path = Path::new($path); + let other = Path::new($other); let res = path.path_relative_from(&other); assert_eq!(res.and_then_ref(|x| x.as_str()), $exp); } @@ -1543,7 +1525,7 @@ mod tests { macro_rules! t( (s: $path:expr, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let comps = path.component_iter().to_owned_vec(); let exp: &[&str] = $exp; let exps = exp.iter().map(|x| x.as_bytes()).to_owned_vec(); @@ -1557,7 +1539,7 @@ mod tests { ); (v: [$($arg:expr),+], [$([$($exp:expr),*]),*]) => ( { - let path = Path::from_vec(b!($($arg),+)); + let path = Path::new(b!($($arg),+)); let comps = path.component_iter().to_owned_vec(); let exp: &[&[u8]] = [$(b!($($exp),*)),*]; assert!(comps.as_slice() == exp, "component_iter: Expected {:?}, found {:?}", @@ -1592,7 +1574,7 @@ mod tests { macro_rules! t( (v: [$($arg:expr),+], $exp:expr) => ( { - let path = Path::from_vec(b!($($arg),+)); + let path = Path::new(b!($($arg),+)); let comps = path.str_component_iter().to_owned_vec(); let exp: &[Option<&str>] = $exp; assert!(comps.as_slice() == exp, @@ -1616,13 +1598,13 @@ mod tests { #[test] fn test_each_parent() { - assert!(Path::from_str("/foo/bar").each_parent(|_| true)); - assert!(!Path::from_str("/foo/bar").each_parent(|_| false)); + assert!(Path::new("/foo/bar").each_parent(|_| true)); + assert!(!Path::new("/foo/bar").each_parent(|_| false)); macro_rules! t( (s: $path:expr, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let exp: &[&str] = $exp; let mut comps = exp.iter().map(|&x|x); do path.each_parent |p| { @@ -1638,7 +1620,7 @@ mod tests { ); (v: $path:expr, $exp:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); let exp: &[&[u8]] = $exp; let mut comps = exp.iter().map(|&x|x); do path.each_parent |p| { diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 5dfe5b4f35a..cc04261ec66 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -22,7 +22,7 @@ use str::{CharSplitIterator, OwnedStr, Str, StrVector}; use to_bytes::IterBytes; use util; use vec::Vector; -use super::{GenericPath, GenericPathUnsafe}; +use super::{BytesContainer, GenericPath, GenericPathUnsafe}; #[cfg(target_os = "win32")] use libc; @@ -97,11 +97,7 @@ impl Eq for Path { impl FromStr for Path { fn from_str(s: &str) -> Option<Path> { - if contains_nul(s.as_bytes()) { - None - } else { - Some(unsafe { GenericPathUnsafe::from_str_unchecked(s) }) - } + Path::new_opt(s) } } @@ -132,18 +128,8 @@ impl GenericPathUnsafe for Path { /// /// Raises the `str::not_utf8` condition if not valid UTF-8. #[inline] - unsafe fn from_vec_unchecked(path: &[u8]) -> Path { - if !str::is_utf8(path) { - let path = str::from_utf8(path); // triggers not_utf8 condition - GenericPathUnsafe::from_str_unchecked(path) - } else { - GenericPathUnsafe::from_str_unchecked(cast::transmute(path)) - } - } - - #[inline] - unsafe fn from_str_unchecked(path: &str) -> Path { - let (prefix, path) = Path::normalize_(path); + unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path { + let (prefix, path) = Path::normalize_(path.container_as_str()); assert!(!path.is_empty()); let mut ret = Path{ repr: path, prefix: prefix, sepidx: None }; ret.update_sepidx(); @@ -155,17 +141,8 @@ impl GenericPathUnsafe for Path { /// # Failure /// /// Raises the `str::not_utf8` condition if not valid UTF-8. - #[inline] - unsafe fn set_dirname_unchecked(&mut self, dirname: &[u8]) { - if !str::is_utf8(dirname) { - let dirname = str::from_utf8(dirname); // triggers not_utf8 condition - self.set_dirname_str_unchecked(dirname); - } else { - self.set_dirname_str_unchecked(cast::transmute(dirname)) - } - } - - unsafe fn set_dirname_str_unchecked(&mut self, dirname: &str) { + unsafe fn set_dirname_unchecked<T: BytesContainer>(&mut self, dirname: T) { + let dirname = dirname.container_as_str(); match self.sepidx_or_prefix_len() { None if "." == self.repr || ".." == self.repr => { self.update_normalized(dirname); @@ -207,17 +184,8 @@ impl GenericPathUnsafe for Path { /// # Failure /// /// Raises the `str::not_utf8` condition if not valid UTF-8. - #[inline] - unsafe fn set_filename_unchecked(&mut self, filename: &[u8]) { - if !str::is_utf8(filename) { - let filename = str::from_utf8(filename); // triggers not_utf8 condition - self.set_filename_str_unchecked(filename) - } else { - self.set_filename_str_unchecked(cast::transmute(filename)) - } - } - - unsafe fn set_filename_str_unchecked(&mut self, filename: &str) { + unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) { + let filename = filename.container_as_str(); match self.sepidx_or_prefix_len() { None if ".." == self.repr => { let mut s = str::with_capacity(3 + filename.len()); @@ -254,20 +222,6 @@ impl GenericPathUnsafe for Path { /// See `GenericPathUnsafe::push_unchecked`. /// - /// # Failure - /// - /// Raises the `str::not_utf8` condition if not valid UTF-8. - unsafe fn push_unchecked(&mut self, path: &[u8]) { - if !str::is_utf8(path) { - let path = str::from_utf8(path); // triggers not_utf8 condition - self.push_str_unchecked(path); - } else { - self.push_str_unchecked(cast::transmute(path)); - } - } - - /// See `GenericPathUnsafe::push_str_unchecked`. - /// /// Concatenating two Windows Paths is rather complicated. /// For the most part, it will behave as expected, except in the case of /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no @@ -276,7 +230,8 @@ impl GenericPathUnsafe for Path { /// the same volume as the new path, it will be treated as the cwd that /// the new path is relative to. Otherwise, the new path will be treated /// as if it were absolute and will replace the receiver outright. - unsafe fn push_str_unchecked(&mut self, path: &str) { + unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) { + let path = path.container_as_str(); fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool { // assume prefix is Some(DiskPrefix) let rest = path.slice_from(prefix_len(prefix)); @@ -357,11 +312,17 @@ impl GenericPathUnsafe for Path { impl GenericPath for Path { #[inline] - fn from_vec_opt(v: &[u8]) -> Option<Path> { - if contains_nul(v) || !str::is_utf8(v) { - None - } else { - Some(unsafe { GenericPathUnsafe::from_vec_unchecked(v) }) + fn new_opt<T: BytesContainer>(path: T) -> Option<Path> { + let s = path.container_as_str_opt(); + match s { + None => None, + Some(s) => { + if contains_nul(s.as_bytes()) { + None + } else { + Some(unsafe { GenericPathUnsafe::new_unchecked(s) }) + } + } } } @@ -372,12 +333,24 @@ impl GenericPath for Path { Some(self.repr.as_slice()) } + /// See `GenericPath::into_str` for info. + /// Always returns a `Some` value. + #[inline] + fn into_str(self) -> Option<~str> { + Some(self.repr) + } + #[inline] fn as_vec<'a>(&'a self) -> &'a [u8] { self.repr.as_bytes() } #[inline] + fn into_vec(self) -> ~[u8] { + self.repr.into_bytes() + } + + #[inline] fn with_display_str<T>(&self, f: &fn(&str) -> T) -> T { f(self.repr.as_slice()) } @@ -448,16 +421,16 @@ impl GenericPath for Path { } fn dir_path(&self) -> Path { - unsafe { GenericPathUnsafe::from_str_unchecked(self.dirname_str().unwrap()) } + unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) } } fn file_path(&self) -> Option<Path> { - self.filename_str().map_move(|s| unsafe { GenericPathUnsafe::from_str_unchecked(s) }) + self.filename_str().map_move(|s| unsafe { GenericPathUnsafe::new_unchecked(s) }) } #[inline] fn push_path(&mut self, path: &Path) { - self.push_str(path.as_str().unwrap()) + self.push(path.as_str().unwrap()) } #[inline] @@ -494,14 +467,14 @@ impl GenericPath for Path { fn root_path(&self) -> Option<Path> { if self.is_absolute() { - Some(Path::from_str(match self.prefix { + Some(Path::new(match self.prefix { Some(VerbatimDiskPrefix)|Some(DiskPrefix) => { self.repr.slice_to(self.prefix_len()+1) } _ => self.repr.slice_to(self.prefix_len()) })) } else if self.is_vol_relative() { - Some(Path::from_str(self.repr.slice_to(1))) + Some(Path::new(self.repr.slice_to(1))) } else { None } @@ -631,11 +604,10 @@ impl GenericPath for Path { } } } - Some(Path::from_str(comps.connect("\\"))) + Some(Path::new(comps.connect("\\"))) } } - /// Executes a callback with the receiver and every parent fn each_parent(&self, f: &fn(&Path) -> bool) -> bool { let mut p = self.clone(); loop { @@ -649,52 +621,39 @@ impl GenericPath for Path { } true } + + fn ends_with_path(&self, child: &Path) -> bool { + if !child.is_relative() { return false; } + let mut selfit = self.str_component_iter().invert(); + let mut childit = child.str_component_iter().invert(); + loop { + match (selfit.next(), childit.next()) { + (Some(a), Some(b)) => if a != b { return false; }, + (Some(_), None) => break, + (None, Some(_)) => return false, + (None, None) => break + } + } + true + } } impl Path { - /// Returns a new Path from a byte vector + /// Returns a new Path from a byte vector or string /// /// # Failure /// /// Raises the `null_byte` condition if the vector contains a NUL. /// Raises the `str::not_utf8` condition if invalid UTF-8. #[inline] - pub fn from_vec(v: &[u8]) -> Path { - GenericPath::from_vec(v) - } - - /// Returns a new Path from a byte vector, if possible - #[inline] - pub fn from_vec_opt(v: &[u8]) -> Option<Path> { - GenericPath::from_vec_opt(v) + pub fn new<T: BytesContainer>(path: T) -> Path { + GenericPath::new(path) } - /// Returns a new Path from a string - /// - /// # Failure - /// - /// Raises the `null_byte` condition if the vector contains a NUL. + /// Returns a new Path from a byte vector or string, if possible #[inline] - pub fn from_str(s: &str) -> Path { - GenericPath::from_str(s) - } - - /// Returns a new Path from a string, if possible - #[inline] - pub fn from_str_opt(s: &str) -> Option<Path> { - GenericPath::from_str_opt(s) - } - - /// Converts the Path into an owned byte vector - pub fn into_vec(self) -> ~[u8] { - self.repr.into_bytes() - } - - /// Converts the Path into an owned string - /// Returns an Option for compatibility with posix::Path, but the - /// return value will always be Some. - pub fn into_str(self) -> Option<~str> { - Some(self.repr) + pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> { + GenericPath::new_opt(path) } /// Returns an iterator that yields each component of the path in turn as a Option<&str>. @@ -745,22 +704,6 @@ impl Path { self.rev_str_component_iter().map(convert) } - /// Returns whether the relative path `child` is a suffix of `self`. - pub fn ends_with_path(&self, child: &Path) -> bool { - if !child.is_relative() { return false; } - let mut selfit = self.str_component_iter().invert(); - let mut childit = child.str_component_iter().invert(); - loop { - match (selfit.next(), childit.next()) { - (Some(a), Some(b)) => if a != b { return false; }, - (Some(_), None) => break, - (None, Some(_)) => return false, - (None, None) => break - } - } - true - } - /// Returns whether the path is considered "volume-relative", which means a path /// that looks like "\foo". Paths of this form are relative to the current volume, /// but absolute within that volume. @@ -1310,102 +1253,103 @@ mod tests { #[test] fn test_paths() { - t!(v: Path::from_vec([]), b!(".")); - t!(v: Path::from_vec(b!("\\")), b!("\\")); - t!(v: Path::from_vec(b!("a\\b\\c")), b!("a\\b\\c")); - - t!(s: Path::from_str(""), "."); - t!(s: Path::from_str("\\"), "\\"); - t!(s: Path::from_str("hi"), "hi"); - t!(s: Path::from_str("hi\\"), "hi"); - t!(s: Path::from_str("\\lib"), "\\lib"); - t!(s: Path::from_str("\\lib\\"), "\\lib"); - t!(s: Path::from_str("hi\\there"), "hi\\there"); - t!(s: Path::from_str("hi\\there.txt"), "hi\\there.txt"); - t!(s: Path::from_str("/"), "\\"); - t!(s: Path::from_str("hi/"), "hi"); - t!(s: Path::from_str("/lib"), "\\lib"); - t!(s: Path::from_str("/lib/"), "\\lib"); - t!(s: Path::from_str("hi/there"), "hi\\there"); - - t!(s: Path::from_str("hi\\there\\"), "hi\\there"); - t!(s: Path::from_str("hi\\..\\there"), "there"); - t!(s: Path::from_str("hi/../there"), "there"); - t!(s: Path::from_str("..\\hi\\there"), "..\\hi\\there"); - t!(s: Path::from_str("\\..\\hi\\there"), "\\hi\\there"); - t!(s: Path::from_str("/../hi/there"), "\\hi\\there"); - t!(s: Path::from_str("foo\\.."), "."); - t!(s: Path::from_str("\\foo\\.."), "\\"); - t!(s: Path::from_str("\\foo\\..\\.."), "\\"); - t!(s: Path::from_str("\\foo\\..\\..\\bar"), "\\bar"); - t!(s: Path::from_str("\\.\\hi\\.\\there\\."), "\\hi\\there"); - t!(s: Path::from_str("\\.\\hi\\.\\there\\.\\.."), "\\hi"); - t!(s: Path::from_str("foo\\..\\.."), ".."); - t!(s: Path::from_str("foo\\..\\..\\.."), "..\\.."); - t!(s: Path::from_str("foo\\..\\..\\bar"), "..\\bar"); - - assert_eq!(Path::from_vec(b!("foo\\bar")).into_vec(), b!("foo\\bar").to_owned()); - assert_eq!(Path::from_vec(b!("\\foo\\..\\..\\bar")).into_vec(), + let empty: &[u8] = []; + t!(v: Path::new(empty), b!(".")); + t!(v: Path::new(b!("\\")), b!("\\")); + t!(v: Path::new(b!("a\\b\\c")), b!("a\\b\\c")); + + t!(s: Path::new(""), "."); + t!(s: Path::new("\\"), "\\"); + t!(s: Path::new("hi"), "hi"); + t!(s: Path::new("hi\\"), "hi"); + t!(s: Path::new("\\lib"), "\\lib"); + t!(s: Path::new("\\lib\\"), "\\lib"); + t!(s: Path::new("hi\\there"), "hi\\there"); + t!(s: Path::new("hi\\there.txt"), "hi\\there.txt"); + t!(s: Path::new("/"), "\\"); + t!(s: Path::new("hi/"), "hi"); + t!(s: Path::new("/lib"), "\\lib"); + t!(s: Path::new("/lib/"), "\\lib"); + t!(s: Path::new("hi/there"), "hi\\there"); + + t!(s: Path::new("hi\\there\\"), "hi\\there"); + t!(s: Path::new("hi\\..\\there"), "there"); + t!(s: Path::new("hi/../there"), "there"); + t!(s: Path::new("..\\hi\\there"), "..\\hi\\there"); + t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there"); + t!(s: Path::new("/../hi/there"), "\\hi\\there"); + t!(s: Path::new("foo\\.."), "."); + t!(s: Path::new("\\foo\\.."), "\\"); + t!(s: Path::new("\\foo\\..\\.."), "\\"); + t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar"); + t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there"); + t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi"); + t!(s: Path::new("foo\\..\\.."), ".."); + t!(s: Path::new("foo\\..\\..\\.."), "..\\.."); + t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar"); + + assert_eq!(Path::new(b!("foo\\bar")).into_vec(), b!("foo\\bar").to_owned()); + assert_eq!(Path::new(b!("\\foo\\..\\..\\bar")).into_vec(), b!("\\bar").to_owned()); - assert_eq!(Path::from_str("foo\\bar").into_str(), Some(~"foo\\bar")); - assert_eq!(Path::from_str("\\foo\\..\\..\\bar").into_str(), Some(~"\\bar")); - - t!(s: Path::from_str("\\\\a"), "\\a"); - t!(s: Path::from_str("\\\\a\\"), "\\a"); - t!(s: Path::from_str("\\\\a\\b"), "\\\\a\\b"); - t!(s: Path::from_str("\\\\a\\b\\"), "\\\\a\\b"); - t!(s: Path::from_str("\\\\a\\b/"), "\\\\a\\b"); - t!(s: Path::from_str("\\\\\\b"), "\\b"); - t!(s: Path::from_str("\\\\a\\\\b"), "\\a\\b"); - t!(s: Path::from_str("\\\\a\\b\\c"), "\\\\a\\b\\c"); - t!(s: Path::from_str("\\\\server\\share/path"), "\\\\server\\share\\path"); - t!(s: Path::from_str("\\\\server/share/path"), "\\\\server\\share\\path"); - t!(s: Path::from_str("C:a\\b.txt"), "C:a\\b.txt"); - t!(s: Path::from_str("C:a/b.txt"), "C:a\\b.txt"); - t!(s: Path::from_str("z:\\a\\b.txt"), "Z:\\a\\b.txt"); - t!(s: Path::from_str("z:/a/b.txt"), "Z:\\a\\b.txt"); - t!(s: Path::from_str("ab:/a/b.txt"), "ab:\\a\\b.txt"); - t!(s: Path::from_str("C:\\"), "C:\\"); - t!(s: Path::from_str("C:"), "C:"); - t!(s: Path::from_str("q:"), "Q:"); - t!(s: Path::from_str("C:/"), "C:\\"); - t!(s: Path::from_str("C:\\foo\\.."), "C:\\"); - t!(s: Path::from_str("C:foo\\.."), "C:"); - t!(s: Path::from_str("C:\\a\\"), "C:\\a"); - t!(s: Path::from_str("C:\\a/"), "C:\\a"); - t!(s: Path::from_str("C:\\a\\b\\"), "C:\\a\\b"); - t!(s: Path::from_str("C:\\a\\b/"), "C:\\a\\b"); - t!(s: Path::from_str("C:a\\"), "C:a"); - t!(s: Path::from_str("C:a/"), "C:a"); - t!(s: Path::from_str("C:a\\b\\"), "C:a\\b"); - t!(s: Path::from_str("C:a\\b/"), "C:a\\b"); - t!(s: Path::from_str("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt"); - t!(s: Path::from_str("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt"); - t!(s: Path::from_str("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt"); - t!(s: Path::from_str("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt"); - t!(s: Path::from_str("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\"); - t!(s: Path::from_str("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar"); - t!(s: Path::from_str("\\\\.\\"), "\\\\.\\"); - t!(s: Path::from_str("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo"); - t!(s: Path::from_str("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\"); - t!(s: Path::from_str("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\"); - t!(s: Path::from_str("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\"); - t!(s: Path::from_str("\\\\?\\UNC"), "\\\\?\\UNC"); + assert_eq!(Path::new("foo\\bar").into_str(), Some(~"foo\\bar")); + assert_eq!(Path::new("\\foo\\..\\..\\bar").into_str(), Some(~"\\bar")); + + t!(s: Path::new("\\\\a"), "\\a"); + t!(s: Path::new("\\\\a\\"), "\\a"); + t!(s: Path::new("\\\\a\\b"), "\\\\a\\b"); + t!(s: Path::new("\\\\a\\b\\"), "\\\\a\\b"); + t!(s: Path::new("\\\\a\\b/"), "\\\\a\\b"); + t!(s: Path::new("\\\\\\b"), "\\b"); + t!(s: Path::new("\\\\a\\\\b"), "\\a\\b"); + t!(s: Path::new("\\\\a\\b\\c"), "\\\\a\\b\\c"); + t!(s: Path::new("\\\\server\\share/path"), "\\\\server\\share\\path"); + t!(s: Path::new("\\\\server/share/path"), "\\\\server\\share\\path"); + t!(s: Path::new("C:a\\b.txt"), "C:a\\b.txt"); + t!(s: Path::new("C:a/b.txt"), "C:a\\b.txt"); + t!(s: Path::new("z:\\a\\b.txt"), "Z:\\a\\b.txt"); + t!(s: Path::new("z:/a/b.txt"), "Z:\\a\\b.txt"); + t!(s: Path::new("ab:/a/b.txt"), "ab:\\a\\b.txt"); + t!(s: Path::new("C:\\"), "C:\\"); + t!(s: Path::new("C:"), "C:"); + t!(s: Path::new("q:"), "Q:"); + t!(s: Path::new("C:/"), "C:\\"); + t!(s: Path::new("C:\\foo\\.."), "C:\\"); + t!(s: Path::new("C:foo\\.."), "C:"); + t!(s: Path::new("C:\\a\\"), "C:\\a"); + t!(s: Path::new("C:\\a/"), "C:\\a"); + t!(s: Path::new("C:\\a\\b\\"), "C:\\a\\b"); + t!(s: Path::new("C:\\a\\b/"), "C:\\a\\b"); + t!(s: Path::new("C:a\\"), "C:a"); + t!(s: Path::new("C:a/"), "C:a"); + t!(s: Path::new("C:a\\b\\"), "C:a\\b"); + t!(s: Path::new("C:a\\b/"), "C:a\\b"); + t!(s: Path::new("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt"); + t!(s: Path::new("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt"); + t!(s: Path::new("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt"); + t!(s: Path::new("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt"); + t!(s: Path::new("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\"); + t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar"); + t!(s: Path::new("\\\\.\\"), "\\\\.\\"); + t!(s: Path::new("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo"); + t!(s: Path::new("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\"); + t!(s: Path::new("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\"); + t!(s: Path::new("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\"); + t!(s: Path::new("\\\\?\\UNC"), "\\\\?\\UNC"); // I'm not sure whether \\.\foo/bar should normalize to \\.\foo\bar // as information is sparse and this isn't really googleable. // I'm going to err on the side of not normalizing it, as this skips the filesystem - t!(s: Path::from_str("\\\\.\\foo/bar"), "\\\\.\\foo/bar"); - t!(s: Path::from_str("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar"); + t!(s: Path::new("\\\\.\\foo/bar"), "\\\\.\\foo/bar"); + t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar"); } #[test] fn test_opt_paths() { - assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0)), None); - assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0x80)), None); - t!(v: Path::from_vec_opt(b!("foo\\bar")).unwrap(), b!("foo\\bar")); - assert_eq!(Path::from_str_opt("foo\\bar\0"), None); - t!(s: Path::from_str_opt("foo\\bar").unwrap(), "foo\\bar"); + assert_eq!(Path::new_opt(b!("foo\\bar", 0)), None); + assert_eq!(Path::new_opt(b!("foo\\bar", 0x80)), None); + t!(v: Path::new_opt(b!("foo\\bar")).unwrap(), b!("foo\\bar")); + assert_eq!(Path::new_opt("foo\\bar\0"), None); + t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar"); } #[test] @@ -1418,7 +1362,7 @@ mod tests { assert_eq!(v.as_slice(), b!("foo\\bar", 0)); (b!("\\bar").to_owned()) }).inside { - Path::from_vec(b!("foo\\bar", 0)) + Path::new(b!("foo\\bar", 0)) }; assert!(handled); assert_eq!(p.as_vec(), b!("\\bar")); @@ -1478,12 +1422,12 @@ mod tests { do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { - Path::from_vec(b!("foo\\bar", 0)) + Path::new(b!("foo\\bar", 0)) }; }) t!(~"set_filename w\\nul" => { - let mut p = Path::from_vec(b!("foo\\bar")); + let mut p = Path::new(b!("foo\\bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -1492,7 +1436,7 @@ mod tests { }) t!(~"set_dirname w\\nul" => { - let mut p = Path::from_vec(b!("foo\\bar")); + let mut p = Path::new(b!("foo\\bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -1501,7 +1445,7 @@ mod tests { }) t!(~"push w\\nul" => { - let mut p = Path::from_vec(b!("foo\\bar")); + let mut p = Path::new(b!("foo\\bar")); do cond.trap(|_| { (b!("null", 0).to_owned()) }).inside { @@ -1513,22 +1457,22 @@ mod tests { #[test] #[should_fail] fn test_not_utf8_fail() { - Path::from_vec(b!("hello", 0x80, ".txt")); + Path::new(b!("hello", 0x80, ".txt")); } #[test] fn test_display_str() { - assert_eq!(Path::from_str("foo").to_display_str(), ~"foo"); - assert_eq!(Path::from_vec(b!("\\")).to_filename_display_str(), None); + assert_eq!(Path::new("foo").to_display_str(), ~"foo"); + assert_eq!(Path::new(b!("\\")).to_filename_display_str(), None); let mut called = false; - do Path::from_str("foo").with_display_str |s| { + do Path::new("foo").with_display_str |s| { assert_eq!(s, "foo"); called = true; }; assert!(called); called = false; - do Path::from_vec(b!("\\")).with_filename_display_str |s| { + do Path::new(b!("\\")).with_filename_display_str |s| { assert!(s.is_none()); called = true; } @@ -1540,7 +1484,7 @@ mod tests { macro_rules! t( ($path:expr, $exp:expr, $expf:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let f = format!("{}", path.display()); assert_eq!(f.as_slice(), $exp); let f = format!("{}", path.filename_display()); @@ -1559,20 +1503,20 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); assert_eq!(path.$op(), Some($exp)); } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let left = path.$op(); assert_eq!(left, $exp); } ); (v: $path:expr, $op:ident, $exp:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); assert_eq!(path.$op(), $exp); } ) @@ -1681,10 +1625,10 @@ mod tests { { let path = ($path); let join = ($join); - let mut p1 = Path::from_str(path); + let mut p1 = Path::new(path); let p2 = p1.clone(); - p1.push_str(join); - assert_eq!(p1, p2.join_str(join)); + p1.push(join); + assert_eq!(p1, p2.join(join)); } ) ) @@ -1693,19 +1637,19 @@ mod tests { t!(s: "\\a\\b\\c", "d"); t!(s: "a\\b", "c\\d"); t!(s: "a\\b", "\\c\\d"); - // this is just a sanity-check test. push_str and join_str share an implementation, + // this is just a sanity-check test. push and join share an implementation, // so there's no need for the full set of prefix tests // we do want to check one odd case though to ensure the prefix is re-parsed - let mut p = Path::from_str("\\\\?\\C:"); + let mut p = Path::new("\\\\?\\C:"); assert_eq!(p.prefix(), Some(VerbatimPrefix(2))); - p.push_str("foo"); + p.push("foo"); assert_eq!(p.prefix(), Some(VerbatimDiskPrefix)); assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo")); // and another with verbatim non-normalized paths - let mut p = Path::from_str("\\\\?\\C:\\a\\"); - p.push_str("foo"); + let mut p = Path::new("\\\\?\\C:\\a\\"); + p.push("foo"); assert_eq!(p.as_str(), Some("\\\\?\\C:\\a\\foo")); } @@ -1714,8 +1658,8 @@ mod tests { macro_rules! t( (s: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_str($path); - let push = Path::from_str($push); + let mut p = Path::new($path); + let push = Path::new($push); p.push_path(&push); assert_eq!(p.as_str(), Some($exp)); } @@ -1766,14 +1710,14 @@ mod tests { macro_rules! t( (s: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_str($path); - p.push_many_str($push); + let mut p = Path::new($path); + p.push_many($push); assert_eq!(p.as_str(), Some($exp)); } ); (v: $path:expr, $push:expr, $exp:expr) => ( { - let mut p = Path::from_vec($path); + let mut p = Path::new($path); p.push_many($push); assert_eq!(p.as_vec(), $exp); } @@ -1798,7 +1742,7 @@ mod tests { (s: $path:expr, $left:expr, $right:expr) => ( { let pstr = $path; - let mut p = Path::from_str(pstr); + let mut p = Path::new(pstr); let file = p.pop_str(); let left = $left; assert!(p.as_str() == Some(left), @@ -1812,7 +1756,7 @@ mod tests { ); (v: [$($path:expr),+], [$($left:expr),+], Some($($right:expr),+)) => ( { - let mut p = Path::from_vec(b!($($path),+)); + let mut p = Path::new(b!($($path),+)); let file = p.pop(); assert_eq!(p.as_vec(), b!($($left),+)); assert_eq!(file.map(|v| v.as_slice()), Some(b!($($right),+))); @@ -1820,7 +1764,7 @@ mod tests { ); (v: [$($path:expr),+], [$($left:expr),+], None) => ( { - let mut p = Path::from_vec(b!($($path),+)); + let mut p = Path::new(b!($($path),+)); let file = p.pop(); assert_eq!(p.as_vec(), b!($($left),+)); assert_eq!(file, None); @@ -1866,28 +1810,28 @@ mod tests { #[test] fn test_root_path() { - assert_eq!(Path::from_str("a\\b\\c").root_path(), None); - assert_eq!(Path::from_str("\\a\\b\\c").root_path(), Some(Path::from_str("\\"))); - assert_eq!(Path::from_str("C:a").root_path(), None); - assert_eq!(Path::from_str("C:\\a").root_path(), Some(Path::from_str("C:\\"))); - assert_eq!(Path::from_str("\\\\a\\b\\c").root_path(), Some(Path::from_str("\\\\a\\b"))); - assert_eq!(Path::from_str("\\\\?\\a\\b").root_path(), Some(Path::from_str("\\\\?\\a"))); - assert_eq!(Path::from_str("\\\\?\\C:\\a").root_path(), Some(Path::from_str("\\\\?\\C:\\"))); - assert_eq!(Path::from_str("\\\\?\\UNC\\a\\b\\c").root_path(), - Some(Path::from_str("\\\\?\\UNC\\a\\b"))); - assert_eq!(Path::from_str("\\\\.\\a\\b").root_path(), Some(Path::from_str("\\\\.\\a"))); + 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(), None); + 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_eq!(Path::new("\\\\.\\a\\b").root_path(), Some(Path::new("\\\\.\\a"))); } #[test] fn test_join() { - t!(s: Path::from_str("a\\b\\c").join_str(".."), "a\\b"); - t!(s: Path::from_str("\\a\\b\\c").join_str("d"), "\\a\\b\\c\\d"); - t!(s: Path::from_str("a\\b").join_str("c\\d"), "a\\b\\c\\d"); - t!(s: Path::from_str("a\\b").join_str("\\c\\d"), "\\c\\d"); - t!(s: Path::from_str(".").join_str("a\\b"), "a\\b"); - t!(s: Path::from_str("\\").join_str("a\\b"), "\\a\\b"); - t!(v: Path::from_vec(b!("a\\b\\c")).join(b!("..")), b!("a\\b")); - t!(v: Path::from_vec(b!("\\a\\b\\c")).join(b!("d")), b!("\\a\\b\\c\\d")); + t!(s: Path::new("a\\b\\c").join(".."), "a\\b"); + t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d"); + t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d"); + t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d"); + t!(s: Path::new(".").join("a\\b"), "a\\b"); + t!(s: Path::new("\\").join("a\\b"), "\\a\\b"); + t!(v: Path::new(b!("a\\b\\c")).join(b!("..")), b!("a\\b")); + t!(v: Path::new(b!("\\a\\b\\c")).join(b!("d")), b!("\\a\\b\\c\\d")); // full join testing is covered under test_push_path, so no need for // the full set of prefix tests } @@ -1897,8 +1841,8 @@ mod tests { macro_rules! t( (s: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let join = Path::from_str($join); + let path = Path::new($path); + let join = Path::new($join); let res = path.join_path(&join); assert_eq!(res.as_str(), Some($exp)); } @@ -1922,14 +1866,14 @@ mod tests { macro_rules! t( (s: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let res = path.join_many_str($join); + let path = Path::new($path); + let res = path.join_many($join); assert_eq!(res.as_str(), Some($exp)); } ); (v: $path:expr, $join:expr, $exp:expr) => ( { - let path = Path::from_vec($path); + let path = Path::new($path); let res = path.join_many($join); assert_eq!(res.as_vec(), $exp); } @@ -1953,7 +1897,7 @@ mod tests { (s: $path:expr, $op:ident, $arg:expr, $res:expr) => ( { let pstr = $path; - let path = Path::from_str(pstr); + let path = Path::new(pstr); let arg = $arg; let res = path.$op(arg); let exp = $res; @@ -1963,137 +1907,137 @@ mod tests { } ) ) - t!(s: "a\\b\\c", with_dirname_str, "d", "d\\c"); - t!(s: "a\\b\\c", with_dirname_str, "d\\e", "d\\e\\c"); - t!(s: "a\\b\\c", with_dirname_str, "", "c"); - t!(s: "a\\b\\c", with_dirname_str, "\\", "\\c"); - t!(s: "a\\b\\c", with_dirname_str, "/", "\\c"); - t!(s: "a\\b\\c", with_dirname_str, ".", "c"); - t!(s: "a\\b\\c", with_dirname_str, "..", "..\\c"); - t!(s: "\\", with_dirname_str, "foo", "foo"); - t!(s: "\\", with_dirname_str, "", "."); - t!(s: "\\foo", with_dirname_str, "bar", "bar\\foo"); - t!(s: "..", with_dirname_str, "foo", "foo"); - t!(s: "..\\..", with_dirname_str, "foo", "foo"); - t!(s: "..", with_dirname_str, "", "."); - t!(s: "..\\..", with_dirname_str, "", "."); - t!(s: ".", with_dirname_str, "foo", "foo"); - t!(s: "foo", with_dirname_str, "..", "..\\foo"); - t!(s: "foo", with_dirname_str, "..\\..", "..\\..\\foo"); - t!(s: "C:\\a\\b", with_dirname_str, "foo", "foo\\b"); - t!(s: "foo", with_dirname_str, "C:\\a\\b", "C:\\a\\b\\foo"); - t!(s: "C:a\\b", with_dirname_str, "\\\\server\\share", "\\\\server\\share\\b"); - t!(s: "a", with_dirname_str, "\\\\server\\share", "\\\\server\\share\\a"); - t!(s: "a\\b", with_dirname_str, "\\\\?\\", "\\\\?\\b"); - t!(s: "a\\b", with_dirname_str, "C:", "C:b"); - t!(s: "a\\b", with_dirname_str, "C:\\", "C:\\b"); - t!(s: "a\\b", with_dirname_str, "C:/", "C:\\b"); - t!(s: "C:\\", with_dirname_str, "foo", "foo"); - t!(s: "C:", with_dirname_str, "foo", "foo"); - t!(s: ".", with_dirname_str, "C:\\", "C:\\"); - t!(s: ".", with_dirname_str, "C:/", "C:\\"); - t!(s: "\\\\?\\C:\\foo", with_dirname_str, "C:\\", "C:\\foo"); - t!(s: "\\\\?\\C:\\", with_dirname_str, "bar", "bar"); - t!(s: "foo\\bar", with_dirname_str, "\\\\?\\C:\\baz", "\\\\?\\C:\\baz\\bar"); - t!(s: "\\\\?\\foo", with_dirname_str, "C:\\bar", "C:\\bar"); - t!(s: "\\\\?\\a\\foo", with_dirname_str, "C:\\bar", "C:\\bar\\foo"); - t!(s: "\\\\?\\a\\foo/bar", with_dirname_str, "C:\\baz", "C:\\baz\\foo\\bar"); - t!(s: "\\\\?\\UNC\\server\\share\\baz", with_dirname_str, "a", "a\\baz"); - t!(s: "foo\\bar", with_dirname_str, "\\\\?\\UNC\\server\\share\\baz", + t!(s: "a\\b\\c", with_dirname, "d", "d\\c"); + t!(s: "a\\b\\c", with_dirname, "d\\e", "d\\e\\c"); + t!(s: "a\\b\\c", with_dirname, "", "c"); + t!(s: "a\\b\\c", with_dirname, "\\", "\\c"); + t!(s: "a\\b\\c", with_dirname, "/", "\\c"); + t!(s: "a\\b\\c", with_dirname, ".", "c"); + t!(s: "a\\b\\c", with_dirname, "..", "..\\c"); + t!(s: "\\", with_dirname, "foo", "foo"); + t!(s: "\\", with_dirname, "", "."); + t!(s: "\\foo", with_dirname, "bar", "bar\\foo"); + t!(s: "..", with_dirname, "foo", "foo"); + t!(s: "..\\..", with_dirname, "foo", "foo"); + t!(s: "..", with_dirname, "", "."); + t!(s: "..\\..", with_dirname, "", "."); + t!(s: ".", with_dirname, "foo", "foo"); + t!(s: "foo", with_dirname, "..", "..\\foo"); + t!(s: "foo", with_dirname, "..\\..", "..\\..\\foo"); + t!(s: "C:\\a\\b", with_dirname, "foo", "foo\\b"); + t!(s: "foo", with_dirname, "C:\\a\\b", "C:\\a\\b\\foo"); + t!(s: "C:a\\b", with_dirname, "\\\\server\\share", "\\\\server\\share\\b"); + t!(s: "a", with_dirname, "\\\\server\\share", "\\\\server\\share\\a"); + t!(s: "a\\b", with_dirname, "\\\\?\\", "\\\\?\\b"); + t!(s: "a\\b", with_dirname, "C:", "C:b"); + t!(s: "a\\b", with_dirname, "C:\\", "C:\\b"); + t!(s: "a\\b", with_dirname, "C:/", "C:\\b"); + t!(s: "C:\\", with_dirname, "foo", "foo"); + t!(s: "C:", with_dirname, "foo", "foo"); + t!(s: ".", with_dirname, "C:\\", "C:\\"); + t!(s: ".", with_dirname, "C:/", "C:\\"); + t!(s: "\\\\?\\C:\\foo", with_dirname, "C:\\", "C:\\foo"); + t!(s: "\\\\?\\C:\\", with_dirname, "bar", "bar"); + t!(s: "foo\\bar", with_dirname, "\\\\?\\C:\\baz", "\\\\?\\C:\\baz\\bar"); + t!(s: "\\\\?\\foo", with_dirname, "C:\\bar", "C:\\bar"); + t!(s: "\\\\?\\a\\foo", with_dirname, "C:\\bar", "C:\\bar\\foo"); + t!(s: "\\\\?\\a\\foo/bar", with_dirname, "C:\\baz", "C:\\baz\\foo\\bar"); + t!(s: "\\\\?\\UNC\\server\\share\\baz", with_dirname, "a", "a\\baz"); + t!(s: "foo\\bar", with_dirname, "\\\\?\\UNC\\server\\share\\baz", "\\\\?\\UNC\\server\\share\\baz\\bar"); - t!(s: "\\\\.\\foo", with_dirname_str, "bar", "bar"); - t!(s: "\\\\.\\foo\\bar", with_dirname_str, "baz", "baz\\bar"); - t!(s: "\\\\.\\foo\\bar", with_dirname_str, "baz\\", "baz\\bar"); - t!(s: "\\\\.\\foo\\bar", with_dirname_str, "baz/", "baz\\bar"); - - t!(s: "a\\b\\c", with_filename_str, "d", "a\\b\\d"); - t!(s: ".", with_filename_str, "foo", "foo"); - t!(s: "\\a\\b\\c", with_filename_str, "d", "\\a\\b\\d"); - t!(s: "\\", with_filename_str, "foo", "\\foo"); - t!(s: "\\a", with_filename_str, "foo", "\\foo"); - t!(s: "foo", with_filename_str, "bar", "bar"); - t!(s: "\\", with_filename_str, "foo\\", "\\foo"); - t!(s: "\\a", with_filename_str, "foo\\", "\\foo"); - t!(s: "a\\b\\c", with_filename_str, "", "a\\b"); - t!(s: "a\\b\\c", with_filename_str, ".", "a\\b"); - t!(s: "a\\b\\c", with_filename_str, "..", "a"); - t!(s: "\\a", with_filename_str, "", "\\"); - t!(s: "foo", with_filename_str, "", "."); - t!(s: "a\\b\\c", with_filename_str, "d\\e", "a\\b\\d\\e"); - t!(s: "a\\b\\c", with_filename_str, "\\d", "a\\b\\d"); - t!(s: "..", with_filename_str, "foo", "..\\foo"); - t!(s: "..\\..", with_filename_str, "foo", "..\\..\\foo"); - t!(s: "..", with_filename_str, "", ".."); - t!(s: "..\\..", with_filename_str, "", "..\\.."); - t!(s: "C:\\foo\\bar", with_filename_str, "baz", "C:\\foo\\baz"); - t!(s: "C:\\foo", with_filename_str, "bar", "C:\\bar"); - t!(s: "C:\\", with_filename_str, "foo", "C:\\foo"); - t!(s: "C:foo\\bar", with_filename_str, "baz", "C:foo\\baz"); - t!(s: "C:foo", with_filename_str, "bar", "C:bar"); - t!(s: "C:", with_filename_str, "foo", "C:foo"); - t!(s: "C:\\foo", with_filename_str, "", "C:\\"); - t!(s: "C:foo", with_filename_str, "", "C:"); - t!(s: "C:\\foo\\bar", with_filename_str, "..", "C:\\"); - t!(s: "C:\\foo", with_filename_str, "..", "C:\\"); - t!(s: "C:\\", with_filename_str, "..", "C:\\"); - t!(s: "C:foo\\bar", with_filename_str, "..", "C:"); - t!(s: "C:foo", with_filename_str, "..", "C:.."); - t!(s: "C:", with_filename_str, "..", "C:.."); - t!(s: "\\\\server\\share\\foo", with_filename_str, "bar", "\\\\server\\share\\bar"); - t!(s: "\\\\server\\share", with_filename_str, "foo", "\\\\server\\share\\foo"); - t!(s: "\\\\server\\share\\foo", with_filename_str, "", "\\\\server\\share"); - t!(s: "\\\\server\\share", with_filename_str, "", "\\\\server\\share"); - t!(s: "\\\\server\\share\\foo", with_filename_str, "..", "\\\\server\\share"); - t!(s: "\\\\server\\share", with_filename_str, "..", "\\\\server\\share"); - t!(s: "\\\\?\\C:\\foo\\bar", with_filename_str, "baz", "\\\\?\\C:\\foo\\baz"); - t!(s: "\\\\?\\C:\\foo", with_filename_str, "bar", "\\\\?\\C:\\bar"); - t!(s: "\\\\?\\C:\\", with_filename_str, "foo", "\\\\?\\C:\\foo"); - t!(s: "\\\\?\\C:\\foo", with_filename_str, "..", "\\\\?\\C:\\.."); - t!(s: "\\\\?\\foo\\bar", with_filename_str, "baz", "\\\\?\\foo\\baz"); - t!(s: "\\\\?\\foo", with_filename_str, "bar", "\\\\?\\foo\\bar"); - t!(s: "\\\\?\\", with_filename_str, "foo", "\\\\?\\\\foo"); - t!(s: "\\\\?\\foo\\bar", with_filename_str, "..", "\\\\?\\foo\\.."); - t!(s: "\\\\.\\foo\\bar", with_filename_str, "baz", "\\\\.\\foo\\baz"); - t!(s: "\\\\.\\foo", with_filename_str, "bar", "\\\\.\\foo\\bar"); - t!(s: "\\\\.\\foo\\bar", with_filename_str, "..", "\\\\.\\foo\\.."); - - t!(s: "hi\\there.txt", with_filestem_str, "here", "hi\\here.txt"); - t!(s: "hi\\there.txt", with_filestem_str, "", "hi\\.txt"); - t!(s: "hi\\there.txt", with_filestem_str, ".", "hi\\..txt"); - t!(s: "hi\\there.txt", with_filestem_str, "..", "hi\\...txt"); - t!(s: "hi\\there.txt", with_filestem_str, "\\", "hi\\.txt"); - t!(s: "hi\\there.txt", with_filestem_str, "foo\\bar", "hi\\foo\\bar.txt"); - t!(s: "hi\\there.foo.txt", with_filestem_str, "here", "hi\\here.txt"); - t!(s: "hi\\there", with_filestem_str, "here", "hi\\here"); - t!(s: "hi\\there", with_filestem_str, "", "hi"); - t!(s: "hi", with_filestem_str, "", "."); - t!(s: "\\hi", with_filestem_str, "", "\\"); - t!(s: "hi\\there", with_filestem_str, "..", "."); - t!(s: "hi\\there", with_filestem_str, ".", "hi"); - t!(s: "hi\\there.", with_filestem_str, "foo", "hi\\foo."); - t!(s: "hi\\there.", with_filestem_str, "", "hi"); - t!(s: "hi\\there.", with_filestem_str, ".", "."); - t!(s: "hi\\there.", with_filestem_str, "..", "hi\\..."); - t!(s: "\\", with_filestem_str, "foo", "\\foo"); - t!(s: ".", with_filestem_str, "foo", "foo"); - t!(s: "hi\\there..", with_filestem_str, "here", "hi\\here."); - t!(s: "hi\\there..", with_filestem_str, "", "hi"); + t!(s: "\\\\.\\foo", with_dirname, "bar", "bar"); + t!(s: "\\\\.\\foo\\bar", with_dirname, "baz", "baz\\bar"); + t!(s: "\\\\.\\foo\\bar", with_dirname, "baz\\", "baz\\bar"); + t!(s: "\\\\.\\foo\\bar", with_dirname, "baz/", "baz\\bar"); + + t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d"); + t!(s: ".", with_filename, "foo", "foo"); + t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d"); + t!(s: "\\", with_filename, "foo", "\\foo"); + t!(s: "\\a", with_filename, "foo", "\\foo"); + t!(s: "foo", with_filename, "bar", "bar"); + t!(s: "\\", with_filename, "foo\\", "\\foo"); + t!(s: "\\a", with_filename, "foo\\", "\\foo"); + t!(s: "a\\b\\c", with_filename, "", "a\\b"); + t!(s: "a\\b\\c", with_filename, ".", "a\\b"); + t!(s: "a\\b\\c", with_filename, "..", "a"); + t!(s: "\\a", with_filename, "", "\\"); + t!(s: "foo", with_filename, "", "."); + t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e"); + t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d"); + t!(s: "..", with_filename, "foo", "..\\foo"); + t!(s: "..\\..", with_filename, "foo", "..\\..\\foo"); + t!(s: "..", with_filename, "", ".."); + t!(s: "..\\..", with_filename, "", "..\\.."); + t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz"); + t!(s: "C:\\foo", with_filename, "bar", "C:\\bar"); + t!(s: "C:\\", with_filename, "foo", "C:\\foo"); + t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz"); + t!(s: "C:foo", with_filename, "bar", "C:bar"); + t!(s: "C:", with_filename, "foo", "C:foo"); + t!(s: "C:\\foo", with_filename, "", "C:\\"); + t!(s: "C:foo", with_filename, "", "C:"); + t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\"); + t!(s: "C:\\foo", with_filename, "..", "C:\\"); + t!(s: "C:\\", with_filename, "..", "C:\\"); + t!(s: "C:foo\\bar", with_filename, "..", "C:"); + t!(s: "C:foo", with_filename, "..", "C:.."); + t!(s: "C:", with_filename, "..", "C:.."); + t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar"); + t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo"); + t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share"); + t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share"); + t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share"); + t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share"); + t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz"); + t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar"); + t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo"); + t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\.."); + t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz"); + t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar"); + t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo"); + t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\.."); + t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz"); + t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar"); + t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\.."); + + t!(s: "hi\\there.txt", with_filestem, "here", "hi\\here.txt"); + t!(s: "hi\\there.txt", with_filestem, "", "hi\\.txt"); + t!(s: "hi\\there.txt", with_filestem, ".", "hi\\..txt"); + t!(s: "hi\\there.txt", with_filestem, "..", "hi\\...txt"); + t!(s: "hi\\there.txt", with_filestem, "\\", "hi\\.txt"); + t!(s: "hi\\there.txt", with_filestem, "foo\\bar", "hi\\foo\\bar.txt"); + t!(s: "hi\\there.foo.txt", with_filestem, "here", "hi\\here.txt"); + t!(s: "hi\\there", with_filestem, "here", "hi\\here"); + t!(s: "hi\\there", with_filestem, "", "hi"); + t!(s: "hi", with_filestem, "", "."); + t!(s: "\\hi", with_filestem, "", "\\"); + t!(s: "hi\\there", with_filestem, "..", "."); + t!(s: "hi\\there", with_filestem, ".", "hi"); + t!(s: "hi\\there.", with_filestem, "foo", "hi\\foo."); + t!(s: "hi\\there.", with_filestem, "", "hi"); + t!(s: "hi\\there.", with_filestem, ".", "."); + t!(s: "hi\\there.", with_filestem, "..", "hi\\..."); + t!(s: "\\", with_filestem, "foo", "\\foo"); + t!(s: ".", with_filestem, "foo", "foo"); + t!(s: "hi\\there..", with_filestem, "here", "hi\\here."); + t!(s: "hi\\there..", with_filestem, "", "hi"); // filestem setter calls filename setter internally, no need for extended tests - t!(s: "hi\\there.txt", with_extension_str, "exe", "hi\\there.exe"); - t!(s: "hi\\there.txt", with_extension_str, "", "hi\\there"); - t!(s: "hi\\there.txt", with_extension_str, ".", "hi\\there.."); - t!(s: "hi\\there.txt", with_extension_str, "..", "hi\\there..."); - t!(s: "hi\\there", with_extension_str, "txt", "hi\\there.txt"); - t!(s: "hi\\there", with_extension_str, ".", "hi\\there.."); - t!(s: "hi\\there", with_extension_str, "..", "hi\\there..."); - t!(s: "hi\\there.", with_extension_str, "txt", "hi\\there.txt"); - t!(s: "hi\\.foo", with_extension_str, "txt", "hi\\.foo.txt"); - t!(s: "hi\\there.txt", with_extension_str, ".foo", "hi\\there..foo"); - t!(s: "\\", with_extension_str, "txt", "\\"); - t!(s: "\\", with_extension_str, ".", "\\"); - t!(s: "\\", with_extension_str, "..", "\\"); - t!(s: ".", with_extension_str, "txt", "."); + t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe"); + t!(s: "hi\\there.txt", with_extension, "", "hi\\there"); + t!(s: "hi\\there.txt", with_extension, ".", "hi\\there.."); + t!(s: "hi\\there.txt", with_extension, "..", "hi\\there..."); + t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt"); + t!(s: "hi\\there", with_extension, ".", "hi\\there.."); + t!(s: "hi\\there", with_extension, "..", "hi\\there..."); + t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt"); + t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt"); + t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo"); + t!(s: "\\", with_extension, "txt", "\\"); + t!(s: "\\", with_extension, ".", "\\"); + t!(s: "\\", with_extension, "..", "\\"); + t!(s: ".", with_extension, "txt", "."); // extension setter calls filename setter internally, no need for extended tests } @@ -2104,9 +2048,9 @@ mod tests { { let path = $path; let arg = $arg; - let mut p1 = Path::from_str(path); + let mut p1 = Path::new(path); p1.$set(arg); - let p2 = Path::from_str(path); + let p2 = Path::new(path); assert_eq!(p1, p2.$with(arg)); } ); @@ -2114,9 +2058,9 @@ mod tests { { let path = $path; let arg = $arg; - let mut p1 = Path::from_vec(path); + let mut p1 = Path::new(path); p1.$set(arg); - let p2 = Path::from_vec(path); + let p2 = Path::new(path); assert_eq!(p1, p2.$with(arg)); } ) @@ -2124,36 +2068,36 @@ mod tests { t!(v: b!("a\\b\\c"), set_dirname, with_dirname, b!("d")); t!(v: b!("a\\b\\c"), set_dirname, with_dirname, b!("d\\e")); - t!(s: "a\\b\\c", set_dirname_str, with_dirname_str, "d"); - t!(s: "a\\b\\c", set_dirname_str, with_dirname_str, "d\\e"); - t!(s: "\\", set_dirname_str, with_dirname_str, "foo"); - t!(s: "\\foo", set_dirname_str, with_dirname_str, "bar"); - t!(s: "a\\b\\c", set_dirname_str, with_dirname_str, ""); - t!(s: "..\\..", set_dirname_str, with_dirname_str, "x"); - t!(s: "foo", set_dirname_str, with_dirname_str, "..\\.."); + t!(s: "a\\b\\c", set_dirname, with_dirname, "d"); + t!(s: "a\\b\\c", set_dirname, with_dirname, "d\\e"); + t!(s: "\\", set_dirname, with_dirname, "foo"); + t!(s: "\\foo", set_dirname, with_dirname, "bar"); + t!(s: "a\\b\\c", set_dirname, with_dirname, ""); + t!(s: "..\\..", set_dirname, with_dirname, "x"); + t!(s: "foo", set_dirname, with_dirname, "..\\.."); t!(v: b!("a\\b\\c"), set_filename, with_filename, b!("d")); t!(v: b!("\\"), set_filename, with_filename, b!("foo")); - t!(s: "a\\b\\c", set_filename_str, with_filename_str, "d"); - t!(s: "\\", set_filename_str, with_filename_str, "foo"); - t!(s: ".", set_filename_str, with_filename_str, "foo"); - t!(s: "a\\b", set_filename_str, with_filename_str, ""); - t!(s: "a", set_filename_str, with_filename_str, ""); + t!(s: "a\\b\\c", set_filename, with_filename, "d"); + t!(s: "\\", set_filename, with_filename, "foo"); + t!(s: ".", set_filename, with_filename, "foo"); + t!(s: "a\\b", set_filename, with_filename, ""); + t!(s: "a", set_filename, with_filename, ""); t!(v: b!("hi\\there.txt"), set_filestem, with_filestem, b!("here")); - t!(s: "hi\\there.txt", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi\\there.", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi\\there", set_filestem_str, with_filestem_str, "here"); - t!(s: "hi\\there.txt", set_filestem_str, with_filestem_str, ""); - t!(s: "hi\\there", set_filestem_str, with_filestem_str, ""); + t!(s: "hi\\there.txt", set_filestem, with_filestem, "here"); + t!(s: "hi\\there.", set_filestem, with_filestem, "here"); + t!(s: "hi\\there", set_filestem, with_filestem, "here"); + t!(s: "hi\\there.txt", set_filestem, with_filestem, ""); + t!(s: "hi\\there", set_filestem, with_filestem, ""); t!(v: b!("hi\\there.txt"), set_extension, with_extension, b!("exe")); - t!(s: "hi\\there.txt", set_extension_str, with_extension_str, "exe"); - t!(s: "hi\\there.", set_extension_str, with_extension_str, "txt"); - t!(s: "hi\\there", set_extension_str, with_extension_str, "txt"); - t!(s: "hi\\there.txt", set_extension_str, with_extension_str, ""); - t!(s: "hi\\there", set_extension_str, with_extension_str, ""); - t!(s: ".", set_extension_str, with_extension_str, "txt"); + t!(s: "hi\\there.txt", set_extension, with_extension, "exe"); + t!(s: "hi\\there.", set_extension, with_extension, "txt"); + t!(s: "hi\\there", set_extension, with_extension, "txt"); + t!(s: "hi\\there.txt", set_extension, with_extension, ""); + t!(s: "hi\\there", set_extension, with_extension, ""); + t!(s: ".", set_extension, with_extension, "txt"); // with_ helpers use the setter internally, so the tests for the with_ helpers // will suffice. No need for the full set of prefix tests. @@ -2164,14 +2108,14 @@ mod tests { macro_rules! t( (s: $path:expr, $ext:expr, $exp:expr) => ( { - let mut path = Path::from_str($path); - path.add_extension_str($ext); + let mut path = Path::new($path); + path.add_extension($ext); assert_eq!(path.as_str(), Some($exp)); } ); (v: $path:expr, $ext:expr, $exp:expr) => ( { - let mut path = Path::from_vec($path); + let mut path = Path::new($path); path.add_extension($ext); assert_eq!(path.as_vec(), $exp); } @@ -2221,19 +2165,19 @@ mod tests { ) ) - t!(v: Path::from_vec(b!("a\\b\\c")), Some(b!("c")), b!("a\\b"), Some(b!("c")), None); - t!(s: Path::from_str("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None); - t!(s: Path::from_str("."), None, Some("."), None, None); - t!(s: Path::from_str("\\"), None, Some("\\"), None, None); - t!(s: Path::from_str(".."), None, Some(".."), None, None); - t!(s: Path::from_str("..\\.."), None, Some("..\\.."), None, None); - t!(s: Path::from_str("hi\\there.txt"), Some("there.txt"), Some("hi"), + 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::from_str("hi\\there"), Some("there"), Some("hi"), Some("there"), None); - t!(s: Path::from_str("hi\\there."), Some("there."), Some("hi"), + 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::from_str("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None); - t!(s: Path::from_str("hi\\..there"), Some("..there"), Some("hi"), + 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")); // these are already tested in test_components, so no need for extended tests @@ -2241,12 +2185,12 @@ mod tests { #[test] fn test_dir_file_path() { - t!(s: Path::from_str("hi\\there").dir_path(), "hi"); - t!(s: Path::from_str("hi").dir_path(), "."); - t!(s: Path::from_str("\\hi").dir_path(), "\\"); - t!(s: Path::from_str("\\").dir_path(), "\\"); - t!(s: Path::from_str("..").dir_path(), ".."); - t!(s: Path::from_str("..\\..").dir_path(), "..\\.."); + t!(s: Path::new("hi\\there").dir_path(), "hi"); + t!(s: Path::new("hi").dir_path(), "."); + t!(s: Path::new("\\hi").dir_path(), "\\"); + t!(s: Path::new("\\").dir_path(), "\\"); + t!(s: Path::new("..").dir_path(), ".."); + t!(s: Path::new("..\\..").dir_path(), "..\\.."); macro_rules! t( ($path:expr, $exp:expr) => ( @@ -2258,12 +2202,12 @@ mod tests { ); ) - t!(Path::from_str("hi\\there").file_path(), Some("there")); - t!(Path::from_str("hi").file_path(), Some("hi")); - t!(Path::from_str(".").file_path(), None); - t!(Path::from_str("\\").file_path(), None); - t!(Path::from_str("..").file_path(), None); - t!(Path::from_str("..\\..").file_path(), None); + t!(Path::new("hi\\there").file_path(), Some("there")); + t!(Path::new("hi").file_path(), Some("hi")); + t!(Path::new(".").file_path(), None); + t!(Path::new("\\").file_path(), None); + t!(Path::new("..").file_path(), None); + t!(Path::new("..\\..").file_path(), None); // dir_path and file_path are just dirname and filename interpreted as paths. // No need for extended tests @@ -2274,7 +2218,7 @@ mod tests { macro_rules! t( ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => ( { - let path = Path::from_str($path); + 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 {:?}", @@ -2314,8 +2258,8 @@ mod tests { macro_rules! t( (s: $path:expr, $dest:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let dest = Path::from_str($dest); + let path = Path::new($path); + let dest = Path::new($dest); let exp = $exp; let res = path.is_ancestor_of(&dest); assert!(res == exp, @@ -2417,8 +2361,8 @@ mod tests { macro_rules! t( (s: $path:expr, $child:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let child = Path::from_str($child); + let path = Path::new($path); + let child = Path::new($child); assert_eq!(path.ends_with_path(&child), $exp); } ); @@ -2449,8 +2393,8 @@ mod tests { macro_rules! t( (s: $path:expr, $other:expr, $exp:expr) => ( { - let path = Path::from_str($path); - let other = Path::from_str($other); + let path = Path::new($path); + let other = Path::new($other); let res = path.path_relative_from(&other); let exp = $exp; assert!(res.and_then_ref(|x| x.as_str()) == exp, @@ -2583,7 +2527,7 @@ mod tests { macro_rules! t( (s: $path:expr, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let comps = path.str_component_iter().map(|x|x.unwrap()).to_owned_vec(); let exp: &[&str] = $exp; assert!(comps.as_slice() == exp, @@ -2598,7 +2542,7 @@ mod tests { ); (v: [$($arg:expr),+], $exp:expr) => ( { - let path = Path::from_vec(b!($($arg),+)); + let path = Path::new(b!($($arg),+)); let comps = path.str_component_iter().map(|x|x.unwrap()).to_owned_vec(); let exp: &[&str] = $exp; assert!(comps.as_slice() == exp, @@ -2658,7 +2602,7 @@ mod tests { macro_rules! t( (s: $path:expr, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let comps = path.component_iter().to_owned_vec(); let exp: &[&[u8]] = $exp; assert!(comps.as_slice() == exp, "component_iter: Expected {:?}, found {:?}", @@ -2679,13 +2623,13 @@ mod tests { #[test] fn test_each_parent() { - assert!(Path::from_str("/foo/bar").each_parent(|_| true)); - assert!(!Path::from_str("/foo/bar").each_parent(|_| false)); + assert!(Path::new("/foo/bar").each_parent(|_| true)); + assert!(!Path::new("/foo/bar").each_parent(|_| false)); macro_rules! t( (s: $path:expr, $exp:expr) => ( { - let path = Path::from_str($path); + let path = Path::new($path); let exp: &[&str] = $exp; let mut comps = exp.iter().map(|&x|x); do path.each_parent |p| { diff --git a/src/libstd/rt/io/file.rs b/src/libstd/rt/io/file.rs index 9fbb897c2a7..39c3c5692f8 100644 --- a/src/libstd/rt/io/file.rs +++ b/src/libstd/rt/io/file.rs @@ -703,7 +703,7 @@ mod test { fn file_test_io_smoke_test() { do run_in_mt_newsched_task { let message = "it's alright. have a good time"; - let filename = &Path::from_str("./tmp/file_rt_io_file_test.txt"); + let filename = &Path::new("./tmp/file_rt_io_file_test.txt"); { let mut write_stream = open(filename, Create, ReadWrite).unwrap(); write_stream.write(message.as_bytes()); @@ -725,7 +725,7 @@ mod test { #[test] fn file_test_io_invalid_path_opened_without_create_should_raise_condition() { do run_in_mt_newsched_task { - let filename = &Path::from_str("./tmp/file_that_does_not_exist.txt"); + let filename = &Path::new("./tmp/file_that_does_not_exist.txt"); let mut called = false; do io_error::cond.trap(|_| { called = true; @@ -740,7 +740,7 @@ mod test { #[test] fn file_test_iounlinking_invalid_path_should_raise_condition() { do run_in_mt_newsched_task { - let filename = &Path::from_str("./tmp/file_another_file_that_does_not_exist.txt"); + let filename = &Path::new("./tmp/file_another_file_that_does_not_exist.txt"); let mut called = false; do io_error::cond.trap(|_| { called = true; @@ -757,7 +757,7 @@ mod test { use str; let message = "ten-four"; let mut read_mem = [0, .. 8]; - let filename = &Path::from_str("./tmp/file_rt_io_file_test_positional.txt"); + let filename = &Path::new("./tmp/file_rt_io_file_test_positional.txt"); { let mut rw_stream = open(filename, Create, ReadWrite).unwrap(); rw_stream.write(message.as_bytes()); @@ -788,7 +788,7 @@ mod test { let set_cursor = 4 as u64; let mut tell_pos_pre_read; let mut tell_pos_post_read; - let filename = &Path::from_str("./tmp/file_rt_io_file_test_seeking.txt"); + let filename = &Path::new("./tmp/file_rt_io_file_test_seeking.txt"); { let mut rw_stream = open(filename, Create, ReadWrite).unwrap(); rw_stream.write(message.as_bytes()); @@ -817,7 +817,7 @@ mod test { let final_msg = "foo-the-bar!!"; let seek_idx = 3; let mut read_mem = [0, .. 13]; - let filename = &Path::from_str("./tmp/file_rt_io_file_test_seek_and_write.txt"); + let filename = &Path::new("./tmp/file_rt_io_file_test_seek_and_write.txt"); { let mut rw_stream = open(filename, Create, ReadWrite).unwrap(); rw_stream.write(initial_msg.as_bytes()); @@ -843,7 +843,7 @@ mod test { let chunk_two = "asdf"; let chunk_three = "zxcv"; let mut read_mem = [0, .. 4]; - let filename = &Path::from_str("./tmp/file_rt_io_file_test_seek_shakedown.txt"); + let filename = &Path::new("./tmp/file_rt_io_file_test_seek_shakedown.txt"); { let mut rw_stream = open(filename, Create, ReadWrite).unwrap(); rw_stream.write(initial_msg.as_bytes()); @@ -873,7 +873,7 @@ mod test { #[test] fn file_test_stat_is_correct_on_is_file() { do run_in_mt_newsched_task { - let filename = &Path::from_str("./tmp/file_stat_correct_on_is_file.txt"); + let filename = &Path::new("./tmp/file_stat_correct_on_is_file.txt"); { let mut fs = open(filename, Create, ReadWrite).unwrap(); let msg = "hw"; @@ -891,7 +891,7 @@ mod test { #[test] fn file_test_stat_is_correct_on_is_dir() { do run_in_mt_newsched_task { - let filename = &Path::from_str("./tmp/file_stat_correct_on_is_dir"); + let filename = &Path::new("./tmp/file_stat_correct_on_is_dir"); mkdir(filename); let stat_res = match stat(filename) { Some(s) => s, @@ -905,7 +905,7 @@ mod test { #[test] fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() { do run_in_mt_newsched_task { - let dir = &Path::from_str("./tmp/fileinfo_false_on_dir"); + let dir = &Path::new("./tmp/fileinfo_false_on_dir"); mkdir(dir); assert!(dir.is_file() == false); rmdir(dir); @@ -915,7 +915,7 @@ mod test { #[test] fn file_test_fileinfo_check_exists_before_and_after_file_creation() { do run_in_mt_newsched_task { - let file = &Path::from_str("./tmp/fileinfo_check_exists_b_and_a.txt"); + let file = &Path::new("./tmp/fileinfo_check_exists_b_and_a.txt"); { let msg = "foo".as_bytes(); let mut w = file.open_writer(Create); @@ -930,7 +930,7 @@ mod test { #[test] fn file_test_directoryinfo_check_exists_before_and_after_mkdir() { do run_in_mt_newsched_task { - let dir = &Path::from_str("./tmp/before_and_after_dir"); + let dir = &Path::new("./tmp/before_and_after_dir"); assert!(!dir.exists()); dir.mkdir(); assert!(dir.exists()); @@ -944,11 +944,11 @@ mod test { fn file_test_directoryinfo_readdir() { use str; do run_in_mt_newsched_task { - let dir = &Path::from_str("./tmp/di_readdir"); + let dir = &Path::new("./tmp/di_readdir"); dir.mkdir(); let prefix = "foo"; for n in range(0,3) { - let f = dir.join_str(format!("{}.txt", n)); + let f = dir.join(format!("{}.txt", n)); let mut w = f.open_writer(Create); let msg_str = (prefix + n.to_str().to_owned()).to_owned(); let msg = msg_str.as_bytes(); diff --git a/src/libstd/rt/io/support.rs b/src/libstd/rt/io/support.rs index a872423c255..31040bc51a1 100644 --- a/src/libstd/rt/io/support.rs +++ b/src/libstd/rt/io/support.rs @@ -35,7 +35,7 @@ mod test { #[test] fn path_like_smoke_test() { let expected = if cfg!(unix) { "/home" } else { "C:\\" }; - let path = Path::from_str(expected); + let path = Path::new(expected); path.path_as_str(|p| assert!(p == expected)); path.path_as_str(|p| assert!(p == expected)); } diff --git a/src/libstd/rt/uv/file.rs b/src/libstd/rt/uv/file.rs index 7756448adf8..cb5054626d4 100644 --- a/src/libstd/rt/uv/file.rs +++ b/src/libstd/rt/uv/file.rs @@ -391,7 +391,7 @@ mod test { let read_mem = vec::from_elem(read_buf_len, 0u8); let read_buf = slice_to_uv_buf(read_mem); let read_buf_ptr: *Buf = &read_buf; - let p = Path::from_str(path_str); + let p = Path::new(path_str); let open_req = FsRequest::new(); do open_req.open(&loop_, &p, create_flags as int, mode as int) |req, uverr| { @@ -405,7 +405,7 @@ mod test { assert!(uverr.is_none()); let loop_ = req.get_loop(); let open_req = FsRequest::new(); - do open_req.open(&loop_, &Path::from_str(path_str), read_flags as int,0) + do open_req.open(&loop_, &Path::new(path_str), read_flags as int,0) |req, uverr| { assert!(uverr.is_none()); let loop_ = req.get_loop(); @@ -431,7 +431,7 @@ mod test { assert!(uverr.is_none()); let loop_ = &req.get_loop(); let unlink_req = FsRequest::new(); - do unlink_req.unlink(loop_, &Path::from_str(path_str)) + do unlink_req.unlink(loop_, &Path::new(path_str)) |_,uverr| { assert!(uverr.is_none()); }; @@ -465,7 +465,7 @@ mod test { let write_buf = slice_to_uv_buf(write_val); // open/create let open_req = FsRequest::new(); - let result = open_req.open_sync(&loop_, &Path::from_str(path_str), + let result = open_req.open_sync(&loop_, &Path::new(path_str), create_flags as int, mode as int); assert!(result.is_ok()); let fd = result.unwrap(); @@ -479,7 +479,7 @@ mod test { assert!(result.is_ok()); // re-open let open_req = FsRequest::new(); - let result = open_req.open_sync(&loop_, &Path::from_str(path_str), + let result = open_req.open_sync(&loop_, &Path::new(path_str), read_flags as int,0); assert!(result.is_ok()); let len = 1028; @@ -503,7 +503,7 @@ mod test { assert!(result.is_ok()); // unlink let unlink_req = FsRequest::new(); - let result = unlink_req.unlink_sync(&loop_, &Path::from_str(path_str)); + let result = unlink_req.unlink_sync(&loop_, &Path::new(path_str)); assert!(result.is_ok()); } else { fail2!("nread was 0.. wudn't expectin' that."); } loop_.close(); diff --git a/src/libstd/rt/uv/uvio.rs b/src/libstd/rt/uv/uvio.rs index a139c4e95ef..d5893d6d014 100644 --- a/src/libstd/rt/uv/uvio.rs +++ b/src/libstd/rt/uv/uvio.rs @@ -18,6 +18,7 @@ use ops::Drop; use option::*; use ptr; use str; +use str::Str; use result::*; use rt::io::IoError; use rt::io::net::ip::{SocketAddr, IpAddr}; @@ -631,7 +632,7 @@ impl IoFactory for UvIoFactory { None => { let stat = req.get_stat(); Ok(FileStat { - path: Path::from_str(path_str), + path: Path::new(path_str.as_slice()), is_file: stat.is_file(), is_dir: stat.is_dir(), size: stat.st_size, @@ -720,8 +721,8 @@ impl IoFactory for UvIoFactory { let rel_paths = req.get_paths(); let mut paths = ~[]; for r in rel_paths.iter() { - let mut p = Path::from_str(path_str); - p.push_str(*r); + let mut p = Path::new(path_str.as_slice()); + p.push(r.as_slice()); paths.push(p); } Ok(paths) @@ -2179,20 +2180,20 @@ fn file_test_uvio_full_simple_impl() { { let create_fm = Create; let create_fa = ReadWrite; - let mut fd = (*io).fs_open(&Path::from_str(path), create_fm, create_fa).unwrap(); + let mut fd = (*io).fs_open(&Path::new(path), create_fm, create_fa).unwrap(); let write_buf = write_val.as_bytes(); fd.write(write_buf); } { let ro_fm = Open; let ro_fa = Read; - let mut fd = (*io).fs_open(&Path::from_str(path), ro_fm, ro_fa).unwrap(); + let mut fd = (*io).fs_open(&Path::new(path), ro_fm, ro_fa).unwrap(); let mut read_vec = [0, .. 1028]; let nread = fd.read(read_vec).unwrap(); let read_val = str::from_utf8(read_vec.slice(0, nread as uint)); assert!(read_val == write_val.to_owned()); } - (*io).fs_unlink(&Path::from_str(path)); + (*io).fs_unlink(&Path::new(path)); } } diff --git a/src/libstd/run.rs b/src/libstd/run.rs index 3f7ce3eae58..0d32efbba88 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -579,7 +579,7 @@ mod tests { let output = str::from_utf8(prog.finish_with_output().output); let parent_dir = os::getcwd(); - let child_dir = Path::from_str(output.trim()); + let child_dir = Path::new(output.trim()); let parent_stat = parent_dir.stat().unwrap(); let child_stat = child_dir.stat().unwrap(); @@ -596,7 +596,7 @@ mod tests { let mut prog = run_pwd(Some(&parent_dir)); let output = str::from_utf8(prog.finish_with_output().output); - let child_dir = Path::from_str(output.trim()); + let child_dir = Path::new(output.trim()); let parent_stat = parent_dir.stat().unwrap(); let child_stat = child_dir.stat().unwrap(); diff --git a/src/libstd/unstable/dynamic_lib.rs b/src/libstd/unstable/dynamic_lib.rs index a91d68366f3..58ff51fe102 100644 --- a/src/libstd/unstable/dynamic_lib.rs +++ b/src/libstd/unstable/dynamic_lib.rs @@ -121,7 +121,7 @@ mod test { fn test_errors_do_not_crash() { // Open /dev/null as a library to get an error, and make sure // that only causes an error, and not a crash. - let path = GenericPath::from_str("/dev/null"); + let path = GenericPath::new("/dev/null"); match DynamicLibrary::open(Some(&path)) { Err(_) => {} Ok(_) => fail2!("Successfully opened the empty library.") |
