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 ops::{FullRange, Index};
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<&'a str, Option<&'a str>, 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<Option<&'a str>, &'a [u8], 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 impl fmt::Show for Path {
89 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 fmt::Show::fmt(&self.display(), f)
94 impl PartialEq for Path {
96 fn eq(&self, other: &Path) -> bool {
97 self.repr == other.repr
103 impl PartialOrd for Path {
104 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
105 Some(self.cmp(other))
110 fn cmp(&self, other: &Path) -> Ordering {
111 self.repr.cmp(&other.repr)
115 impl FromStr for Path {
116 fn from_str(s: &str) -> Option<Path> {
121 impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for Path {
124 fn hash(&self, state: &mut S) {
125 self.repr.hash(state)
130 fn hash(&self, _: &mut S) {
131 // No-op because the `hash` implementation will be wrong.
135 impl BytesContainer for Path {
137 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
141 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
145 fn is_str(_: Option<&Path>) -> bool { true }
148 impl GenericPathUnsafe for Path {
149 /// See `GenericPathUnsafe::from_vec_unchecked`.
153 /// Panics if not valid UTF-8.
155 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
156 let (prefix, path) = Path::normalize_(path.container_as_str().unwrap());
157 assert!(!path.is_empty());
158 let mut ret = Path{ repr: path, prefix: prefix, sepidx: None };
163 /// See `GenericPathUnsafe::set_filename_unchecked`.
167 /// Panics if not valid UTF-8.
168 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
169 let filename = filename.container_as_str().unwrap();
170 match self.sepidx_or_prefix_len() {
171 None if ".." == self.repr => {
172 let mut s = String::with_capacity(3 + filename.len());
175 s.push_str(filename);
176 self.update_normalized(s.index(&FullRange));
179 self.update_normalized(filename);
181 Some((_,idxa,end)) if self.repr.index(&(idxa..end)) == ".." => {
182 let mut s = String::with_capacity(end + 1 + filename.len());
183 s.push_str(self.repr.index(&(0..end)));
185 s.push_str(filename);
186 self.update_normalized(s.index(&FullRange));
188 Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => {
189 let mut s = String::with_capacity(idxb + filename.len());
190 s.push_str(self.repr.index(&(0..idxb)));
191 s.push_str(filename);
192 self.update_normalized(s.index(&FullRange));
194 Some((idxb,_,_)) => {
195 let mut s = String::with_capacity(idxb + 1 + filename.len());
196 s.push_str(self.repr.index(&(0..idxb)));
198 s.push_str(filename);
199 self.update_normalized(s.index(&FullRange));
204 /// See `GenericPathUnsafe::push_unchecked`.
206 /// Concatenating two Windows Paths is rather complicated.
207 /// For the most part, it will behave as expected, except in the case of
208 /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no
209 /// concept of per-volume cwds like Windows does, we can't behave exactly
210 /// like Windows will. Instead, if the receiver is an absolute path on
211 /// the same volume as the new path, it will be treated as the cwd that
212 /// the new path is relative to. Otherwise, the new path will be treated
213 /// as if it were absolute and will replace the receiver outright.
214 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
215 let path = path.container_as_str().unwrap();
216 fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
217 // assume prefix is Some(DiskPrefix)
218 let rest = path.index(&(prefix_len(prefix)..));
219 !rest.is_empty() && rest.as_bytes()[0].is_ascii() && is_sep(rest.as_bytes()[0] as char)
221 fn shares_volume(me: &Path, path: &str) -> bool {
222 // path is assumed to have a prefix of Some(DiskPrefix)
223 let repr = me.repr.index(&FullRange);
225 Some(DiskPrefix) => {
226 repr.as_bytes()[0] == path.as_bytes()[0].to_ascii_uppercase()
228 Some(VerbatimDiskPrefix) => {
229 repr.as_bytes()[4] == path.as_bytes()[0].to_ascii_uppercase()
234 fn is_sep_(prefix: Option<PathPrefix>, u: u8) -> bool {
235 if prefix_is_verbatim(prefix) { is_sep_verbatim(u as char) }
236 else { is_sep(u as char) }
239 fn replace_path(me: &mut Path, path: &str, prefix: Option<PathPrefix>) {
240 let newpath = Path::normalize__(path, prefix);
241 me.repr = match newpath {
243 None => String::from_str(path)
248 fn append_path(me: &mut Path, path: &str) {
249 // appends a path that has no prefix
250 // if me is verbatim, we need to pre-normalize the new path
251 let path_ = if is_verbatim(me) { Path::normalize__(path, None) }
253 let pathlen = path_.as_ref().map_or(path.len(), |p| p.len());
254 let mut s = String::with_capacity(me.repr.len() + 1 + pathlen);
255 s.push_str(me.repr.index(&FullRange));
256 let plen = me.prefix_len();
257 // if me is "C:" we don't want to add a path separator
259 Some(DiskPrefix) if me.repr.len() == plen => (),
260 _ if !(me.repr.len() > plen && me.repr.as_bytes()[me.repr.len()-1] == SEP_BYTE) => {
266 None => s.push_str(path),
267 Some(p) => s.push_str(p.index(&FullRange)),
269 me.update_normalized(s.index(&FullRange))
272 if !path.is_empty() {
273 let prefix = parse_prefix(path);
275 Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => {
276 // cwd-relative path, self is on the same volume
277 append_path(self, path.index(&(prefix_len(prefix)..)));
280 // absolute path, or cwd-relative and self is not same volume
281 replace_path(self, path, prefix);
283 None if !path.is_empty() && is_sep_(self.prefix, path.as_bytes()[0]) => {
284 // volume-relative path
285 if self.prefix.is_some() {
286 // truncate self down to the prefix, then append
287 let n = self.prefix_len();
288 self.repr.truncate(n);
289 append_path(self, path);
291 // we have no prefix, so nothing to be relative to
292 replace_path(self, path, prefix);
297 append_path(self, path);
304 impl GenericPath for Path {
306 fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
307 match path.container_as_str() {
313 Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
319 /// See `GenericPath::as_str` for info.
320 /// Always returns a `Some` value.
322 fn as_str<'a>(&'a self) -> Option<&'a str> {
323 Some(self.repr.index(&FullRange))
327 fn as_vec<'a>(&'a self) -> &'a [u8] {
332 fn into_vec(self) -> Vec<u8> {
333 self.repr.into_bytes()
337 fn dirname<'a>(&'a self) -> &'a [u8] {
338 self.dirname_str().unwrap().as_bytes()
341 /// See `GenericPath::dirname_str` for info.
342 /// Always returns a `Some` value.
343 fn dirname_str<'a>(&'a self) -> Option<&'a str> {
344 Some(match self.sepidx_or_prefix_len() {
345 None if ".." == self.repr => self.repr.index(&FullRange),
347 Some((_,idxa,end)) if self.repr.index(&(idxa..end)) == ".." => {
348 self.repr.index(&FullRange)
350 Some((idxb,_,end)) if self.repr.index(&(idxb..end)) == "\\" => {
351 self.repr.index(&FullRange)
353 Some((0,idxa,_)) => self.repr.index(&(0..idxa)),
354 Some((idxb,idxa,_)) => {
356 Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => {
357 self.repr.index(&(0..idxa))
359 _ => self.repr.index(&(0..idxb))
366 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
367 self.filename_str().map(|x| x.as_bytes())
370 /// See `GenericPath::filename_str` for info.
371 /// Always returns a `Some` value if `filename` returns a `Some` value.
372 fn filename_str<'a>(&'a self) -> Option<&'a str> {
373 let repr = self.repr.index(&FullRange);
374 match self.sepidx_or_prefix_len() {
375 None if "." == repr || ".." == repr => None,
377 Some((_,idxa,end)) if repr.index(&(idxa..end)) == ".." => None,
378 Some((_,idxa,end)) if idxa == end => None,
379 Some((_,idxa,end)) => Some(repr.index(&(idxa..end)))
383 /// See `GenericPath::filestem_str` for info.
384 /// Always returns a `Some` value if `filestem` returns a `Some` value.
386 fn filestem_str<'a>(&'a self) -> Option<&'a str> {
387 // filestem() returns a byte vector that's guaranteed valid UTF-8
388 self.filestem().map(|t| unsafe { mem::transmute(t) })
392 fn extension_str<'a>(&'a self) -> Option<&'a str> {
393 // extension() returns a byte vector that's guaranteed valid UTF-8
394 self.extension().map(|t| unsafe { mem::transmute(t) })
397 fn dir_path(&self) -> Path {
398 unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) }
402 fn pop(&mut self) -> bool {
403 match self.sepidx_or_prefix_len() {
404 None if "." == self.repr => false,
406 self.repr = String::from_str(".");
410 Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false,
411 Some((idxb,_,end)) if self.repr.index(&(idxb..end)) == "\\" => false,
412 Some((idxb,idxa,_)) => {
413 let trunc = match self.prefix {
414 Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => {
415 let plen = self.prefix_len();
416 if idxb == plen { idxa } else { idxb }
420 self.repr.truncate(trunc);
421 self.update_sepidx();
427 fn root_path(&self) -> Option<Path> {
428 if self.prefix.is_some() {
429 Some(Path::new(match self.prefix {
430 Some(DiskPrefix) if self.is_absolute() => {
431 self.repr.index(&(0..(self.prefix_len()+1)))
433 Some(VerbatimDiskPrefix) => {
434 self.repr.index(&(0..(self.prefix_len()+1)))
436 _ => self.repr.index(&(0..self.prefix_len()))
438 } else if is_vol_relative(self) {
439 Some(Path::new(self.repr.index(&(0..1))))
445 /// See `GenericPath::is_absolute` for info.
447 /// A Windows Path is considered absolute only if it has a non-volume prefix,
448 /// or if it has a volume prefix and the path starts with '\'.
449 /// A path of `\foo` is not considered absolute because it's actually
450 /// relative to the "current volume". A separate method `Path::is_vol_relative`
451 /// is provided to indicate this case. Similarly a path of `C:foo` is not
452 /// considered absolute because it's relative to the cwd on volume C:. A
453 /// separate method `Path::is_cwd_relative` is provided to indicate this case.
455 fn is_absolute(&self) -> bool {
457 Some(DiskPrefix) => {
458 let rest = self.repr.index(&(self.prefix_len()..));
459 rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE
467 fn is_relative(&self) -> bool {
468 self.prefix.is_none() && !is_vol_relative(self)
471 fn is_ancestor_of(&self, other: &Path) -> bool {
472 if !self.equiv_prefix(other) {
474 } else if self.is_absolute() != other.is_absolute() ||
475 is_vol_relative(self) != is_vol_relative(other) {
478 let mut ita = self.str_components().map(|x|x.unwrap());
479 let mut itb = other.str_components().map(|x|x.unwrap());
480 if "." == self.repr {
481 return itb.next() != Some("..");
484 match (ita.next(), itb.next()) {
486 (Some(a), Some(b)) if a == b => { continue },
487 (Some(a), _) if a == ".." => {
488 // if ita contains only .. components, it's an ancestor
489 return ita.all(|x| x == "..");
498 fn path_relative_from(&self, base: &Path) -> Option<Path> {
499 fn comp_requires_verbatim(s: &str) -> bool {
500 s == "." || s == ".." || s.contains_char(SEP2)
503 if !self.equiv_prefix(base) {
505 if self.is_absolute() {
507 } else if self.prefix == Some(DiskPrefix) && base.prefix == Some(DiskPrefix) {
508 // both drives, drive letters must differ or they'd be equiv
513 } else if self.is_absolute() != base.is_absolute() {
514 if self.is_absolute() {
519 } else if is_vol_relative(self) != is_vol_relative(base) {
520 if is_vol_relative(self) {
526 let mut ita = self.str_components().map(|x|x.unwrap());
527 let mut itb = base.str_components().map(|x|x.unwrap());
528 let mut comps = vec![];
530 let a_verb = is_verbatim(self);
531 let b_verb = is_verbatim(base);
533 match (ita.next(), itb.next()) {
534 (None, None) => break,
535 (Some(a), None) if a_verb && comp_requires_verbatim(a) => {
536 return Some(self.clone())
541 comps.extend(ita.by_ref());
545 (None, _) => comps.push(".."),
546 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
547 (Some(a), Some(b)) if !b_verb && b == "." => {
548 if a_verb && comp_requires_verbatim(a) {
549 return Some(self.clone())
550 } else { comps.push(a) }
552 (Some(_), Some(b)) if !b_verb && b == ".." => return None,
553 (Some(a), Some(_)) if a_verb && comp_requires_verbatim(a) => {
554 return Some(self.clone())
556 (Some(a), Some(_)) => {
563 comps.extend(ita.by_ref());
569 Some(Path::new(comps.connect("\\")))
573 fn ends_with_path(&self, child: &Path) -> bool {
574 if !child.is_relative() { return false; }
575 let mut selfit = self.str_components().rev();
576 let mut childit = child.str_components().rev();
578 match (selfit.next(), childit.next()) {
579 (Some(a), Some(b)) => if a != b { return false; },
580 (Some(_), None) => break,
581 (None, Some(_)) => return false,
582 (None, None) => break
590 /// Returns a new `Path` from a `BytesContainer`.
594 /// Panics if the vector contains a `NUL`, or if it contains invalid UTF-8.
599 /// println!("{}", Path::new(r"C:\some\path").display());
602 pub fn new<T: BytesContainer>(path: T) -> Path {
603 GenericPath::new(path)
606 /// Returns a new `Some(Path)` from a `BytesContainer`.
608 /// Returns `None` if the vector contains a `NUL`, or if it contains invalid UTF-8.
613 /// let path = Path::new_opt(r"C:\some\path");
616 /// Some(path) => println!("{}", path.display()),
617 /// None => println!("There was a problem with your path."),
621 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
622 GenericPath::new_opt(path)
625 /// Returns an iterator that yields each component of the path in turn as a Option<&str>.
626 /// Every component is guaranteed to be Some.
627 /// Does not yield the path prefix (including server/share components in UNC paths).
628 /// Does not distinguish between volume-relative and relative paths, e.g.
629 /// \a\b\c and a\b\c.
630 /// Does not distinguish between absolute and cwd-relative paths, e.g.
631 /// C:\foo and C:foo.
632 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
633 let repr = self.repr.index(&FullRange);
634 let s = match self.prefix {
636 let plen = self.prefix_len();
637 if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE {
638 repr.index(&((plen+1)..))
639 } else { repr.index(&(plen..)) }
641 None if repr.as_bytes()[0] == SEP_BYTE => repr.index(&(1..)),
644 let some: fn(&'a str) -> Option<&'a str> = Some; // coerce to fn ptr
645 let ret = s.split_terminator(SEP).map(some);
649 /// Returns an iterator that yields each component of the path in turn as a &[u8].
650 /// See str_components() for details.
651 pub fn components<'a>(&'a self) -> Components<'a> {
652 fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
654 x.unwrap().as_bytes()
656 let convert: for<'b> fn(Option<&'b str>) -> &'b [u8] = convert; // coerce to fn ptr
657 self.str_components().map(convert)
660 fn equiv_prefix(&self, other: &Path) -> bool {
661 let s_repr = self.repr.index(&FullRange);
662 let o_repr = other.repr.index(&FullRange);
663 match (self.prefix, other.prefix) {
664 (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
665 self.is_absolute() &&
666 s_repr.as_bytes()[0].to_ascii_lowercase() ==
667 o_repr.as_bytes()[4].to_ascii_lowercase()
669 (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
670 other.is_absolute() &&
671 s_repr.as_bytes()[4].to_ascii_lowercase() ==
672 o_repr.as_bytes()[0].to_ascii_lowercase()
674 (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
675 s_repr.as_bytes()[4].to_ascii_lowercase() ==
676 o_repr.as_bytes()[4].to_ascii_lowercase()
678 (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
679 s_repr.index(&(2..self.prefix_len())) == o_repr.index(&(8..other.prefix_len()))
681 (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => {
682 s_repr.index(&(8..self.prefix_len())) == o_repr.index(&(2..other.prefix_len()))
684 (None, None) => true,
685 (a, b) if a == b => {
686 s_repr.index(&(0..self.prefix_len())) == o_repr.index(&(0..other.prefix_len()))
692 fn normalize_(s: &str) -> (Option<PathPrefix>, String) {
693 // make borrowck happy
694 let (prefix, val) = {
695 let prefix = parse_prefix(s);
696 let path = Path::normalize__(s, prefix);
700 None => s.to_string(),
705 fn normalize__(s: &str, prefix: Option<PathPrefix>) -> Option<String> {
706 if prefix_is_verbatim(prefix) {
707 // don't do any normalization
709 Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
710 // the server component has no trailing '\'
711 let mut s = String::from_str(s);
718 let (is_abs, comps) = normalize_helper(s, prefix);
719 let mut comps = comps;
720 match (comps.is_some(),prefix) {
721 (false, Some(DiskPrefix)) => {
722 if s.as_bytes()[0] >= b'a' && s.as_bytes()[0] <= b'z' {
723 comps = Some(vec![]);
726 (false, Some(VerbatimDiskPrefix)) => {
727 if s.as_bytes()[4] >= b'a' && s.as_bytes()[0] <= b'z' {
728 comps = Some(vec![]);
736 if prefix.is_some() && comps.is_empty() {
737 match prefix.unwrap() {
739 let len = prefix_len(prefix) + is_abs as uint;
740 let mut s = String::from_str(s.index(&(0..len)));
742 let v = s.as_mut_vec();
743 v[0] = (*v)[0].to_ascii_uppercase();
746 // normalize C:/ to C:\
748 s.as_mut_vec()[2] = SEP_BYTE;
753 VerbatimDiskPrefix => {
754 let len = prefix_len(prefix) + is_abs as uint;
755 let mut s = String::from_str(s.index(&(0..len)));
757 let v = s.as_mut_vec();
758 v[4] = (*v)[4].to_ascii_uppercase();
763 let plen = prefix_len(prefix);
765 Some(String::from_str(s.index(&(0..plen))))
769 } else if is_abs && comps.is_empty() {
770 Some(repeat(SEP).take(1).collect())
772 let prefix_ = s.index(&(0..prefix_len(prefix)));
773 let n = prefix_.len() +
774 if is_abs { comps.len() } else { comps.len() - 1} +
775 comps.iter().map(|v| v.len()).sum();
776 let mut s = String::with_capacity(n);
778 Some(DiskPrefix) => {
779 s.push(prefix_.as_bytes()[0].to_ascii_uppercase() as char);
782 Some(VerbatimDiskPrefix) => {
783 s.push_str(prefix_.index(&(0..4)));
784 s.push(prefix_.as_bytes()[4].to_ascii_uppercase() as char);
785 s.push_str(prefix_.index(&(5..)));
787 Some(UNCPrefix(a,b)) => {
789 s.push_str(prefix_.index(&(2..(a+2))));
791 s.push_str(prefix_.index(&((3+a)..(3+a+b))));
793 Some(_) => s.push_str(prefix_),
796 let mut it = comps.into_iter();
800 Some(comp) => s.push_str(comp)
814 fn update_sepidx(&mut self) {
815 let s = if self.has_nonsemantic_trailing_slash() {
816 self.repr.index(&(0..(self.repr.len()-1)))
817 } else { self.repr.index(&FullRange) };
818 let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) {
823 let idx = s.rfind(sep_test);
824 let prefixlen = self.prefix_len();
825 self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
828 fn prefix_len(&self) -> uint {
829 prefix_len(self.prefix)
832 // Returns a tuple (before, after, end) where before is the index of the separator
833 // and after is the index just after the separator.
834 // end is the length of the string, normally, or the index of the final character if it is
835 // a non-semantic trailing separator in a verbatim string.
836 // If the prefix is considered the separator, before and after are the same.
837 fn sepidx_or_prefix_len(&self) -> Option<(uint,uint,uint)> {
839 None => match self.prefix_len() { 0 => None, x => Some((x,x,self.repr.len())) },
841 if self.has_nonsemantic_trailing_slash() {
842 Some((x,x+1,self.repr.len()-1))
843 } else { Some((x,x+1,self.repr.len())) }
848 fn has_nonsemantic_trailing_slash(&self) -> bool {
849 is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
850 self.repr.as_bytes()[self.repr.len()-1] == SEP_BYTE
853 fn update_normalized(&mut self, s: &str) {
854 let (prefix, path) = Path::normalize_(s);
856 self.prefix = prefix;
857 self.update_sepidx();
861 /// Returns whether the path is considered "volume-relative", which means a path
862 /// that looks like "\foo". Paths of this form are relative to the current volume,
863 /// but absolute within that volume.
865 pub fn is_vol_relative(path: &Path) -> bool {
866 path.prefix.is_none() && is_sep_byte(&path.repr.as_bytes()[0])
869 /// Returns whether the path is considered "cwd-relative", which means a path
870 /// with a volume prefix that is not absolute. This look like "C:foo.txt". Paths
871 /// of this form are relative to the cwd on the given volume.
873 pub fn is_cwd_relative(path: &Path) -> bool {
874 path.prefix == Some(DiskPrefix) && !path.is_absolute()
877 /// Returns the PathPrefix for this Path
879 pub fn prefix(path: &Path) -> Option<PathPrefix> {
883 /// Returns whether the Path's prefix is a verbatim prefix, i.e. `\\?\`
885 pub fn is_verbatim(path: &Path) -> bool {
886 prefix_is_verbatim(path.prefix)
889 /// Returns the non-verbatim equivalent of the input path, if possible.
890 /// If the input path is a device namespace path, None is returned.
891 /// If the input path is not verbatim, it is returned as-is.
892 /// If the input path is verbatim, but the same path can be expressed as
893 /// non-verbatim, the non-verbatim version is returned.
894 /// Otherwise, None is returned.
895 pub fn make_non_verbatim(path: &Path) -> Option<Path> {
896 let repr = path.repr.index(&FullRange);
897 let new_path = match path.prefix {
898 Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None,
899 Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()),
900 Some(VerbatimDiskPrefix) => {
902 Path::new(repr.index(&(4..)))
904 Some(VerbatimUNCPrefix(_,_)) => {
905 // \\?\UNC\server\share
906 Path::new(format!(r"\{}", repr.index(&(7..))))
909 if new_path.prefix.is_none() {
910 // \\?\UNC\server is a VerbatimUNCPrefix
911 // but \\server is nothing
914 // now ensure normalization didn't change anything
915 if repr.index(&(path.prefix_len()..)) ==
916 new_path.repr.index(&(new_path.prefix_len()..)) {
923 /// The standard path separator character
924 pub const SEP: char = '\\';
925 /// The standard path separator byte
926 pub const SEP_BYTE: u8 = SEP as u8;
928 /// The alternative path separator character
929 pub const SEP2: char = '/';
930 /// The alternative path separator character
931 pub const SEP2_BYTE: u8 = SEP2 as u8;
933 /// Returns whether the given char is a path separator.
934 /// Allows both the primary separator '\' and the alternative separator '/'.
936 pub fn is_sep(c: char) -> bool {
937 c == SEP || c == SEP2
940 /// Returns whether the given char is a path separator.
941 /// Only allows the primary separator '\'; use is_sep to allow '/'.
943 pub fn is_sep_verbatim(c: char) -> bool {
947 /// Returns whether the given byte is a path separator.
948 /// Allows both the primary separator '\' and the alternative separator '/'.
950 pub fn is_sep_byte(u: &u8) -> bool {
951 *u == SEP_BYTE || *u == SEP2_BYTE
954 /// Returns whether the given byte is a path separator.
955 /// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
957 pub fn is_sep_byte_verbatim(u: &u8) -> bool {
961 /// Prefix types for Path
962 #[derive(Copy, PartialEq, Clone, Show)]
963 pub enum PathPrefix {
964 /// Prefix `\\?\`, uint is the length of the following component
965 VerbatimPrefix(uint),
966 /// Prefix `\\?\UNC\`, uints are the lengths of the UNC components
967 VerbatimUNCPrefix(uint, uint),
968 /// Prefix `\\?\C:\` (for any alphabetic character)
970 /// Prefix `\\.\`, uint is the length of the following component
971 DeviceNSPrefix(uint),
972 /// UNC prefix `\\server\share`, uints are the lengths of the server/share
973 UNCPrefix(uint, uint),
974 /// Prefix `C:` for any alphabetic character
978 fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
979 if path.starts_with("\\\\") {
981 path = path.index(&(2..));
982 if path.starts_with("?\\") {
984 path = path.index(&(2..));
985 if path.starts_with("UNC\\") {
986 // \\?\UNC\server\share
987 path = path.index(&(4..));
988 let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) {
990 None => (path.len(), 0)
992 return Some(VerbatimUNCPrefix(idx_a, idx_b));
995 let idx = path.find('\\');
996 if idx == Some(2) && path.as_bytes()[1] == b':' {
997 let c = path.as_bytes()[0];
998 if c.is_ascii() && (c as char).is_alphabetic() {
1000 return Some(VerbatimDiskPrefix);
1003 let idx = idx.unwrap_or(path.len());
1004 return Some(VerbatimPrefix(idx));
1006 } else if path.starts_with(".\\") {
1008 path = path.index(&(2..));
1009 let idx = path.find('\\').unwrap_or(path.len());
1010 return Some(DeviceNSPrefix(idx));
1012 match parse_two_comps(path, is_sep) {
1013 Some((idx_a, idx_b)) if idx_a > 0 && idx_b > 0 => {
1015 return Some(UNCPrefix(idx_a, idx_b));
1019 } else if path.len() > 1 && path.as_bytes()[1] == b':' {
1021 let c = path.as_bytes()[0];
1022 if c.is_ascii() && (c as char).is_alphabetic() {
1023 return Some(DiskPrefix);
1028 fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> {
1029 let idx_a = match path.find(f) {
1030 None => return None,
1033 path = path.index(&((idx_a+1)..));
1034 let idx_b = path.find(f).unwrap_or(path.len());
1035 Some((idx_a, idx_b))
1039 // None result means the string didn't need normalizing
1040 fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) {
1041 let f: fn(char) -> bool = if !prefix_is_verbatim(prefix) {
1046 let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
1047 let s_ = s.index(&(prefix_len(prefix)..));
1048 let s_ = if is_abs { s_.index(&(1..)) } else { s_ };
1050 if is_abs && s_.is_empty() {
1051 return (is_abs, match prefix {
1052 Some(DiskPrefix) | None => (if is_sep_verbatim(s.char_at(prefix_len(prefix))) { None }
1053 else { Some(vec![]) }),
1054 Some(_) => Some(vec![]), // need to trim the trailing separator
1057 let mut comps: Vec<&'a str> = vec![];
1059 let mut changed = false;
1060 for comp in s_.split(f) {
1061 if comp.is_empty() { changed = true }
1062 else if comp == "." { changed = true }
1063 else if comp == ".." {
1064 let has_abs_prefix = match prefix {
1065 Some(DiskPrefix) => false,
1069 if (is_abs || has_abs_prefix) && comps.is_empty() { changed = true }
1070 else if comps.len() == n_up { comps.push(".."); n_up += 1 }
1071 else { comps.pop().unwrap(); changed = true }
1072 } else { comps.push(comp) }
1074 if !changed && !prefix_is_verbatim(prefix) {
1075 changed = s.find(is_sep).is_some();
1078 if comps.is_empty() && !is_abs && prefix.is_none() {
1080 return (is_abs, None);
1084 (is_abs, Some(comps))
1090 fn prefix_is_verbatim(p: Option<PathPrefix>) -> bool {
1092 Some(VerbatimPrefix(_)) | Some(VerbatimUNCPrefix(_,_)) | Some(VerbatimDiskPrefix) => true,
1093 Some(DeviceNSPrefix(_)) => true, // not really sure, but I think so
1098 fn prefix_len(p: Option<PathPrefix>) -> uint {
1101 Some(VerbatimPrefix(x)) => 4 + x,
1102 Some(VerbatimUNCPrefix(x,y)) => 8 + x + 1 + y,
1103 Some(VerbatimDiskPrefix) => 6,
1104 Some(UNCPrefix(x,y)) => 2 + x + 1 + y,
1105 Some(DeviceNSPrefix(x)) => 4 + x,
1106 Some(DiskPrefix) => 2
1112 use super::PathPrefix::*;
1113 use super::parse_prefix;
1117 use iter::IteratorExt;
1118 use option::Option::{self, Some, None};
1119 use path::GenericPath;
1120 use slice::{AsSlice, SliceExt};
1122 use string::ToString;
1126 (s: $path:expr, $exp:expr) => (
1129 assert_eq!(path.as_str(), Some($exp));
1132 (v: $path:expr, $exp:expr) => (
1135 assert_eq!(path.as_vec(), $exp);
1141 fn test_parse_prefix() {
1143 ($path:expr, $exp:expr) => (
1147 let res = parse_prefix(path);
1148 assert_eq!(res, exp);
1153 t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5)));
1155 t!("\\\\SERVER", None);
1156 t!("\\\\SERVER\\", None);
1157 t!("\\\\SERVER\\\\", None);
1158 t!("\\\\SERVER\\\\foo", None);
1159 t!("\\\\SERVER\\share", Some(UNCPrefix(6,5)));
1160 t!("\\\\SERVER/share/foo", Some(UNCPrefix(6,5)));
1161 t!("\\\\SERVER\\share/foo", Some(UNCPrefix(6,5)));
1162 t!("//SERVER/share/foo", None);
1163 t!("\\\\\\a\\b\\c", None);
1164 t!("\\\\?\\a\\b\\c", Some(VerbatimPrefix(1)));
1165 t!("\\\\?\\a/b/c", Some(VerbatimPrefix(5)));
1166 t!("//?/a/b/c", None);
1167 t!("\\\\.\\a\\b", Some(DeviceNSPrefix(1)));
1168 t!("\\\\.\\a/b", Some(DeviceNSPrefix(3)));
1169 t!("//./a/b", None);
1170 t!("\\\\?\\UNC\\server\\share\\foo", Some(VerbatimUNCPrefix(6,5)));
1171 t!("\\\\?\\UNC\\\\share\\foo", Some(VerbatimUNCPrefix(0,5)));
1172 t!("\\\\?\\UNC\\", Some(VerbatimUNCPrefix(0,0)));
1173 t!("\\\\?\\UNC\\server/share/foo", Some(VerbatimUNCPrefix(16,0)));
1174 t!("\\\\?\\UNC\\server", Some(VerbatimUNCPrefix(6,0)));
1175 t!("\\\\?\\UNC\\server\\", Some(VerbatimUNCPrefix(6,0)));
1176 t!("\\\\?\\UNC/server/share", Some(VerbatimPrefix(16)));
1177 t!("\\\\?\\UNC", Some(VerbatimPrefix(3)));
1178 t!("\\\\?\\C:\\a\\b.txt", Some(VerbatimDiskPrefix));
1179 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1180 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1181 t!("\\\\?\\C:a.txt", Some(VerbatimPrefix(7)));
1182 t!("\\\\?\\C:a\\b.txt", Some(VerbatimPrefix(3)));
1183 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1184 t!("C:\\foo", Some(DiskPrefix));
1185 t!("z:/foo", Some(DiskPrefix));
1186 t!("d:", Some(DiskPrefix));
1188 t!("ü:\\foo", None);
1189 t!("3:\\foo", None);
1190 t!(" :\\foo", None);
1191 t!("::\\foo", None);
1192 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1193 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1194 t!("\\\\?\\ab:\\", Some(VerbatimPrefix(3)));
1195 t!("\\\\?\\C:\\a", Some(VerbatimDiskPrefix));
1196 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1197 t!("\\\\?\\C:\\a/b", Some(VerbatimDiskPrefix));
1202 let empty: &[u8] = &[];
1203 t!(v: Path::new(empty), b".");
1204 t!(v: Path::new(b"\\"), b"\\");
1205 t!(v: Path::new(b"a\\b\\c"), b"a\\b\\c");
1207 t!(s: Path::new(""), ".");
1208 t!(s: Path::new("\\"), "\\");
1209 t!(s: Path::new("hi"), "hi");
1210 t!(s: Path::new("hi\\"), "hi");
1211 t!(s: Path::new("\\lib"), "\\lib");
1212 t!(s: Path::new("\\lib\\"), "\\lib");
1213 t!(s: Path::new("hi\\there"), "hi\\there");
1214 t!(s: Path::new("hi\\there.txt"), "hi\\there.txt");
1215 t!(s: Path::new("/"), "\\");
1216 t!(s: Path::new("hi/"), "hi");
1217 t!(s: Path::new("/lib"), "\\lib");
1218 t!(s: Path::new("/lib/"), "\\lib");
1219 t!(s: Path::new("hi/there"), "hi\\there");
1221 t!(s: Path::new("hi\\there\\"), "hi\\there");
1222 t!(s: Path::new("hi\\..\\there"), "there");
1223 t!(s: Path::new("hi/../there"), "there");
1224 t!(s: Path::new("..\\hi\\there"), "..\\hi\\there");
1225 t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there");
1226 t!(s: Path::new("/../hi/there"), "\\hi\\there");
1227 t!(s: Path::new("foo\\.."), ".");
1228 t!(s: Path::new("\\foo\\.."), "\\");
1229 t!(s: Path::new("\\foo\\..\\.."), "\\");
1230 t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar");
1231 t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there");
1232 t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi");
1233 t!(s: Path::new("foo\\..\\.."), "..");
1234 t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
1235 t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
1237 assert_eq!(Path::new(b"foo\\bar").into_vec(), b"foo\\bar");
1238 assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec(), b"\\bar");
1240 t!(s: Path::new("\\\\a"), "\\a");
1241 t!(s: Path::new("\\\\a\\"), "\\a");
1242 t!(s: Path::new("\\\\a\\b"), "\\\\a\\b");
1243 t!(s: Path::new("\\\\a\\b\\"), "\\\\a\\b");
1244 t!(s: Path::new("\\\\a\\b/"), "\\\\a\\b");
1245 t!(s: Path::new("\\\\\\b"), "\\b");
1246 t!(s: Path::new("\\\\a\\\\b"), "\\a\\b");
1247 t!(s: Path::new("\\\\a\\b\\c"), "\\\\a\\b\\c");
1248 t!(s: Path::new("\\\\server\\share/path"), "\\\\server\\share\\path");
1249 t!(s: Path::new("\\\\server/share/path"), "\\\\server\\share\\path");
1250 t!(s: Path::new("C:a\\b.txt"), "C:a\\b.txt");
1251 t!(s: Path::new("C:a/b.txt"), "C:a\\b.txt");
1252 t!(s: Path::new("z:\\a\\b.txt"), "Z:\\a\\b.txt");
1253 t!(s: Path::new("z:/a/b.txt"), "Z:\\a\\b.txt");
1254 t!(s: Path::new("ab:/a/b.txt"), "ab:\\a\\b.txt");
1255 t!(s: Path::new("C:\\"), "C:\\");
1256 t!(s: Path::new("C:"), "C:");
1257 t!(s: Path::new("q:"), "Q:");
1258 t!(s: Path::new("C:/"), "C:\\");
1259 t!(s: Path::new("C:\\foo\\.."), "C:\\");
1260 t!(s: Path::new("C:foo\\.."), "C:");
1261 t!(s: Path::new("C:\\a\\"), "C:\\a");
1262 t!(s: Path::new("C:\\a/"), "C:\\a");
1263 t!(s: Path::new("C:\\a\\b\\"), "C:\\a\\b");
1264 t!(s: Path::new("C:\\a\\b/"), "C:\\a\\b");
1265 t!(s: Path::new("C:a\\"), "C:a");
1266 t!(s: Path::new("C:a/"), "C:a");
1267 t!(s: Path::new("C:a\\b\\"), "C:a\\b");
1268 t!(s: Path::new("C:a\\b/"), "C:a\\b");
1269 t!(s: Path::new("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt");
1270 t!(s: Path::new("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt");
1271 t!(s: Path::new("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt");
1272 t!(s: Path::new("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt");
1273 t!(s: Path::new("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\");
1274 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1275 t!(s: Path::new("\\\\.\\"), "\\\\.\\");
1276 t!(s: Path::new("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo");
1277 t!(s: Path::new("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\");
1278 t!(s: Path::new("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\");
1279 t!(s: Path::new("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\");
1280 t!(s: Path::new("\\\\?\\UNC"), "\\\\?\\UNC");
1282 // I'm not sure whether \\.\foo/bar should normalize to \\.\foo\bar
1283 // as information is sparse and this isn't really googleable.
1284 // I'm going to err on the side of not normalizing it, as this skips the filesystem
1285 t!(s: Path::new("\\\\.\\foo/bar"), "\\\\.\\foo/bar");
1286 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1290 fn test_opt_paths() {
1291 assert!(Path::new_opt(b"foo\\bar\0") == None);
1292 assert!(Path::new_opt(b"foo\\bar\x80") == None);
1293 t!(v: Path::new_opt(b"foo\\bar").unwrap(), b"foo\\bar");
1294 assert!(Path::new_opt("foo\\bar\0") == None);
1295 t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar");
1299 fn test_null_byte() {
1301 let result = Thread::scoped(move|| {
1302 Path::new(b"foo/bar\0")
1304 assert!(result.is_err());
1306 let result = Thread::scoped(move|| {
1307 Path::new("test").set_filename(b"f\0o")
1309 assert!(result.is_err());
1311 let result = Thread::scoped(move || {
1312 Path::new("test").push(b"f\0o");
1314 assert!(result.is_err());
1319 fn test_not_utf8_panics() {
1320 Path::new(b"hello\x80.txt");
1324 fn test_display_str() {
1325 let path = Path::new("foo");
1326 assert_eq!(path.display().to_string(), "foo");
1327 let path = Path::new(b"\\");
1328 assert_eq!(path.filename_display().to_string(), "");
1330 let path = Path::new("foo");
1331 let mo = path.display().as_cow();
1332 assert_eq!(mo.as_slice(), "foo");
1333 let path = Path::new(b"\\");
1334 let mo = path.filename_display().as_cow();
1335 assert_eq!(mo.as_slice(), "");
1341 ($path:expr, $exp:expr, $expf:expr) => (
1343 let path = Path::new($path);
1344 let f = format!("{}", path.display());
1345 assert_eq!(f, $exp);
1346 let f = format!("{}", path.filename_display());
1347 assert_eq!(f, $expf);
1352 t!("foo", "foo", "foo");
1353 t!("foo\\bar", "foo\\bar", "bar");
1358 fn test_components() {
1360 (s: $path:expr, $op:ident, $exp:expr) => (
1363 let path = Path::new(path);
1364 assert_eq!(path.$op(), Some($exp));
1367 (s: $path:expr, $op:ident, $exp:expr, opt) => (
1370 let path = Path::new(path);
1371 let left = path.$op();
1372 assert_eq!(left, $exp);
1375 (v: $path:expr, $op:ident, $exp:expr) => (
1378 let path = Path::new(path);
1379 assert_eq!(path.$op(), $exp);
1384 t!(v: b"a\\b\\c", filename, Some(b"c"));
1385 t!(s: "a\\b\\c", filename_str, "c");
1386 t!(s: "\\a\\b\\c", filename_str, "c");
1387 t!(s: "a", filename_str, "a");
1388 t!(s: "\\a", filename_str, "a");
1389 t!(s: ".", filename_str, None, opt);
1390 t!(s: "\\", filename_str, None, opt);
1391 t!(s: "..", filename_str, None, opt);
1392 t!(s: "..\\..", filename_str, None, opt);
1393 t!(s: "c:\\foo.txt", filename_str, "foo.txt");
1394 t!(s: "C:\\", filename_str, None, opt);
1395 t!(s: "C:", filename_str, None, opt);
1396 t!(s: "\\\\server\\share\\foo.txt", filename_str, "foo.txt");
1397 t!(s: "\\\\server\\share", filename_str, None, opt);
1398 t!(s: "\\\\server", filename_str, "server");
1399 t!(s: "\\\\?\\bar\\foo.txt", filename_str, "foo.txt");
1400 t!(s: "\\\\?\\bar", filename_str, None, opt);
1401 t!(s: "\\\\?\\", filename_str, None, opt);
1402 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", filename_str, "foo.txt");
1403 t!(s: "\\\\?\\UNC\\server", filename_str, None, opt);
1404 t!(s: "\\\\?\\UNC\\", filename_str, None, opt);
1405 t!(s: "\\\\?\\C:\\foo.txt", filename_str, "foo.txt");
1406 t!(s: "\\\\?\\C:\\", filename_str, None, opt);
1407 t!(s: "\\\\?\\C:", filename_str, None, opt);
1408 t!(s: "\\\\?\\foo/bar", filename_str, None, opt);
1409 t!(s: "\\\\?\\C:/foo", filename_str, None, opt);
1410 t!(s: "\\\\.\\foo\\bar", filename_str, "bar");
1411 t!(s: "\\\\.\\foo", filename_str, None, opt);
1412 t!(s: "\\\\.\\foo/bar", filename_str, None, opt);
1413 t!(s: "\\\\.\\foo\\bar/baz", filename_str, "bar/baz");
1414 t!(s: "\\\\.\\", filename_str, None, opt);
1415 t!(s: "\\\\?\\a\\b\\", filename_str, "b");
1417 t!(v: b"a\\b\\c", dirname, b"a\\b");
1418 t!(s: "a\\b\\c", dirname_str, "a\\b");
1419 t!(s: "\\a\\b\\c", dirname_str, "\\a\\b");
1420 t!(s: "a", dirname_str, ".");
1421 t!(s: "\\a", dirname_str, "\\");
1422 t!(s: ".", dirname_str, ".");
1423 t!(s: "\\", dirname_str, "\\");
1424 t!(s: "..", dirname_str, "..");
1425 t!(s: "..\\..", dirname_str, "..\\..");
1426 t!(s: "c:\\foo.txt", dirname_str, "C:\\");
1427 t!(s: "C:\\", dirname_str, "C:\\");
1428 t!(s: "C:", dirname_str, "C:");
1429 t!(s: "C:foo.txt", dirname_str, "C:");
1430 t!(s: "\\\\server\\share\\foo.txt", dirname_str, "\\\\server\\share");
1431 t!(s: "\\\\server\\share", dirname_str, "\\\\server\\share");
1432 t!(s: "\\\\server", dirname_str, "\\");
1433 t!(s: "\\\\?\\bar\\foo.txt", dirname_str, "\\\\?\\bar");
1434 t!(s: "\\\\?\\bar", dirname_str, "\\\\?\\bar");
1435 t!(s: "\\\\?\\", dirname_str, "\\\\?\\");
1436 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", dirname_str, "\\\\?\\UNC\\server\\share");
1437 t!(s: "\\\\?\\UNC\\server", dirname_str, "\\\\?\\UNC\\server\\");
1438 t!(s: "\\\\?\\UNC\\", dirname_str, "\\\\?\\UNC\\\\");
1439 t!(s: "\\\\?\\C:\\foo.txt", dirname_str, "\\\\?\\C:\\");
1440 t!(s: "\\\\?\\C:\\", dirname_str, "\\\\?\\C:\\");
1441 t!(s: "\\\\?\\C:", dirname_str, "\\\\?\\C:");
1442 t!(s: "\\\\?\\C:/foo/bar", dirname_str, "\\\\?\\C:/foo/bar");
1443 t!(s: "\\\\?\\foo/bar", dirname_str, "\\\\?\\foo/bar");
1444 t!(s: "\\\\.\\foo\\bar", dirname_str, "\\\\.\\foo");
1445 t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
1446 t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
1448 t!(v: b"hi\\there.txt", filestem, Some(b"there"));
1449 t!(s: "hi\\there.txt", filestem_str, "there");
1450 t!(s: "hi\\there", filestem_str, "there");
1451 t!(s: "there.txt", filestem_str, "there");
1452 t!(s: "there", filestem_str, "there");
1453 t!(s: ".", filestem_str, None, opt);
1454 t!(s: "\\", filestem_str, None, opt);
1455 t!(s: "foo\\.bar", filestem_str, ".bar");
1456 t!(s: ".bar", filestem_str, ".bar");
1457 t!(s: "..bar", filestem_str, ".");
1458 t!(s: "hi\\there..txt", filestem_str, "there.");
1459 t!(s: "..", filestem_str, None, opt);
1460 t!(s: "..\\..", filestem_str, None, opt);
1461 // filestem is based on filename, so we don't need the full set of prefix tests
1463 t!(v: b"hi\\there.txt", extension, Some(b"txt"));
1464 t!(v: b"hi\\there", extension, None);
1465 t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
1466 t!(s: "hi\\there", extension_str, None, opt);
1467 t!(s: "there.txt", extension_str, Some("txt"), opt);
1468 t!(s: "there", extension_str, None, opt);
1469 t!(s: ".", extension_str, None, opt);
1470 t!(s: "\\", extension_str, None, opt);
1471 t!(s: "foo\\.bar", extension_str, None, opt);
1472 t!(s: ".bar", extension_str, None, opt);
1473 t!(s: "..bar", extension_str, Some("bar"), opt);
1474 t!(s: "hi\\there..txt", extension_str, Some("txt"), opt);
1475 t!(s: "..", extension_str, None, opt);
1476 t!(s: "..\\..", extension_str, None, opt);
1477 // extension is based on filename, so we don't need the full set of prefix tests
1483 (s: $path:expr, $join:expr) => (
1487 let mut p1 = Path::new(path);
1488 let p2 = p1.clone();
1490 assert_eq!(p1, p2.join(join));
1495 t!(s: "a\\b\\c", "..");
1496 t!(s: "\\a\\b\\c", "d");
1497 t!(s: "a\\b", "c\\d");
1498 t!(s: "a\\b", "\\c\\d");
1499 // this is just a sanity-check test. push and join share an implementation,
1500 // so there's no need for the full set of prefix tests
1502 // we do want to check one odd case though to ensure the prefix is re-parsed
1503 let mut p = Path::new("\\\\?\\C:");
1504 assert_eq!(prefix(&p), Some(VerbatimPrefix(2)));
1506 assert_eq!(prefix(&p), Some(VerbatimDiskPrefix));
1507 assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo"));
1509 // and another with verbatim non-normalized paths
1510 let mut p = Path::new("\\\\?\\C:\\a\\");
1512 assert_eq!(p.as_str(), Some("\\\\?\\C:\\a\\foo"));
1516 fn test_push_path() {
1518 (s: $path:expr, $push:expr, $exp:expr) => (
1520 let mut p = Path::new($path);
1521 let push = Path::new($push);
1523 assert_eq!(p.as_str(), Some($exp));
1528 t!(s: "a\\b\\c", "d", "a\\b\\c\\d");
1529 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1530 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1531 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1532 t!(s: "a\\b", ".", "a\\b");
1533 t!(s: "a\\b", "..\\c", "a\\c");
1534 t!(s: "a\\b", "C:a.txt", "C:a.txt");
1535 t!(s: "a\\b", "..\\..\\..\\c", "..\\c");
1536 t!(s: "a\\b", "C:\\a.txt", "C:\\a.txt");
1537 t!(s: "C:\\a", "C:\\b.txt", "C:\\b.txt");
1538 t!(s: "C:\\a\\b\\c", "C:d", "C:\\a\\b\\c\\d");
1539 t!(s: "C:a\\b\\c", "C:d", "C:a\\b\\c\\d");
1540 t!(s: "C:a\\b", "..\\..\\..\\c", "C:..\\c");
1541 t!(s: "C:\\a\\b", "..\\..\\..\\c", "C:\\c");
1542 t!(s: "C:", r"a\b\c", r"C:a\b\c");
1543 t!(s: "C:", r"..\a", r"C:..\a");
1544 t!(s: "\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
1545 t!(s: "\\\\server\\share\\foo", "..\\..\\bar", "\\\\server\\share\\bar");
1546 t!(s: "\\\\server\\share\\foo", "C:baz", "C:baz");
1547 t!(s: "\\\\?\\C:\\a\\b", "C:c\\d", "\\\\?\\C:\\a\\b\\c\\d");
1548 t!(s: "\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
1549 t!(s: "\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
1550 t!(s: "\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
1551 t!(s: "\\\\?\\C:\\a\\b", "..\\..\\..\\c", "\\\\?\\C:\\a\\b\\..\\..\\..\\c");
1552 t!(s: "\\\\?\\foo\\bar", "..\\..\\c", "\\\\?\\foo\\bar\\..\\..\\c");
1553 t!(s: "\\\\?\\", "foo", "\\\\?\\\\foo");
1554 t!(s: "\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
1555 t!(s: "\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
1556 t!(s: "\\\\?\\UNC\\server\\share", "C:a", "C:a");
1557 t!(s: "\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\\\foo");
1558 t!(s: "C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
1559 t!(s: "\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
1560 t!(s: "\\\\.\\foo\\bar", "C:a", "C:a");
1561 // again, not sure about the following, but I'm assuming \\.\ should be verbatim
1562 t!(s: "\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
1564 t!(s: "\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
1568 fn test_push_many() {
1570 (s: $path:expr, $push:expr, $exp:expr) => (
1572 let mut p = Path::new($path);
1573 p.push_many(&$push);
1574 assert_eq!(p.as_str(), Some($exp));
1577 (v: $path:expr, $push:expr, $exp:expr) => (
1579 let mut p = Path::new($path);
1580 p.push_many(&$push);
1581 assert_eq!(p.as_vec(), $exp);
1586 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1587 t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
1588 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1589 t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1590 t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1591 t!(v: b"a\\b\\c", [b"d", b"\\e", b"f"], b"\\e\\f");
1592 t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
1599 (s: $path:expr, $left:expr, $right:expr) => (
1602 let mut p = Path::new(pstr);
1603 let result = p.pop();
1605 assert_eq!(p.as_str(), Some(left));
1606 assert_eq!(result, $right);
1609 (b: $path:expr, $left:expr, $right:expr) => (
1611 let mut p = Path::new($path);
1612 let result = p.pop();
1613 assert_eq!(p.as_vec(), $left);
1614 assert_eq!(result, $right);
1619 t!(s: "a\\b\\c", "a\\b", true);
1620 t!(s: "a", ".", true);
1621 t!(s: ".", ".", false);
1622 t!(s: "\\a", "\\", true);
1623 t!(s: "\\", "\\", false);
1624 t!(b: b"a\\b\\c", b"a\\b", true);
1625 t!(b: b"a", b".", true);
1626 t!(b: b".", b".", false);
1627 t!(b: b"\\a", b"\\", true);
1628 t!(b: b"\\", b"\\", false);
1630 t!(s: "C:\\a\\b", "C:\\a", true);
1631 t!(s: "C:\\a", "C:\\", true);
1632 t!(s: "C:\\", "C:\\", false);
1633 t!(s: "C:a\\b", "C:a", true);
1634 t!(s: "C:a", "C:", true);
1635 t!(s: "C:", "C:", false);
1636 t!(s: "\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1637 t!(s: "\\\\server\\share\\a", "\\\\server\\share", true);
1638 t!(s: "\\\\server\\share", "\\\\server\\share", false);
1639 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1640 t!(s: "\\\\?\\a\\b", "\\\\?\\a", true);
1641 t!(s: "\\\\?\\a", "\\\\?\\a", false);
1642 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1643 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1644 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\", false);
1645 t!(s: "\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1646 t!(s: "\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share", true);
1647 t!(s: "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1648 t!(s: "\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1649 t!(s: "\\\\.\\a\\b", "\\\\.\\a", true);
1650 t!(s: "\\\\.\\a", "\\\\.\\a", false);
1652 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a", true);
1656 fn test_root_path() {
1657 assert_eq!(Path::new("a\\b\\c").root_path(), None);
1658 assert_eq!(Path::new("\\a\\b\\c").root_path(), Some(Path::new("\\")));
1659 assert_eq!(Path::new("C:a").root_path(), Some(Path::new("C:")));
1660 assert_eq!(Path::new("C:\\a").root_path(), Some(Path::new("C:\\")));
1661 assert_eq!(Path::new("\\\\a\\b\\c").root_path(), Some(Path::new("\\\\a\\b")));
1662 assert_eq!(Path::new("\\\\?\\a\\b").root_path(), Some(Path::new("\\\\?\\a")));
1663 assert_eq!(Path::new("\\\\?\\C:\\a").root_path(), Some(Path::new("\\\\?\\C:\\")));
1664 assert_eq!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path(),
1665 Some(Path::new("\\\\?\\UNC\\a\\b")));
1666 assert_eq!(Path::new("\\\\.\\a\\b").root_path(), Some(Path::new("\\\\.\\a")));
1671 t!(s: Path::new("a\\b\\c").join(".."), "a\\b");
1672 t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d");
1673 t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d");
1674 t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
1675 t!(s: Path::new(".").join("a\\b"), "a\\b");
1676 t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
1677 t!(v: Path::new(b"a\\b\\c").join(b".."), b"a\\b");
1678 t!(v: Path::new(b"\\a\\b\\c").join(b"d"), b"\\a\\b\\c\\d");
1679 // full join testing is covered under test_push_path, so no need for
1680 // the full set of prefix tests
1684 fn test_join_path() {
1686 (s: $path:expr, $join:expr, $exp:expr) => (
1688 let path = Path::new($path);
1689 let join = Path::new($join);
1690 let res = path.join(&join);
1691 assert_eq!(res.as_str(), Some($exp));
1696 t!(s: "a\\b\\c", "..", "a\\b");
1697 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1698 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1699 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1700 t!(s: ".", "a\\b", "a\\b");
1701 t!(s: "\\", "a\\b", "\\a\\b");
1702 // join is implemented using push, so there's no need for
1703 // the full set of prefix tests
1707 fn test_join_many() {
1709 (s: $path:expr, $join:expr, $exp:expr) => (
1711 let path = Path::new($path);
1712 let res = path.join_many(&$join);
1713 assert_eq!(res.as_str(), Some($exp));
1716 (v: $path:expr, $join:expr, $exp:expr) => (
1718 let path = Path::new($path);
1719 let res = path.join_many(&$join);
1720 assert_eq!(res.as_vec(), $exp);
1725 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1726 t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
1727 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1728 t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1729 t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1730 t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
1735 fn test_with_helpers() {
1737 (s: $path:expr, $op:ident, $arg:expr, $res:expr) => (
1740 let path = Path::new(pstr);
1742 let res = path.$op(arg);
1743 let exp = Path::new($res);
1744 assert_eq!(res, exp);
1749 t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d");
1750 t!(s: ".", with_filename, "foo", "foo");
1751 t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d");
1752 t!(s: "\\", with_filename, "foo", "\\foo");
1753 t!(s: "\\a", with_filename, "foo", "\\foo");
1754 t!(s: "foo", with_filename, "bar", "bar");
1755 t!(s: "\\", with_filename, "foo\\", "\\foo");
1756 t!(s: "\\a", with_filename, "foo\\", "\\foo");
1757 t!(s: "a\\b\\c", with_filename, "", "a\\b");
1758 t!(s: "a\\b\\c", with_filename, ".", "a\\b");
1759 t!(s: "a\\b\\c", with_filename, "..", "a");
1760 t!(s: "\\a", with_filename, "", "\\");
1761 t!(s: "foo", with_filename, "", ".");
1762 t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e");
1763 t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d");
1764 t!(s: "..", with_filename, "foo", "..\\foo");
1765 t!(s: "..\\..", with_filename, "foo", "..\\..\\foo");
1766 t!(s: "..", with_filename, "", "..");
1767 t!(s: "..\\..", with_filename, "", "..\\..");
1768 t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz");
1769 t!(s: "C:\\foo", with_filename, "bar", "C:\\bar");
1770 t!(s: "C:\\", with_filename, "foo", "C:\\foo");
1771 t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz");
1772 t!(s: "C:foo", with_filename, "bar", "C:bar");
1773 t!(s: "C:", with_filename, "foo", "C:foo");
1774 t!(s: "C:\\foo", with_filename, "", "C:\\");
1775 t!(s: "C:foo", with_filename, "", "C:");
1776 t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\");
1777 t!(s: "C:\\foo", with_filename, "..", "C:\\");
1778 t!(s: "C:\\", with_filename, "..", "C:\\");
1779 t!(s: "C:foo\\bar", with_filename, "..", "C:");
1780 t!(s: "C:foo", with_filename, "..", "C:..");
1781 t!(s: "C:", with_filename, "..", "C:..");
1782 t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar");
1783 t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo");
1784 t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share");
1785 t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share");
1786 t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share");
1787 t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share");
1788 t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz");
1789 t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar");
1790 t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo");
1791 t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\..");
1792 t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz");
1793 t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar");
1794 t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo");
1795 t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\..");
1796 t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz");
1797 t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar");
1798 t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\..");
1800 t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe");
1801 t!(s: "hi\\there.txt", with_extension, "", "hi\\there");
1802 t!(s: "hi\\there.txt", with_extension, ".", "hi\\there..");
1803 t!(s: "hi\\there.txt", with_extension, "..", "hi\\there...");
1804 t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt");
1805 t!(s: "hi\\there", with_extension, ".", "hi\\there..");
1806 t!(s: "hi\\there", with_extension, "..", "hi\\there...");
1807 t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt");
1808 t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt");
1809 t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo");
1810 t!(s: "\\", with_extension, "txt", "\\");
1811 t!(s: "\\", with_extension, ".", "\\");
1812 t!(s: "\\", with_extension, "..", "\\");
1813 t!(s: ".", with_extension, "txt", ".");
1814 // extension setter calls filename setter internally, no need for extended tests
1820 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1824 let mut p1 = Path::new(path);
1826 let p2 = Path::new(path);
1827 assert_eq!(p1, p2.$with(arg));
1830 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1834 let mut p1 = Path::new(path);
1836 let p2 = Path::new(path);
1837 assert_eq!(p1, p2.$with(arg));
1842 t!(v: b"a\\b\\c", set_filename, with_filename, b"d");
1843 t!(v: b"\\", set_filename, with_filename, b"foo");
1844 t!(s: "a\\b\\c", set_filename, with_filename, "d");
1845 t!(s: "\\", set_filename, with_filename, "foo");
1846 t!(s: ".", set_filename, with_filename, "foo");
1847 t!(s: "a\\b", set_filename, with_filename, "");
1848 t!(s: "a", set_filename, with_filename, "");
1850 t!(v: b"hi\\there.txt", set_extension, with_extension, b"exe");
1851 t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
1852 t!(s: "hi\\there.", set_extension, with_extension, "txt");
1853 t!(s: "hi\\there", set_extension, with_extension, "txt");
1854 t!(s: "hi\\there.txt", set_extension, with_extension, "");
1855 t!(s: "hi\\there", set_extension, with_extension, "");
1856 t!(s: ".", set_extension, with_extension, "txt");
1858 // with_ helpers use the setter internally, so the tests for the with_ helpers
1859 // will suffice. No need for the full set of prefix tests.
1865 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1868 assert_eq!(path.filename_str(), $filename);
1869 assert_eq!(path.dirname_str(), $dirname);
1870 assert_eq!(path.filestem_str(), $filestem);
1871 assert_eq!(path.extension_str(), $ext);
1874 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1877 assert_eq!(path.filename(), $filename);
1878 assert_eq!(path.dirname(), $dirname);
1879 assert_eq!(path.filestem(), $filestem);
1880 assert_eq!(path.extension(), $ext);
1885 t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None);
1886 t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
1887 t!(s: Path::new("."), None, Some("."), None, None);
1888 t!(s: Path::new("\\"), None, Some("\\"), None, None);
1889 t!(s: Path::new(".."), None, Some(".."), None, None);
1890 t!(s: Path::new("..\\.."), None, Some("..\\.."), None, None);
1891 t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"),
1892 Some("there"), Some("txt"));
1893 t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None);
1894 t!(s: Path::new("hi\\there."), Some("there."), Some("hi"),
1895 Some("there"), Some(""));
1896 t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None);
1897 t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"),
1898 Some("."), Some("there"));
1900 // these are already tested in test_components, so no need for extended tests
1904 fn test_dir_path() {
1905 t!(s: Path::new("hi\\there").dir_path(), "hi");
1906 t!(s: Path::new("hi").dir_path(), ".");
1907 t!(s: Path::new("\\hi").dir_path(), "\\");
1908 t!(s: Path::new("\\").dir_path(), "\\");
1909 t!(s: Path::new("..").dir_path(), "..");
1910 t!(s: Path::new("..\\..").dir_path(), "..\\..");
1912 // dir_path is just dirname interpreted as a path.
1913 // No need for extended tests
1917 fn test_is_absolute() {
1919 ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => (
1921 let path = Path::new($path);
1922 let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel);
1923 assert_eq!(path.is_absolute(), abs);
1924 assert_eq!(is_vol_relative(&path), vol);
1925 assert_eq!(is_cwd_relative(&path), cwd);
1926 assert_eq!(path.is_relative(), rel);
1930 t!("a\\b\\c", false, false, false, true);
1931 t!("\\a\\b\\c", false, true, false, false);
1932 t!("a", false, false, false, true);
1933 t!("\\a", false, true, false, false);
1934 t!(".", false, false, false, true);
1935 t!("\\", false, true, false, false);
1936 t!("..", false, false, false, true);
1937 t!("..\\..", false, false, false, true);
1938 t!("C:a\\b.txt", false, false, true, false);
1939 t!("C:\\a\\b.txt", true, false, false, false);
1940 t!("\\\\server\\share\\a\\b.txt", true, false, false, false);
1941 t!("\\\\?\\a\\b\\c.txt", true, false, false, false);
1942 t!("\\\\?\\C:\\a\\b.txt", true, false, false, false);
1943 t!("\\\\?\\C:a\\b.txt", true, false, false, false); // NB: not equivalent to C:a\b.txt
1944 t!("\\\\?\\UNC\\server\\share\\a\\b.txt", true, false, false, false);
1945 t!("\\\\.\\a\\b", true, false, false, false);
1949 fn test_is_ancestor_of() {
1951 (s: $path:expr, $dest:expr, $exp:expr) => (
1953 let path = Path::new($path);
1954 let dest = Path::new($dest);
1956 let res = path.is_ancestor_of(&dest);
1957 assert_eq!(res, exp);
1962 t!(s: "a\\b\\c", "a\\b\\c\\d", true);
1963 t!(s: "a\\b\\c", "a\\b\\c", true);
1964 t!(s: "a\\b\\c", "a\\b", false);
1965 t!(s: "\\a\\b\\c", "\\a\\b\\c", true);
1966 t!(s: "\\a\\b", "\\a\\b\\c", true);
1967 t!(s: "\\a\\b\\c\\d", "\\a\\b\\c", false);
1968 t!(s: "\\a\\b", "a\\b\\c", false);
1969 t!(s: "a\\b", "\\a\\b\\c", false);
1970 t!(s: "a\\b\\c", "a\\b\\d", false);
1971 t!(s: "..\\a\\b\\c", "a\\b\\c", false);
1972 t!(s: "a\\b\\c", "..\\a\\b\\c", false);
1973 t!(s: "a\\b\\c", "a\\b\\cd", false);
1974 t!(s: "a\\b\\cd", "a\\b\\c", false);
1975 t!(s: "..\\a\\b", "..\\a\\b\\c", true);
1976 t!(s: ".", "a\\b", true);
1977 t!(s: ".", ".", true);
1978 t!(s: "\\", "\\", true);
1979 t!(s: "\\", "\\a\\b", true);
1980 t!(s: "..", "a\\b", true);
1981 t!(s: "..\\..", "a\\b", true);
1982 t!(s: "foo\\bar", "foobar", false);
1983 t!(s: "foobar", "foo\\bar", false);
1985 t!(s: "foo", "C:foo", false);
1986 t!(s: "C:foo", "foo", false);
1987 t!(s: "C:foo", "C:foo\\bar", true);
1988 t!(s: "C:foo\\bar", "C:foo", false);
1989 t!(s: "C:\\foo", "C:\\foo\\bar", true);
1990 t!(s: "C:", "C:", true);
1991 t!(s: "C:", "C:\\", false);
1992 t!(s: "C:\\", "C:", false);
1993 t!(s: "C:\\", "C:\\", true);
1994 t!(s: "C:\\foo\\bar", "C:\\foo", false);
1995 t!(s: "C:foo\\bar", "C:foo", false);
1996 t!(s: "C:\\foo", "\\foo", false);
1997 t!(s: "\\foo", "C:\\foo", false);
1998 t!(s: "\\\\server\\share\\foo", "\\\\server\\share\\foo\\bar", true);
1999 t!(s: "\\\\server\\share", "\\\\server\\share\\foo", true);
2000 t!(s: "\\\\server\\share\\foo", "\\\\server\\share", false);
2001 t!(s: "C:\\foo", "\\\\server\\share\\foo", false);
2002 t!(s: "\\\\server\\share\\foo", "C:\\foo", false);
2003 t!(s: "\\\\?\\foo\\bar", "\\\\?\\foo\\bar\\baz", true);
2004 t!(s: "\\\\?\\foo\\bar\\baz", "\\\\?\\foo\\bar", false);
2005 t!(s: "\\\\?\\foo\\bar", "\\foo\\bar\\baz", false);
2006 t!(s: "\\foo\\bar", "\\\\?\\foo\\bar\\baz", false);
2007 t!(s: "\\\\?\\C:\\foo\\bar", "\\\\?\\C:\\foo\\bar\\baz", true);
2008 t!(s: "\\\\?\\C:\\foo\\bar\\baz", "\\\\?\\C:\\foo\\bar", false);
2009 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\foo", true);
2010 t!(s: "\\\\?\\C:", "\\\\?\\C:\\", false); // this is a weird one
2011 t!(s: "\\\\?\\C:\\", "\\\\?\\C:", false);
2012 t!(s: "\\\\?\\C:\\a", "\\\\?\\c:\\a\\b", true);
2013 t!(s: "\\\\?\\c:\\a", "\\\\?\\C:\\a\\b", true);
2014 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a\\b", false);
2015 t!(s: "\\\\?\\foo", "\\\\?\\foobar", false);
2016 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", true);
2017 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\", true);
2018 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a\\b", true);
2019 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", false);
2020 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b\\", false);
2021 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c\\d", true);
2022 t!(s: "\\\\?\\UNC\\a\\b\\c\\d", "\\\\?\\UNC\\a\\b\\c", false);
2023 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", true);
2024 t!(s: "\\\\.\\foo\\bar", "\\\\.\\foo\\bar\\baz", true);
2025 t!(s: "\\\\.\\foo\\bar\\baz", "\\\\.\\foo\\bar", false);
2026 t!(s: "\\\\.\\foo", "\\\\.\\foo\\bar", true);
2027 t!(s: "\\\\.\\foo", "\\\\.\\foobar", false);
2029 t!(s: "\\a\\b", "\\\\?\\a\\b", false);
2030 t!(s: "\\\\?\\a\\b", "\\a\\b", false);
2031 t!(s: "\\a\\b", "\\\\?\\C:\\a\\b", false);
2032 t!(s: "\\\\?\\C:\\a\\b", "\\a\\b", false);
2033 t!(s: "Z:\\a\\b", "\\\\?\\z:\\a\\b", true);
2034 t!(s: "C:\\a\\b", "\\\\?\\D:\\a\\b", false);
2035 t!(s: "a\\b", "\\\\?\\a\\b", false);
2036 t!(s: "\\\\?\\a\\b", "a\\b", false);
2037 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b", true);
2038 t!(s: "\\\\?\\C:\\a\\b", "C:\\a\\b", true);
2039 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", false);
2040 t!(s: "C:a\\b", "\\\\?\\C:a\\b", false);
2041 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", false);
2042 t!(s: "\\\\?\\C:a\\b", "C:a\\b", false);
2043 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b\\", true);
2044 t!(s: "\\\\?\\C:\\a\\b\\", "C:\\a\\b", true);
2045 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c", true);
2046 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b\\c", true);
2050 fn test_ends_with_path() {
2052 (s: $path:expr, $child:expr, $exp:expr) => (
2054 let path = Path::new($path);
2055 let child = Path::new($child);
2056 assert_eq!(path.ends_with_path(&child), $exp);
2061 t!(s: "a\\b\\c", "c", true);
2062 t!(s: "a\\b\\c", "d", false);
2063 t!(s: "foo\\bar\\quux", "bar", false);
2064 t!(s: "foo\\bar\\quux", "barquux", false);
2065 t!(s: "a\\b\\c", "b\\c", true);
2066 t!(s: "a\\b\\c", "a\\b\\c", true);
2067 t!(s: "a\\b\\c", "foo\\a\\b\\c", false);
2068 t!(s: "\\a\\b\\c", "a\\b\\c", true);
2069 t!(s: "\\a\\b\\c", "\\a\\b\\c", false); // child must be relative
2070 t!(s: "\\a\\b\\c", "foo\\a\\b\\c", false);
2071 t!(s: "a\\b\\c", "", false);
2072 t!(s: "", "", true);
2073 t!(s: "\\a\\b\\c", "d\\e\\f", false);
2074 t!(s: "a\\b\\c", "a\\b", false);
2075 t!(s: "a\\b\\c", "b", false);
2076 t!(s: "C:\\a\\b", "b", true);
2077 t!(s: "C:\\a\\b", "C:b", false);
2078 t!(s: "C:\\a\\b", "C:a\\b", false);
2082 fn test_path_relative_from() {
2084 (s: $path:expr, $other:expr, $exp:expr) => (
2086 assert_eq!(Path::new($path).path_relative_from(&Path::new($other))
2087 .as_ref().and_then(|x| x.as_str()), $exp);
2092 t!(s: "a\\b\\c", "a\\b", Some("c"));
2093 t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c"));
2094 t!(s: "a\\b\\c", "a\\b\\c\\d", Some(".."));
2095 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2096 t!(s: "a\\b\\c", "a\\b\\c\\d\\e", Some("..\\.."));
2097 t!(s: "a\\b\\c", "a\\d\\e", Some("..\\..\\b\\c"));
2098 t!(s: "a\\b\\c", "d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2099 t!(s: "a\\b\\c", "\\a\\b\\c", None);
2100 t!(s: "\\a\\b\\c", "a\\b\\c", Some("\\a\\b\\c"));
2101 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d", Some(".."));
2102 t!(s: "\\a\\b\\c", "\\a\\b", Some("c"));
2103 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d\\e", Some("..\\.."));
2104 t!(s: "\\a\\b\\c", "\\a\\d\\e", Some("..\\..\\b\\c"));
2105 t!(s: "\\a\\b\\c", "\\d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2106 t!(s: "hi\\there.txt", "hi\\there", Some("..\\there.txt"));
2107 t!(s: ".", "a", Some(".."));
2108 t!(s: ".", "a\\b", Some("..\\.."));
2109 t!(s: ".", ".", Some("."));
2110 t!(s: "a", ".", Some("a"));
2111 t!(s: "a\\b", ".", Some("a\\b"));
2112 t!(s: "..", ".", Some(".."));
2113 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2114 t!(s: "\\a\\b\\c", "\\a\\b\\c", Some("."));
2115 t!(s: "\\", "\\", Some("."));
2116 t!(s: "\\", ".", Some("\\"));
2117 t!(s: "..\\..\\a", "b", Some("..\\..\\..\\a"));
2118 t!(s: "a", "..\\..\\b", None);
2119 t!(s: "..\\..\\a", "..\\..\\b", Some("..\\a"));
2120 t!(s: "..\\..\\a", "..\\..\\a\\b", Some(".."));
2121 t!(s: "..\\..\\a\\b", "..\\..\\a", Some("b"));
2123 t!(s: "C:a\\b\\c", "C:a\\b", Some("c"));
2124 t!(s: "C:a\\b", "C:a\\b\\c", Some(".."));
2125 t!(s: "C:" ,"C:a\\b", Some("..\\.."));
2126 t!(s: "C:a\\b", "C:c\\d", Some("..\\..\\a\\b"));
2127 t!(s: "C:a\\b", "D:c\\d", Some("C:a\\b"));
2128 t!(s: "C:a\\b", "C:..\\c", None);
2129 t!(s: "C:..\\a", "C:b\\c", Some("..\\..\\..\\a"));
2130 t!(s: "C:\\a\\b\\c", "C:\\a\\b", Some("c"));
2131 t!(s: "C:\\a\\b", "C:\\a\\b\\c", Some(".."));
2132 t!(s: "C:\\", "C:\\a\\b", Some("..\\.."));
2133 t!(s: "C:\\a\\b", "C:\\c\\d", Some("..\\..\\a\\b"));
2134 t!(s: "C:\\a\\b", "C:a\\b", Some("C:\\a\\b"));
2135 t!(s: "C:a\\b", "C:\\a\\b", None);
2136 t!(s: "\\a\\b", "C:\\a\\b", None);
2137 t!(s: "\\a\\b", "C:a\\b", None);
2138 t!(s: "a\\b", "C:\\a\\b", None);
2139 t!(s: "a\\b", "C:a\\b", None);
2141 t!(s: "\\\\a\\b\\c", "\\\\a\\b", Some("c"));
2142 t!(s: "\\\\a\\b", "\\\\a\\b\\c", Some(".."));
2143 t!(s: "\\\\a\\b\\c\\e", "\\\\a\\b\\c\\d", Some("..\\e"));
2144 t!(s: "\\\\a\\c\\d", "\\\\a\\b\\d", Some("\\\\a\\c\\d"));
2145 t!(s: "\\\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\b\\c\\d"));
2146 t!(s: "\\\\a\\b\\c", "\\d\\e", Some("\\\\a\\b\\c"));
2147 t!(s: "\\d\\e", "\\\\a\\b\\c", None);
2148 t!(s: "d\\e", "\\\\a\\b\\c", None);
2149 t!(s: "C:\\a\\b\\c", "\\\\a\\b\\c", Some("C:\\a\\b\\c"));
2150 t!(s: "C:\\c", "\\\\a\\b\\c", Some("C:\\c"));
2152 t!(s: "\\\\?\\a\\b", "\\a\\b", Some("\\\\?\\a\\b"));
2153 t!(s: "\\\\?\\a\\b", "a\\b", Some("\\\\?\\a\\b"));
2154 t!(s: "\\\\?\\a\\b", "\\b", Some("\\\\?\\a\\b"));
2155 t!(s: "\\\\?\\a\\b", "b", Some("\\\\?\\a\\b"));
2156 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", Some(".."));
2157 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", Some("c"));
2158 t!(s: "\\\\?\\a\\b", "\\\\?\\c\\d", Some("\\\\?\\a\\b"));
2159 t!(s: "\\\\?\\a", "\\\\?\\b", Some("\\\\?\\a"));
2161 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2162 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2163 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\b", Some("..\\a"));
2164 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a", Some("\\\\?\\C:\\a"));
2165 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\c:\\a", Some("b"));
2166 t!(s: "\\\\?\\C:\\a\\b", "C:\\a", Some("b"));
2167 t!(s: "\\\\?\\C:\\a", "C:\\a\\b", Some(".."));
2168 t!(s: "C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2169 t!(s: "C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2170 t!(s: "\\\\?\\C:\\a", "D:\\a", Some("\\\\?\\C:\\a"));
2171 t!(s: "\\\\?\\c:\\a\\b", "C:\\a", Some("b"));
2172 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", Some("\\\\?\\C:\\a\\b"));
2173 t!(s: "\\\\?\\C:\\a\\.\\b", "C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2174 t!(s: "\\\\?\\C:\\a\\b/c", "C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2175 t!(s: "\\\\?\\C:\\a\\..\\b", "C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2176 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", None);
2177 t!(s: "\\\\?\\C:\\a\\.\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2178 t!(s: "\\\\?\\C:\\a\\b/c", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2179 t!(s: "\\\\?\\C:\\a\\..\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2180 t!(s: "\\\\?\\C:\\a\\b\\", "\\\\?\\C:\\a", Some("b"));
2181 t!(s: "\\\\?\\C:\\.\\b", "\\\\?\\C:\\.", Some("b"));
2182 t!(s: "C:\\b", "\\\\?\\C:\\.", Some("..\\b"));
2183 t!(s: "\\\\?\\a\\.\\b\\c", "\\\\?\\a\\.\\b", Some("c"));
2184 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\.\\d", Some("..\\..\\b\\c"));
2185 t!(s: "\\\\?\\a\\..\\b", "\\\\?\\a\\..", Some("b"));
2186 t!(s: "\\\\?\\a\\b\\..", "\\\\?\\a\\b", Some("\\\\?\\a\\b\\.."));
2187 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\..\\b", Some("..\\..\\b\\c"));
2189 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2190 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", Some(".."));
2191 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2192 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2193 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2194 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\C:\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2195 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2196 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2197 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2198 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b", Some("c"));
2199 t!(s: "\\\\?\\UNC\\a\\b", "\\\\a\\b\\c", Some(".."));
2200 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2201 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2202 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2203 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2204 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2205 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2206 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\a\\b\\c"));
2210 fn test_str_components() {
2212 (s: $path:expr, $exp:expr) => (
2214 let path = Path::new($path);
2215 let comps = path.str_components().map(|x|x.unwrap())
2216 .collect::<Vec<&str>>();
2217 let exp: &[&str] = &$exp;
2218 assert_eq!(comps, exp);
2219 let comps = path.str_components().rev().map(|x|x.unwrap())
2220 .collect::<Vec<&str>>();
2221 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>();
2222 assert_eq!(comps, exp);
2227 t!(s: b"a\\b\\c", ["a", "b", "c"]);
2228 t!(s: "a\\b\\c", ["a", "b", "c"]);
2229 t!(s: "a\\b\\d", ["a", "b", "d"]);
2230 t!(s: "a\\b\\cd", ["a", "b", "cd"]);
2231 t!(s: "\\a\\b\\c", ["a", "b", "c"]);
2233 t!(s: "\\a", ["a"]);
2236 t!(s: "..", [".."]);
2237 t!(s: "..\\..", ["..", ".."]);
2238 t!(s: "..\\..\\foo", ["..", "..", "foo"]);
2239 t!(s: "C:foo\\bar", ["foo", "bar"]);
2240 t!(s: "C:foo", ["foo"]);
2242 t!(s: "C:\\foo\\bar", ["foo", "bar"]);
2243 t!(s: "C:\\foo", ["foo"]);
2245 t!(s: "\\\\server\\share\\foo\\bar", ["foo", "bar"]);
2246 t!(s: "\\\\server\\share\\foo", ["foo"]);
2247 t!(s: "\\\\server\\share", []);
2248 t!(s: "\\\\?\\foo\\bar\\baz", ["bar", "baz"]);
2249 t!(s: "\\\\?\\foo\\bar", ["bar"]);
2250 t!(s: "\\\\?\\foo", []);
2251 t!(s: "\\\\?\\", []);
2252 t!(s: "\\\\?\\a\\b", ["b"]);
2253 t!(s: "\\\\?\\a\\b\\", ["b"]);
2254 t!(s: "\\\\?\\foo\\bar\\\\baz", ["bar", "", "baz"]);
2255 t!(s: "\\\\?\\C:\\foo\\bar", ["foo", "bar"]);
2256 t!(s: "\\\\?\\C:\\foo", ["foo"]);
2257 t!(s: "\\\\?\\C:\\", []);
2258 t!(s: "\\\\?\\C:\\foo\\", ["foo"]);
2259 t!(s: "\\\\?\\UNC\\server\\share\\foo\\bar", ["foo", "bar"]);
2260 t!(s: "\\\\?\\UNC\\server\\share\\foo", ["foo"]);
2261 t!(s: "\\\\?\\UNC\\server\\share", []);
2262 t!(s: "\\\\.\\foo\\bar\\baz", ["bar", "baz"]);
2263 t!(s: "\\\\.\\foo\\bar", ["bar"]);
2264 t!(s: "\\\\.\\foo", []);
2268 fn test_components_iter() {
2270 (s: $path:expr, $exp:expr) => (
2272 let path = Path::new($path);
2273 let comps = path.components().collect::<Vec<&[u8]>>();
2274 let exp: &[&[u8]] = &$exp;
2275 assert_eq!(comps, exp);
2276 let comps = path.components().rev().collect::<Vec<&[u8]>>();
2277 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
2278 assert_eq!(comps, exp);
2283 t!(s: "a\\b\\c", [b"a", b"b", b"c"]);
2285 // since this is really a wrapper around str_components, those tests suffice
2289 fn test_make_non_verbatim() {
2291 ($path:expr, $exp:expr) => (
2293 let path = Path::new($path);
2294 let exp: Option<&str> = $exp;
2295 let exp = exp.map(|s| Path::new(s));
2296 assert_eq!(make_non_verbatim(&path), exp);
2301 t!(r"\a\b\c", Some(r"\a\b\c"));
2302 t!(r"a\b\c", Some(r"a\b\c"));
2303 t!(r"C:\a\b\c", Some(r"C:\a\b\c"));
2304 t!(r"C:a\b\c", Some(r"C:a\b\c"));
2305 t!(r"\\server\share\foo", Some(r"\\server\share\foo"));
2306 t!(r"\\.\foo", None);
2307 t!(r"\\?\foo", None);
2308 t!(r"\\?\C:", None);
2309 t!(r"\\?\C:foo", None);
2310 t!(r"\\?\C:\", Some(r"C:\"));
2311 t!(r"\\?\C:\foo", Some(r"C:\foo"));
2312 t!(r"\\?\C:\foo\bar\baz", Some(r"C:\foo\bar\baz"));
2313 t!(r"\\?\C:\foo\.\bar\baz", None);
2314 t!(r"\\?\C:\foo\bar\..\baz", None);
2315 t!(r"\\?\C:\foo\bar\..", None);
2316 t!(r"\\?\UNC\server\share\foo", Some(r"\\server\share\foo"));
2317 t!(r"\\?\UNC\server\share", Some(r"\\server\share"));
2318 t!(r"\\?\UNC\server", None);
2319 t!(r"\\?\UNC\server\", None);