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 //! Windows file path handling
14 use c_str::{CString, ToCStr};
16 use cmp::{Eq, TotalEq};
17 use container::Container;
18 use from_str::FromStr;
20 use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Iterator, Map};
22 use option::{Option, Some, None};
23 use slice::{Vector, OwnedVector, ImmutableVector};
24 use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
28 use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe};
30 /// Iterator that yields successive components of a Path as &str
32 /// Each component is yielded as Option<&str> for compatibility with PosixPath, but
33 /// every component in WindowsPath is guaranteed to be Some.
34 pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>,
35 CharSplits<'a, char>>;
37 /// Iterator that yields successive components of a Path as &[u8]
38 pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8],
41 /// Represents a Windows path
42 // Notes for Windows path impl:
43 // The MAX_PATH is 260, but 253 is the practical limit due to some API bugs
44 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for good information
45 // about windows paths.
46 // That same page puts a bunch of restrictions on allowed characters in a path.
47 // `\foo.txt` means "relative to current drive", but will not be considered to be absolute here
48 // as `∃P | P.join("\foo.txt") != "\foo.txt"`.
49 // `C:` is interesting, that means "the current directory on drive C".
50 // Long absolute paths need to have \\?\ prefix (or, for UNC, \\?\UNC\). I think that can be
51 // ignored for now, though, and only added in a hypothetical .to_pwstr() function.
52 // However, if a path is parsed that has \\?\, this needs to be preserved as it disables the
53 // processing of "." and ".." components and / as a separator.
54 // Experimentally, \\?\foo is not the same thing as \foo.
55 // Also, \\foo is not valid either (certainly not equivalent to \foo).
56 // Similarly, C:\\Users is not equivalent to C:\Users, although C:\Users\\foo is equivalent
57 // to C:\Users\foo. In fact the command prompt treats C:\\foo\bar as UNC path. But it might be
58 // best to just ignore that and normalize it to C:\foo\bar.
60 // Based on all this, I think the right approach is to do the following:
61 // * Require valid utf-8 paths. Windows API may use WCHARs, but we don't, and utf-8 is convertible
62 // to UTF-16 anyway (though does Windows use UTF-16 or UCS-2? Not sure).
63 // * Parse the prefixes \\?\UNC\, \\?\, and \\.\ explicitly.
64 // * If \\?\UNC\, treat following two path components as server\share. Don't error for missing
66 // * If \\?\, parse disk from following component, if present. Don't error for missing disk.
67 // * If \\.\, treat rest of path as just regular components. I don't know how . and .. are handled
68 // here, they probably aren't, but I'm not going to worry about that.
69 // * Else if starts with \\, treat following two components as server\share. Don't error for missing
71 // * Otherwise, attempt to parse drive from start of path.
73 // The only error condition imposed here is valid utf-8. All other invalid paths are simply
74 // preserved by the data structure; let the Windows API error out on them.
77 repr: StrBuf, // assumed to never be empty
78 prefix: Option<PathPrefix>,
79 sepidx: Option<uint> // index of the final separator in the non-prefix portion of repr
84 fn eq(&self, other: &Path) -> bool {
85 self.repr == other.repr
89 impl TotalEq for Path {}
91 impl FromStr for Path {
92 fn from_str(s: &str) -> Option<Path> {
97 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
98 // we cannot usefully take ToCStr arguments by reference (without forcing an
99 // additional & around &str). So we are instead temporarily adding an instance
100 // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
101 // instance should be removed, and arguments bound by ToCStr should be passed by
104 impl ToCStr for Path {
106 fn to_c_str(&self) -> CString {
107 // The Path impl guarantees no internal NUL
108 unsafe { self.to_c_str_unchecked() }
112 unsafe fn to_c_str_unchecked(&self) -> CString {
113 self.as_vec().to_c_str_unchecked()
117 impl<'a> ToCStr for &'a Path {
119 fn to_c_str(&self) -> CString {
124 unsafe fn to_c_str_unchecked(&self) -> CString {
125 (*self).to_c_str_unchecked()
129 impl<S: Writer> ::hash::Hash<S> for Path {
131 fn hash(&self, state: &mut S) {
132 self.repr.hash(state)
136 impl BytesContainer for Path {
138 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
142 fn container_into_owned_bytes(self) -> Vec<u8> {
146 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
150 fn is_str(_: Option<Path>) -> bool { true }
153 impl<'a> BytesContainer for &'a Path {
155 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
159 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
163 fn is_str(_: Option<&'a Path>) -> bool { true }
166 impl GenericPathUnsafe for Path {
167 /// See `GenericPathUnsafe::from_vec_unchecked`.
171 /// Fails if not valid UTF-8.
173 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
174 let (prefix, path) = Path::normalize_(path.container_as_str().unwrap());
175 assert!(!path.is_empty());
176 let mut ret = Path{ repr: path, prefix: prefix, sepidx: None };
181 /// See `GenericPathUnsafe::set_filename_unchecekd`.
185 /// Fails if not valid UTF-8.
186 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
187 let filename = filename.container_as_str().unwrap();
188 match self.sepidx_or_prefix_len() {
189 None if ".." == self.repr.as_slice() => {
190 let mut s = StrBuf::with_capacity(3 + filename.len());
193 s.push_str(filename);
194 self.update_normalized(s);
197 self.update_normalized(filename);
199 Some((_,idxa,end)) if self.repr.as_slice().slice(idxa,end) == ".." => {
200 let mut s = StrBuf::with_capacity(end + 1 + filename.len());
201 s.push_str(self.repr.as_slice().slice_to(end));
203 s.push_str(filename);
204 self.update_normalized(s);
206 Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => {
207 let mut s = StrBuf::with_capacity(idxb + filename.len());
208 s.push_str(self.repr.as_slice().slice_to(idxb));
209 s.push_str(filename);
210 self.update_normalized(s);
212 Some((idxb,_,_)) => {
213 let mut s = StrBuf::with_capacity(idxb + 1 + filename.len());
214 s.push_str(self.repr.as_slice().slice_to(idxb));
216 s.push_str(filename);
217 self.update_normalized(s);
222 /// See `GenericPathUnsafe::push_unchecked`.
224 /// Concatenating two Windows Paths is rather complicated.
225 /// For the most part, it will behave as expected, except in the case of
226 /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no
227 /// concept of per-volume cwds like Windows does, we can't behave exactly
228 /// like Windows will. Instead, if the receiver is an absolute path on
229 /// the same volume as the new path, it will be treated as the cwd that
230 /// the new path is relative to. Otherwise, the new path will be treated
231 /// as if it were absolute and will replace the receiver outright.
232 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
233 let path = path.container_as_str().unwrap();
234 fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
235 // assume prefix is Some(DiskPrefix)
236 let rest = path.slice_from(prefix_len(prefix));
237 !rest.is_empty() && rest[0].is_ascii() && is_sep(rest[0] as char)
239 fn shares_volume(me: &Path, path: &str) -> bool {
240 // path is assumed to have a prefix of Some(DiskPrefix)
241 let repr = me.repr.as_slice();
243 Some(DiskPrefix) => repr[0] == path[0].to_ascii().to_upper().to_byte(),
244 Some(VerbatimDiskPrefix) => repr[4] == path[0].to_ascii().to_upper().to_byte(),
248 fn is_sep_(prefix: Option<PathPrefix>, u: u8) -> bool {
249 if prefix_is_verbatim(prefix) { is_sep_verbatim(u as char) }
250 else { is_sep(u as char) }
253 fn replace_path(me: &mut Path, path: &str, prefix: Option<PathPrefix>) {
254 let newpath = Path::normalize__(path, prefix);
255 me.repr = match newpath {
257 None => StrBuf::from_str(path)
262 fn append_path(me: &mut Path, path: &str) {
263 // appends a path that has no prefix
264 // if me is verbatim, we need to pre-normalize the new path
265 let path_ = if is_verbatim(me) { Path::normalize__(path, None) }
267 let pathlen = path_.as_ref().map_or(path.len(), |p| p.len());
268 let mut s = StrBuf::with_capacity(me.repr.len() + 1 + pathlen);
269 s.push_str(me.repr.as_slice());
270 let plen = me.prefix_len();
271 // if me is "C:" we don't want to add a path separator
273 Some(DiskPrefix) if me.repr.len() == plen => (),
274 _ if !(me.repr.len() > plen && me.repr.as_slice()[me.repr.len()-1] == SEP_BYTE) => {
280 None => s.push_str(path),
281 Some(p) => s.push_str(p.as_slice())
283 me.update_normalized(s)
286 if !path.is_empty() {
287 let prefix = parse_prefix(path);
289 Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => {
290 // cwd-relative path, self is on the same volume
291 append_path(self, path.slice_from(prefix_len(prefix)));
294 // absolute path, or cwd-relative and self is not same volume
295 replace_path(self, path, prefix);
297 None if !path.is_empty() && is_sep_(self.prefix, path[0]) => {
298 // volume-relative path
299 if self.prefix.is_some() {
300 // truncate self down to the prefix, then append
301 let n = self.prefix_len();
302 self.repr.truncate(n);
303 append_path(self, path);
305 // we have no prefix, so nothing to be relative to
306 replace_path(self, path, prefix);
311 append_path(self, path);
318 impl GenericPath for Path {
320 fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
321 match path.container_as_str() {
327 Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
333 /// See `GenericPath::as_str` for info.
334 /// Always returns a `Some` value.
336 fn as_str<'a>(&'a self) -> Option<&'a str> {
337 Some(self.repr.as_slice())
341 fn as_vec<'a>(&'a self) -> &'a [u8] {
346 fn into_vec(self) -> Vec<u8> {
347 Vec::from_slice(self.repr.as_bytes())
351 fn dirname<'a>(&'a self) -> &'a [u8] {
352 self.dirname_str().unwrap().as_bytes()
355 /// See `GenericPath::dirname_str` for info.
356 /// Always returns a `Some` value.
357 fn dirname_str<'a>(&'a self) -> Option<&'a str> {
358 Some(match self.sepidx_or_prefix_len() {
359 None if ".." == self.repr.as_slice() => self.repr.as_slice(),
361 Some((_,idxa,end)) if self.repr.as_slice().slice(idxa, end) == ".." => {
364 Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => {
367 Some((0,idxa,_)) => self.repr.as_slice().slice_to(idxa),
368 Some((idxb,idxa,_)) => {
370 Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => {
371 self.repr.as_slice().slice_to(idxa)
373 _ => self.repr.as_slice().slice_to(idxb)
380 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
381 self.filename_str().map(|x| x.as_bytes())
384 /// See `GenericPath::filename_str` for info.
385 /// Always returns a `Some` value if `filename` returns a `Some` value.
386 fn filename_str<'a>(&'a self) -> Option<&'a str> {
387 let repr = self.repr.as_slice();
388 match self.sepidx_or_prefix_len() {
389 None if "." == repr || ".." == repr => None,
391 Some((_,idxa,end)) if repr.slice(idxa, end) == ".." => None,
392 Some((_,idxa,end)) if idxa == end => None,
393 Some((_,idxa,end)) => Some(repr.slice(idxa, end))
397 /// See `GenericPath::filestem_str` for info.
398 /// Always returns a `Some` value if `filestem` returns a `Some` value.
400 fn filestem_str<'a>(&'a self) -> Option<&'a str> {
401 // filestem() returns a byte vector that's guaranteed valid UTF-8
402 self.filestem().map(|t| unsafe { mem::transmute(t) })
406 fn extension_str<'a>(&'a self) -> Option<&'a str> {
407 // extension() returns a byte vector that's guaranteed valid UTF-8
408 self.extension().map(|t| unsafe { mem::transmute(t) })
411 fn dir_path(&self) -> Path {
412 unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) }
416 fn pop(&mut self) -> bool {
417 match self.sepidx_or_prefix_len() {
418 None if "." == self.repr.as_slice() => false,
420 self.repr = StrBuf::from_str(".");
424 Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false,
425 Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => false,
426 Some((idxb,idxa,_)) => {
427 let trunc = match self.prefix {
428 Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => {
429 let plen = self.prefix_len();
430 if idxb == plen { idxa } else { idxb }
434 self.repr.truncate(trunc);
435 self.update_sepidx();
441 fn root_path(&self) -> Option<Path> {
442 if self.prefix.is_some() {
443 Some(Path::new(match self.prefix {
444 Some(DiskPrefix) if self.is_absolute() => {
445 self.repr.as_slice().slice_to(self.prefix_len()+1)
447 Some(VerbatimDiskPrefix) => {
448 self.repr.as_slice().slice_to(self.prefix_len()+1)
450 _ => self.repr.as_slice().slice_to(self.prefix_len())
452 } else if is_vol_relative(self) {
453 Some(Path::new(self.repr.as_slice().slice_to(1)))
459 /// See `GenericPath::is_absolute` for info.
461 /// A Windows Path is considered absolute only if it has a non-volume prefix,
462 /// or if it has a volume prefix and the path starts with '\'.
463 /// A path of `\foo` is not considered absolute because it's actually
464 /// relative to the "current volume". A separate method `Path::is_vol_relative`
465 /// is provided to indicate this case. Similarly a path of `C:foo` is not
466 /// considered absolute because it's relative to the cwd on volume C:. A
467 /// separate method `Path::is_cwd_relative` is provided to indicate this case.
469 fn is_absolute(&self) -> bool {
471 Some(DiskPrefix) => {
472 let rest = self.repr.as_slice().slice_from(self.prefix_len());
473 rest.len() > 0 && rest[0] == SEP_BYTE
481 fn is_relative(&self) -> bool {
482 self.prefix.is_none() && !is_vol_relative(self)
485 fn is_ancestor_of(&self, other: &Path) -> bool {
486 if !self.equiv_prefix(other) {
488 } else if self.is_absolute() != other.is_absolute() ||
489 is_vol_relative(self) != is_vol_relative(other) {
492 let mut ita = self.str_components().map(|x|x.unwrap());
493 let mut itb = other.str_components().map(|x|x.unwrap());
494 if "." == self.repr.as_slice() {
495 return itb.next() != Some("..");
498 match (ita.next(), itb.next()) {
500 (Some(a), Some(b)) if a == b => { continue },
501 (Some(a), _) if a == ".." => {
502 // if ita contains only .. components, it's an ancestor
503 return ita.all(|x| x == "..");
512 fn path_relative_from(&self, base: &Path) -> Option<Path> {
513 fn comp_requires_verbatim(s: &str) -> bool {
514 s == "." || s == ".." || s.contains_char(SEP2)
517 if !self.equiv_prefix(base) {
519 if self.is_absolute() {
521 } else if self.prefix == Some(DiskPrefix) && base.prefix == Some(DiskPrefix) {
522 // both drives, drive letters must differ or they'd be equiv
527 } else if self.is_absolute() != base.is_absolute() {
528 if self.is_absolute() {
533 } else if is_vol_relative(self) != is_vol_relative(base) {
534 if is_vol_relative(self) {
540 let mut ita = self.str_components().map(|x|x.unwrap());
541 let mut itb = base.str_components().map(|x|x.unwrap());
542 let mut comps = vec![];
544 let a_verb = is_verbatim(self);
545 let b_verb = is_verbatim(base);
547 match (ita.next(), itb.next()) {
548 (None, None) => break,
549 (Some(a), None) if a_verb && comp_requires_verbatim(a) => {
550 return Some(self.clone())
555 comps.extend(ita.by_ref());
559 (None, _) => comps.push(".."),
560 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
561 (Some(a), Some(b)) if !b_verb && b == "." => {
562 if a_verb && comp_requires_verbatim(a) {
563 return Some(self.clone())
564 } else { comps.push(a) }
566 (Some(_), Some(b)) if !b_verb && b == ".." => return None,
567 (Some(a), Some(_)) if a_verb && comp_requires_verbatim(a) => {
568 return Some(self.clone())
570 (Some(a), Some(_)) => {
577 comps.extend(ita.by_ref());
583 Some(Path::new(comps.connect("\\")))
587 fn ends_with_path(&self, child: &Path) -> bool {
588 if !child.is_relative() { return false; }
589 let mut selfit = self.str_components().rev();
590 let mut childit = child.str_components().rev();
592 match (selfit.next(), childit.next()) {
593 (Some(a), Some(b)) => if a != b { return false; },
594 (Some(_), None) => break,
595 (None, Some(_)) => return false,
596 (None, None) => break
604 /// Returns a new Path from a byte vector or string
608 /// Fails the task if the vector contains a NUL.
609 /// Fails if invalid UTF-8.
611 pub fn new<T: BytesContainer>(path: T) -> Path {
612 GenericPath::new(path)
615 /// Returns a new Path from a byte vector or string, if possible
617 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
618 GenericPath::new_opt(path)
621 /// Returns an iterator that yields each component of the path in turn as a Option<&str>.
622 /// Every component is guaranteed to be Some.
623 /// Does not yield the path prefix (including server/share components in UNC paths).
624 /// Does not distinguish between volume-relative and relative paths, e.g.
625 /// \a\b\c and a\b\c.
626 /// Does not distinguish between absolute and cwd-relative paths, e.g.
627 /// C:\foo and C:foo.
628 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
629 let repr = self.repr.as_slice();
630 let s = match self.prefix {
632 let plen = self.prefix_len();
633 if repr.len() > plen && repr[plen] == SEP_BYTE {
634 repr.slice_from(plen+1)
635 } else { repr.slice_from(plen) }
637 None if repr[0] == SEP_BYTE => repr.slice_from(1),
640 let ret = s.split_terminator(SEP).map(Some);
644 /// Returns an iterator that yields each component of the path in turn as a &[u8].
645 /// See str_components() for details.
646 pub fn components<'a>(&'a self) -> Components<'a> {
647 fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
649 x.unwrap().as_bytes()
651 self.str_components().map(convert)
654 fn equiv_prefix(&self, other: &Path) -> bool {
655 let s_repr = self.repr.as_slice();
656 let o_repr = other.repr.as_slice();
657 match (self.prefix, other.prefix) {
658 (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
659 self.is_absolute() &&
660 s_repr[0].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
662 (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
663 other.is_absolute() &&
664 s_repr[4].to_ascii().eq_ignore_case(o_repr[0].to_ascii())
666 (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
667 s_repr[4].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
669 (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
670 s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len())
672 (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => {
673 s_repr.slice(8, self.prefix_len()) == o_repr.slice(2, other.prefix_len())
675 (None, None) => true,
676 (a, b) if a == b => {
677 s_repr.slice_to(self.prefix_len()) == o_repr.slice_to(other.prefix_len())
683 fn normalize_<S: StrAllocating>(s: S) -> (Option<PathPrefix>, StrBuf) {
684 // make borrowck happy
685 let (prefix, val) = {
686 let prefix = parse_prefix(s.as_slice());
687 let path = Path::normalize__(s.as_slice(), prefix);
691 None => s.into_strbuf(),
696 fn normalize__(s: &str, prefix: Option<PathPrefix>) -> Option<StrBuf> {
697 if prefix_is_verbatim(prefix) {
698 // don't do any normalization
700 Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
701 // the server component has no trailing '\'
702 let mut s = StrBuf::from_str(s);
709 let (is_abs, comps) = normalize_helper(s, prefix);
710 let mut comps = comps;
711 match (comps.is_some(),prefix) {
712 (false, Some(DiskPrefix)) => {
713 if s[0] >= 'a' as u8 && s[0] <= 'z' as u8 {
714 comps = Some(vec![]);
717 (false, Some(VerbatimDiskPrefix)) => {
718 if s[4] >= 'a' as u8 && s[0] <= 'z' as u8 {
719 comps = Some(vec![]);
727 if prefix.is_some() && comps.is_empty() {
728 match prefix.unwrap() {
730 let len = prefix_len(prefix) + is_abs as uint;
731 let mut s = StrBuf::from_str(s.slice_to(len));
733 let v = s.as_mut_vec();
734 *v.get_mut(0) = v.get(0).to_ascii().to_upper().to_byte();
737 // normalize C:/ to C:\
739 *s.as_mut_vec().get_mut(2) = SEP_BYTE;
744 VerbatimDiskPrefix => {
745 let len = prefix_len(prefix) + is_abs as uint;
746 let mut s = StrBuf::from_str(s.slice_to(len));
748 let v = s.as_mut_vec();
749 *v.get_mut(4) = v.get(4).to_ascii().to_upper().to_byte();
754 let plen = prefix_len(prefix);
756 Some(StrBuf::from_str(s.slice_to(plen)))
760 } else if is_abs && comps.is_empty() {
761 Some(StrBuf::from_char(1, SEP))
763 let prefix_ = s.slice_to(prefix_len(prefix));
764 let n = prefix_.len() +
765 if is_abs { comps.len() } else { comps.len() - 1} +
766 comps.iter().map(|v| v.len()).sum();
767 let mut s = StrBuf::with_capacity(n);
769 Some(DiskPrefix) => {
770 s.push_char(prefix_[0].to_ascii().to_upper().to_char());
773 Some(VerbatimDiskPrefix) => {
774 s.push_str(prefix_.slice_to(4));
775 s.push_char(prefix_[4].to_ascii().to_upper().to_char());
776 s.push_str(prefix_.slice_from(5));
778 Some(UNCPrefix(a,b)) => {
780 s.push_str(prefix_.slice(2, a+2));
782 s.push_str(prefix_.slice(3+a, 3+a+b));
784 Some(_) => s.push_str(prefix_),
787 let mut it = comps.move_iter();
791 Some(comp) => s.push_str(comp)
805 fn update_sepidx(&mut self) {
806 let s = if self.has_nonsemantic_trailing_slash() {
807 self.repr.as_slice().slice_to(self.repr.len()-1)
808 } else { self.repr.as_slice() };
809 let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep }
810 else { is_sep_verbatim });
811 let prefixlen = self.prefix_len();
812 self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
815 fn prefix_len(&self) -> uint {
816 prefix_len(self.prefix)
819 // Returns a tuple (before, after, end) where before is the index of the separator
820 // and after is the index just after the separator.
821 // end is the length of the string, normally, or the index of the final character if it is
822 // a non-semantic trailing separator in a verbatim string.
823 // If the prefix is considered the separator, before and after are the same.
824 fn sepidx_or_prefix_len(&self) -> Option<(uint,uint,uint)> {
826 None => match self.prefix_len() { 0 => None, x => Some((x,x,self.repr.len())) },
828 if self.has_nonsemantic_trailing_slash() {
829 Some((x,x+1,self.repr.len()-1))
830 } else { Some((x,x+1,self.repr.len())) }
835 fn has_nonsemantic_trailing_slash(&self) -> bool {
836 is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
837 self.repr.as_slice()[self.repr.len()-1] == SEP_BYTE
840 fn update_normalized<S: Str>(&mut self, s: S) {
841 let (prefix, path) = Path::normalize_(s.as_slice());
843 self.prefix = prefix;
844 self.update_sepidx();
848 /// Returns whether the path is considered "volume-relative", which means a path
849 /// that looks like "\foo". Paths of this form are relative to the current volume,
850 /// but absolute within that volume.
852 pub fn is_vol_relative(path: &Path) -> bool {
853 path.prefix.is_none() && is_sep_byte(&path.repr.as_slice()[0])
856 /// Returns whether the path is considered "cwd-relative", which means a path
857 /// with a volume prefix that is not absolute. This look like "C:foo.txt". Paths
858 /// of this form are relative to the cwd on the given volume.
860 pub fn is_cwd_relative(path: &Path) -> bool {
861 path.prefix == Some(DiskPrefix) && !path.is_absolute()
864 /// Returns the PathPrefix for this Path
866 pub fn prefix(path: &Path) -> Option<PathPrefix> {
870 /// Returns whether the Path's prefix is a verbatim prefix, i.e. `\\?\`
872 pub fn is_verbatim(path: &Path) -> bool {
873 prefix_is_verbatim(path.prefix)
876 /// Returns the non-verbatim equivalent of the input path, if possible.
877 /// If the input path is a device namespace path, None is returned.
878 /// If the input path is not verbatim, it is returned as-is.
879 /// If the input path is verbatim, but the same path can be expressed as
880 /// non-verbatim, the non-verbatim version is returned.
881 /// Otherwise, None is returned.
882 pub fn make_non_verbatim(path: &Path) -> Option<Path> {
883 let repr = path.repr.as_slice();
884 let new_path = match path.prefix {
885 Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None,
886 Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()),
887 Some(VerbatimDiskPrefix) => {
889 Path::new(repr.slice_from(4))
891 Some(VerbatimUNCPrefix(_,_)) => {
892 // \\?\UNC\server\share
893 Path::new(format!(r"\\{}", repr.slice_from(7)))
896 if new_path.prefix.is_none() {
897 // \\?\UNC\server is a VerbatimUNCPrefix
898 // but \\server is nothing
901 // now ensure normalization didn't change anything
902 if repr.slice_from(path.prefix_len()) ==
903 new_path.repr.as_slice().slice_from(new_path.prefix_len()) {
910 /// The standard path separator character
911 pub static SEP: char = '\\';
912 /// The standard path separator byte
913 pub static SEP_BYTE: u8 = SEP as u8;
915 /// The alternative path separator character
916 pub static SEP2: char = '/';
917 /// The alternative path separator character
918 pub static SEP2_BYTE: u8 = SEP2 as u8;
920 /// Returns whether the given char is a path separator.
921 /// Allows both the primary separator '\' and the alternative separator '/'.
923 pub fn is_sep(c: char) -> bool {
924 c == SEP || c == SEP2
927 /// Returns whether the given char is a path separator.
928 /// Only allows the primary separator '\'; use is_sep to allow '/'.
930 pub fn is_sep_verbatim(c: char) -> bool {
934 /// Returns whether the given byte is a path separator.
935 /// Allows both the primary separator '\' and the alternative separator '/'.
937 pub fn is_sep_byte(u: &u8) -> bool {
938 *u == SEP_BYTE || *u == SEP2_BYTE
941 /// Returns whether the given byte is a path separator.
942 /// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
944 pub fn is_sep_byte_verbatim(u: &u8) -> bool {
948 /// Prefix types for Path
949 #[deriving(Eq, Clone)]
950 pub enum PathPrefix {
951 /// Prefix `\\?\`, uint is the length of the following component
952 VerbatimPrefix(uint),
953 /// Prefix `\\?\UNC\`, uints are the lengths of the UNC components
954 VerbatimUNCPrefix(uint, uint),
955 /// Prefix `\\?\C:\` (for any alphabetic character)
957 /// Prefix `\\.\`, uint is the length of the following component
958 DeviceNSPrefix(uint),
959 /// UNC prefix `\\server\share`, uints are the lengths of the server/share
960 UNCPrefix(uint, uint),
961 /// Prefix `C:` for any alphabetic character
965 fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
966 if path.starts_with("\\\\") {
968 path = path.slice_from(2);
969 if path.starts_with("?\\") {
971 path = path.slice_from(2);
972 if path.starts_with("UNC\\") {
973 // \\?\UNC\server\share
974 path = path.slice_from(4);
975 let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) {
977 None => (path.len(), 0)
979 return Some(VerbatimUNCPrefix(idx_a, idx_b));
982 let idx = path.find('\\');
983 if idx == Some(2) && path[1] == ':' as u8 {
985 if c.is_ascii() && ::char::is_alphabetic(c as char) {
987 return Some(VerbatimDiskPrefix);
990 let idx = idx.unwrap_or(path.len());
991 return Some(VerbatimPrefix(idx));
993 } else if path.starts_with(".\\") {
995 path = path.slice_from(2);
996 let idx = path.find('\\').unwrap_or(path.len());
997 return Some(DeviceNSPrefix(idx));
999 match parse_two_comps(path, is_sep) {
1000 Some((idx_a, idx_b)) if idx_a > 0 && idx_b > 0 => {
1002 return Some(UNCPrefix(idx_a, idx_b));
1006 } else if path.len() > 1 && path[1] == ':' as u8 {
1009 if c.is_ascii() && ::char::is_alphabetic(c as char) {
1010 return Some(DiskPrefix);
1015 fn parse_two_comps<'a>(mut path: &'a str, f: |char| -> bool)
1016 -> Option<(uint, uint)> {
1017 let idx_a = match path.find(|x| f(x)) {
1018 None => return None,
1021 path = path.slice_from(idx_a+1);
1022 let idx_b = path.find(f).unwrap_or(path.len());
1023 Some((idx_a, idx_b))
1027 // None result means the string didn't need normalizing
1028 fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) {
1029 let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim };
1030 let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
1031 let s_ = s.slice_from(prefix_len(prefix));
1032 let s_ = if is_abs { s_.slice_from(1) } else { s_ };
1034 if is_abs && s_.is_empty() {
1035 return (is_abs, match prefix {
1036 Some(DiskPrefix) | None => (if is_sep_verbatim(s.char_at(prefix_len(prefix))) { None }
1037 else { Some(vec![]) }),
1038 Some(_) => Some(vec![]), // need to trim the trailing separator
1041 let mut comps: Vec<&'a str> = vec![];
1043 let mut changed = false;
1044 for comp in s_.split(f) {
1045 if comp.is_empty() { changed = true }
1046 else if comp == "." { changed = true }
1047 else if comp == ".." {
1048 let has_abs_prefix = match prefix {
1049 Some(DiskPrefix) => false,
1053 if (is_abs || has_abs_prefix) && comps.is_empty() { changed = true }
1054 else if comps.len() == n_up { comps.push(".."); n_up += 1 }
1055 else { comps.pop().unwrap(); changed = true }
1056 } else { comps.push(comp) }
1058 if !changed && !prefix_is_verbatim(prefix) {
1059 changed = s.find(is_sep).is_some();
1062 if comps.is_empty() && !is_abs && prefix.is_none() {
1064 return (is_abs, None);
1068 (is_abs, Some(comps))
1074 fn prefix_is_verbatim(p: Option<PathPrefix>) -> bool {
1076 Some(VerbatimPrefix(_)) | Some(VerbatimUNCPrefix(_,_)) | Some(VerbatimDiskPrefix) => true,
1077 Some(DeviceNSPrefix(_)) => true, // not really sure, but I think so
1082 fn prefix_len(p: Option<PathPrefix>) -> uint {
1085 Some(VerbatimPrefix(x)) => 4 + x,
1086 Some(VerbatimUNCPrefix(x,y)) => 8 + x + 1 + y,
1087 Some(VerbatimDiskPrefix) => 6,
1088 Some(UNCPrefix(x,y)) => 2 + x + 1 + y,
1089 Some(DeviceNSPrefix(x)) => 4 + x,
1090 Some(DiskPrefix) => 2
1098 use super::parse_prefix;
1101 (s: $path:expr, $exp:expr) => (
1104 assert!(path.as_str() == Some($exp));
1107 (v: $path:expr, $exp:expr) => (
1110 assert!(path.as_vec() == $exp);
1116 ($($arg:expr),+) => (
1118 static the_bytes: &'static [u8] = bytes!($($arg),+);
1125 fn test_parse_prefix() {
1127 ($path:expr, $exp:expr) => (
1131 let res = parse_prefix(path);
1133 "parse_prefix(\"{}\"): expected {:?}, found {:?}", path, exp, res);
1138 t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5)));
1140 t!("\\\\SERVER", None);
1141 t!("\\\\SERVER\\", None);
1142 t!("\\\\SERVER\\\\", None);
1143 t!("\\\\SERVER\\\\foo", None);
1144 t!("\\\\SERVER\\share", Some(UNCPrefix(6,5)));
1145 t!("\\\\SERVER/share/foo", Some(UNCPrefix(6,5)));
1146 t!("\\\\SERVER\\share/foo", Some(UNCPrefix(6,5)));
1147 t!("//SERVER/share/foo", None);
1148 t!("\\\\\\a\\b\\c", None);
1149 t!("\\\\?\\a\\b\\c", Some(VerbatimPrefix(1)));
1150 t!("\\\\?\\a/b/c", Some(VerbatimPrefix(5)));
1151 t!("//?/a/b/c", None);
1152 t!("\\\\.\\a\\b", Some(DeviceNSPrefix(1)));
1153 t!("\\\\.\\a/b", Some(DeviceNSPrefix(3)));
1154 t!("//./a/b", None);
1155 t!("\\\\?\\UNC\\server\\share\\foo", Some(VerbatimUNCPrefix(6,5)));
1156 t!("\\\\?\\UNC\\\\share\\foo", Some(VerbatimUNCPrefix(0,5)));
1157 t!("\\\\?\\UNC\\", Some(VerbatimUNCPrefix(0,0)));
1158 t!("\\\\?\\UNC\\server/share/foo", Some(VerbatimUNCPrefix(16,0)));
1159 t!("\\\\?\\UNC\\server", Some(VerbatimUNCPrefix(6,0)));
1160 t!("\\\\?\\UNC\\server\\", Some(VerbatimUNCPrefix(6,0)));
1161 t!("\\\\?\\UNC/server/share", Some(VerbatimPrefix(16)));
1162 t!("\\\\?\\UNC", Some(VerbatimPrefix(3)));
1163 t!("\\\\?\\C:\\a\\b.txt", Some(VerbatimDiskPrefix));
1164 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1165 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1166 t!("\\\\?\\C:a.txt", Some(VerbatimPrefix(7)));
1167 t!("\\\\?\\C:a\\b.txt", Some(VerbatimPrefix(3)));
1168 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1169 t!("C:\\foo", Some(DiskPrefix));
1170 t!("z:/foo", Some(DiskPrefix));
1171 t!("d:", Some(DiskPrefix));
1173 t!("ü:\\foo", None);
1174 t!("3:\\foo", None);
1175 t!(" :\\foo", None);
1176 t!("::\\foo", None);
1177 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1178 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1179 t!("\\\\?\\ab:\\", Some(VerbatimPrefix(3)));
1180 t!("\\\\?\\C:\\a", Some(VerbatimDiskPrefix));
1181 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1182 t!("\\\\?\\C:\\a/b", Some(VerbatimDiskPrefix));
1187 let empty: &[u8] = [];
1188 t!(v: Path::new(empty), b!("."));
1189 t!(v: Path::new(b!("\\")), b!("\\"));
1190 t!(v: Path::new(b!("a\\b\\c")), b!("a\\b\\c"));
1192 t!(s: Path::new(""), ".");
1193 t!(s: Path::new("\\"), "\\");
1194 t!(s: Path::new("hi"), "hi");
1195 t!(s: Path::new("hi\\"), "hi");
1196 t!(s: Path::new("\\lib"), "\\lib");
1197 t!(s: Path::new("\\lib\\"), "\\lib");
1198 t!(s: Path::new("hi\\there"), "hi\\there");
1199 t!(s: Path::new("hi\\there.txt"), "hi\\there.txt");
1200 t!(s: Path::new("/"), "\\");
1201 t!(s: Path::new("hi/"), "hi");
1202 t!(s: Path::new("/lib"), "\\lib");
1203 t!(s: Path::new("/lib/"), "\\lib");
1204 t!(s: Path::new("hi/there"), "hi\\there");
1206 t!(s: Path::new("hi\\there\\"), "hi\\there");
1207 t!(s: Path::new("hi\\..\\there"), "there");
1208 t!(s: Path::new("hi/../there"), "there");
1209 t!(s: Path::new("..\\hi\\there"), "..\\hi\\there");
1210 t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there");
1211 t!(s: Path::new("/../hi/there"), "\\hi\\there");
1212 t!(s: Path::new("foo\\.."), ".");
1213 t!(s: Path::new("\\foo\\.."), "\\");
1214 t!(s: Path::new("\\foo\\..\\.."), "\\");
1215 t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar");
1216 t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there");
1217 t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi");
1218 t!(s: Path::new("foo\\..\\.."), "..");
1219 t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
1220 t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
1222 assert_eq!(Path::new(b!("foo\\bar")).into_vec().as_slice(), b!("foo\\bar"));
1223 assert_eq!(Path::new(b!("\\foo\\..\\..\\bar")).into_vec().as_slice(), b!("\\bar"));
1225 t!(s: Path::new("\\\\a"), "\\a");
1226 t!(s: Path::new("\\\\a\\"), "\\a");
1227 t!(s: Path::new("\\\\a\\b"), "\\\\a\\b");
1228 t!(s: Path::new("\\\\a\\b\\"), "\\\\a\\b");
1229 t!(s: Path::new("\\\\a\\b/"), "\\\\a\\b");
1230 t!(s: Path::new("\\\\\\b"), "\\b");
1231 t!(s: Path::new("\\\\a\\\\b"), "\\a\\b");
1232 t!(s: Path::new("\\\\a\\b\\c"), "\\\\a\\b\\c");
1233 t!(s: Path::new("\\\\server\\share/path"), "\\\\server\\share\\path");
1234 t!(s: Path::new("\\\\server/share/path"), "\\\\server\\share\\path");
1235 t!(s: Path::new("C:a\\b.txt"), "C:a\\b.txt");
1236 t!(s: Path::new("C:a/b.txt"), "C:a\\b.txt");
1237 t!(s: Path::new("z:\\a\\b.txt"), "Z:\\a\\b.txt");
1238 t!(s: Path::new("z:/a/b.txt"), "Z:\\a\\b.txt");
1239 t!(s: Path::new("ab:/a/b.txt"), "ab:\\a\\b.txt");
1240 t!(s: Path::new("C:\\"), "C:\\");
1241 t!(s: Path::new("C:"), "C:");
1242 t!(s: Path::new("q:"), "Q:");
1243 t!(s: Path::new("C:/"), "C:\\");
1244 t!(s: Path::new("C:\\foo\\.."), "C:\\");
1245 t!(s: Path::new("C:foo\\.."), "C:");
1246 t!(s: Path::new("C:\\a\\"), "C:\\a");
1247 t!(s: Path::new("C:\\a/"), "C:\\a");
1248 t!(s: Path::new("C:\\a\\b\\"), "C:\\a\\b");
1249 t!(s: Path::new("C:\\a\\b/"), "C:\\a\\b");
1250 t!(s: Path::new("C:a\\"), "C:a");
1251 t!(s: Path::new("C:a/"), "C:a");
1252 t!(s: Path::new("C:a\\b\\"), "C:a\\b");
1253 t!(s: Path::new("C:a\\b/"), "C:a\\b");
1254 t!(s: Path::new("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt");
1255 t!(s: Path::new("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt");
1256 t!(s: Path::new("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt");
1257 t!(s: Path::new("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt");
1258 t!(s: Path::new("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\");
1259 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1260 t!(s: Path::new("\\\\.\\"), "\\\\.\\");
1261 t!(s: Path::new("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo");
1262 t!(s: Path::new("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\");
1263 t!(s: Path::new("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\");
1264 t!(s: Path::new("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\");
1265 t!(s: Path::new("\\\\?\\UNC"), "\\\\?\\UNC");
1267 // I'm not sure whether \\.\foo/bar should normalize to \\.\foo\bar
1268 // as information is sparse and this isn't really googleable.
1269 // I'm going to err on the side of not normalizing it, as this skips the filesystem
1270 t!(s: Path::new("\\\\.\\foo/bar"), "\\\\.\\foo/bar");
1271 t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1275 fn test_opt_paths() {
1276 assert!(Path::new_opt(b!("foo\\bar", 0)) == None);
1277 assert!(Path::new_opt(b!("foo\\bar", 0x80)) == None);
1278 t!(v: Path::new_opt(b!("foo\\bar")).unwrap(), b!("foo\\bar"));
1279 assert!(Path::new_opt("foo\\bar\0") == None);
1280 t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar");
1284 fn test_null_byte() {
1286 let result = task::try(proc() {
1287 Path::new(b!("foo/bar", 0))
1289 assert!(result.is_err());
1291 let result = task::try(proc() {
1292 Path::new("test").set_filename(b!("f", 0, "o"))
1294 assert!(result.is_err());
1296 let result = task::try(proc() {
1297 Path::new("test").push(b!("f", 0, "o"));
1299 assert!(result.is_err());
1304 fn test_not_utf8_fail() {
1305 Path::new(b!("hello", 0x80, ".txt"));
1309 fn test_display_str() {
1310 let path = Path::new("foo");
1311 assert_eq!(path.display().to_str(), "foo".to_owned());
1312 let path = Path::new(b!("\\"));
1313 assert_eq!(path.filename_display().to_str(), "".to_owned());
1315 let path = Path::new("foo");
1316 let mo = path.display().as_maybe_owned();
1317 assert_eq!(mo.as_slice(), "foo");
1318 let path = Path::new(b!("\\"));
1319 let mo = path.filename_display().as_maybe_owned();
1320 assert_eq!(mo.as_slice(), "");
1326 ($path:expr, $exp:expr, $expf:expr) => (
1328 let path = Path::new($path);
1329 let f = format!("{}", path.display());
1330 assert_eq!(f.as_slice(), $exp);
1331 let f = format!("{}", path.filename_display());
1332 assert_eq!(f.as_slice(), $expf);
1337 t!("foo", "foo", "foo");
1338 t!("foo\\bar", "foo\\bar", "bar");
1343 fn test_components() {
1345 (s: $path:expr, $op:ident, $exp:expr) => (
1348 let path = Path::new(path);
1349 assert!(path.$op() == Some($exp));
1352 (s: $path:expr, $op:ident, $exp:expr, opt) => (
1355 let path = Path::new(path);
1356 let left = path.$op();
1357 assert!(left == $exp);
1360 (v: $path:expr, $op:ident, $exp:expr) => (
1363 let path = Path::new(path);
1364 assert!(path.$op() == $exp);
1369 t!(v: b!("a\\b\\c"), filename, Some(b!("c")));
1370 t!(s: "a\\b\\c", filename_str, "c");
1371 t!(s: "\\a\\b\\c", filename_str, "c");
1372 t!(s: "a", filename_str, "a");
1373 t!(s: "\\a", filename_str, "a");
1374 t!(s: ".", filename_str, None, opt);
1375 t!(s: "\\", filename_str, None, opt);
1376 t!(s: "..", filename_str, None, opt);
1377 t!(s: "..\\..", filename_str, None, opt);
1378 t!(s: "c:\\foo.txt", filename_str, "foo.txt");
1379 t!(s: "C:\\", filename_str, None, opt);
1380 t!(s: "C:", filename_str, None, opt);
1381 t!(s: "\\\\server\\share\\foo.txt", filename_str, "foo.txt");
1382 t!(s: "\\\\server\\share", filename_str, None, opt);
1383 t!(s: "\\\\server", filename_str, "server");
1384 t!(s: "\\\\?\\bar\\foo.txt", filename_str, "foo.txt");
1385 t!(s: "\\\\?\\bar", filename_str, None, opt);
1386 t!(s: "\\\\?\\", filename_str, None, opt);
1387 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", filename_str, "foo.txt");
1388 t!(s: "\\\\?\\UNC\\server", filename_str, None, opt);
1389 t!(s: "\\\\?\\UNC\\", filename_str, None, opt);
1390 t!(s: "\\\\?\\C:\\foo.txt", filename_str, "foo.txt");
1391 t!(s: "\\\\?\\C:\\", filename_str, None, opt);
1392 t!(s: "\\\\?\\C:", filename_str, None, opt);
1393 t!(s: "\\\\?\\foo/bar", filename_str, None, opt);
1394 t!(s: "\\\\?\\C:/foo", filename_str, None, opt);
1395 t!(s: "\\\\.\\foo\\bar", filename_str, "bar");
1396 t!(s: "\\\\.\\foo", filename_str, None, opt);
1397 t!(s: "\\\\.\\foo/bar", filename_str, None, opt);
1398 t!(s: "\\\\.\\foo\\bar/baz", filename_str, "bar/baz");
1399 t!(s: "\\\\.\\", filename_str, None, opt);
1400 t!(s: "\\\\?\\a\\b\\", filename_str, "b");
1402 t!(v: b!("a\\b\\c"), dirname, b!("a\\b"));
1403 t!(s: "a\\b\\c", dirname_str, "a\\b");
1404 t!(s: "\\a\\b\\c", dirname_str, "\\a\\b");
1405 t!(s: "a", dirname_str, ".");
1406 t!(s: "\\a", dirname_str, "\\");
1407 t!(s: ".", dirname_str, ".");
1408 t!(s: "\\", dirname_str, "\\");
1409 t!(s: "..", dirname_str, "..");
1410 t!(s: "..\\..", dirname_str, "..\\..");
1411 t!(s: "c:\\foo.txt", dirname_str, "C:\\");
1412 t!(s: "C:\\", dirname_str, "C:\\");
1413 t!(s: "C:", dirname_str, "C:");
1414 t!(s: "C:foo.txt", dirname_str, "C:");
1415 t!(s: "\\\\server\\share\\foo.txt", dirname_str, "\\\\server\\share");
1416 t!(s: "\\\\server\\share", dirname_str, "\\\\server\\share");
1417 t!(s: "\\\\server", dirname_str, "\\");
1418 t!(s: "\\\\?\\bar\\foo.txt", dirname_str, "\\\\?\\bar");
1419 t!(s: "\\\\?\\bar", dirname_str, "\\\\?\\bar");
1420 t!(s: "\\\\?\\", dirname_str, "\\\\?\\");
1421 t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", dirname_str, "\\\\?\\UNC\\server\\share");
1422 t!(s: "\\\\?\\UNC\\server", dirname_str, "\\\\?\\UNC\\server\\");
1423 t!(s: "\\\\?\\UNC\\", dirname_str, "\\\\?\\UNC\\\\");
1424 t!(s: "\\\\?\\C:\\foo.txt", dirname_str, "\\\\?\\C:\\");
1425 t!(s: "\\\\?\\C:\\", dirname_str, "\\\\?\\C:\\");
1426 t!(s: "\\\\?\\C:", dirname_str, "\\\\?\\C:");
1427 t!(s: "\\\\?\\C:/foo/bar", dirname_str, "\\\\?\\C:/foo/bar");
1428 t!(s: "\\\\?\\foo/bar", dirname_str, "\\\\?\\foo/bar");
1429 t!(s: "\\\\.\\foo\\bar", dirname_str, "\\\\.\\foo");
1430 t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
1431 t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
1433 t!(v: b!("hi\\there.txt"), filestem, Some(b!("there")));
1434 t!(s: "hi\\there.txt", filestem_str, "there");
1435 t!(s: "hi\\there", filestem_str, "there");
1436 t!(s: "there.txt", filestem_str, "there");
1437 t!(s: "there", filestem_str, "there");
1438 t!(s: ".", filestem_str, None, opt);
1439 t!(s: "\\", filestem_str, None, opt);
1440 t!(s: "foo\\.bar", filestem_str, ".bar");
1441 t!(s: ".bar", filestem_str, ".bar");
1442 t!(s: "..bar", filestem_str, ".");
1443 t!(s: "hi\\there..txt", filestem_str, "there.");
1444 t!(s: "..", filestem_str, None, opt);
1445 t!(s: "..\\..", filestem_str, None, opt);
1446 // filestem is based on filename, so we don't need the full set of prefix tests
1448 t!(v: b!("hi\\there.txt"), extension, Some(b!("txt")));
1449 t!(v: b!("hi\\there"), extension, None);
1450 t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
1451 t!(s: "hi\\there", extension_str, None, opt);
1452 t!(s: "there.txt", extension_str, Some("txt"), opt);
1453 t!(s: "there", extension_str, None, opt);
1454 t!(s: ".", extension_str, None, opt);
1455 t!(s: "\\", extension_str, None, opt);
1456 t!(s: "foo\\.bar", extension_str, None, opt);
1457 t!(s: ".bar", extension_str, None, opt);
1458 t!(s: "..bar", extension_str, Some("bar"), opt);
1459 t!(s: "hi\\there..txt", extension_str, Some("txt"), opt);
1460 t!(s: "..", extension_str, None, opt);
1461 t!(s: "..\\..", extension_str, None, opt);
1462 // extension is based on filename, so we don't need the full set of prefix tests
1468 (s: $path:expr, $join:expr) => (
1472 let mut p1 = Path::new(path);
1473 let p2 = p1.clone();
1475 assert!(p1 == p2.join(join));
1480 t!(s: "a\\b\\c", "..");
1481 t!(s: "\\a\\b\\c", "d");
1482 t!(s: "a\\b", "c\\d");
1483 t!(s: "a\\b", "\\c\\d");
1484 // this is just a sanity-check test. push and join share an implementation,
1485 // so there's no need for the full set of prefix tests
1487 // we do want to check one odd case though to ensure the prefix is re-parsed
1488 let mut p = Path::new("\\\\?\\C:");
1489 assert!(prefix(&p) == Some(VerbatimPrefix(2)));
1491 assert!(prefix(&p) == Some(VerbatimDiskPrefix));
1492 assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo"));
1494 // and another with verbatim non-normalized paths
1495 let mut p = Path::new("\\\\?\\C:\\a\\");
1497 assert_eq!(p.as_str(), Some("\\\\?\\C:\\a\\foo"));
1501 fn test_push_path() {
1503 (s: $path:expr, $push:expr, $exp:expr) => (
1505 let mut p = Path::new($path);
1506 let push = Path::new($push);
1508 assert_eq!(p.as_str(), Some($exp));
1513 t!(s: "a\\b\\c", "d", "a\\b\\c\\d");
1514 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1515 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1516 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1517 t!(s: "a\\b", ".", "a\\b");
1518 t!(s: "a\\b", "..\\c", "a\\c");
1519 t!(s: "a\\b", "C:a.txt", "C:a.txt");
1520 t!(s: "a\\b", "..\\..\\..\\c", "..\\c");
1521 t!(s: "a\\b", "C:\\a.txt", "C:\\a.txt");
1522 t!(s: "C:\\a", "C:\\b.txt", "C:\\b.txt");
1523 t!(s: "C:\\a\\b\\c", "C:d", "C:\\a\\b\\c\\d");
1524 t!(s: "C:a\\b\\c", "C:d", "C:a\\b\\c\\d");
1525 t!(s: "C:a\\b", "..\\..\\..\\c", "C:..\\c");
1526 t!(s: "C:\\a\\b", "..\\..\\..\\c", "C:\\c");
1527 t!(s: "C:", r"a\b\c", r"C:a\b\c");
1528 t!(s: "C:", r"..\a", r"C:..\a");
1529 t!(s: "\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
1530 t!(s: "\\\\server\\share\\foo", "..\\..\\bar", "\\\\server\\share\\bar");
1531 t!(s: "\\\\server\\share\\foo", "C:baz", "C:baz");
1532 t!(s: "\\\\?\\C:\\a\\b", "C:c\\d", "\\\\?\\C:\\a\\b\\c\\d");
1533 t!(s: "\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
1534 t!(s: "\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
1535 t!(s: "\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
1536 t!(s: "\\\\?\\C:\\a\\b", "..\\..\\..\\c", "\\\\?\\C:\\a\\b\\..\\..\\..\\c");
1537 t!(s: "\\\\?\\foo\\bar", "..\\..\\c", "\\\\?\\foo\\bar\\..\\..\\c");
1538 t!(s: "\\\\?\\", "foo", "\\\\?\\\\foo");
1539 t!(s: "\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
1540 t!(s: "\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
1541 t!(s: "\\\\?\\UNC\\server\\share", "C:a", "C:a");
1542 t!(s: "\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\\\foo");
1543 t!(s: "C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
1544 t!(s: "\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
1545 t!(s: "\\\\.\\foo\\bar", "C:a", "C:a");
1546 // again, not sure about the following, but I'm assuming \\.\ should be verbatim
1547 t!(s: "\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
1549 t!(s: "\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
1553 fn test_push_many() {
1555 (s: $path:expr, $push:expr, $exp:expr) => (
1557 let mut p = Path::new($path);
1559 assert_eq!(p.as_str(), Some($exp));
1562 (v: $path:expr, $push:expr, $exp:expr) => (
1564 let mut p = Path::new($path);
1566 assert_eq!(p.as_vec(), $exp);
1571 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1572 t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
1573 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1574 t!(s: "a\\b\\c", ["d".to_owned(), "e".to_owned()], "a\\b\\c\\d\\e");
1575 t!(v: b!("a\\b\\c"), [b!("d"), b!("e")], b!("a\\b\\c\\d\\e"));
1576 t!(v: b!("a\\b\\c"), [b!("d"), b!("\\e"), b!("f")], b!("\\e\\f"));
1577 t!(v: b!("a\\b\\c"), [Vec::from_slice(b!("d")), Vec::from_slice(b!("e"))],
1578 b!("a\\b\\c\\d\\e"));
1584 (s: $path:expr, $left:expr, $right:expr) => (
1587 let mut p = Path::new(pstr);
1588 let result = p.pop();
1590 assert!(p.as_str() == Some(left),
1591 "`{}`.pop() failed; expected remainder `{}`, found `{}`",
1592 pstr, left, p.as_str().unwrap());
1593 assert!(result == $right);
1596 (v: [$($path:expr),+], [$($left:expr),+], $right:expr) => (
1598 let mut p = Path::new(b!($($path),+));
1599 let result = p.pop();
1600 assert_eq!(p.as_vec(), b!($($left),+));
1601 assert!(result == $right);
1606 t!(s: "a\\b\\c", "a\\b", true);
1607 t!(s: "a", ".", true);
1608 t!(s: ".", ".", false);
1609 t!(s: "\\a", "\\", true);
1610 t!(s: "\\", "\\", false);
1611 t!(v: ["a\\b\\c"], ["a\\b"], true);
1612 t!(v: ["a"], ["."], true);
1613 t!(v: ["."], ["."], false);
1614 t!(v: ["\\a"], ["\\"], true);
1615 t!(v: ["\\"], ["\\"], false);
1617 t!(s: "C:\\a\\b", "C:\\a", true);
1618 t!(s: "C:\\a", "C:\\", true);
1619 t!(s: "C:\\", "C:\\", false);
1620 t!(s: "C:a\\b", "C:a", true);
1621 t!(s: "C:a", "C:", true);
1622 t!(s: "C:", "C:", false);
1623 t!(s: "\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1624 t!(s: "\\\\server\\share\\a", "\\\\server\\share", true);
1625 t!(s: "\\\\server\\share", "\\\\server\\share", false);
1626 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1627 t!(s: "\\\\?\\a\\b", "\\\\?\\a", true);
1628 t!(s: "\\\\?\\a", "\\\\?\\a", false);
1629 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1630 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1631 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\", false);
1632 t!(s: "\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1633 t!(s: "\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share", true);
1634 t!(s: "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1635 t!(s: "\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1636 t!(s: "\\\\.\\a\\b", "\\\\.\\a", true);
1637 t!(s: "\\\\.\\a", "\\\\.\\a", false);
1639 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a", true);
1643 fn test_root_path() {
1644 assert!(Path::new("a\\b\\c").root_path() == None);
1645 assert!(Path::new("\\a\\b\\c").root_path() == Some(Path::new("\\")));
1646 assert!(Path::new("C:a").root_path() == Some(Path::new("C:")));
1647 assert!(Path::new("C:\\a").root_path() == Some(Path::new("C:\\")));
1648 assert!(Path::new("\\\\a\\b\\c").root_path() == Some(Path::new("\\\\a\\b")));
1649 assert!(Path::new("\\\\?\\a\\b").root_path() == Some(Path::new("\\\\?\\a")));
1650 assert!(Path::new("\\\\?\\C:\\a").root_path() == Some(Path::new("\\\\?\\C:\\")));
1651 assert!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path() ==
1652 Some(Path::new("\\\\?\\UNC\\a\\b")));
1653 assert!(Path::new("\\\\.\\a\\b").root_path() == Some(Path::new("\\\\.\\a")));
1658 t!(s: Path::new("a\\b\\c").join(".."), "a\\b");
1659 t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d");
1660 t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d");
1661 t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
1662 t!(s: Path::new(".").join("a\\b"), "a\\b");
1663 t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
1664 t!(v: Path::new(b!("a\\b\\c")).join(b!("..")), b!("a\\b"));
1665 t!(v: Path::new(b!("\\a\\b\\c")).join(b!("d")), b!("\\a\\b\\c\\d"));
1666 // full join testing is covered under test_push_path, so no need for
1667 // the full set of prefix tests
1671 fn test_join_path() {
1673 (s: $path:expr, $join:expr, $exp:expr) => (
1675 let path = Path::new($path);
1676 let join = Path::new($join);
1677 let res = path.join(&join);
1678 assert_eq!(res.as_str(), Some($exp));
1683 t!(s: "a\\b\\c", "..", "a\\b");
1684 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1685 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1686 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1687 t!(s: ".", "a\\b", "a\\b");
1688 t!(s: "\\", "a\\b", "\\a\\b");
1689 // join is implemented using push, so there's no need for
1690 // the full set of prefix tests
1694 fn test_join_many() {
1696 (s: $path:expr, $join:expr, $exp:expr) => (
1698 let path = Path::new($path);
1699 let res = path.join_many($join);
1700 assert_eq!(res.as_str(), Some($exp));
1703 (v: $path:expr, $join:expr, $exp:expr) => (
1705 let path = Path::new($path);
1706 let res = path.join_many($join);
1707 assert_eq!(res.as_vec(), $exp);
1712 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1713 t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
1714 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1715 t!(s: "a\\b\\c", ["d".to_owned(), "e".to_owned()], "a\\b\\c\\d\\e");
1716 t!(v: b!("a\\b\\c"), [b!("d"), b!("e")], b!("a\\b\\c\\d\\e"));
1717 t!(v: b!("a\\b\\c"), [Vec::from_slice(b!("d")), Vec::from_slice(b!("e"))],
1718 b!("a\\b\\c\\d\\e"));
1722 fn test_with_helpers() {
1724 (s: $path:expr, $op:ident, $arg:expr, $res:expr) => (
1727 let path = Path::new(pstr);
1729 let res = path.$op(arg);
1731 assert!(res.as_str() == Some(exp),
1732 "`{}`.{}(\"{}\"): Expected `{}`, found `{}`",
1733 pstr, stringify!($op), arg, exp, res.as_str().unwrap());
1738 t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d");
1739 t!(s: ".", with_filename, "foo", "foo");
1740 t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d");
1741 t!(s: "\\", with_filename, "foo", "\\foo");
1742 t!(s: "\\a", with_filename, "foo", "\\foo");
1743 t!(s: "foo", with_filename, "bar", "bar");
1744 t!(s: "\\", with_filename, "foo\\", "\\foo");
1745 t!(s: "\\a", with_filename, "foo\\", "\\foo");
1746 t!(s: "a\\b\\c", with_filename, "", "a\\b");
1747 t!(s: "a\\b\\c", with_filename, ".", "a\\b");
1748 t!(s: "a\\b\\c", with_filename, "..", "a");
1749 t!(s: "\\a", with_filename, "", "\\");
1750 t!(s: "foo", with_filename, "", ".");
1751 t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e");
1752 t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d");
1753 t!(s: "..", with_filename, "foo", "..\\foo");
1754 t!(s: "..\\..", with_filename, "foo", "..\\..\\foo");
1755 t!(s: "..", with_filename, "", "..");
1756 t!(s: "..\\..", with_filename, "", "..\\..");
1757 t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz");
1758 t!(s: "C:\\foo", with_filename, "bar", "C:\\bar");
1759 t!(s: "C:\\", with_filename, "foo", "C:\\foo");
1760 t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz");
1761 t!(s: "C:foo", with_filename, "bar", "C:bar");
1762 t!(s: "C:", with_filename, "foo", "C:foo");
1763 t!(s: "C:\\foo", with_filename, "", "C:\\");
1764 t!(s: "C:foo", with_filename, "", "C:");
1765 t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\");
1766 t!(s: "C:\\foo", with_filename, "..", "C:\\");
1767 t!(s: "C:\\", with_filename, "..", "C:\\");
1768 t!(s: "C:foo\\bar", with_filename, "..", "C:");
1769 t!(s: "C:foo", with_filename, "..", "C:..");
1770 t!(s: "C:", with_filename, "..", "C:..");
1771 t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar");
1772 t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo");
1773 t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share");
1774 t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share");
1775 t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share");
1776 t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share");
1777 t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz");
1778 t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar");
1779 t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo");
1780 t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\..");
1781 t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz");
1782 t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar");
1783 t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo");
1784 t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\..");
1785 t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz");
1786 t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar");
1787 t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\..");
1789 t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe");
1790 t!(s: "hi\\there.txt", with_extension, "", "hi\\there");
1791 t!(s: "hi\\there.txt", with_extension, ".", "hi\\there..");
1792 t!(s: "hi\\there.txt", with_extension, "..", "hi\\there...");
1793 t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt");
1794 t!(s: "hi\\there", with_extension, ".", "hi\\there..");
1795 t!(s: "hi\\there", with_extension, "..", "hi\\there...");
1796 t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt");
1797 t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt");
1798 t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo");
1799 t!(s: "\\", with_extension, "txt", "\\");
1800 t!(s: "\\", with_extension, ".", "\\");
1801 t!(s: "\\", with_extension, "..", "\\");
1802 t!(s: ".", with_extension, "txt", ".");
1803 // extension setter calls filename setter internally, no need for extended tests
1809 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1813 let mut p1 = Path::new(path);
1815 let p2 = Path::new(path);
1816 assert!(p1 == p2.$with(arg));
1819 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1823 let mut p1 = Path::new(path);
1825 let p2 = Path::new(path);
1826 assert!(p1 == p2.$with(arg));
1831 t!(v: b!("a\\b\\c"), set_filename, with_filename, b!("d"));
1832 t!(v: b!("\\"), set_filename, with_filename, b!("foo"));
1833 t!(s: "a\\b\\c", set_filename, with_filename, "d");
1834 t!(s: "\\", set_filename, with_filename, "foo");
1835 t!(s: ".", set_filename, with_filename, "foo");
1836 t!(s: "a\\b", set_filename, with_filename, "");
1837 t!(s: "a", set_filename, with_filename, "");
1839 t!(v: b!("hi\\there.txt"), set_extension, with_extension, b!("exe"));
1840 t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
1841 t!(s: "hi\\there.", set_extension, with_extension, "txt");
1842 t!(s: "hi\\there", set_extension, with_extension, "txt");
1843 t!(s: "hi\\there.txt", set_extension, with_extension, "");
1844 t!(s: "hi\\there", set_extension, with_extension, "");
1845 t!(s: ".", set_extension, with_extension, "txt");
1847 // with_ helpers use the setter internally, so the tests for the with_ helpers
1848 // will suffice. No need for the full set of prefix tests.
1854 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1857 let filename = $filename;
1858 assert!(path.filename_str() == filename,
1859 "`{}`.filename_str(): Expected `{:?}`, found `{:?}`",
1860 path.as_str().unwrap(), filename, path.filename_str());
1861 let dirname = $dirname;
1862 assert!(path.dirname_str() == dirname,
1863 "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
1864 path.as_str().unwrap(), dirname, path.dirname_str());
1865 let filestem = $filestem;
1866 assert!(path.filestem_str() == filestem,
1867 "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
1868 path.as_str().unwrap(), filestem, path.filestem_str());
1870 assert!(path.extension_str() == ext,
1871 "`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
1872 path.as_str().unwrap(), ext, path.extension_str());
1875 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1878 assert!(path.filename() == $filename);
1879 assert!(path.dirname() == $dirname);
1880 assert!(path.filestem() == $filestem);
1881 assert!(path.extension() == $ext);
1886 t!(v: Path::new(b!("a\\b\\c")), Some(b!("c")), b!("a\\b"), Some(b!("c")), None);
1887 t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), 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("..\\.."), None, Some("..\\.."), None, None);
1892 t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"),
1893 Some("there"), Some("txt"));
1894 t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None);
1895 t!(s: Path::new("hi\\there."), Some("there."), Some("hi"),
1896 Some("there"), Some(""));
1897 t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None);
1898 t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"),
1899 Some("."), Some("there"));
1901 // these are already tested in test_components, so no need for extended tests
1905 fn test_dir_path() {
1906 t!(s: Path::new("hi\\there").dir_path(), "hi");
1907 t!(s: Path::new("hi").dir_path(), ".");
1908 t!(s: Path::new("\\hi").dir_path(), "\\");
1909 t!(s: Path::new("\\").dir_path(), "\\");
1910 t!(s: Path::new("..").dir_path(), "..");
1911 t!(s: Path::new("..\\..").dir_path(), "..\\..");
1913 // dir_path is just dirname interpreted as a path.
1914 // No need for extended tests
1918 fn test_is_absolute() {
1920 ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => (
1922 let path = Path::new($path);
1923 let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel);
1924 let b = path.is_absolute();
1925 assert!(b == abs, "Path '{}'.is_absolute(): expected {:?}, found {:?}",
1926 path.as_str().unwrap(), abs, b);
1927 let b = is_vol_relative(&path);
1928 assert!(b == vol, "is_vol_relative('{}'): expected {:?}, found {:?}",
1929 path.as_str().unwrap(), vol, b);
1930 let b = is_cwd_relative(&path);
1931 assert!(b == cwd, "is_cwd_relative('{}'): expected {:?}, found {:?}",
1932 path.as_str().unwrap(), cwd, b);
1933 let b = path.is_relative();
1934 assert!(b == rel, "Path '{}'.is_relativf(): expected {:?}, found {:?}",
1935 path.as_str().unwrap(), rel, b);
1939 t!("a\\b\\c", false, false, false, true);
1940 t!("\\a\\b\\c", false, true, false, false);
1941 t!("a", false, false, false, true);
1942 t!("\\a", false, true, false, false);
1943 t!(".", false, false, false, true);
1944 t!("\\", false, true, false, false);
1945 t!("..", false, false, false, true);
1946 t!("..\\..", false, false, false, true);
1947 t!("C:a\\b.txt", false, false, true, false);
1948 t!("C:\\a\\b.txt", true, false, false, false);
1949 t!("\\\\server\\share\\a\\b.txt", true, false, false, false);
1950 t!("\\\\?\\a\\b\\c.txt", true, false, false, false);
1951 t!("\\\\?\\C:\\a\\b.txt", true, false, false, false);
1952 t!("\\\\?\\C:a\\b.txt", true, false, false, false); // NB: not equivalent to C:a\b.txt
1953 t!("\\\\?\\UNC\\server\\share\\a\\b.txt", true, false, false, false);
1954 t!("\\\\.\\a\\b", true, false, false, false);
1958 fn test_is_ancestor_of() {
1960 (s: $path:expr, $dest:expr, $exp:expr) => (
1962 let path = Path::new($path);
1963 let dest = Path::new($dest);
1965 let res = path.is_ancestor_of(&dest);
1967 "`{}`.is_ancestor_of(`{}`): Expected {:?}, found {:?}",
1968 path.as_str().unwrap(), dest.as_str().unwrap(), exp, res);
1973 t!(s: "a\\b\\c", "a\\b\\c\\d", true);
1974 t!(s: "a\\b\\c", "a\\b\\c", true);
1975 t!(s: "a\\b\\c", "a\\b", false);
1976 t!(s: "\\a\\b\\c", "\\a\\b\\c", true);
1977 t!(s: "\\a\\b", "\\a\\b\\c", true);
1978 t!(s: "\\a\\b\\c\\d", "\\a\\b\\c", false);
1979 t!(s: "\\a\\b", "a\\b\\c", false);
1980 t!(s: "a\\b", "\\a\\b\\c", false);
1981 t!(s: "a\\b\\c", "a\\b\\d", false);
1982 t!(s: "..\\a\\b\\c", "a\\b\\c", false);
1983 t!(s: "a\\b\\c", "..\\a\\b\\c", false);
1984 t!(s: "a\\b\\c", "a\\b\\cd", false);
1985 t!(s: "a\\b\\cd", "a\\b\\c", false);
1986 t!(s: "..\\a\\b", "..\\a\\b\\c", true);
1987 t!(s: ".", "a\\b", true);
1988 t!(s: ".", ".", true);
1989 t!(s: "\\", "\\", true);
1990 t!(s: "\\", "\\a\\b", true);
1991 t!(s: "..", "a\\b", true);
1992 t!(s: "..\\..", "a\\b", true);
1993 t!(s: "foo\\bar", "foobar", false);
1994 t!(s: "foobar", "foo\\bar", false);
1996 t!(s: "foo", "C:foo", false);
1997 t!(s: "C:foo", "foo", false);
1998 t!(s: "C:foo", "C:foo\\bar", true);
1999 t!(s: "C:foo\\bar", "C:foo", false);
2000 t!(s: "C:\\foo", "C:\\foo\\bar", true);
2001 t!(s: "C:", "C:", true);
2002 t!(s: "C:", "C:\\", false);
2003 t!(s: "C:\\", "C:", false);
2004 t!(s: "C:\\", "C:\\", true);
2005 t!(s: "C:\\foo\\bar", "C:\\foo", false);
2006 t!(s: "C:foo\\bar", "C:foo", false);
2007 t!(s: "C:\\foo", "\\foo", false);
2008 t!(s: "\\foo", "C:\\foo", false);
2009 t!(s: "\\\\server\\share\\foo", "\\\\server\\share\\foo\\bar", true);
2010 t!(s: "\\\\server\\share", "\\\\server\\share\\foo", true);
2011 t!(s: "\\\\server\\share\\foo", "\\\\server\\share", false);
2012 t!(s: "C:\\foo", "\\\\server\\share\\foo", false);
2013 t!(s: "\\\\server\\share\\foo", "C:\\foo", false);
2014 t!(s: "\\\\?\\foo\\bar", "\\\\?\\foo\\bar\\baz", true);
2015 t!(s: "\\\\?\\foo\\bar\\baz", "\\\\?\\foo\\bar", false);
2016 t!(s: "\\\\?\\foo\\bar", "\\foo\\bar\\baz", false);
2017 t!(s: "\\foo\\bar", "\\\\?\\foo\\bar\\baz", false);
2018 t!(s: "\\\\?\\C:\\foo\\bar", "\\\\?\\C:\\foo\\bar\\baz", true);
2019 t!(s: "\\\\?\\C:\\foo\\bar\\baz", "\\\\?\\C:\\foo\\bar", false);
2020 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\foo", true);
2021 t!(s: "\\\\?\\C:", "\\\\?\\C:\\", false); // this is a weird one
2022 t!(s: "\\\\?\\C:\\", "\\\\?\\C:", false);
2023 t!(s: "\\\\?\\C:\\a", "\\\\?\\c:\\a\\b", true);
2024 t!(s: "\\\\?\\c:\\a", "\\\\?\\C:\\a\\b", true);
2025 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a\\b", false);
2026 t!(s: "\\\\?\\foo", "\\\\?\\foobar", false);
2027 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", true);
2028 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\", true);
2029 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a\\b", true);
2030 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", false);
2031 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b\\", false);
2032 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c\\d", true);
2033 t!(s: "\\\\?\\UNC\\a\\b\\c\\d", "\\\\?\\UNC\\a\\b\\c", false);
2034 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", true);
2035 t!(s: "\\\\.\\foo\\bar", "\\\\.\\foo\\bar\\baz", true);
2036 t!(s: "\\\\.\\foo\\bar\\baz", "\\\\.\\foo\\bar", false);
2037 t!(s: "\\\\.\\foo", "\\\\.\\foo\\bar", true);
2038 t!(s: "\\\\.\\foo", "\\\\.\\foobar", false);
2040 t!(s: "\\a\\b", "\\\\?\\a\\b", false);
2041 t!(s: "\\\\?\\a\\b", "\\a\\b", false);
2042 t!(s: "\\a\\b", "\\\\?\\C:\\a\\b", false);
2043 t!(s: "\\\\?\\C:\\a\\b", "\\a\\b", false);
2044 t!(s: "Z:\\a\\b", "\\\\?\\z:\\a\\b", true);
2045 t!(s: "C:\\a\\b", "\\\\?\\D:\\a\\b", false);
2046 t!(s: "a\\b", "\\\\?\\a\\b", false);
2047 t!(s: "\\\\?\\a\\b", "a\\b", false);
2048 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b", true);
2049 t!(s: "\\\\?\\C:\\a\\b", "C:\\a\\b", true);
2050 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", false);
2051 t!(s: "C:a\\b", "\\\\?\\C:a\\b", false);
2052 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", false);
2053 t!(s: "\\\\?\\C:a\\b", "C:a\\b", false);
2054 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b\\", true);
2055 t!(s: "\\\\?\\C:\\a\\b\\", "C:\\a\\b", true);
2056 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c", true);
2057 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b\\c", true);
2061 fn test_ends_with_path() {
2063 (s: $path:expr, $child:expr, $exp:expr) => (
2065 let path = Path::new($path);
2066 let child = Path::new($child);
2067 assert_eq!(path.ends_with_path(&child), $exp);
2072 t!(s: "a\\b\\c", "c", true);
2073 t!(s: "a\\b\\c", "d", false);
2074 t!(s: "foo\\bar\\quux", "bar", false);
2075 t!(s: "foo\\bar\\quux", "barquux", false);
2076 t!(s: "a\\b\\c", "b\\c", true);
2077 t!(s: "a\\b\\c", "a\\b\\c", true);
2078 t!(s: "a\\b\\c", "foo\\a\\b\\c", false);
2079 t!(s: "\\a\\b\\c", "a\\b\\c", true);
2080 t!(s: "\\a\\b\\c", "\\a\\b\\c", false); // child must be relative
2081 t!(s: "\\a\\b\\c", "foo\\a\\b\\c", false);
2082 t!(s: "a\\b\\c", "", false);
2083 t!(s: "", "", true);
2084 t!(s: "\\a\\b\\c", "d\\e\\f", false);
2085 t!(s: "a\\b\\c", "a\\b", false);
2086 t!(s: "a\\b\\c", "b", false);
2087 t!(s: "C:\\a\\b", "b", true);
2088 t!(s: "C:\\a\\b", "C:b", false);
2089 t!(s: "C:\\a\\b", "C:a\\b", false);
2093 fn test_path_relative_from() {
2095 (s: $path:expr, $other:expr, $exp:expr) => (
2097 let path = Path::new($path);
2098 let other = Path::new($other);
2099 let res = path.path_relative_from(&other);
2101 assert!(res.as_ref().and_then(|x| x.as_str()) == exp,
2102 "`{}`.path_relative_from(`{}`): Expected {:?}, got {:?}",
2103 path.as_str().unwrap(), other.as_str().unwrap(), exp,
2104 res.as_ref().and_then(|x| x.as_str()));
2109 t!(s: "a\\b\\c", "a\\b", Some("c"));
2110 t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c"));
2111 t!(s: "a\\b\\c", "a\\b\\c\\d", Some(".."));
2112 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2113 t!(s: "a\\b\\c", "a\\b\\c\\d\\e", Some("..\\.."));
2114 t!(s: "a\\b\\c", "a\\d\\e", Some("..\\..\\b\\c"));
2115 t!(s: "a\\b\\c", "d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2116 t!(s: "a\\b\\c", "\\a\\b\\c", None);
2117 t!(s: "\\a\\b\\c", "a\\b\\c", Some("\\a\\b\\c"));
2118 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d", Some(".."));
2119 t!(s: "\\a\\b\\c", "\\a\\b", Some("c"));
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: "hi\\there.txt", "hi\\there", Some("..\\there.txt"));
2124 t!(s: ".", "a", Some(".."));
2125 t!(s: ".", "a\\b", Some("..\\.."));
2126 t!(s: ".", ".", Some("."));
2127 t!(s: "a", ".", Some("a"));
2128 t!(s: "a\\b", ".", Some("a\\b"));
2129 t!(s: "..", ".", Some(".."));
2130 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2131 t!(s: "\\a\\b\\c", "\\a\\b\\c", Some("."));
2132 t!(s: "\\", "\\", Some("."));
2133 t!(s: "\\", ".", Some("\\"));
2134 t!(s: "..\\..\\a", "b", Some("..\\..\\..\\a"));
2135 t!(s: "a", "..\\..\\b", None);
2136 t!(s: "..\\..\\a", "..\\..\\b", Some("..\\a"));
2137 t!(s: "..\\..\\a", "..\\..\\a\\b", Some(".."));
2138 t!(s: "..\\..\\a\\b", "..\\..\\a", Some("b"));
2140 t!(s: "C:a\\b\\c", "C:a\\b", Some("c"));
2141 t!(s: "C:a\\b", "C:a\\b\\c", Some(".."));
2142 t!(s: "C:" ,"C:a\\b", Some("..\\.."));
2143 t!(s: "C:a\\b", "C:c\\d", Some("..\\..\\a\\b"));
2144 t!(s: "C:a\\b", "D:c\\d", Some("C:a\\b"));
2145 t!(s: "C:a\\b", "C:..\\c", None);
2146 t!(s: "C:..\\a", "C:b\\c", Some("..\\..\\..\\a"));
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", "C:a\\b", Some("C:\\a\\b"));
2152 t!(s: "C:a\\b", "C:\\a\\b", None);
2153 t!(s: "\\a\\b", "C:\\a\\b", None);
2154 t!(s: "\\a\\b", "C:a\\b", None);
2155 t!(s: "a\\b", "C:\\a\\b", None);
2156 t!(s: "a\\b", "C:a\\b", None);
2158 t!(s: "\\\\a\\b\\c", "\\\\a\\b", Some("c"));
2159 t!(s: "\\\\a\\b", "\\\\a\\b\\c", Some(".."));
2160 t!(s: "\\\\a\\b\\c\\e", "\\\\a\\b\\c\\d", Some("..\\e"));
2161 t!(s: "\\\\a\\c\\d", "\\\\a\\b\\d", Some("\\\\a\\c\\d"));
2162 t!(s: "\\\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\b\\c\\d"));
2163 t!(s: "\\\\a\\b\\c", "\\d\\e", Some("\\\\a\\b\\c"));
2164 t!(s: "\\d\\e", "\\\\a\\b\\c", None);
2165 t!(s: "d\\e", "\\\\a\\b\\c", None);
2166 t!(s: "C:\\a\\b\\c", "\\\\a\\b\\c", Some("C:\\a\\b\\c"));
2167 t!(s: "C:\\c", "\\\\a\\b\\c", Some("C:\\c"));
2169 t!(s: "\\\\?\\a\\b", "\\a\\b", Some("\\\\?\\a\\b"));
2170 t!(s: "\\\\?\\a\\b", "a\\b", Some("\\\\?\\a\\b"));
2171 t!(s: "\\\\?\\a\\b", "\\b", Some("\\\\?\\a\\b"));
2172 t!(s: "\\\\?\\a\\b", "b", Some("\\\\?\\a\\b"));
2173 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", Some(".."));
2174 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", Some("c"));
2175 t!(s: "\\\\?\\a\\b", "\\\\?\\c\\d", Some("\\\\?\\a\\b"));
2176 t!(s: "\\\\?\\a", "\\\\?\\b", Some("\\\\?\\a"));
2178 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2179 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2180 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\b", Some("..\\a"));
2181 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a", Some("\\\\?\\C:\\a"));
2182 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\c:\\a", Some("b"));
2183 t!(s: "\\\\?\\C:\\a\\b", "C:\\a", Some("b"));
2184 t!(s: "\\\\?\\C:\\a", "C:\\a\\b", Some(".."));
2185 t!(s: "C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2186 t!(s: "C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2187 t!(s: "\\\\?\\C:\\a", "D:\\a", Some("\\\\?\\C:\\a"));
2188 t!(s: "\\\\?\\c:\\a\\b", "C:\\a", Some("b"));
2189 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", Some("\\\\?\\C:\\a\\b"));
2190 t!(s: "\\\\?\\C:\\a\\.\\b", "C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2191 t!(s: "\\\\?\\C:\\a\\b/c", "C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2192 t!(s: "\\\\?\\C:\\a\\..\\b", "C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2193 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", None);
2194 t!(s: "\\\\?\\C:\\a\\.\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2195 t!(s: "\\\\?\\C:\\a\\b/c", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2196 t!(s: "\\\\?\\C:\\a\\..\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2197 t!(s: "\\\\?\\C:\\a\\b\\", "\\\\?\\C:\\a", Some("b"));
2198 t!(s: "\\\\?\\C:\\.\\b", "\\\\?\\C:\\.", Some("b"));
2199 t!(s: "C:\\b", "\\\\?\\C:\\.", Some("..\\b"));
2200 t!(s: "\\\\?\\a\\.\\b\\c", "\\\\?\\a\\.\\b", Some("c"));
2201 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\.\\d", Some("..\\..\\b\\c"));
2202 t!(s: "\\\\?\\a\\..\\b", "\\\\?\\a\\..", Some("b"));
2203 t!(s: "\\\\?\\a\\b\\..", "\\\\?\\a\\b", Some("\\\\?\\a\\b\\.."));
2204 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\..\\b", Some("..\\..\\b\\c"));
2206 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2207 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", Some(".."));
2208 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2209 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2210 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2211 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\C:\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2212 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2213 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2214 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2215 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b", Some("c"));
2216 t!(s: "\\\\?\\UNC\\a\\b", "\\\\a\\b\\c", Some(".."));
2217 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2218 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2219 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2220 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2221 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2222 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2223 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\a\\b\\c"));
2227 fn test_str_components() {
2229 (s: $path:expr, $exp:expr) => (
2231 let path = Path::new($path);
2232 let comps = path.str_components().map(|x|x.unwrap())
2233 .collect::<Vec<&str>>();
2234 let exp: &[&str] = $exp;
2235 assert_eq!(comps.as_slice(), exp);
2236 let comps = path.str_components().rev().map(|x|x.unwrap())
2237 .collect::<Vec<&str>>();
2238 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>();
2239 assert_eq!(comps, exp);
2242 (v: [$($arg:expr),+], $exp:expr) => (
2244 let path = Path::new(b!($($arg),+));
2245 let comps = path.str_components().map(|x|x.unwrap()).collect::<Vec<&str>>();
2246 let exp: &[&str] = $exp;
2247 assert_eq!(comps.as_slice(), exp);
2248 let comps = path.str_components().rev().map(|x|x.unwrap())
2249 .collect::<Vec<&str>>();
2250 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>();
2251 assert_eq!(comps, exp);
2256 t!(v: ["a\\b\\c"], ["a", "b", "c"]);
2257 t!(s: "a\\b\\c", ["a", "b", "c"]);
2258 t!(s: "a\\b\\d", ["a", "b", "d"]);
2259 t!(s: "a\\b\\cd", ["a", "b", "cd"]);
2260 t!(s: "\\a\\b\\c", ["a", "b", "c"]);
2262 t!(s: "\\a", ["a"]);
2265 t!(s: "..", [".."]);
2266 t!(s: "..\\..", ["..", ".."]);
2267 t!(s: "..\\..\\foo", ["..", "..", "foo"]);
2268 t!(s: "C:foo\\bar", ["foo", "bar"]);
2269 t!(s: "C:foo", ["foo"]);
2271 t!(s: "C:\\foo\\bar", ["foo", "bar"]);
2272 t!(s: "C:\\foo", ["foo"]);
2274 t!(s: "\\\\server\\share\\foo\\bar", ["foo", "bar"]);
2275 t!(s: "\\\\server\\share\\foo", ["foo"]);
2276 t!(s: "\\\\server\\share", []);
2277 t!(s: "\\\\?\\foo\\bar\\baz", ["bar", "baz"]);
2278 t!(s: "\\\\?\\foo\\bar", ["bar"]);
2279 t!(s: "\\\\?\\foo", []);
2280 t!(s: "\\\\?\\", []);
2281 t!(s: "\\\\?\\a\\b", ["b"]);
2282 t!(s: "\\\\?\\a\\b\\", ["b"]);
2283 t!(s: "\\\\?\\foo\\bar\\\\baz", ["bar", "", "baz"]);
2284 t!(s: "\\\\?\\C:\\foo\\bar", ["foo", "bar"]);
2285 t!(s: "\\\\?\\C:\\foo", ["foo"]);
2286 t!(s: "\\\\?\\C:\\", []);
2287 t!(s: "\\\\?\\C:\\foo\\", ["foo"]);
2288 t!(s: "\\\\?\\UNC\\server\\share\\foo\\bar", ["foo", "bar"]);
2289 t!(s: "\\\\?\\UNC\\server\\share\\foo", ["foo"]);
2290 t!(s: "\\\\?\\UNC\\server\\share", []);
2291 t!(s: "\\\\.\\foo\\bar\\baz", ["bar", "baz"]);
2292 t!(s: "\\\\.\\foo\\bar", ["bar"]);
2293 t!(s: "\\\\.\\foo", []);
2297 fn test_components_iter() {
2299 (s: $path:expr, $exp:expr) => (
2301 let path = Path::new($path);
2302 let comps = path.components().collect::<Vec<&[u8]>>();
2303 let exp: &[&[u8]] = $exp;
2304 assert_eq!(comps.as_slice(), exp);
2305 let comps = path.components().rev().collect::<Vec<&[u8]>>();
2306 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
2307 assert_eq!(comps, exp);
2312 t!(s: "a\\b\\c", [b!("a"), b!("b"), b!("c")]);
2313 t!(s: ".", [b!(".")]);
2314 // since this is really a wrapper around str_components, those tests suffice
2318 fn test_make_non_verbatim() {
2320 ($path:expr, $exp:expr) => (
2322 let path = Path::new($path);
2323 let exp: Option<&str> = $exp;
2324 let exp = exp.map(|s| Path::new(s));
2325 assert!(make_non_verbatim(&path) == exp);
2330 t!(r"\a\b\c", Some(r"\a\b\c"));
2331 t!(r"a\b\c", Some(r"a\b\c"));
2332 t!(r"C:\a\b\c", Some(r"C:\a\b\c"));
2333 t!(r"C:a\b\c", Some(r"C:a\b\c"));
2334 t!(r"\\server\share\foo", Some(r"\\server\share\foo"));
2335 t!(r"\\.\foo", None);
2336 t!(r"\\?\foo", None);
2337 t!(r"\\?\C:", None);
2338 t!(r"\\?\C:foo", None);
2339 t!(r"\\?\C:\", Some(r"C:\"));
2340 t!(r"\\?\C:\foo", Some(r"C:\foo"));
2341 t!(r"\\?\C:\foo\bar\baz", Some(r"C:\foo\bar\baz"));
2342 t!(r"\\?\C:\foo\.\bar\baz", None);
2343 t!(r"\\?\C:\foo\bar\..\baz", None);
2344 t!(r"\\?\C:\foo\bar\..", None);
2345 t!(r"\\?\UNC\server\share\foo", Some(r"\\server\share\foo"));
2346 t!(r"\\?\UNC\server\share", Some(r"\\server\share"));
2347 t!(r"\\?\UNC\server", None);
2348 t!(r"\\?\UNC\server\", None);