1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15883
13 //! Windows file path handling
15 use self::PathPrefix::*;
20 use cmp::{Ordering, Eq, Ord, PartialEq, PartialOrd};
24 use iter::{AdditiveIterator, Extend};
25 use iter::{Iterator, IteratorExt, Map, repeat};
27 use option::Option::{self, Some, None};
28 use result::Result::{self, Ok, Err};
29 use slice::{SliceExt, SliceConcatExt};
30 use str::{SplitTerminator, FromStr, StrExt};
31 use string::{String, ToString};
34 use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe};
36 /// Iterator that yields successive components of a Path as &str
38 /// Each component is yielded as Option<&str> for compatibility with PosixPath, but
39 /// every component in WindowsPath is guaranteed to be Some.
40 pub type StrComponents<'a> =
41 Map<SplitTerminator<'a, char>, fn(&'a str) -> Option<&'a str>>;
43 /// Iterator that yields successive components of a Path as &[u8]
44 pub type Components<'a> =
45 Map<StrComponents<'a>, fn(Option<&str>) -> &[u8]>;
47 /// Represents a Windows path
48 // Notes for Windows path impl:
49 // The MAX_PATH is 260, but 253 is the practical limit due to some API bugs
50 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for good information
51 // about windows paths.
52 // That same page puts a bunch of restrictions on allowed characters in a path.
53 // `\foo.txt` means "relative to current drive", but will not be considered to be absolute here
54 // as `∃P | P.join("\foo.txt") != "\foo.txt"`.
55 // `C:` is interesting, that means "the current directory on drive C".
56 // Long absolute paths need to have \\?\ prefix (or, for UNC, \\?\UNC\). I think that can be
57 // ignored for now, though, and only added in a hypothetical .to_pwstr() function.
58 // However, if a path is parsed that has \\?\, this needs to be preserved as it disables the
59 // processing of "." and ".." components and / as a separator.
60 // Experimentally, \\?\foo is not the same thing as \foo.
61 // Also, \\foo is not valid either (certainly not equivalent to \foo).
62 // Similarly, C:\\Users is not equivalent to C:\Users, although C:\Users\\foo is equivalent
63 // to C:\Users\foo. In fact the command prompt treats C:\\foo\bar as UNC path. But it might be
64 // best to just ignore that and normalize it to C:\foo\bar.
66 // Based on all this, I think the right approach is to do the following:
67 // * Require valid utf-8 paths. Windows API may use WCHARs, but we don't, and utf-8 is convertible
68 // to UTF-16 anyway (though does Windows use UTF-16 or UCS-2? Not sure).
69 // * Parse the prefixes \\?\UNC\, \\?\, and \\.\ explicitly.
70 // * If \\?\UNC\, treat following two path components as server\share. Don't error for missing
72 // * If \\?\, parse disk from following component, if present. Don't error for missing disk.
73 // * If \\.\, treat rest of path as just regular components. I don't know how . and .. are handled
74 // here, they probably aren't, but I'm not going to worry about that.
75 // * Else if starts with \\, treat following two components as server\share. Don't error for missing
77 // * Otherwise, attempt to parse drive from start of path.
79 // The only error condition imposed here is valid utf-8. All other invalid paths are simply
80 // preserved by the data structure; let the Windows API error out on them.
83 repr: String, // assumed to never be empty
84 prefix: Option<PathPrefix>,
85 sepidx: Option<uint> // index of the final separator in the non-prefix portion of repr
88 #[stable(feature = "rust1", since = "1.0.0")]
89 impl fmt::Debug for Path {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 fmt::Debug::fmt(&self.display(), f)
95 impl PartialEq for Path {
97 fn eq(&self, other: &Path) -> bool {
98 self.repr == other.repr
104 impl PartialOrd for Path {
105 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
106 Some(self.cmp(other))
111 fn cmp(&self, other: &Path) -> Ordering {
112 self.repr.cmp(&other.repr)
116 impl FromStr for Path {
117 type Err = ParsePathError;
118 fn from_str(s: &str) -> Result<Path, ParsePathError> {
119 match Path::new_opt(s) {
121 None => Err(ParsePathError),
126 /// Value indicating that a path could not be parsed from a string.
127 #[derive(Debug, Clone, PartialEq, Copy)]
128 pub struct ParsePathError;
131 impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for Path {
134 fn hash(&self, state: &mut S) {
135 self.repr.hash(state)
140 fn hash(&self, _: &mut S) {
141 // No-op because the `hash` implementation will be wrong.
145 #[stable(feature = "rust1", since = "1.0.0")]
146 impl hash::Hash for Path {
149 fn hash<H: hash::Hasher>(&self, state: &mut H) {
150 self.repr.hash(state)
155 fn hash<H: hash::Hasher>(&self, _: &mut H) {
156 // No-op because the `hash` implementation will be wrong.
160 impl BytesContainer for Path {
162 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
166 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
170 fn is_str(_: Option<&Path>) -> bool { true }
173 impl GenericPathUnsafe for Path {
174 /// See `GenericPathUnsafe::from_vec_unchecked`.
178 /// Panics if not valid UTF-8.
180 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
181 let (prefix, path) = Path::normalize_(path.container_as_str().unwrap());
182 assert!(!path.is_empty());
183 let mut ret = Path{ repr: path, prefix: prefix, sepidx: None };
188 /// See `GenericPathUnsafe::set_filename_unchecked`.
192 /// Panics if not valid UTF-8.
193 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
194 let filename = filename.container_as_str().unwrap();
195 match self.sepidx_or_prefix_len() {
196 None if ".." == self.repr => {
197 let mut s = String::with_capacity(3 + filename.len());
200 s.push_str(filename);
201 self.update_normalized(&s[..]);
204 self.update_normalized(filename);
206 Some((_,idxa,end)) if &self.repr[idxa..end] == ".." => {
207 let mut s = String::with_capacity(end + 1 + filename.len());
208 s.push_str(&self.repr[..end]);
210 s.push_str(filename);
211 self.update_normalized(&s[..]);
213 Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => {
214 let mut s = String::with_capacity(idxb + filename.len());
215 s.push_str(&self.repr[..idxb]);
216 s.push_str(filename);
217 self.update_normalized(&s[..]);
219 Some((idxb,_,_)) => {
220 let mut s = String::with_capacity(idxb + 1 + filename.len());
221 s.push_str(&self.repr[..idxb]);
223 s.push_str(filename);
224 self.update_normalized(&s[..]);
229 /// See `GenericPathUnsafe::push_unchecked`.
231 /// Concatenating two Windows Paths is rather complicated.
232 /// For the most part, it will behave as expected, except in the case of
233 /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no
234 /// concept of per-volume cwds like Windows does, we can't behave exactly
235 /// like Windows will. Instead, if the receiver is an absolute path on
236 /// the same volume as the new path, it will be treated as the cwd that
237 /// the new path is relative to. Otherwise, the new path will be treated
238 /// as if it were absolute and will replace the receiver outright.
239 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
240 let path = path.container_as_str().unwrap();
241 fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
242 // assume prefix is Some(DiskPrefix)
243 let rest = &path[prefix_len(prefix)..];
244 !rest.is_empty() && rest.as_bytes()[0].is_ascii() && is_sep(rest.as_bytes()[0] as char)
246 fn shares_volume(me: &Path, path: &str) -> bool {
247 // path is assumed to have a prefix of Some(DiskPrefix)
248 let repr = &me.repr[..];
250 Some(DiskPrefix) => {
251 repr.as_bytes()[0] == path.as_bytes()[0].to_ascii_uppercase()
253 Some(VerbatimDiskPrefix) => {
254 repr.as_bytes()[4] == path.as_bytes()[0].to_ascii_uppercase()
259 fn is_sep_(prefix: Option<PathPrefix>, u: u8) -> bool {
260 if prefix_is_verbatim(prefix) { is_sep_verbatim(u as char) }
261 else { is_sep(u as char) }
264 fn replace_path(me: &mut Path, path: &str, prefix: Option<PathPrefix>) {
265 let newpath = Path::normalize__(path, prefix);
266 me.repr = match newpath {
268 None => String::from_str(path)
273 fn append_path(me: &mut Path, path: &str) {
274 // appends a path that has no prefix
275 // if me is verbatim, we need to pre-normalize the new path
276 let path_ = if is_verbatim(me) { Path::normalize__(path, None) }
278 let pathlen = path_.as_ref().map_or(path.len(), |p| p.len());
279 let mut s = String::with_capacity(me.repr.len() + 1 + pathlen);
280 s.push_str(&me.repr[..]);
281 let plen = me.prefix_len();
282 // if me is "C:" we don't want to add a path separator
284 Some(DiskPrefix) if me.repr.len() == plen => (),
285 _ if !(me.repr.len() > plen && me.repr.as_bytes()[me.repr.len()-1] == SEP_BYTE) => {
291 None => s.push_str(path),
292 Some(p) => s.push_str(&p[..]),
294 me.update_normalized(&s[..])
297 if !path.is_empty() {
298 let prefix = parse_prefix(path);
300 Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => {
301 // cwd-relative path, self is on the same volume
302 append_path(self, &path[prefix_len(prefix)..]);
305 // absolute path, or cwd-relative and self is not same volume
306 replace_path(self, path, prefix);
308 None if !path.is_empty() && is_sep_(self.prefix, path.as_bytes()[0]) => {
309 // volume-relative path
310 if self.prefix.is_some() {
311 // truncate self down to the prefix, then append
312 let n = self.prefix_len();
313 self.repr.truncate(n);
314 append_path(self, path);
316 // we have no prefix, so nothing to be relative to
317 replace_path(self, path, prefix);
322 append_path(self, path);
329 impl GenericPath for Path {
331 fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
332 match path.container_as_str() {
338 Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
344 /// See `GenericPath::as_str` for info.
345 /// Always returns a `Some` value.
347 fn as_str<'a>(&'a self) -> Option<&'a str> {
352 fn as_vec<'a>(&'a self) -> &'a [u8] {
357 fn into_vec(self) -> Vec<u8> {
358 self.repr.into_bytes()
362 fn dirname<'a>(&'a self) -> &'a [u8] {
363 self.dirname_str().unwrap().as_bytes()
366 /// See `GenericPath::dirname_str` for info.
367 /// Always returns a `Some` value.
368 fn dirname_str<'a>(&'a self) -> Option<&'a str> {
369 Some(match self.sepidx_or_prefix_len() {
370 None if ".." == self.repr => &self.repr[..],
372 Some((_,idxa,end)) if &self.repr[idxa..end] == ".." => {
375 Some((idxb,_,end)) if &self.repr[idxb..end] == "\\" => {
378 Some((0,idxa,_)) => &self.repr[..idxa],
379 Some((idxb,idxa,_)) => {
381 Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => {
384 _ => &self.repr[..idxb]
391 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
392 self.filename_str().map(|x| x.as_bytes())
395 /// See `GenericPath::filename_str` for info.
396 /// Always returns a `Some` value if `filename` returns a `Some` value.
397 fn filename_str<'a>(&'a self) -> Option<&'a str> {
398 let repr = &self.repr[..];
399 match self.sepidx_or_prefix_len() {
400 None if "." == repr || ".." == repr => None,
402 Some((_,idxa,end)) if &repr[idxa..end] == ".." => None,
403 Some((_,idxa,end)) if idxa == end => None,
404 Some((_,idxa,end)) => Some(&repr[idxa..end])
408 /// See `GenericPath::filestem_str` for info.
409 /// Always returns a `Some` value if `filestem` returns a `Some` value.
411 fn filestem_str<'a>(&'a self) -> Option<&'a str> {
412 // filestem() returns a byte vector that's guaranteed valid UTF-8
413 self.filestem().map(|t| unsafe { mem::transmute(t) })
417 fn extension_str<'a>(&'a self) -> Option<&'a str> {
418 // extension() returns a byte vector that's guaranteed valid UTF-8
419 self.extension().map(|t| unsafe { mem::transmute(t) })
422 fn dir_path(&self) -> Path {
423 unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) }
427 fn pop(&mut self) -> bool {
428 match self.sepidx_or_prefix_len() {
429 None if "." == self.repr => false,
431 self.repr = String::from_str(".");
435 Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false,
436 Some((idxb,_,end)) if &self.repr[idxb..end] == "\\" => false,
437 Some((idxb,idxa,_)) => {
438 let trunc = match self.prefix {
439 Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => {
440 let plen = self.prefix_len();
441 if idxb == plen { idxa } else { idxb }
445 self.repr.truncate(trunc);
446 self.update_sepidx();
452 fn root_path(&self) -> Option<Path> {
453 if self.prefix.is_some() {
454 Some(Path::new(match self.prefix {
455 Some(DiskPrefix) if self.is_absolute() => {
456 &self.repr[..self.prefix_len()+1]
458 Some(VerbatimDiskPrefix) => {
459 &self.repr[..self.prefix_len()+1]
461 _ => &self.repr[..self.prefix_len()]
463 } else if is_vol_relative(self) {
464 Some(Path::new(&self.repr[..1]))
470 /// See `GenericPath::is_absolute` for info.
472 /// A Windows Path is considered absolute only if it has a non-volume prefix,
473 /// or if it has a volume prefix and the path starts with '\'.
474 /// A path of `\foo` is not considered absolute because it's actually
475 /// relative to the "current volume". A separate method `Path::is_vol_relative`
476 /// is provided to indicate this case. Similarly a path of `C:foo` is not
477 /// considered absolute because it's relative to the cwd on volume C:. A
478 /// separate method `Path::is_cwd_relative` is provided to indicate this case.
480 fn is_absolute(&self) -> bool {
482 Some(DiskPrefix) => {
483 let rest = &self.repr[self.prefix_len()..];
484 rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE
492 fn is_relative(&self) -> bool {
493 self.prefix.is_none() && !is_vol_relative(self)
496 fn is_ancestor_of(&self, other: &Path) -> bool {
497 if !self.equiv_prefix(other) {
499 } else if self.is_absolute() != other.is_absolute() ||
500 is_vol_relative(self) != is_vol_relative(other) {
503 let mut ita = self.str_components().map(|x|x.unwrap());
504 let mut itb = other.str_components().map(|x|x.unwrap());
505 if "." == self.repr {
506 return itb.next() != Some("..");
509 match (ita.next(), itb.next()) {
511 (Some(a), Some(b)) if a == b => { continue },
512 (Some(a), _) if a == ".." => {
513 // if ita contains only .. components, it's an ancestor
514 return ita.all(|x| x == "..");
523 fn path_relative_from(&self, base: &Path) -> Option<Path> {
524 fn comp_requires_verbatim(s: &str) -> bool {
525 s == "." || s == ".." || s.contains_char(SEP2)
528 if !self.equiv_prefix(base) {
530 if self.is_absolute() {
532 } else if self.prefix == Some(DiskPrefix) && base.prefix == Some(DiskPrefix) {
533 // both drives, drive letters must differ or they'd be equiv
538 } else if self.is_absolute() != base.is_absolute() {
539 if self.is_absolute() {
544 } else if is_vol_relative(self) != is_vol_relative(base) {
545 if is_vol_relative(self) {
551 let mut ita = self.str_components().map(|x|x.unwrap());
552 let mut itb = base.str_components().map(|x|x.unwrap());
553 let mut comps = vec![];
555 let a_verb = is_verbatim(self);
556 let b_verb = is_verbatim(base);
558 match (ita.next(), itb.next()) {
559 (None, None) => break,
560 (Some(a), None) if a_verb && comp_requires_verbatim(a) => {
561 return Some(self.clone())
566 comps.extend(ita.by_ref());
570 (None, _) => comps.push(".."),
571 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
572 (Some(a), Some(b)) if !b_verb && b == "." => {
573 if a_verb && comp_requires_verbatim(a) {
574 return Some(self.clone())
575 } else { comps.push(a) }
577 (Some(_), Some(b)) if !b_verb && b == ".." => return None,
578 (Some(a), Some(_)) if a_verb && comp_requires_verbatim(a) => {
579 return Some(self.clone())
581 (Some(a), Some(_)) => {
583 for _ in itb.by_ref() {
588 comps.extend(ita.by_ref());
594 Some(Path::new(comps.connect("\\")))
598 fn ends_with_path(&self, child: &Path) -> bool {
599 if !child.is_relative() { return false; }
600 let mut selfit = self.str_components().rev();
601 let mut childit = child.str_components().rev();
603 match (selfit.next(), childit.next()) {
604 (Some(a), Some(b)) => if a != b { return false; },
605 (Some(_), None) => break,
606 (None, Some(_)) => return false,
607 (None, None) => break
615 /// Returns a new `Path` from a `BytesContainer`.
619 /// Panics if the vector contains a `NUL`, or if it contains invalid UTF-8.
624 /// println!("{}", Path::new(r"C:\some\path").display());
627 pub fn new<T: BytesContainer>(path: T) -> Path {
628 GenericPath::new(path)
631 /// Returns a new `Some(Path)` from a `BytesContainer`.
633 /// Returns `None` if the vector contains a `NUL`, or if it contains invalid UTF-8.
638 /// let path = Path::new_opt(r"C:\some\path");
641 /// Some(path) => println!("{}", path.display()),
642 /// None => println!("There was a problem with your path."),
646 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
647 GenericPath::new_opt(path)
650 /// Returns an iterator that yields each component of the path in turn as a Option<&str>.
651 /// Every component is guaranteed to be Some.
652 /// Does not yield the path prefix (including server/share components in UNC paths).
653 /// Does not distinguish between volume-relative and relative paths, e.g.
654 /// \a\b\c and a\b\c.
655 /// Does not distinguish between absolute and cwd-relative paths, e.g.
656 /// C:\foo and C:foo.
657 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
658 let repr = &self.repr[..];
659 let s = match self.prefix {
661 let plen = self.prefix_len();
662 if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE {
664 } else { &repr[plen..] }
666 None if repr.as_bytes()[0] == SEP_BYTE => &repr[1..],
669 let some: fn(&'a str) -> Option<&'a str> = Some; // coerce to fn ptr
670 let ret = s.split_terminator(SEP).map(some);
674 /// Returns an iterator that yields each component of the path in turn as a &[u8].
675 /// See str_components() for details.
676 pub fn components<'a>(&'a self) -> Components<'a> {
677 fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
679 x.unwrap().as_bytes()
681 let convert: for<'b> fn(Option<&'b str>) -> &'b [u8] = convert; // coerce to fn ptr
682 self.str_components().map(convert)
685 fn equiv_prefix(&self, other: &Path) -> bool {
686 let s_repr = &self.repr[..];
687 let o_repr = &other.repr[..];
688 match (self.prefix, other.prefix) {
689 (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
690 self.is_absolute() &&
691 s_repr.as_bytes()[0].to_ascii_lowercase() ==
692 o_repr.as_bytes()[4].to_ascii_lowercase()
694 (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
695 other.is_absolute() &&
696 s_repr.as_bytes()[4].to_ascii_lowercase() ==
697 o_repr.as_bytes()[0].to_ascii_lowercase()
699 (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
700 s_repr.as_bytes()[4].to_ascii_lowercase() ==
701 o_repr.as_bytes()[4].to_ascii_lowercase()
703 (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
704 &s_repr[2..self.prefix_len()] == &o_repr[8..other.prefix_len()]
706 (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => {
707 &s_repr[8..self.prefix_len()] == &o_repr[2..other.prefix_len()]
709 (None, None) => true,
710 (a, b) if a == b => {
711 &s_repr[..self.prefix_len()] == &o_repr[..other.prefix_len()]
717 fn normalize_(s: &str) -> (Option<PathPrefix>, String) {
718 // make borrowck happy
719 let (prefix, val) = {
720 let prefix = parse_prefix(s);
721 let path = Path::normalize__(s, prefix);
725 None => s.to_string(),
730 fn normalize__(s: &str, prefix: Option<PathPrefix>) -> Option<String> {
731 if prefix_is_verbatim(prefix) {
732 // don't do any normalization
734 Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
735 // the server component has no trailing '\'
736 let mut s = String::from_str(s);
743 let (is_abs, comps) = normalize_helper(s, prefix);
744 let mut comps = comps;
745 match (comps.is_some(),prefix) {
746 (false, Some(DiskPrefix)) => {
747 if s.as_bytes()[0] >= b'a' && s.as_bytes()[0] <= b'z' {
748 comps = Some(vec![]);
751 (false, Some(VerbatimDiskPrefix)) => {
752 if s.as_bytes()[4] >= b'a' && s.as_bytes()[0] <= b'z' {
753 comps = Some(vec![]);
761 if prefix.is_some() && comps.is_empty() {
762 match prefix.unwrap() {
764 let len = prefix_len(prefix) + is_abs as uint;
765 let mut s = String::from_str(&s[..len]);
767 let v = s.as_mut_vec();
768 v[0] = (*v)[0].to_ascii_uppercase();
771 // normalize C:/ to C:\
773 s.as_mut_vec()[2] = SEP_BYTE;
778 VerbatimDiskPrefix => {
779 let len = prefix_len(prefix) + is_abs as uint;
780 let mut s = String::from_str(&s[..len]);
782 let v = s.as_mut_vec();
783 v[4] = (*v)[4].to_ascii_uppercase();
788 let plen = prefix_len(prefix);
790 Some(String::from_str(&s[..plen]))
794 } else if is_abs && comps.is_empty() {
795 Some(repeat(SEP).take(1).collect())
797 let prefix_ = &s[..prefix_len(prefix)];
798 let n = prefix_.len() +
799 if is_abs { comps.len() } else { comps.len() - 1} +
800 comps.iter().map(|v| v.len()).sum();
801 let mut s = String::with_capacity(n);
803 Some(DiskPrefix) => {
804 s.push(prefix_.as_bytes()[0].to_ascii_uppercase() as char);
807 Some(VerbatimDiskPrefix) => {
808 s.push_str(&prefix_[..4]);
809 s.push(prefix_.as_bytes()[4].to_ascii_uppercase() as char);
810 s.push_str(&prefix_[5..]);
812 Some(UNCPrefix(a,b)) => {
814 s.push_str(&prefix_[2..a+2]);
816 s.push_str(&prefix_[3+a..3+a+b]);
818 Some(_) => s.push_str(prefix_),
821 let mut it = comps.into_iter();
825 Some(comp) => s.push_str(comp)
839 fn update_sepidx(&mut self) {
840 let s = if self.has_nonsemantic_trailing_slash() {
841 &self.repr[..self.repr.len()-1]
842 } else { &self.repr[..] };
843 let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) {
848 let idx = s.rfind(sep_test);
849 let prefixlen = self.prefix_len();
850 self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
853 fn prefix_len(&self) -> uint {
854 prefix_len(self.prefix)
857 // Returns a tuple (before, after, end) where before is the index of the separator
858 // and after is the index just after the separator.
859 // end is the length of the string, normally, or the index of the final character if it is
860 // a non-semantic trailing separator in a verbatim string.
861 // If the prefix is considered the separator, before and after are the same.
862 fn sepidx_or_prefix_len(&self) -> Option<(uint,uint,uint)> {
864 None => match self.prefix_len() { 0 => None, x => Some((x,x,self.repr.len())) },
866 if self.has_nonsemantic_trailing_slash() {
867 Some((x,x+1,self.repr.len()-1))
868 } else { Some((x,x+1,self.repr.len())) }
873 fn has_nonsemantic_trailing_slash(&self) -> bool {
874 is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
875 self.repr.as_bytes()[self.repr.len()-1] == SEP_BYTE
878 fn update_normalized(&mut self, s: &str) {
879 let (prefix, path) = Path::normalize_(s);
881 self.prefix = prefix;
882 self.update_sepidx();
886 /// Returns whether the path is considered "volume-relative", which means a path
887 /// that looks like "\foo". Paths of this form are relative to the current volume,
888 /// but absolute within that volume.
890 pub fn is_vol_relative(path: &Path) -> bool {
891 path.prefix.is_none() && is_sep_byte(&path.repr.as_bytes()[0])
894 /// Returns whether the path is considered "cwd-relative", which means a path
895 /// with a volume prefix that is not absolute. This look like "C:foo.txt". Paths
896 /// of this form are relative to the cwd on the given volume.
898 pub fn is_cwd_relative(path: &Path) -> bool {
899 path.prefix == Some(DiskPrefix) && !path.is_absolute()
902 /// Returns the PathPrefix for this Path
904 pub fn prefix(path: &Path) -> Option<PathPrefix> {
908 /// Returns whether the Path's prefix is a verbatim prefix, i.e. `\\?\`
910 pub fn is_verbatim(path: &Path) -> bool {
911 prefix_is_verbatim(path.prefix)
914 /// Returns the non-verbatim equivalent of the input path, if possible.
915 /// If the input path is a device namespace path, None is returned.
916 /// If the input path is not verbatim, it is returned as-is.
917 /// If the input path is verbatim, but the same path can be expressed as
918 /// non-verbatim, the non-verbatim version is returned.
919 /// Otherwise, None is returned.
920 pub fn make_non_verbatim(path: &Path) -> Option<Path> {
921 let repr = &path.repr[..];
922 let new_path = match path.prefix {
923 Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None,
924 Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()),
925 Some(VerbatimDiskPrefix) => {
927 Path::new(&repr[4..])
929 Some(VerbatimUNCPrefix(_,_)) => {
930 // \\?\UNC\server\share
931 Path::new(format!(r"\{}", &repr[7..]))
934 if new_path.prefix.is_none() {
935 // \\?\UNC\server is a VerbatimUNCPrefix
936 // but \\server is nothing
939 // now ensure normalization didn't change anything
940 if &repr[path.prefix_len()..] == &new_path.repr[new_path.prefix_len()..] {
947 /// The standard path separator character
948 pub const SEP: char = '\\';
949 /// The standard path separator byte
950 pub const SEP_BYTE: u8 = SEP as u8;
952 /// The alternative path separator character
953 pub const SEP2: char = '/';
954 /// The alternative path separator character
955 pub const SEP2_BYTE: u8 = SEP2 as u8;
957 /// Returns whether the given char is a path separator.
958 /// Allows both the primary separator '\' and the alternative separator '/'.
960 pub fn is_sep(c: char) -> bool {
961 c == SEP || c == SEP2
964 /// Returns whether the given char is a path separator.
965 /// Only allows the primary separator '\'; use is_sep to allow '/'.
967 pub fn is_sep_verbatim(c: char) -> bool {
971 /// Returns whether the given byte is a path separator.
972 /// Allows both the primary separator '\' and the alternative separator '/'.
974 pub fn is_sep_byte(u: &u8) -> bool {
975 *u == SEP_BYTE || *u == SEP2_BYTE
978 /// Returns whether the given byte is a path separator.
979 /// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
981 pub fn is_sep_byte_verbatim(u: &u8) -> bool {
985 /// Prefix types for Path
986 #[derive(Copy, PartialEq, Clone, Debug)]
987 pub enum PathPrefix {
988 /// Prefix `\\?\`, uint is the length of the following component
989 VerbatimPrefix(uint),
990 /// Prefix `\\?\UNC\`, uints are the lengths of the UNC components
991 VerbatimUNCPrefix(uint, uint),
992 /// Prefix `\\?\C:\` (for any alphabetic character)
994 /// Prefix `\\.\`, uint is the length of the following component
995 DeviceNSPrefix(uint),
996 /// UNC prefix `\\server\share`, uints are the lengths of the server/share
997 UNCPrefix(uint, uint),
998 /// Prefix `C:` for any alphabetic character
1002 fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
1003 if path.starts_with("\\\\") {
1006 if path.starts_with("?\\") {
1009 if path.starts_with("UNC\\") {
1010 // \\?\UNC\server\share
1012 let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) {
1014 None => (path.len(), 0)
1016 return Some(VerbatimUNCPrefix(idx_a, idx_b));
1019 let idx = path.find('\\');
1020 if idx == Some(2) && path.as_bytes()[1] == b':' {
1021 let c = path.as_bytes()[0];
1022 if c.is_ascii() && (c as char).is_alphabetic() {
1024 return Some(VerbatimDiskPrefix);
1027 let idx = idx.unwrap_or(path.len());
1028 return Some(VerbatimPrefix(idx));
1030 } else if path.starts_with(".\\") {
1033 let idx = path.find('\\').unwrap_or(path.len());
1034 return Some(DeviceNSPrefix(idx));
1036 match parse_two_comps(path, is_sep) {
1037 Some((idx_a, idx_b)) if idx_a > 0 && idx_b > 0 => {
1039 return Some(UNCPrefix(idx_a, idx_b));
1043 } else if path.len() > 1 && path.as_bytes()[1] == b':' {
1045 let c = path.as_bytes()[0];
1046 if c.is_ascii() && (c as char).is_alphabetic() {
1047 return Some(DiskPrefix);
1052 fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> {
1053 let idx_a = match path.find(f) {
1054 None => return None,
1057 path = &path[idx_a+1..];
1058 let idx_b = path.find(f).unwrap_or(path.len());
1059 Some((idx_a, idx_b))
1063 // None result means the string didn't need normalizing
1064 fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) {
1065 let f: fn(char) -> bool = if !prefix_is_verbatim(prefix) {
1070 let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
1071 let s_ = &s[prefix_len(prefix)..];
1072 let s_ = if is_abs { &s_[1..] } else { s_ };
1074 if is_abs && s_.is_empty() {
1075 return (is_abs, match prefix {
1076 Some(DiskPrefix) | None => (if is_sep_verbatim(s.char_at(prefix_len(prefix))) { None }
1077 else { Some(vec![]) }),
1078 Some(_) => Some(vec![]), // need to trim the trailing separator
1081 let mut comps: Vec<&'a str> = vec![];
1083 let mut changed = false;
1084 for comp in s_.split(f) {
1085 if comp.is_empty() { changed = true }
1086 else if comp == "." { changed = true }
1087 else if comp == ".." {
1088 let has_abs_prefix = match prefix {
1089 Some(DiskPrefix) => false,
1093 if (is_abs || has_abs_prefix) && comps.is_empty() { changed = true }
1094 else if comps.len() == n_up { comps.push(".."); n_up += 1 }
1095 else { comps.pop().unwrap(); changed = true }
1096 } else { comps.push(comp) }
1098 if !changed && !prefix_is_verbatim(prefix) {
1099 changed = s.find(is_sep).is_some();
1102 if comps.is_empty() && !is_abs && prefix.is_none() {
1104 return (is_abs, None);
1108 (is_abs, Some(comps))
1114 fn prefix_is_verbatim(p: Option<PathPrefix>) -> bool {
1116 Some(VerbatimPrefix(_)) | Some(VerbatimUNCPrefix(_,_)) | Some(VerbatimDiskPrefix) => true,
1117 Some(DeviceNSPrefix(_)) => true, // not really sure, but I think so
1122 fn prefix_len(p: Option<PathPrefix>) -> uint {
1125 Some(VerbatimPrefix(x)) => 4 + x,
1126 Some(VerbatimUNCPrefix(x,y)) => 8 + x + 1 + y,
1127 Some(VerbatimDiskPrefix) => 6,
1128 Some(UNCPrefix(x,y)) => 2 + x + 1 + y,
1129 Some(DeviceNSPrefix(x)) => 4 + x,
1130 Some(DiskPrefix) => 2
1136 use super::PathPrefix::*;
1137 use super::parse_prefix;
1141 use iter::IteratorExt;
1142 use option::Option::{self, Some, None};
1143 use old_path::GenericPath;
1144 use slice::{AsSlice, SliceExt};
1146 use string::ToString;
1150 (s: $path:expr, $exp:expr) => (
1153 assert_eq!(path.as_str(), Some($exp));
1156 (v: $path:expr, $exp:expr) => (
1159 assert_eq!(path.as_vec(), $exp);
1165 fn test_parse_prefix() {
1167 ($path:expr, $exp:expr) => (
1171 let res = parse_prefix(path);
1172 assert_eq!(res, exp);
1177 t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5)));
1179 t!("\\\\SERVER", None);
1180 t!("\\\\SERVER\\", None);
1181 t!("\\\\SERVER\\\\", None);
1182 t!("\\\\SERVER\\\\foo", None);
1183 t!("\\\\SERVER\\share", Some(UNCPrefix(6,5)));
1184 t!("\\\\SERVER/share/foo", Some(UNCPrefix(6,5)));
1185 t!("\\\\SERVER\\share/foo", Some(UNCPrefix(6,5)));
1186 t!("//SERVER/share/foo", None);
1187 t!("\\\\\\a\\b\\c", None);
1188 t!("\\\\?\\a\\b\\c", Some(VerbatimPrefix(1)));
1189 t!("\\\\?\\a/b/c", Some(VerbatimPrefix(5)));
1190 t!("//?/a/b/c", None);
1191 t!("\\\\.\\a\\b", Some(DeviceNSPrefix(1)));
1192 t!("\\\\.\\a/b", Some(DeviceNSPrefix(3)));
1193 t!("//./a/b", None);
1194 t!("\\\\?\\UNC\\server\\share\\foo", Some(VerbatimUNCPrefix(6,5)));
1195 t!("\\\\?\\UNC\\\\share\\foo", Some(VerbatimUNCPrefix(0,5)));
1196 t!("\\\\?\\UNC\\", Some(VerbatimUNCPrefix(0,0)));
1197 t!("\\\\?\\UNC\\server/share/foo", Some(VerbatimUNCPrefix(16,0)));
1198 t!("\\\\?\\UNC\\server", Some(VerbatimUNCPrefix(6,0)));
1199 t!("\\\\?\\UNC\\server\\", Some(VerbatimUNCPrefix(6,0)));
1200 t!("\\\\?\\UNC/server/share", Some(VerbatimPrefix(16)));
1201 t!("\\\\?\\UNC", Some(VerbatimPrefix(3)));
1202 t!("\\\\?\\C:\\a\\b.txt", Some(VerbatimDiskPrefix));
1203 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1204 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1205 t!("\\\\?\\C:a.txt", Some(VerbatimPrefix(7)));
1206 t!("\\\\?\\C:a\\b.txt", Some(VerbatimPrefix(3)));
1207 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1208 t!("C:\\foo", Some(DiskPrefix));
1209 t!("z:/foo", Some(DiskPrefix));
1210 t!("d:", Some(DiskPrefix));
1212 t!("ü:\\foo", None);
1213 t!("3:\\foo", None);
1214 t!(" :\\foo", None);
1215 t!("::\\foo", None);
1216 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1217 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1218 t!("\\\\?\\ab:\\", Some(VerbatimPrefix(3)));
1219 t!("\\\\?\\C:\\a", Some(VerbatimDiskPrefix));
1220 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1221 t!("\\\\?\\C:\\a/b", Some(VerbatimDiskPrefix));
1226 let empty: &[u8] = &[];
1227 t!(v: Path::new(empty), b".");
1228 t!(v: Path::new(b"\\"), b"\\");
1229 t!(v: Path::new(b"a\\b\\c"), b"a\\b\\c");
1231 t!(s: Path::new(""), ".");
1232 t!(s: Path::new("\\"), "\\");
1233 t!(s: Path::new("hi"), "hi");
1234 t!(s: Path::new("hi\\"), "hi");
1235 t!(s: Path::new("\\lib"), "\\lib");
1236 t!(s: Path::new("\\lib\\"), "\\lib");
1237 t!(s: Path::new("hi\\there"), "hi\\there");
1238 t!(s: Path::new("hi\\there.txt"), "hi\\there.txt");
1239 t!(s: Path::new("/"), "\\");
1240 t!(s: Path::new("hi/"), "hi");
1241 t!(s: Path::new("/lib"), "\\lib");
1242 t!(s: Path::new("/lib/"), "\\lib");
1243 t!(s: Path::new("hi/there"), "hi\\there");
1245 t!(s: Path::new("hi\\there\\"), "hi\\there");
1246 t!(s: Path::new("hi\\..\\there"), "there");
1247 t!(s: Path::new("hi/../there"), "there");
1248 t!(s: Path::new("..\\hi\\there"), "..\\hi\\there");
1249 t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there");
1250 t!(s: Path::new("/../hi/there"), "\\hi\\there");
1251 t!(s: Path::new("foo\\.."), ".");
1252 t!(s: Path::new("\\foo\\.."), "\\");
1253 t!(s: Path::new("\\foo\\..\\.."), "\\");
1254 t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar");
1255 t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there");
1256 t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi");
1257 t!(s: Path::new("foo\\..\\.."), "..");
1258 t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
1259 t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
1261 assert_eq!(Path::new(b"foo\\bar").into_vec(), b"foo\\bar");
1262 assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec(), b"\\bar");
1264 t!(s: Path::new("\\\\a"), "\\a");
1265 t!(s: Path::new("\\\\a\\"), "\\a");
1266 t!(s: Path::new("\\\\a\\b"), "\\\\a\\b");
1267 t!(s: Path::new("\\\\a\\b\\"), "\\\\a\\b");
1268 t!(s: Path::new("\\\\a\\b/"), "\\\\a\\b");
1269 t!(s: Path::new("\\\\\\b"), "\\b");
1270 t!(s: Path::new("\\\\a\\\\b"), "\\a\\b");
1271 t!(s: Path::new("\\\\a\\b\\c"), "\\\\a\\b\\c");
1272 t!(s: Path::new("\\\\server\\share/path"), "\\\\server\\share\\path");
1273 t!(s: Path::new("\\\\server/share/path"), "\\\\server\\share\\path");
1274 t!(s: Path::new("C:a\\b.txt"), "C:a\\b.txt");
1275 t!(s: Path::new("C:a/b.txt"), "C:a\\b.txt");
1276 t!(s: Path::new("z:\\a\\b.txt"), "Z:\\a\\b.txt");
1277 t!(s: Path::new("z:/a/b.txt"), "Z:\\a\\b.txt");
1278 t!(s: Path::new("ab:/a/b.txt"), "ab:\\a\\b.txt");
1279 t!(s: Path::new("C:\\"), "C:\\");
1280 t!(s: Path::new("C:"), "C:");
1281 t!(s: Path::new("q:"), "Q:");
1282 t!(s: Path::new("C:/"), "C:\\");
1283 t!(s: Path::new("C:\\foo\\.."), "C:\\");
1284 t!(s: Path::new("C:foo\\.."), "C:");
1285 t!(s: Path::new("C:\\a\\"), "C:\\a");
1286 t!(s: Path::new("C:\\a/"), "C:\\a");
1287 t!(s: Path::new("C:\\a\\b\\"), "C:\\a\\b");
1288 t!(s: Path::new("C:\\a\\b/"), "C:\\a\\b");
1289 t!(s: Path::new("C:a\\"), "C:a");
1290 t!(s: Path::new("C:a/"), "C:a");
1291 t!(s: Path::new("C:a\\b\\"), "C:a\\b");
1292 t!(s: Path::new("C:a\\b/"), "C:a\\b");
1293 t!(s: Path::new("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt");
1294 t!(s: Path::new("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt");
1295 t!(s: Path::new("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt");
1296 t!(s: Path::new("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt");
1297 t!(s: Path::new("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\");
1298 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1299 t!(s: Path::new("\\\\.\\"), "\\\\.\\");
1300 t!(s: Path::new("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo");
1301 t!(s: Path::new("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\");
1302 t!(s: Path::new("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\");
1303 t!(s: Path::new("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\");
1304 t!(s: Path::new("\\\\?\\UNC"), "\\\\?\\UNC");
1306 // I'm not sure whether \\.\foo/bar should normalize to \\.\foo\bar
1307 // as information is sparse and this isn't really googleable.
1308 // I'm going to err on the side of not normalizing it, as this skips the filesystem
1309 t!(s: Path::new("\\\\.\\foo/bar"), "\\\\.\\foo/bar");
1310 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1314 fn test_opt_paths() {
1315 assert!(Path::new_opt(b"foo\\bar\0") == None);
1316 assert!(Path::new_opt(b"foo\\bar\x80") == None);
1317 t!(v: Path::new_opt(b"foo\\bar").unwrap(), b"foo\\bar");
1318 assert!(Path::new_opt("foo\\bar\0") == None);
1319 t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar");
1323 fn test_null_byte() {
1325 let result = thread::spawn(move|| {
1326 Path::new(b"foo/bar\0");
1328 assert!(result.is_err());
1330 let result = thread::spawn(move|| {
1331 Path::new("test").set_filename(b"f\0o")
1333 assert!(result.is_err());
1335 let result = thread::spawn(move || {
1336 Path::new("test").push(b"f\0o");
1338 assert!(result.is_err());
1343 fn test_not_utf8_panics() {
1344 Path::new(b"hello\x80.txt");
1348 fn test_display_str() {
1349 let path = Path::new("foo");
1350 assert_eq!(path.display().to_string(), "foo");
1351 let path = Path::new(b"\\");
1352 assert_eq!(path.filename_display().to_string(), "");
1354 let path = Path::new("foo");
1355 let mo = path.display().as_cow();
1356 assert_eq!(mo, "foo");
1357 let path = Path::new(b"\\");
1358 let mo = path.filename_display().as_cow();
1365 ($path:expr, $exp:expr, $expf:expr) => (
1367 let path = Path::new($path);
1368 let f = format!("{}", path.display());
1369 assert_eq!(f, $exp);
1370 let f = format!("{}", path.filename_display());
1371 assert_eq!(f, $expf);
1376 t!("foo", "foo", "foo");
1377 t!("foo\\bar", "foo\\bar", "bar");
1382 fn test_components() {
1384 (s: $path:expr, $op:ident, $exp:expr) => (
1387 let path = Path::new(path);
1388 assert_eq!(path.$op(), Some($exp));
1391 (s: $path:expr, $op:ident, $exp:expr, opt) => (
1394 let path = Path::new(path);
1395 let left = path.$op();
1396 assert_eq!(left, $exp);
1399 (v: $path:expr, $op:ident, $exp:expr) => (
1402 let path = Path::new(path);
1403 assert_eq!(path.$op(), $exp);
1408 t!(v: b"a\\b\\c", filename, Some(b"c"));
1409 t!(s: "a\\b\\c", filename_str, "c");
1410 t!(s: "\\a\\b\\c", filename_str, "c");
1411 t!(s: "a", filename_str, "a");
1412 t!(s: "\\a", filename_str, "a");
1413 t!(s: ".", filename_str, None, opt);
1414 t!(s: "\\", filename_str, None, opt);
1415 t!(s: "..", filename_str, None, opt);
1416 t!(s: "..\\..", filename_str, None, opt);
1417 t!(s: "c:\\foo.txt", filename_str, "foo.txt");
1418 t!(s: "C:\\", filename_str, None, opt);
1419 t!(s: "C:", filename_str, None, opt);
1420 t!(s: "\\\\server\\share\\foo.txt", filename_str, "foo.txt");
1421 t!(s: "\\\\server\\share", filename_str, None, opt);
1422 t!(s: "\\\\server", filename_str, "server");
1423 t!(s: "\\\\?\\bar\\foo.txt", filename_str, "foo.txt");
1424 t!(s: "\\\\?\\bar", filename_str, None, opt);
1425 t!(s: "\\\\?\\", filename_str, None, opt);
1426 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", filename_str, "foo.txt");
1427 t!(s: "\\\\?\\UNC\\server", filename_str, None, opt);
1428 t!(s: "\\\\?\\UNC\\", filename_str, None, opt);
1429 t!(s: "\\\\?\\C:\\foo.txt", filename_str, "foo.txt");
1430 t!(s: "\\\\?\\C:\\", filename_str, None, opt);
1431 t!(s: "\\\\?\\C:", filename_str, None, opt);
1432 t!(s: "\\\\?\\foo/bar", filename_str, None, opt);
1433 t!(s: "\\\\?\\C:/foo", filename_str, None, opt);
1434 t!(s: "\\\\.\\foo\\bar", filename_str, "bar");
1435 t!(s: "\\\\.\\foo", filename_str, None, opt);
1436 t!(s: "\\\\.\\foo/bar", filename_str, None, opt);
1437 t!(s: "\\\\.\\foo\\bar/baz", filename_str, "bar/baz");
1438 t!(s: "\\\\.\\", filename_str, None, opt);
1439 t!(s: "\\\\?\\a\\b\\", filename_str, "b");
1441 t!(v: b"a\\b\\c", dirname, b"a\\b");
1442 t!(s: "a\\b\\c", dirname_str, "a\\b");
1443 t!(s: "\\a\\b\\c", dirname_str, "\\a\\b");
1444 t!(s: "a", dirname_str, ".");
1445 t!(s: "\\a", dirname_str, "\\");
1446 t!(s: ".", dirname_str, ".");
1447 t!(s: "\\", dirname_str, "\\");
1448 t!(s: "..", dirname_str, "..");
1449 t!(s: "..\\..", dirname_str, "..\\..");
1450 t!(s: "c:\\foo.txt", dirname_str, "C:\\");
1451 t!(s: "C:\\", dirname_str, "C:\\");
1452 t!(s: "C:", dirname_str, "C:");
1453 t!(s: "C:foo.txt", dirname_str, "C:");
1454 t!(s: "\\\\server\\share\\foo.txt", dirname_str, "\\\\server\\share");
1455 t!(s: "\\\\server\\share", dirname_str, "\\\\server\\share");
1456 t!(s: "\\\\server", dirname_str, "\\");
1457 t!(s: "\\\\?\\bar\\foo.txt", dirname_str, "\\\\?\\bar");
1458 t!(s: "\\\\?\\bar", dirname_str, "\\\\?\\bar");
1459 t!(s: "\\\\?\\", dirname_str, "\\\\?\\");
1460 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", dirname_str, "\\\\?\\UNC\\server\\share");
1461 t!(s: "\\\\?\\UNC\\server", dirname_str, "\\\\?\\UNC\\server\\");
1462 t!(s: "\\\\?\\UNC\\", dirname_str, "\\\\?\\UNC\\\\");
1463 t!(s: "\\\\?\\C:\\foo.txt", dirname_str, "\\\\?\\C:\\");
1464 t!(s: "\\\\?\\C:\\", dirname_str, "\\\\?\\C:\\");
1465 t!(s: "\\\\?\\C:", dirname_str, "\\\\?\\C:");
1466 t!(s: "\\\\?\\C:/foo/bar", dirname_str, "\\\\?\\C:/foo/bar");
1467 t!(s: "\\\\?\\foo/bar", dirname_str, "\\\\?\\foo/bar");
1468 t!(s: "\\\\.\\foo\\bar", dirname_str, "\\\\.\\foo");
1469 t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
1470 t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
1472 t!(v: b"hi\\there.txt", filestem, Some(b"there"));
1473 t!(s: "hi\\there.txt", filestem_str, "there");
1474 t!(s: "hi\\there", filestem_str, "there");
1475 t!(s: "there.txt", filestem_str, "there");
1476 t!(s: "there", filestem_str, "there");
1477 t!(s: ".", filestem_str, None, opt);
1478 t!(s: "\\", filestem_str, None, opt);
1479 t!(s: "foo\\.bar", filestem_str, ".bar");
1480 t!(s: ".bar", filestem_str, ".bar");
1481 t!(s: "..bar", filestem_str, ".");
1482 t!(s: "hi\\there..txt", filestem_str, "there.");
1483 t!(s: "..", filestem_str, None, opt);
1484 t!(s: "..\\..", filestem_str, None, opt);
1485 // filestem is based on filename, so we don't need the full set of prefix tests
1487 t!(v: b"hi\\there.txt", extension, Some(b"txt"));
1488 t!(v: b"hi\\there", extension, None);
1489 t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
1490 t!(s: "hi\\there", extension_str, None, opt);
1491 t!(s: "there.txt", extension_str, Some("txt"), opt);
1492 t!(s: "there", extension_str, None, opt);
1493 t!(s: ".", extension_str, None, opt);
1494 t!(s: "\\", extension_str, None, opt);
1495 t!(s: "foo\\.bar", extension_str, None, opt);
1496 t!(s: ".bar", extension_str, None, opt);
1497 t!(s: "..bar", extension_str, Some("bar"), opt);
1498 t!(s: "hi\\there..txt", extension_str, Some("txt"), opt);
1499 t!(s: "..", extension_str, None, opt);
1500 t!(s: "..\\..", extension_str, None, opt);
1501 // extension is based on filename, so we don't need the full set of prefix tests
1507 (s: $path:expr, $join:expr) => (
1511 let mut p1 = Path::new(path);
1512 let p2 = p1.clone();
1514 assert_eq!(p1, p2.join(join));
1519 t!(s: "a\\b\\c", "..");
1520 t!(s: "\\a\\b\\c", "d");
1521 t!(s: "a\\b", "c\\d");
1522 t!(s: "a\\b", "\\c\\d");
1523 // this is just a sanity-check test. push and join share an implementation,
1524 // so there's no need for the full set of prefix tests
1526 // we do want to check one odd case though to ensure the prefix is re-parsed
1527 let mut p = Path::new("\\\\?\\C:");
1528 assert_eq!(prefix(&p), Some(VerbatimPrefix(2)));
1530 assert_eq!(prefix(&p), Some(VerbatimDiskPrefix));
1531 assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo"));
1533 // and another with verbatim non-normalized paths
1534 let mut p = Path::new("\\\\?\\C:\\a\\");
1536 assert_eq!(p.as_str(), Some("\\\\?\\C:\\a\\foo"));
1540 fn test_push_path() {
1542 (s: $path:expr, $push:expr, $exp:expr) => (
1544 let mut p = Path::new($path);
1545 let push = Path::new($push);
1547 assert_eq!(p.as_str(), Some($exp));
1552 t!(s: "a\\b\\c", "d", "a\\b\\c\\d");
1553 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1554 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1555 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1556 t!(s: "a\\b", ".", "a\\b");
1557 t!(s: "a\\b", "..\\c", "a\\c");
1558 t!(s: "a\\b", "C:a.txt", "C:a.txt");
1559 t!(s: "a\\b", "..\\..\\..\\c", "..\\c");
1560 t!(s: "a\\b", "C:\\a.txt", "C:\\a.txt");
1561 t!(s: "C:\\a", "C:\\b.txt", "C:\\b.txt");
1562 t!(s: "C:\\a\\b\\c", "C:d", "C:\\a\\b\\c\\d");
1563 t!(s: "C:a\\b\\c", "C:d", "C:a\\b\\c\\d");
1564 t!(s: "C:a\\b", "..\\..\\..\\c", "C:..\\c");
1565 t!(s: "C:\\a\\b", "..\\..\\..\\c", "C:\\c");
1566 t!(s: "C:", r"a\b\c", r"C:a\b\c");
1567 t!(s: "C:", r"..\a", r"C:..\a");
1568 t!(s: "\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
1569 t!(s: "\\\\server\\share\\foo", "..\\..\\bar", "\\\\server\\share\\bar");
1570 t!(s: "\\\\server\\share\\foo", "C:baz", "C:baz");
1571 t!(s: "\\\\?\\C:\\a\\b", "C:c\\d", "\\\\?\\C:\\a\\b\\c\\d");
1572 t!(s: "\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
1573 t!(s: "\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
1574 t!(s: "\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
1575 t!(s: "\\\\?\\C:\\a\\b", "..\\..\\..\\c", "\\\\?\\C:\\a\\b\\..\\..\\..\\c");
1576 t!(s: "\\\\?\\foo\\bar", "..\\..\\c", "\\\\?\\foo\\bar\\..\\..\\c");
1577 t!(s: "\\\\?\\", "foo", "\\\\?\\\\foo");
1578 t!(s: "\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
1579 t!(s: "\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
1580 t!(s: "\\\\?\\UNC\\server\\share", "C:a", "C:a");
1581 t!(s: "\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\\\foo");
1582 t!(s: "C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
1583 t!(s: "\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
1584 t!(s: "\\\\.\\foo\\bar", "C:a", "C:a");
1585 // again, not sure about the following, but I'm assuming \\.\ should be verbatim
1586 t!(s: "\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
1588 t!(s: "\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
1592 fn test_push_many() {
1594 (s: $path:expr, $push:expr, $exp:expr) => (
1596 let mut p = Path::new($path);
1597 p.push_many(&$push);
1598 assert_eq!(p.as_str(), Some($exp));
1601 (v: $path:expr, $push:expr, $exp:expr) => (
1603 let mut p = Path::new($path);
1604 p.push_many(&$push);
1605 assert_eq!(p.as_vec(), $exp);
1610 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1611 t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
1612 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1613 t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1614 t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1615 t!(v: b"a\\b\\c", [b"d", b"\\e", b"f"], b"\\e\\f");
1616 t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
1623 (s: $path:expr, $left:expr, $right:expr) => (
1626 let mut p = Path::new(pstr);
1627 let result = p.pop();
1629 assert_eq!(p.as_str(), Some(left));
1630 assert_eq!(result, $right);
1633 (b: $path:expr, $left:expr, $right:expr) => (
1635 let mut p = Path::new($path);
1636 let result = p.pop();
1637 assert_eq!(p.as_vec(), $left);
1638 assert_eq!(result, $right);
1643 t!(s: "a\\b\\c", "a\\b", true);
1644 t!(s: "a", ".", true);
1645 t!(s: ".", ".", false);
1646 t!(s: "\\a", "\\", true);
1647 t!(s: "\\", "\\", false);
1648 t!(b: b"a\\b\\c", b"a\\b", true);
1649 t!(b: b"a", b".", true);
1650 t!(b: b".", b".", false);
1651 t!(b: b"\\a", b"\\", true);
1652 t!(b: b"\\", b"\\", false);
1654 t!(s: "C:\\a\\b", "C:\\a", true);
1655 t!(s: "C:\\a", "C:\\", true);
1656 t!(s: "C:\\", "C:\\", false);
1657 t!(s: "C:a\\b", "C:a", true);
1658 t!(s: "C:a", "C:", true);
1659 t!(s: "C:", "C:", false);
1660 t!(s: "\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1661 t!(s: "\\\\server\\share\\a", "\\\\server\\share", true);
1662 t!(s: "\\\\server\\share", "\\\\server\\share", false);
1663 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1664 t!(s: "\\\\?\\a\\b", "\\\\?\\a", true);
1665 t!(s: "\\\\?\\a", "\\\\?\\a", false);
1666 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1667 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1668 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\", false);
1669 t!(s: "\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1670 t!(s: "\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share", true);
1671 t!(s: "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1672 t!(s: "\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1673 t!(s: "\\\\.\\a\\b", "\\\\.\\a", true);
1674 t!(s: "\\\\.\\a", "\\\\.\\a", false);
1676 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a", true);
1680 fn test_root_path() {
1681 assert_eq!(Path::new("a\\b\\c").root_path(), None);
1682 assert_eq!(Path::new("\\a\\b\\c").root_path(), Some(Path::new("\\")));
1683 assert_eq!(Path::new("C:a").root_path(), Some(Path::new("C:")));
1684 assert_eq!(Path::new("C:\\a").root_path(), Some(Path::new("C:\\")));
1685 assert_eq!(Path::new("\\\\a\\b\\c").root_path(), Some(Path::new("\\\\a\\b")));
1686 assert_eq!(Path::new("\\\\?\\a\\b").root_path(), Some(Path::new("\\\\?\\a")));
1687 assert_eq!(Path::new("\\\\?\\C:\\a").root_path(), Some(Path::new("\\\\?\\C:\\")));
1688 assert_eq!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path(),
1689 Some(Path::new("\\\\?\\UNC\\a\\b")));
1690 assert_eq!(Path::new("\\\\.\\a\\b").root_path(), Some(Path::new("\\\\.\\a")));
1695 t!(s: Path::new("a\\b\\c").join(".."), "a\\b");
1696 t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d");
1697 t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d");
1698 t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
1699 t!(s: Path::new(".").join("a\\b"), "a\\b");
1700 t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
1701 t!(v: Path::new(b"a\\b\\c").join(b".."), b"a\\b");
1702 t!(v: Path::new(b"\\a\\b\\c").join(b"d"), b"\\a\\b\\c\\d");
1703 // full join testing is covered under test_push_path, so no need for
1704 // the full set of prefix tests
1708 fn test_join_path() {
1710 (s: $path:expr, $join:expr, $exp:expr) => (
1712 let path = Path::new($path);
1713 let join = Path::new($join);
1714 let res = path.join(&join);
1715 assert_eq!(res.as_str(), Some($exp));
1720 t!(s: "a\\b\\c", "..", "a\\b");
1721 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1722 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1723 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1724 t!(s: ".", "a\\b", "a\\b");
1725 t!(s: "\\", "a\\b", "\\a\\b");
1726 // join is implemented using push, so there's no need for
1727 // the full set of prefix tests
1731 fn test_join_many() {
1733 (s: $path:expr, $join:expr, $exp:expr) => (
1735 let path = Path::new($path);
1736 let res = path.join_many(&$join);
1737 assert_eq!(res.as_str(), Some($exp));
1740 (v: $path:expr, $join:expr, $exp:expr) => (
1742 let path = Path::new($path);
1743 let res = path.join_many(&$join);
1744 assert_eq!(res.as_vec(), $exp);
1749 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1750 t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
1751 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1752 t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1753 t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1754 t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
1759 fn test_with_helpers() {
1761 (s: $path:expr, $op:ident, $arg:expr, $res:expr) => (
1764 let path = Path::new(pstr);
1766 let res = path.$op(arg);
1767 let exp = Path::new($res);
1768 assert_eq!(res, exp);
1773 t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d");
1774 t!(s: ".", with_filename, "foo", "foo");
1775 t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d");
1776 t!(s: "\\", with_filename, "foo", "\\foo");
1777 t!(s: "\\a", with_filename, "foo", "\\foo");
1778 t!(s: "foo", with_filename, "bar", "bar");
1779 t!(s: "\\", with_filename, "foo\\", "\\foo");
1780 t!(s: "\\a", with_filename, "foo\\", "\\foo");
1781 t!(s: "a\\b\\c", with_filename, "", "a\\b");
1782 t!(s: "a\\b\\c", with_filename, ".", "a\\b");
1783 t!(s: "a\\b\\c", with_filename, "..", "a");
1784 t!(s: "\\a", with_filename, "", "\\");
1785 t!(s: "foo", with_filename, "", ".");
1786 t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e");
1787 t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d");
1788 t!(s: "..", with_filename, "foo", "..\\foo");
1789 t!(s: "..\\..", with_filename, "foo", "..\\..\\foo");
1790 t!(s: "..", with_filename, "", "..");
1791 t!(s: "..\\..", with_filename, "", "..\\..");
1792 t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz");
1793 t!(s: "C:\\foo", with_filename, "bar", "C:\\bar");
1794 t!(s: "C:\\", with_filename, "foo", "C:\\foo");
1795 t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz");
1796 t!(s: "C:foo", with_filename, "bar", "C:bar");
1797 t!(s: "C:", with_filename, "foo", "C:foo");
1798 t!(s: "C:\\foo", with_filename, "", "C:\\");
1799 t!(s: "C:foo", with_filename, "", "C:");
1800 t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\");
1801 t!(s: "C:\\foo", with_filename, "..", "C:\\");
1802 t!(s: "C:\\", with_filename, "..", "C:\\");
1803 t!(s: "C:foo\\bar", with_filename, "..", "C:");
1804 t!(s: "C:foo", with_filename, "..", "C:..");
1805 t!(s: "C:", with_filename, "..", "C:..");
1806 t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar");
1807 t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo");
1808 t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share");
1809 t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share");
1810 t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share");
1811 t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share");
1812 t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz");
1813 t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar");
1814 t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo");
1815 t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\..");
1816 t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz");
1817 t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar");
1818 t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo");
1819 t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\..");
1820 t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz");
1821 t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar");
1822 t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\..");
1824 t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe");
1825 t!(s: "hi\\there.txt", with_extension, "", "hi\\there");
1826 t!(s: "hi\\there.txt", with_extension, ".", "hi\\there..");
1827 t!(s: "hi\\there.txt", with_extension, "..", "hi\\there...");
1828 t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt");
1829 t!(s: "hi\\there", with_extension, ".", "hi\\there..");
1830 t!(s: "hi\\there", with_extension, "..", "hi\\there...");
1831 t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt");
1832 t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt");
1833 t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo");
1834 t!(s: "\\", with_extension, "txt", "\\");
1835 t!(s: "\\", with_extension, ".", "\\");
1836 t!(s: "\\", with_extension, "..", "\\");
1837 t!(s: ".", with_extension, "txt", ".");
1838 // extension setter calls filename setter internally, no need for extended tests
1844 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1848 let mut p1 = Path::new(path);
1850 let p2 = Path::new(path);
1851 assert_eq!(p1, p2.$with(arg));
1854 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1858 let mut p1 = Path::new(path);
1860 let p2 = Path::new(path);
1861 assert_eq!(p1, p2.$with(arg));
1866 t!(v: b"a\\b\\c", set_filename, with_filename, b"d");
1867 t!(v: b"\\", set_filename, with_filename, b"foo");
1868 t!(s: "a\\b\\c", set_filename, with_filename, "d");
1869 t!(s: "\\", set_filename, with_filename, "foo");
1870 t!(s: ".", set_filename, with_filename, "foo");
1871 t!(s: "a\\b", set_filename, with_filename, "");
1872 t!(s: "a", set_filename, with_filename, "");
1874 t!(v: b"hi\\there.txt", set_extension, with_extension, b"exe");
1875 t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
1876 t!(s: "hi\\there.", set_extension, with_extension, "txt");
1877 t!(s: "hi\\there", set_extension, with_extension, "txt");
1878 t!(s: "hi\\there.txt", set_extension, with_extension, "");
1879 t!(s: "hi\\there", set_extension, with_extension, "");
1880 t!(s: ".", set_extension, with_extension, "txt");
1882 // with_ helpers use the setter internally, so the tests for the with_ helpers
1883 // will suffice. No need for the full set of prefix tests.
1889 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1892 assert_eq!(path.filename_str(), $filename);
1893 assert_eq!(path.dirname_str(), $dirname);
1894 assert_eq!(path.filestem_str(), $filestem);
1895 assert_eq!(path.extension_str(), $ext);
1898 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1901 assert_eq!(path.filename(), $filename);
1902 assert_eq!(path.dirname(), $dirname);
1903 assert_eq!(path.filestem(), $filestem);
1904 assert_eq!(path.extension(), $ext);
1909 t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None);
1910 t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
1911 t!(s: Path::new("."), None, Some("."), None, None);
1912 t!(s: Path::new("\\"), None, Some("\\"), None, None);
1913 t!(s: Path::new(".."), None, Some(".."), None, None);
1914 t!(s: Path::new("..\\.."), None, Some("..\\.."), None, None);
1915 t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"),
1916 Some("there"), Some("txt"));
1917 t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None);
1918 t!(s: Path::new("hi\\there."), Some("there."), Some("hi"),
1919 Some("there"), Some(""));
1920 t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None);
1921 t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"),
1922 Some("."), Some("there"));
1924 // these are already tested in test_components, so no need for extended tests
1928 fn test_dir_path() {
1929 t!(s: Path::new("hi\\there").dir_path(), "hi");
1930 t!(s: Path::new("hi").dir_path(), ".");
1931 t!(s: Path::new("\\hi").dir_path(), "\\");
1932 t!(s: Path::new("\\").dir_path(), "\\");
1933 t!(s: Path::new("..").dir_path(), "..");
1934 t!(s: Path::new("..\\..").dir_path(), "..\\..");
1936 // dir_path is just dirname interpreted as a path.
1937 // No need for extended tests
1941 fn test_is_absolute() {
1943 ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => (
1945 let path = Path::new($path);
1946 let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel);
1947 assert_eq!(path.is_absolute(), abs);
1948 assert_eq!(is_vol_relative(&path), vol);
1949 assert_eq!(is_cwd_relative(&path), cwd);
1950 assert_eq!(path.is_relative(), rel);
1954 t!("a\\b\\c", false, false, false, true);
1955 t!("\\a\\b\\c", false, true, false, false);
1956 t!("a", false, false, false, true);
1957 t!("\\a", false, true, false, false);
1958 t!(".", false, false, false, true);
1959 t!("\\", false, true, false, false);
1960 t!("..", false, false, false, true);
1961 t!("..\\..", false, false, false, true);
1962 t!("C:a\\b.txt", false, false, true, false);
1963 t!("C:\\a\\b.txt", true, false, false, false);
1964 t!("\\\\server\\share\\a\\b.txt", true, false, false, false);
1965 t!("\\\\?\\a\\b\\c.txt", true, false, false, false);
1966 t!("\\\\?\\C:\\a\\b.txt", true, false, false, false);
1967 t!("\\\\?\\C:a\\b.txt", true, false, false, false); // NB: not equivalent to C:a\b.txt
1968 t!("\\\\?\\UNC\\server\\share\\a\\b.txt", true, false, false, false);
1969 t!("\\\\.\\a\\b", true, false, false, false);
1973 fn test_is_ancestor_of() {
1975 (s: $path:expr, $dest:expr, $exp:expr) => (
1977 let path = Path::new($path);
1978 let dest = Path::new($dest);
1980 let res = path.is_ancestor_of(&dest);
1981 assert_eq!(res, exp);
1986 t!(s: "a\\b\\c", "a\\b\\c\\d", true);
1987 t!(s: "a\\b\\c", "a\\b\\c", true);
1988 t!(s: "a\\b\\c", "a\\b", false);
1989 t!(s: "\\a\\b\\c", "\\a\\b\\c", true);
1990 t!(s: "\\a\\b", "\\a\\b\\c", true);
1991 t!(s: "\\a\\b\\c\\d", "\\a\\b\\c", false);
1992 t!(s: "\\a\\b", "a\\b\\c", false);
1993 t!(s: "a\\b", "\\a\\b\\c", false);
1994 t!(s: "a\\b\\c", "a\\b\\d", false);
1995 t!(s: "..\\a\\b\\c", "a\\b\\c", false);
1996 t!(s: "a\\b\\c", "..\\a\\b\\c", false);
1997 t!(s: "a\\b\\c", "a\\b\\cd", false);
1998 t!(s: "a\\b\\cd", "a\\b\\c", false);
1999 t!(s: "..\\a\\b", "..\\a\\b\\c", true);
2000 t!(s: ".", "a\\b", true);
2001 t!(s: ".", ".", true);
2002 t!(s: "\\", "\\", true);
2003 t!(s: "\\", "\\a\\b", true);
2004 t!(s: "..", "a\\b", true);
2005 t!(s: "..\\..", "a\\b", true);
2006 t!(s: "foo\\bar", "foobar", false);
2007 t!(s: "foobar", "foo\\bar", false);
2009 t!(s: "foo", "C:foo", false);
2010 t!(s: "C:foo", "foo", false);
2011 t!(s: "C:foo", "C:foo\\bar", true);
2012 t!(s: "C:foo\\bar", "C:foo", false);
2013 t!(s: "C:\\foo", "C:\\foo\\bar", true);
2014 t!(s: "C:", "C:", true);
2015 t!(s: "C:", "C:\\", false);
2016 t!(s: "C:\\", "C:", false);
2017 t!(s: "C:\\", "C:\\", true);
2018 t!(s: "C:\\foo\\bar", "C:\\foo", false);
2019 t!(s: "C:foo\\bar", "C:foo", false);
2020 t!(s: "C:\\foo", "\\foo", false);
2021 t!(s: "\\foo", "C:\\foo", false);
2022 t!(s: "\\\\server\\share\\foo", "\\\\server\\share\\foo\\bar", true);
2023 t!(s: "\\\\server\\share", "\\\\server\\share\\foo", true);
2024 t!(s: "\\\\server\\share\\foo", "\\\\server\\share", false);
2025 t!(s: "C:\\foo", "\\\\server\\share\\foo", false);
2026 t!(s: "\\\\server\\share\\foo", "C:\\foo", false);
2027 t!(s: "\\\\?\\foo\\bar", "\\\\?\\foo\\bar\\baz", true);
2028 t!(s: "\\\\?\\foo\\bar\\baz", "\\\\?\\foo\\bar", false);
2029 t!(s: "\\\\?\\foo\\bar", "\\foo\\bar\\baz", false);
2030 t!(s: "\\foo\\bar", "\\\\?\\foo\\bar\\baz", false);
2031 t!(s: "\\\\?\\C:\\foo\\bar", "\\\\?\\C:\\foo\\bar\\baz", true);
2032 t!(s: "\\\\?\\C:\\foo\\bar\\baz", "\\\\?\\C:\\foo\\bar", false);
2033 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\foo", true);
2034 t!(s: "\\\\?\\C:", "\\\\?\\C:\\", false); // this is a weird one
2035 t!(s: "\\\\?\\C:\\", "\\\\?\\C:", false);
2036 t!(s: "\\\\?\\C:\\a", "\\\\?\\c:\\a\\b", true);
2037 t!(s: "\\\\?\\c:\\a", "\\\\?\\C:\\a\\b", true);
2038 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a\\b", false);
2039 t!(s: "\\\\?\\foo", "\\\\?\\foobar", false);
2040 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", true);
2041 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\", true);
2042 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a\\b", true);
2043 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", false);
2044 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b\\", false);
2045 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c\\d", true);
2046 t!(s: "\\\\?\\UNC\\a\\b\\c\\d", "\\\\?\\UNC\\a\\b\\c", false);
2047 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", true);
2048 t!(s: "\\\\.\\foo\\bar", "\\\\.\\foo\\bar\\baz", true);
2049 t!(s: "\\\\.\\foo\\bar\\baz", "\\\\.\\foo\\bar", false);
2050 t!(s: "\\\\.\\foo", "\\\\.\\foo\\bar", true);
2051 t!(s: "\\\\.\\foo", "\\\\.\\foobar", false);
2053 t!(s: "\\a\\b", "\\\\?\\a\\b", false);
2054 t!(s: "\\\\?\\a\\b", "\\a\\b", false);
2055 t!(s: "\\a\\b", "\\\\?\\C:\\a\\b", false);
2056 t!(s: "\\\\?\\C:\\a\\b", "\\a\\b", false);
2057 t!(s: "Z:\\a\\b", "\\\\?\\z:\\a\\b", true);
2058 t!(s: "C:\\a\\b", "\\\\?\\D:\\a\\b", false);
2059 t!(s: "a\\b", "\\\\?\\a\\b", false);
2060 t!(s: "\\\\?\\a\\b", "a\\b", false);
2061 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b", true);
2062 t!(s: "\\\\?\\C:\\a\\b", "C:\\a\\b", true);
2063 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", false);
2064 t!(s: "C:a\\b", "\\\\?\\C:a\\b", false);
2065 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", false);
2066 t!(s: "\\\\?\\C:a\\b", "C:a\\b", false);
2067 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b\\", true);
2068 t!(s: "\\\\?\\C:\\a\\b\\", "C:\\a\\b", true);
2069 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c", true);
2070 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b\\c", true);
2074 fn test_ends_with_path() {
2076 (s: $path:expr, $child:expr, $exp:expr) => (
2078 let path = Path::new($path);
2079 let child = Path::new($child);
2080 assert_eq!(path.ends_with_path(&child), $exp);
2085 t!(s: "a\\b\\c", "c", true);
2086 t!(s: "a\\b\\c", "d", false);
2087 t!(s: "foo\\bar\\quux", "bar", false);
2088 t!(s: "foo\\bar\\quux", "barquux", false);
2089 t!(s: "a\\b\\c", "b\\c", true);
2090 t!(s: "a\\b\\c", "a\\b\\c", true);
2091 t!(s: "a\\b\\c", "foo\\a\\b\\c", false);
2092 t!(s: "\\a\\b\\c", "a\\b\\c", true);
2093 t!(s: "\\a\\b\\c", "\\a\\b\\c", false); // child must be relative
2094 t!(s: "\\a\\b\\c", "foo\\a\\b\\c", false);
2095 t!(s: "a\\b\\c", "", false);
2096 t!(s: "", "", true);
2097 t!(s: "\\a\\b\\c", "d\\e\\f", false);
2098 t!(s: "a\\b\\c", "a\\b", false);
2099 t!(s: "a\\b\\c", "b", false);
2100 t!(s: "C:\\a\\b", "b", true);
2101 t!(s: "C:\\a\\b", "C:b", false);
2102 t!(s: "C:\\a\\b", "C:a\\b", false);
2106 fn test_path_relative_from() {
2108 (s: $path:expr, $other:expr, $exp:expr) => (
2110 assert_eq!(Path::new($path).path_relative_from(&Path::new($other))
2111 .as_ref().and_then(|x| x.as_str()), $exp);
2116 t!(s: "a\\b\\c", "a\\b", Some("c"));
2117 t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c"));
2118 t!(s: "a\\b\\c", "a\\b\\c\\d", Some(".."));
2119 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2120 t!(s: "a\\b\\c", "a\\b\\c\\d\\e", Some("..\\.."));
2121 t!(s: "a\\b\\c", "a\\d\\e", Some("..\\..\\b\\c"));
2122 t!(s: "a\\b\\c", "d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2123 t!(s: "a\\b\\c", "\\a\\b\\c", None);
2124 t!(s: "\\a\\b\\c", "a\\b\\c", Some("\\a\\b\\c"));
2125 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d", Some(".."));
2126 t!(s: "\\a\\b\\c", "\\a\\b", Some("c"));
2127 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d\\e", Some("..\\.."));
2128 t!(s: "\\a\\b\\c", "\\a\\d\\e", Some("..\\..\\b\\c"));
2129 t!(s: "\\a\\b\\c", "\\d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2130 t!(s: "hi\\there.txt", "hi\\there", Some("..\\there.txt"));
2131 t!(s: ".", "a", Some(".."));
2132 t!(s: ".", "a\\b", Some("..\\.."));
2133 t!(s: ".", ".", Some("."));
2134 t!(s: "a", ".", Some("a"));
2135 t!(s: "a\\b", ".", Some("a\\b"));
2136 t!(s: "..", ".", Some(".."));
2137 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2138 t!(s: "\\a\\b\\c", "\\a\\b\\c", Some("."));
2139 t!(s: "\\", "\\", Some("."));
2140 t!(s: "\\", ".", Some("\\"));
2141 t!(s: "..\\..\\a", "b", Some("..\\..\\..\\a"));
2142 t!(s: "a", "..\\..\\b", None);
2143 t!(s: "..\\..\\a", "..\\..\\b", Some("..\\a"));
2144 t!(s: "..\\..\\a", "..\\..\\a\\b", Some(".."));
2145 t!(s: "..\\..\\a\\b", "..\\..\\a", Some("b"));
2147 t!(s: "C:a\\b\\c", "C:a\\b", Some("c"));
2148 t!(s: "C:a\\b", "C:a\\b\\c", Some(".."));
2149 t!(s: "C:" ,"C:a\\b", Some("..\\.."));
2150 t!(s: "C:a\\b", "C:c\\d", Some("..\\..\\a\\b"));
2151 t!(s: "C:a\\b", "D:c\\d", Some("C:a\\b"));
2152 t!(s: "C:a\\b", "C:..\\c", None);
2153 t!(s: "C:..\\a", "C:b\\c", Some("..\\..\\..\\a"));
2154 t!(s: "C:\\a\\b\\c", "C:\\a\\b", Some("c"));
2155 t!(s: "C:\\a\\b", "C:\\a\\b\\c", Some(".."));
2156 t!(s: "C:\\", "C:\\a\\b", Some("..\\.."));
2157 t!(s: "C:\\a\\b", "C:\\c\\d", Some("..\\..\\a\\b"));
2158 t!(s: "C:\\a\\b", "C:a\\b", Some("C:\\a\\b"));
2159 t!(s: "C:a\\b", "C:\\a\\b", None);
2160 t!(s: "\\a\\b", "C:\\a\\b", None);
2161 t!(s: "\\a\\b", "C:a\\b", None);
2162 t!(s: "a\\b", "C:\\a\\b", None);
2163 t!(s: "a\\b", "C:a\\b", None);
2165 t!(s: "\\\\a\\b\\c", "\\\\a\\b", Some("c"));
2166 t!(s: "\\\\a\\b", "\\\\a\\b\\c", Some(".."));
2167 t!(s: "\\\\a\\b\\c\\e", "\\\\a\\b\\c\\d", Some("..\\e"));
2168 t!(s: "\\\\a\\c\\d", "\\\\a\\b\\d", Some("\\\\a\\c\\d"));
2169 t!(s: "\\\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\b\\c\\d"));
2170 t!(s: "\\\\a\\b\\c", "\\d\\e", Some("\\\\a\\b\\c"));
2171 t!(s: "\\d\\e", "\\\\a\\b\\c", None);
2172 t!(s: "d\\e", "\\\\a\\b\\c", None);
2173 t!(s: "C:\\a\\b\\c", "\\\\a\\b\\c", Some("C:\\a\\b\\c"));
2174 t!(s: "C:\\c", "\\\\a\\b\\c", Some("C:\\c"));
2176 t!(s: "\\\\?\\a\\b", "\\a\\b", Some("\\\\?\\a\\b"));
2177 t!(s: "\\\\?\\a\\b", "a\\b", Some("\\\\?\\a\\b"));
2178 t!(s: "\\\\?\\a\\b", "\\b", Some("\\\\?\\a\\b"));
2179 t!(s: "\\\\?\\a\\b", "b", Some("\\\\?\\a\\b"));
2180 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", Some(".."));
2181 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", Some("c"));
2182 t!(s: "\\\\?\\a\\b", "\\\\?\\c\\d", Some("\\\\?\\a\\b"));
2183 t!(s: "\\\\?\\a", "\\\\?\\b", Some("\\\\?\\a"));
2185 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2186 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2187 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\b", Some("..\\a"));
2188 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a", Some("\\\\?\\C:\\a"));
2189 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\c:\\a", Some("b"));
2190 t!(s: "\\\\?\\C:\\a\\b", "C:\\a", Some("b"));
2191 t!(s: "\\\\?\\C:\\a", "C:\\a\\b", Some(".."));
2192 t!(s: "C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2193 t!(s: "C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2194 t!(s: "\\\\?\\C:\\a", "D:\\a", Some("\\\\?\\C:\\a"));
2195 t!(s: "\\\\?\\c:\\a\\b", "C:\\a", Some("b"));
2196 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", Some("\\\\?\\C:\\a\\b"));
2197 t!(s: "\\\\?\\C:\\a\\.\\b", "C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2198 t!(s: "\\\\?\\C:\\a\\b/c", "C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2199 t!(s: "\\\\?\\C:\\a\\..\\b", "C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2200 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", None);
2201 t!(s: "\\\\?\\C:\\a\\.\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2202 t!(s: "\\\\?\\C:\\a\\b/c", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2203 t!(s: "\\\\?\\C:\\a\\..\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2204 t!(s: "\\\\?\\C:\\a\\b\\", "\\\\?\\C:\\a", Some("b"));
2205 t!(s: "\\\\?\\C:\\.\\b", "\\\\?\\C:\\.", Some("b"));
2206 t!(s: "C:\\b", "\\\\?\\C:\\.", Some("..\\b"));
2207 t!(s: "\\\\?\\a\\.\\b\\c", "\\\\?\\a\\.\\b", Some("c"));
2208 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\.\\d", Some("..\\..\\b\\c"));
2209 t!(s: "\\\\?\\a\\..\\b", "\\\\?\\a\\..", Some("b"));
2210 t!(s: "\\\\?\\a\\b\\..", "\\\\?\\a\\b", Some("\\\\?\\a\\b\\.."));
2211 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\..\\b", Some("..\\..\\b\\c"));
2213 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2214 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", Some(".."));
2215 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2216 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2217 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2218 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\C:\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2219 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2220 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2221 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2222 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b", Some("c"));
2223 t!(s: "\\\\?\\UNC\\a\\b", "\\\\a\\b\\c", Some(".."));
2224 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2225 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2226 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2227 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2228 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2229 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2230 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\a\\b\\c"));
2234 fn test_str_components() {
2236 (s: $path:expr, $exp:expr) => (
2238 let path = Path::new($path);
2239 let comps = path.str_components().map(|x|x.unwrap())
2240 .collect::<Vec<&str>>();
2241 let exp: &[&str] = &$exp;
2242 assert_eq!(comps, exp);
2243 let comps = path.str_components().rev().map(|x|x.unwrap())
2244 .collect::<Vec<&str>>();
2245 let exp = exp.iter().rev().cloned().collect::<Vec<&str>>();
2246 assert_eq!(comps, exp);
2251 t!(s: b"a\\b\\c", ["a", "b", "c"]);
2252 t!(s: "a\\b\\c", ["a", "b", "c"]);
2253 t!(s: "a\\b\\d", ["a", "b", "d"]);
2254 t!(s: "a\\b\\cd", ["a", "b", "cd"]);
2255 t!(s: "\\a\\b\\c", ["a", "b", "c"]);
2257 t!(s: "\\a", ["a"]);
2260 t!(s: "..", [".."]);
2261 t!(s: "..\\..", ["..", ".."]);
2262 t!(s: "..\\..\\foo", ["..", "..", "foo"]);
2263 t!(s: "C:foo\\bar", ["foo", "bar"]);
2264 t!(s: "C:foo", ["foo"]);
2266 t!(s: "C:\\foo\\bar", ["foo", "bar"]);
2267 t!(s: "C:\\foo", ["foo"]);
2269 t!(s: "\\\\server\\share\\foo\\bar", ["foo", "bar"]);
2270 t!(s: "\\\\server\\share\\foo", ["foo"]);
2271 t!(s: "\\\\server\\share", []);
2272 t!(s: "\\\\?\\foo\\bar\\baz", ["bar", "baz"]);
2273 t!(s: "\\\\?\\foo\\bar", ["bar"]);
2274 t!(s: "\\\\?\\foo", []);
2275 t!(s: "\\\\?\\", []);
2276 t!(s: "\\\\?\\a\\b", ["b"]);
2277 t!(s: "\\\\?\\a\\b\\", ["b"]);
2278 t!(s: "\\\\?\\foo\\bar\\\\baz", ["bar", "", "baz"]);
2279 t!(s: "\\\\?\\C:\\foo\\bar", ["foo", "bar"]);
2280 t!(s: "\\\\?\\C:\\foo", ["foo"]);
2281 t!(s: "\\\\?\\C:\\", []);
2282 t!(s: "\\\\?\\C:\\foo\\", ["foo"]);
2283 t!(s: "\\\\?\\UNC\\server\\share\\foo\\bar", ["foo", "bar"]);
2284 t!(s: "\\\\?\\UNC\\server\\share\\foo", ["foo"]);
2285 t!(s: "\\\\?\\UNC\\server\\share", []);
2286 t!(s: "\\\\.\\foo\\bar\\baz", ["bar", "baz"]);
2287 t!(s: "\\\\.\\foo\\bar", ["bar"]);
2288 t!(s: "\\\\.\\foo", []);
2292 fn test_components_iter() {
2294 (s: $path:expr, $exp:expr) => (
2296 let path = Path::new($path);
2297 let comps = path.components().collect::<Vec<&[u8]>>();
2298 let exp: &[&[u8]] = &$exp;
2299 assert_eq!(comps, exp);
2300 let comps = path.components().rev().collect::<Vec<&[u8]>>();
2301 let exp = exp.iter().rev().cloned().collect::<Vec<&[u8]>>();
2302 assert_eq!(comps, exp);
2307 t!(s: "a\\b\\c", [b"a", b"b", b"c"]);
2309 // since this is really a wrapper around str_components, those tests suffice
2313 fn test_make_non_verbatim() {
2315 ($path:expr, $exp:expr) => (
2317 let path = Path::new($path);
2318 let exp: Option<&str> = $exp;
2319 let exp = exp.map(|s| Path::new(s));
2320 assert_eq!(make_non_verbatim(&path), exp);
2325 t!(r"\a\b\c", Some(r"\a\b\c"));
2326 t!(r"a\b\c", Some(r"a\b\c"));
2327 t!(r"C:\a\b\c", Some(r"C:\a\b\c"));
2328 t!(r"C:a\b\c", Some(r"C:a\b\c"));
2329 t!(r"\\server\share\foo", Some(r"\\server\share\foo"));
2330 t!(r"\\.\foo", None);
2331 t!(r"\\?\foo", None);
2332 t!(r"\\?\C:", None);
2333 t!(r"\\?\C:foo", None);
2334 t!(r"\\?\C:\", Some(r"C:\"));
2335 t!(r"\\?\C:\foo", Some(r"C:\foo"));
2336 t!(r"\\?\C:\foo\bar\baz", Some(r"C:\foo\bar\baz"));
2337 t!(r"\\?\C:\foo\.\bar\baz", None);
2338 t!(r"\\?\C:\foo\bar\..\baz", None);
2339 t!(r"\\?\C:\foo\bar\..", None);
2340 t!(r"\\?\UNC\server\share\foo", Some(r"\\server\share\foo"));
2341 t!(r"\\?\UNC\server\share", Some(r"\\server\share"));
2342 t!(r"\\?\UNC\server", None);
2343 t!(r"\\?\UNC\server\", None);