1 // Copyright 2013 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};
17 use container::Container;
18 use cmp::{Eq, TotalEq};
19 use from_str::FromStr;
21 use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Rev, Iterator, Map};
22 use option::{Option, Some, None};
23 use slice::{Vector, OwnedVector, ImmutableVector};
24 use str::{CharSplits, OwnedStr, Str, StrVector, StrSlice};
27 use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe};
29 /// Iterator that yields successive components of a Path as &str
31 /// Each component is yielded as Option<&str> for compatibility with PosixPath, but
32 /// every component in WindowsPath is guaranteed to be Some.
33 pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>,
34 CharSplits<'a, char>>;
35 /// Iterator that yields components of a Path in reverse as &str
37 /// Each component is yielded as Option<&str> for compatibility with PosixPath, but
38 /// every component in WindowsPath is guaranteed to be Some.
39 pub type RevStrComponents<'a> = Rev<Map<'a, &'a str, Option<&'a str>,
40 CharSplits<'a, char>>>;
42 /// Iterator that yields successive components of a Path as &[u8]
43 pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8],
45 /// Iterator that yields components of a Path in reverse as &[u8]
46 pub type RevComponents<'a> = Map<'a, Option<&'a str>, &'a [u8],
47 RevStrComponents<'a>>;
49 /// Represents a Windows path
50 // Notes for Windows path impl:
51 // The MAX_PATH is 260, but 253 is the practical limit due to some API bugs
52 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for good information
53 // about windows paths.
54 // That same page puts a bunch of restrictions on allowed characters in a path.
55 // `\foo.txt` means "relative to current drive", but will not be considered to be absolute here
56 // as `∃P | P.join("\foo.txt") != "\foo.txt"`.
57 // `C:` is interesting, that means "the current directory on drive C".
58 // Long absolute paths need to have \\?\ prefix (or, for UNC, \\?\UNC\). I think that can be
59 // ignored for now, though, and only added in a hypothetical .to_pwstr() function.
60 // However, if a path is parsed that has \\?\, this needs to be preserved as it disables the
61 // processing of "." and ".." components and / as a separator.
62 // Experimentally, \\?\foo is not the same thing as \foo.
63 // Also, \\foo is not valid either (certainly not equivalent to \foo).
64 // Similarly, C:\\Users is not equivalent to C:\Users, although C:\Users\\foo is equivalent
65 // to C:\Users\foo. In fact the command prompt treats C:\\foo\bar as UNC path. But it might be
66 // best to just ignore that and normalize it to C:\foo\bar.
68 // Based on all this, I think the right approach is to do the following:
69 // * Require valid utf-8 paths. Windows API may use WCHARs, but we don't, and utf-8 is convertible
70 // to UTF-16 anyway (though does Windows use UTF-16 or UCS-2? Not sure).
71 // * Parse the prefixes \\?\UNC\, \\?\, and \\.\ explicitly.
72 // * If \\?\UNC\, treat following two path components as server\share. Don't error for missing
74 // * If \\?\, parse disk from following component, if present. Don't error for missing disk.
75 // * If \\.\, treat rest of path as just regular components. I don't know how . and .. are handled
76 // here, they probably aren't, but I'm not going to worry about that.
77 // * Else if starts with \\, treat following two components as server\share. Don't error for missing
79 // * Otherwise, attempt to parse drive from start of path.
81 // The only error condition imposed here is valid utf-8. All other invalid paths are simply
82 // preserved by the data structure; let the Windows API error out on them.
85 repr: ~str, // assumed to never be empty
86 prefix: Option<PathPrefix>,
87 sepidx: Option<uint> // index of the final separator in the non-prefix portion of repr
92 fn eq(&self, other: &Path) -> bool {
93 self.repr == other.repr
97 impl TotalEq for Path {}
99 impl FromStr for Path {
100 fn from_str(s: &str) -> Option<Path> {
105 impl ToCStr for Path {
107 fn to_c_str(&self) -> CString {
108 // The Path impl guarantees no embedded NULs
109 unsafe { self.as_vec().to_c_str_unchecked() }
113 unsafe fn to_c_str_unchecked(&self) -> CString {
114 self.as_vec().to_c_str_unchecked()
118 impl<S: Writer> ::hash::Hash<S> for Path {
120 fn hash(&self, state: &mut S) {
121 self.repr.hash(state)
125 impl BytesContainer for Path {
127 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
131 fn container_into_owned_bytes(self) -> ~[u8] {
135 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
139 fn is_str(_: Option<Path>) -> bool { true }
142 impl<'a> BytesContainer for &'a Path {
144 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
148 fn container_as_str<'a>(&'a self) -> Option<&'a str> {
152 fn is_str(_: Option<&'a Path>) -> bool { true }
155 impl GenericPathUnsafe for Path {
156 /// See `GenericPathUnsafe::from_vec_unchecked`.
160 /// Fails if not valid UTF-8.
162 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
163 let (prefix, path) = Path::normalize_(path.container_as_str().unwrap());
164 assert!(!path.is_empty());
165 let mut ret = Path{ repr: path, prefix: prefix, sepidx: None };
170 /// See `GenericPathUnsafe::set_filename_unchecekd`.
174 /// Fails if not valid UTF-8.
175 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
176 let filename = filename.container_as_str().unwrap();
177 match self.sepidx_or_prefix_len() {
178 None if ".." == self.repr => {
179 let mut s = StrBuf::with_capacity(3 + filename.len());
182 s.push_str(filename);
183 self.update_normalized(s);
186 self.update_normalized(filename);
188 Some((_,idxa,end)) if self.repr.slice(idxa,end) == ".." => {
189 let mut s = StrBuf::with_capacity(end + 1 + filename.len());
190 s.push_str(self.repr.slice_to(end));
192 s.push_str(filename);
193 self.update_normalized(s);
195 Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => {
196 let mut s = StrBuf::with_capacity(idxb + filename.len());
197 s.push_str(self.repr.slice_to(idxb));
198 s.push_str(filename);
199 self.update_normalized(s);
201 Some((idxb,_,_)) => {
202 let mut s = StrBuf::with_capacity(idxb + 1 + filename.len());
203 s.push_str(self.repr.slice_to(idxb));
205 s.push_str(filename);
206 self.update_normalized(s);
211 /// See `GenericPathUnsafe::push_unchecked`.
213 /// Concatenating two Windows Paths is rather complicated.
214 /// For the most part, it will behave as expected, except in the case of
215 /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no
216 /// concept of per-volume cwds like Windows does, we can't behave exactly
217 /// like Windows will. Instead, if the receiver is an absolute path on
218 /// the same volume as the new path, it will be treated as the cwd that
219 /// the new path is relative to. Otherwise, the new path will be treated
220 /// as if it were absolute and will replace the receiver outright.
221 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
222 let path = path.container_as_str().unwrap();
223 fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
224 // assume prefix is Some(DiskPrefix)
225 let rest = path.slice_from(prefix_len(prefix));
226 !rest.is_empty() && rest[0].is_ascii() && is_sep(rest[0] as char)
228 fn shares_volume(me: &Path, path: &str) -> bool {
229 // path is assumed to have a prefix of Some(DiskPrefix)
231 Some(DiskPrefix) => me.repr[0] == path[0].to_ascii().to_upper().to_byte(),
232 Some(VerbatimDiskPrefix) => me.repr[4] == path[0].to_ascii().to_upper().to_byte(),
236 fn is_sep_(prefix: Option<PathPrefix>, u: u8) -> bool {
237 if prefix_is_verbatim(prefix) { is_sep_verbatim(u as char) }
238 else { is_sep(u as char) }
241 fn replace_path(me: &mut Path, path: &str, prefix: Option<PathPrefix>) {
242 let newpath = Path::normalize__(path, prefix);
243 me.repr = match newpath {
245 None => path.to_owned()
250 fn append_path(me: &mut Path, path: &str) {
251 // appends a path that has no prefix
252 // if me is verbatim, we need to pre-normalize the new path
253 let path_ = if is_verbatim(me) { Path::normalize__(path, None) }
255 let pathlen = path_.as_ref().map_or(path.len(), |p| p.len());
256 let mut s = StrBuf::with_capacity(me.repr.len() + 1 + pathlen);
258 let plen = me.prefix_len();
259 // if me is "C:" we don't want to add a path separator
261 Some(DiskPrefix) if me.repr.len() == plen => (),
262 _ if !(me.repr.len() > plen && me.repr[me.repr.len()-1] == SEP_BYTE) => {
268 None => s.push_str(path),
269 Some(p) => s.push_str(p)
271 me.update_normalized(s)
274 if !path.is_empty() {
275 let prefix = parse_prefix(path);
277 Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => {
278 // cwd-relative path, self is on the same volume
279 append_path(self, path.slice_from(prefix_len(prefix)));
282 // absolute path, or cwd-relative and self is not same volume
283 replace_path(self, path, prefix);
285 None if !path.is_empty() && is_sep_(self.prefix, path[0]) => {
286 // volume-relative path
287 if self.prefix.is_some() {
288 // truncate self down to the prefix, then append
289 let n = self.prefix_len();
290 self.repr.truncate(n);
291 append_path(self, path);
293 // we have no prefix, so nothing to be relative to
294 replace_path(self, path, prefix);
299 append_path(self, path);
306 impl GenericPath for Path {
308 fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
309 let s = path.container_as_str();
313 if contains_nul(s.as_bytes()) {
316 Some(unsafe { GenericPathUnsafe::new_unchecked(s) })
322 /// See `GenericPath::as_str` for info.
323 /// Always returns a `Some` value.
325 fn as_str<'a>(&'a self) -> Option<&'a str> {
326 Some(self.repr.as_slice())
330 fn as_vec<'a>(&'a self) -> &'a [u8] {
335 fn into_vec(self) -> ~[u8] {
336 self.repr.into_bytes()
340 fn dirname<'a>(&'a self) -> &'a [u8] {
341 self.dirname_str().unwrap().as_bytes()
344 /// See `GenericPath::dirname_str` for info.
345 /// Always returns a `Some` value.
346 fn dirname_str<'a>(&'a self) -> Option<&'a str> {
347 Some(match self.sepidx_or_prefix_len() {
348 None if ".." == self.repr => self.repr.as_slice(),
350 Some((_,idxa,end)) if self.repr.slice(idxa, end) == ".." => {
353 Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => {
356 Some((0,idxa,_)) => self.repr.slice_to(idxa),
357 Some((idxb,idxa,_)) => {
359 Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => {
360 self.repr.slice_to(idxa)
362 _ => self.repr.slice_to(idxb)
369 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
370 self.filename_str().map(|x| x.as_bytes())
373 /// See `GenericPath::filename_str` for info.
374 /// Always returns a `Some` value if `filename` returns a `Some` value.
375 fn filename_str<'a>(&'a self) -> Option<&'a str> {
376 match self.sepidx_or_prefix_len() {
377 None if "." == self.repr || ".." == self.repr => None,
378 None => Some(self.repr.as_slice()),
379 Some((_,idxa,end)) if self.repr.slice(idxa, end) == ".." => None,
380 Some((_,idxa,end)) if idxa == end => None,
381 Some((_,idxa,end)) => Some(self.repr.slice(idxa, end))
385 /// See `GenericPath::filestem_str` for info.
386 /// Always returns a `Some` value if `filestem` returns a `Some` value.
388 fn filestem_str<'a>(&'a self) -> Option<&'a str> {
389 // filestem() returns a byte vector that's guaranteed valid UTF-8
390 self.filestem().map(|t| unsafe { cast::transmute(t) })
394 fn extension_str<'a>(&'a self) -> Option<&'a str> {
395 // extension() returns a byte vector that's guaranteed valid UTF-8
396 self.extension().map(|t| unsafe { cast::transmute(t) })
399 fn dir_path(&self) -> Path {
400 unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) }
404 fn pop(&mut self) -> bool {
405 match self.sepidx_or_prefix_len() {
406 None if "." == self.repr => false,
412 Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false,
413 Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => false,
414 Some((idxb,idxa,_)) => {
415 let trunc = match self.prefix {
416 Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => {
417 let plen = self.prefix_len();
418 if idxb == plen { idxa } else { idxb }
422 self.repr.truncate(trunc);
423 self.update_sepidx();
429 fn root_path(&self) -> Option<Path> {
430 if self.prefix.is_some() {
431 Some(Path::new(match self.prefix {
432 Some(DiskPrefix) if self.is_absolute() => {
433 self.repr.slice_to(self.prefix_len()+1)
435 Some(VerbatimDiskPrefix) => {
436 self.repr.slice_to(self.prefix_len()+1)
438 _ => self.repr.slice_to(self.prefix_len())
440 } else if is_vol_relative(self) {
441 Some(Path::new(self.repr.slice_to(1)))
447 /// See `GenericPath::is_absolute` for info.
449 /// A Windows Path is considered absolute only if it has a non-volume prefix,
450 /// or if it has a volume prefix and the path starts with '\'.
451 /// A path of `\foo` is not considered absolute because it's actually
452 /// relative to the "current volume". A separate method `Path::is_vol_relative`
453 /// is provided to indicate this case. Similarly a path of `C:foo` is not
454 /// considered absolute because it's relative to the cwd on volume C:. A
455 /// separate method `Path::is_cwd_relative` is provided to indicate this case.
457 fn is_absolute(&self) -> bool {
459 Some(DiskPrefix) => {
460 let rest = self.repr.slice_from(self.prefix_len());
461 rest.len() > 0 && rest[0] == SEP_BYTE
469 fn is_relative(&self) -> bool {
470 self.prefix.is_none() && !is_vol_relative(self)
473 fn is_ancestor_of(&self, other: &Path) -> bool {
474 if !self.equiv_prefix(other) {
476 } else if self.is_absolute() != other.is_absolute() ||
477 is_vol_relative(self) != is_vol_relative(other) {
480 let mut ita = self.str_components().map(|x|x.unwrap());
481 let mut itb = other.str_components().map(|x|x.unwrap());
482 if "." == self.repr {
483 return itb.next() != Some("..");
486 match (ita.next(), itb.next()) {
488 (Some(a), Some(b)) if a == b => { continue },
489 (Some(a), _) if a == ".." => {
490 // if ita contains only .. components, it's an ancestor
491 return ita.all(|x| x == "..");
500 fn path_relative_from(&self, base: &Path) -> Option<Path> {
501 fn comp_requires_verbatim(s: &str) -> bool {
502 s == "." || s == ".." || s.contains_char(SEP2)
505 if !self.equiv_prefix(base) {
507 if self.is_absolute() {
509 } else if self.prefix == Some(DiskPrefix) && base.prefix == Some(DiskPrefix) {
510 // both drives, drive letters must differ or they'd be equiv
515 } else if self.is_absolute() != base.is_absolute() {
516 if self.is_absolute() {
521 } else if is_vol_relative(self) != is_vol_relative(base) {
522 if is_vol_relative(self) {
528 let mut ita = self.str_components().map(|x|x.unwrap());
529 let mut itb = base.str_components().map(|x|x.unwrap());
532 let a_verb = is_verbatim(self);
533 let b_verb = is_verbatim(base);
535 match (ita.next(), itb.next()) {
536 (None, None) => break,
537 (Some(a), None) if a_verb && comp_requires_verbatim(a) => {
538 return Some(self.clone())
543 comps.extend(ita.by_ref());
547 (None, _) => comps.push(".."),
548 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
549 (Some(a), Some(b)) if !b_verb && b == "." => {
550 if a_verb && comp_requires_verbatim(a) {
551 return Some(self.clone())
552 } else { comps.push(a) }
554 (Some(_), Some(b)) if !b_verb && b == ".." => return None,
555 (Some(a), Some(_)) if a_verb && comp_requires_verbatim(a) => {
556 return Some(self.clone())
558 (Some(a), Some(_)) => {
565 comps.extend(ita.by_ref());
571 Some(Path::new(comps.connect("\\")))
575 fn ends_with_path(&self, child: &Path) -> bool {
576 if !child.is_relative() { return false; }
577 let mut selfit = self.str_components().rev();
578 let mut childit = child.str_components().rev();
580 match (selfit.next(), childit.next()) {
581 (Some(a), Some(b)) => if a != b { return false; },
582 (Some(_), None) => break,
583 (None, Some(_)) => return false,
584 (None, None) => break
592 /// Returns a new Path from a byte vector or string
596 /// Fails the task if the vector contains a NUL.
597 /// Fails if invalid UTF-8.
599 pub fn new<T: BytesContainer>(path: T) -> Path {
600 GenericPath::new(path)
603 /// Returns a new Path from a byte vector or string, if possible
605 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
606 GenericPath::new_opt(path)
609 /// Returns an iterator that yields each component of the path in turn as a Option<&str>.
610 /// Every component is guaranteed to be Some.
611 /// Does not yield the path prefix (including server/share components in UNC paths).
612 /// Does not distinguish between volume-relative and relative paths, e.g.
613 /// \a\b\c and a\b\c.
614 /// Does not distinguish between absolute and cwd-relative paths, e.g.
615 /// C:\foo and C:foo.
616 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
617 let s = match self.prefix {
619 let plen = self.prefix_len();
620 if self.repr.len() > plen && self.repr[plen] == SEP_BYTE {
621 self.repr.slice_from(plen+1)
622 } else { self.repr.slice_from(plen) }
624 None if self.repr[0] == SEP_BYTE => self.repr.slice_from(1),
625 None => self.repr.as_slice()
627 let ret = s.split_terminator(SEP).map(Some);
631 /// Returns an iterator that yields each component of the path in reverse as an Option<&str>
632 /// See str_components() for details.
633 pub fn rev_str_components<'a>(&'a self) -> RevStrComponents<'a> {
634 self.str_components().rev()
637 /// Returns an iterator that yields each component of the path in turn as a &[u8].
638 /// See str_components() for details.
639 pub fn components<'a>(&'a self) -> Components<'a> {
640 fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
642 x.unwrap().as_bytes()
644 self.str_components().map(convert)
647 /// Returns an iterator that yields each component of the path in reverse as a &[u8].
648 /// See str_components() for details.
649 pub fn rev_components<'a>(&'a self) -> RevComponents<'a> {
650 fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
652 x.unwrap().as_bytes()
654 self.rev_str_components().map(convert)
657 fn equiv_prefix(&self, other: &Path) -> bool {
658 match (self.prefix, other.prefix) {
659 (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
660 self.is_absolute() &&
661 self.repr[0].to_ascii().eq_ignore_case(other.repr[4].to_ascii())
663 (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
664 other.is_absolute() &&
665 self.repr[4].to_ascii().eq_ignore_case(other.repr[0].to_ascii())
667 (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
668 self.repr[4].to_ascii().eq_ignore_case(other.repr[4].to_ascii())
670 (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
671 self.repr.slice(2, self.prefix_len()) == other.repr.slice(8, other.prefix_len())
673 (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => {
674 self.repr.slice(8, self.prefix_len()) == other.repr.slice(2, other.prefix_len())
676 (None, None) => true,
677 (a, b) if a == b => {
678 self.repr.slice_to(self.prefix_len()) == other.repr.slice_to(other.prefix_len())
684 fn normalize_<S: Str>(s: S) -> (Option<PathPrefix>, ~str) {
685 // make borrowck happy
686 let (prefix, val) = {
687 let prefix = parse_prefix(s.as_slice());
688 let path = Path::normalize__(s.as_slice(), prefix);
692 None => s.into_owned(),
697 fn normalize__(s: &str, prefix: Option<PathPrefix>) -> Option<~str> {
698 if prefix_is_verbatim(prefix) {
699 // don't do any normalization
701 Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
702 // the server component has no trailing '\'
703 let mut s = StrBuf::from_owned_str(s.into_owned());
710 let (is_abs, comps) = normalize_helper(s, prefix);
711 let mut comps = comps;
712 match (comps.is_some(),prefix) {
713 (false, Some(DiskPrefix)) => {
714 if s[0] >= 'a' as u8 && s[0] <= 'z' as u8 {
718 (false, Some(VerbatimDiskPrefix)) => {
719 if s[4] >= 'a' as u8 && s[0] <= 'z' as u8 {
728 if prefix.is_some() && comps.is_empty() {
729 match prefix.unwrap() {
731 let len = prefix_len(prefix) + is_abs as uint;
732 let mut s = s.slice_to(len).to_owned();
734 str::raw::as_owned_vec(&mut s)[0] =
735 s[0].to_ascii().to_upper().to_byte();
738 // normalize C:/ to C:\
740 str::raw::as_owned_vec(&mut s)[2] = SEP_BYTE;
745 VerbatimDiskPrefix => {
746 let len = prefix_len(prefix) + is_abs as uint;
747 let mut s = s.slice_to(len).to_owned();
749 str::raw::as_owned_vec(&mut s)[4] =
750 s[4].to_ascii().to_upper().to_byte();
755 let plen = prefix_len(prefix);
757 Some(s.slice_to(plen).to_owned())
761 } else if is_abs && comps.is_empty() {
762 Some(str::from_char(SEP))
764 let prefix_ = s.slice_to(prefix_len(prefix));
765 let n = prefix_.len() +
766 if is_abs { comps.len() } else { comps.len() - 1} +
767 comps.iter().map(|v| v.len()).sum();
768 let mut s = StrBuf::with_capacity(n);
770 Some(DiskPrefix) => {
771 s.push_char(prefix_[0].to_ascii().to_upper().to_char());
774 Some(VerbatimDiskPrefix) => {
775 s.push_str(prefix_.slice_to(4));
776 s.push_char(prefix_[4].to_ascii().to_upper().to_char());
777 s.push_str(prefix_.slice_from(5));
779 Some(UNCPrefix(a,b)) => {
781 s.push_str(prefix_.slice(2, a+2));
783 s.push_str(prefix_.slice(3+a, 3+a+b));
785 Some(_) => s.push_str(prefix_),
788 let mut it = comps.move_iter();
792 Some(comp) => s.push_str(comp)
806 fn update_sepidx(&mut self) {
807 let s = if self.has_nonsemantic_trailing_slash() {
808 self.repr.slice_to(self.repr.len()-1)
809 } else { self.repr.as_slice() };
810 let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep }
811 else { is_sep_verbatim });
812 let prefixlen = self.prefix_len();
813 self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
816 fn prefix_len(&self) -> uint {
817 prefix_len(self.prefix)
820 // Returns a tuple (before, after, end) where before is the index of the separator
821 // and after is the index just after the separator.
822 // end is the length of the string, normally, or the index of the final character if it is
823 // a non-semantic trailing separator in a verbatim string.
824 // If the prefix is considered the separator, before and after are the same.
825 fn sepidx_or_prefix_len(&self) -> Option<(uint,uint,uint)> {
827 None => match self.prefix_len() { 0 => None, x => Some((x,x,self.repr.len())) },
829 if self.has_nonsemantic_trailing_slash() {
830 Some((x,x+1,self.repr.len()-1))
831 } else { Some((x,x+1,self.repr.len())) }
836 fn has_nonsemantic_trailing_slash(&self) -> bool {
837 is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
838 self.repr[self.repr.len()-1] == SEP_BYTE
841 fn update_normalized<S: Str>(&mut self, s: S) {
842 let (prefix, path) = Path::normalize_(s);
844 self.prefix = prefix;
845 self.update_sepidx();
849 /// Returns whether the path is considered "volume-relative", which means a path
850 /// that looks like "\foo". Paths of this form are relative to the current volume,
851 /// but absolute within that volume.
853 pub fn is_vol_relative(path: &Path) -> bool {
854 path.prefix.is_none() && is_sep_byte(&path.repr[0])
857 /// Returns whether the path is considered "cwd-relative", which means a path
858 /// with a volume prefix that is not absolute. This look like "C:foo.txt". Paths
859 /// of this form are relative to the cwd on the given volume.
861 pub fn is_cwd_relative(path: &Path) -> bool {
862 path.prefix == Some(DiskPrefix) && !path.is_absolute()
865 /// Returns the PathPrefix for this Path
867 pub fn prefix(path: &Path) -> Option<PathPrefix> {
871 /// Returns whether the Path's prefix is a verbatim prefix, i.e. `\\?\`
873 pub fn is_verbatim(path: &Path) -> bool {
874 prefix_is_verbatim(path.prefix)
877 /// Returns the non-verbatim equivalent of the input path, if possible.
878 /// If the input path is a device namespace path, None is returned.
879 /// If the input path is not verbatim, it is returned as-is.
880 /// If the input path is verbatim, but the same path can be expressed as
881 /// non-verbatim, the non-verbatim version is returned.
882 /// Otherwise, None is returned.
883 pub fn make_non_verbatim(path: &Path) -> Option<Path> {
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(path.repr.slice_from(4))
891 Some(VerbatimUNCPrefix(_,_)) => {
892 // \\?\UNC\server\share
893 Path::new(format!(r"\\{}", path.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 path.repr.slice_from(path.prefix_len()) == new_path.repr.slice_from(new_path.prefix_len()) {
909 /// The standard path separator character
910 pub static SEP: char = '\\';
911 /// The standard path separator byte
912 pub static SEP_BYTE: u8 = SEP as u8;
914 /// The alternative path separator character
915 pub static SEP2: char = '/';
916 /// The alternative path separator character
917 pub static SEP2_BYTE: u8 = SEP2 as u8;
919 /// Returns whether the given char is a path separator.
920 /// Allows both the primary separator '\' and the alternative separator '/'.
922 pub fn is_sep(c: char) -> bool {
923 c == SEP || c == SEP2
926 /// Returns whether the given char is a path separator.
927 /// Only allows the primary separator '\'; use is_sep to allow '/'.
929 pub fn is_sep_verbatim(c: char) -> bool {
933 /// Returns whether the given byte is a path separator.
934 /// Allows both the primary separator '\' and the alternative separator '/'.
936 pub fn is_sep_byte(u: &u8) -> bool {
937 *u == SEP_BYTE || *u == SEP2_BYTE
940 /// Returns whether the given byte is a path separator.
941 /// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
943 pub fn is_sep_byte_verbatim(u: &u8) -> bool {
947 /// Prefix types for Path
948 #[deriving(Eq, Clone)]
949 pub enum PathPrefix {
950 /// Prefix `\\?\`, uint is the length of the following component
951 VerbatimPrefix(uint),
952 /// Prefix `\\?\UNC\`, uints are the lengths of the UNC components
953 VerbatimUNCPrefix(uint, uint),
954 /// Prefix `\\?\C:\` (for any alphabetic character)
956 /// Prefix `\\.\`, uint is the length of the following component
957 DeviceNSPrefix(uint),
958 /// UNC prefix `\\server\share`, uints are the lengths of the server/share
959 UNCPrefix(uint, uint),
960 /// Prefix `C:` for any alphabetic character
964 fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
965 if path.starts_with("\\\\") {
967 path = path.slice_from(2);
968 if path.starts_with("?\\") {
970 path = path.slice_from(2);
971 if path.starts_with("UNC\\") {
972 // \\?\UNC\server\share
973 path = path.slice_from(4);
974 let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) {
976 None => (path.len(), 0)
978 return Some(VerbatimUNCPrefix(idx_a, idx_b));
981 let idx = path.find('\\');
982 if idx == Some(2) && path[1] == ':' as u8 {
984 if c.is_ascii() && ::char::is_alphabetic(c as char) {
986 return Some(VerbatimDiskPrefix);
989 let idx = idx.unwrap_or(path.len());
990 return Some(VerbatimPrefix(idx));
992 } else if path.starts_with(".\\") {
994 path = path.slice_from(2);
995 let idx = path.find('\\').unwrap_or(path.len());
996 return Some(DeviceNSPrefix(idx));
998 match parse_two_comps(path, is_sep) {
999 Some((idx_a, idx_b)) if idx_a > 0 && idx_b > 0 => {
1001 return Some(UNCPrefix(idx_a, idx_b));
1005 } else if path.len() > 1 && path[1] == ':' as u8 {
1008 if c.is_ascii() && ::char::is_alphabetic(c as char) {
1009 return Some(DiskPrefix);
1014 fn parse_two_comps<'a>(mut path: &'a str, f: |char| -> bool)
1015 -> Option<(uint, uint)> {
1016 let idx_a = match path.find(|x| f(x)) {
1017 None => return None,
1020 path = path.slice_from(idx_a+1);
1021 let idx_b = path.find(f).unwrap_or(path.len());
1022 Some((idx_a, idx_b))
1026 // None result means the string didn't need normalizing
1027 fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool,Option<~[&'a str]>) {
1028 let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim };
1029 let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
1030 let s_ = s.slice_from(prefix_len(prefix));
1031 let s_ = if is_abs { s_.slice_from(1) } else { s_ };
1033 if is_abs && s_.is_empty() {
1034 return (is_abs, match prefix {
1035 Some(DiskPrefix) | None => (if is_sep_verbatim(s.char_at(prefix_len(prefix))) { None }
1036 else { Some(~[]) }),
1037 Some(_) => Some(~[]), // need to trim the trailing separator
1040 let mut comps: ~[&'a str] = ~[];
1042 let mut changed = false;
1043 for comp in s_.split(f) {
1044 if comp.is_empty() { changed = true }
1045 else if comp == "." { changed = true }
1046 else if comp == ".." {
1047 let has_abs_prefix = match prefix {
1048 Some(DiskPrefix) => false,
1052 if (is_abs || has_abs_prefix) && comps.is_empty() { changed = true }
1053 else if comps.len() == n_up { comps.push(".."); n_up += 1 }
1054 else { comps.pop().unwrap(); changed = true }
1055 } else { comps.push(comp) }
1057 if !changed && !prefix_is_verbatim(prefix) {
1058 changed = s.find(is_sep).is_some();
1061 if comps.is_empty() && !is_abs && prefix.is_none() {
1063 return (is_abs, None);
1067 (is_abs, Some(comps))
1073 fn prefix_is_verbatim(p: Option<PathPrefix>) -> bool {
1075 Some(VerbatimPrefix(_)) | Some(VerbatimUNCPrefix(_,_)) | Some(VerbatimDiskPrefix) => true,
1076 Some(DeviceNSPrefix(_)) => true, // not really sure, but I think so
1081 fn prefix_len(p: Option<PathPrefix>) -> uint {
1084 Some(VerbatimPrefix(x)) => 4 + x,
1085 Some(VerbatimUNCPrefix(x,y)) => 8 + x + 1 + y,
1086 Some(VerbatimDiskPrefix) => 6,
1087 Some(UNCPrefix(x,y)) => 2 + x + 1 + y,
1088 Some(DeviceNSPrefix(x)) => 4 + x,
1089 Some(DiskPrefix) => 2
1097 use super::parse_prefix;
1100 (s: $path:expr, $exp:expr) => (
1103 assert!(path.as_str() == Some($exp));
1106 (v: $path:expr, $exp:expr) => (
1109 assert!(path.as_vec() == $exp);
1115 ($($arg:expr),+) => (
1117 static the_bytes: &'static [u8] = bytes!($($arg),+);
1124 fn test_parse_prefix() {
1126 ($path:expr, $exp:expr) => (
1130 let res = parse_prefix(path);
1132 "parse_prefix(\"{}\"): expected {:?}, found {:?}", path, exp, res);
1137 t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5)));
1139 t!("\\\\SERVER", None);
1140 t!("\\\\SERVER\\", None);
1141 t!("\\\\SERVER\\\\", None);
1142 t!("\\\\SERVER\\\\foo", None);
1143 t!("\\\\SERVER\\share", Some(UNCPrefix(6,5)));
1144 t!("\\\\SERVER/share/foo", Some(UNCPrefix(6,5)));
1145 t!("\\\\SERVER\\share/foo", Some(UNCPrefix(6,5)));
1146 t!("//SERVER/share/foo", None);
1147 t!("\\\\\\a\\b\\c", None);
1148 t!("\\\\?\\a\\b\\c", Some(VerbatimPrefix(1)));
1149 t!("\\\\?\\a/b/c", Some(VerbatimPrefix(5)));
1150 t!("//?/a/b/c", None);
1151 t!("\\\\.\\a\\b", Some(DeviceNSPrefix(1)));
1152 t!("\\\\.\\a/b", Some(DeviceNSPrefix(3)));
1153 t!("//./a/b", None);
1154 t!("\\\\?\\UNC\\server\\share\\foo", Some(VerbatimUNCPrefix(6,5)));
1155 t!("\\\\?\\UNC\\\\share\\foo", Some(VerbatimUNCPrefix(0,5)));
1156 t!("\\\\?\\UNC\\", Some(VerbatimUNCPrefix(0,0)));
1157 t!("\\\\?\\UNC\\server/share/foo", Some(VerbatimUNCPrefix(16,0)));
1158 t!("\\\\?\\UNC\\server", Some(VerbatimUNCPrefix(6,0)));
1159 t!("\\\\?\\UNC\\server\\", Some(VerbatimUNCPrefix(6,0)));
1160 t!("\\\\?\\UNC/server/share", Some(VerbatimPrefix(16)));
1161 t!("\\\\?\\UNC", Some(VerbatimPrefix(3)));
1162 t!("\\\\?\\C:\\a\\b.txt", Some(VerbatimDiskPrefix));
1163 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1164 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1165 t!("\\\\?\\C:a.txt", Some(VerbatimPrefix(7)));
1166 t!("\\\\?\\C:a\\b.txt", Some(VerbatimPrefix(3)));
1167 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1168 t!("C:\\foo", Some(DiskPrefix));
1169 t!("z:/foo", Some(DiskPrefix));
1170 t!("d:", Some(DiskPrefix));
1172 t!("ü:\\foo", None);
1173 t!("3:\\foo", None);
1174 t!(" :\\foo", None);
1175 t!("::\\foo", None);
1176 t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1177 t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1178 t!("\\\\?\\ab:\\", Some(VerbatimPrefix(3)));
1179 t!("\\\\?\\C:\\a", Some(VerbatimDiskPrefix));
1180 t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1181 t!("\\\\?\\C:\\a/b", Some(VerbatimDiskPrefix));
1186 let empty: &[u8] = [];
1187 t!(v: Path::new(empty), b!("."));
1188 t!(v: Path::new(b!("\\")), b!("\\"));
1189 t!(v: Path::new(b!("a\\b\\c")), b!("a\\b\\c"));
1191 t!(s: Path::new(""), ".");
1192 t!(s: Path::new("\\"), "\\");
1193 t!(s: Path::new("hi"), "hi");
1194 t!(s: Path::new("hi\\"), "hi");
1195 t!(s: Path::new("\\lib"), "\\lib");
1196 t!(s: Path::new("\\lib\\"), "\\lib");
1197 t!(s: Path::new("hi\\there"), "hi\\there");
1198 t!(s: Path::new("hi\\there.txt"), "hi\\there.txt");
1199 t!(s: Path::new("/"), "\\");
1200 t!(s: Path::new("hi/"), "hi");
1201 t!(s: Path::new("/lib"), "\\lib");
1202 t!(s: Path::new("/lib/"), "\\lib");
1203 t!(s: Path::new("hi/there"), "hi\\there");
1205 t!(s: Path::new("hi\\there\\"), "hi\\there");
1206 t!(s: Path::new("hi\\..\\there"), "there");
1207 t!(s: Path::new("hi/../there"), "there");
1208 t!(s: Path::new("..\\hi\\there"), "..\\hi\\there");
1209 t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there");
1210 t!(s: Path::new("/../hi/there"), "\\hi\\there");
1211 t!(s: Path::new("foo\\.."), ".");
1212 t!(s: Path::new("\\foo\\.."), "\\");
1213 t!(s: Path::new("\\foo\\..\\.."), "\\");
1214 t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar");
1215 t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there");
1216 t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi");
1217 t!(s: Path::new("foo\\..\\.."), "..");
1218 t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
1219 t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
1221 assert_eq!(Path::new(b!("foo\\bar")).into_vec(), b!("foo\\bar").to_owned());
1222 assert_eq!(Path::new(b!("\\foo\\..\\..\\bar")).into_vec(),
1223 b!("\\bar").to_owned());
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");
1312 let path = Path::new(b!("\\"));
1313 assert_eq!(path.filename_display().to_str(), ~"");
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", ~"e"], "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"), [b!("d").to_owned(), b!("e").to_owned()], b!("a\\b\\c\\d\\e"));
1583 (s: $path:expr, $left:expr, $right:expr) => (
1586 let mut p = Path::new(pstr);
1587 let result = p.pop();
1589 assert!(p.as_str() == Some(left),
1590 "`{}`.pop() failed; expected remainder `{}`, found `{}`",
1591 pstr, left, p.as_str().unwrap());
1592 assert!(result == $right);
1595 (v: [$($path:expr),+], [$($left:expr),+], $right:expr) => (
1597 let mut p = Path::new(b!($($path),+));
1598 let result = p.pop();
1599 assert_eq!(p.as_vec(), b!($($left),+));
1600 assert!(result == $right);
1605 t!(s: "a\\b\\c", "a\\b", true);
1606 t!(s: "a", ".", true);
1607 t!(s: ".", ".", false);
1608 t!(s: "\\a", "\\", true);
1609 t!(s: "\\", "\\", false);
1610 t!(v: ["a\\b\\c"], ["a\\b"], true);
1611 t!(v: ["a"], ["."], true);
1612 t!(v: ["."], ["."], false);
1613 t!(v: ["\\a"], ["\\"], true);
1614 t!(v: ["\\"], ["\\"], false);
1616 t!(s: "C:\\a\\b", "C:\\a", true);
1617 t!(s: "C:\\a", "C:\\", true);
1618 t!(s: "C:\\", "C:\\", false);
1619 t!(s: "C:a\\b", "C:a", true);
1620 t!(s: "C:a", "C:", true);
1621 t!(s: "C:", "C:", false);
1622 t!(s: "\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1623 t!(s: "\\\\server\\share\\a", "\\\\server\\share", true);
1624 t!(s: "\\\\server\\share", "\\\\server\\share", false);
1625 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1626 t!(s: "\\\\?\\a\\b", "\\\\?\\a", true);
1627 t!(s: "\\\\?\\a", "\\\\?\\a", false);
1628 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1629 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1630 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\", false);
1631 t!(s: "\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1632 t!(s: "\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share", true);
1633 t!(s: "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1634 t!(s: "\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1635 t!(s: "\\\\.\\a\\b", "\\\\.\\a", true);
1636 t!(s: "\\\\.\\a", "\\\\.\\a", false);
1638 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a", true);
1642 fn test_root_path() {
1643 assert!(Path::new("a\\b\\c").root_path() == None);
1644 assert!(Path::new("\\a\\b\\c").root_path() == Some(Path::new("\\")));
1645 assert!(Path::new("C:a").root_path() == Some(Path::new("C:")));
1646 assert!(Path::new("C:\\a").root_path() == Some(Path::new("C:\\")));
1647 assert!(Path::new("\\\\a\\b\\c").root_path() == Some(Path::new("\\\\a\\b")));
1648 assert!(Path::new("\\\\?\\a\\b").root_path() == Some(Path::new("\\\\?\\a")));
1649 assert!(Path::new("\\\\?\\C:\\a").root_path() == Some(Path::new("\\\\?\\C:\\")));
1650 assert!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path() ==
1651 Some(Path::new("\\\\?\\UNC\\a\\b")));
1652 assert!(Path::new("\\\\.\\a\\b").root_path() == Some(Path::new("\\\\.\\a")));
1657 t!(s: Path::new("a\\b\\c").join(".."), "a\\b");
1658 t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d");
1659 t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d");
1660 t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
1661 t!(s: Path::new(".").join("a\\b"), "a\\b");
1662 t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
1663 t!(v: Path::new(b!("a\\b\\c")).join(b!("..")), b!("a\\b"));
1664 t!(v: Path::new(b!("\\a\\b\\c")).join(b!("d")), b!("\\a\\b\\c\\d"));
1665 // full join testing is covered under test_push_path, so no need for
1666 // the full set of prefix tests
1670 fn test_join_path() {
1672 (s: $path:expr, $join:expr, $exp:expr) => (
1674 let path = Path::new($path);
1675 let join = Path::new($join);
1676 let res = path.join(&join);
1677 assert_eq!(res.as_str(), Some($exp));
1682 t!(s: "a\\b\\c", "..", "a\\b");
1683 t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1684 t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1685 t!(s: "a\\b", "\\c\\d", "\\c\\d");
1686 t!(s: ".", "a\\b", "a\\b");
1687 t!(s: "\\", "a\\b", "\\a\\b");
1688 // join is implemented using push, so there's no need for
1689 // the full set of prefix tests
1693 fn test_join_many() {
1695 (s: $path:expr, $join:expr, $exp:expr) => (
1697 let path = Path::new($path);
1698 let res = path.join_many($join);
1699 assert_eq!(res.as_str(), Some($exp));
1702 (v: $path:expr, $join:expr, $exp:expr) => (
1704 let path = Path::new($path);
1705 let res = path.join_many($join);
1706 assert_eq!(res.as_vec(), $exp);
1711 t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1712 t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
1713 t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1714 t!(s: "a\\b\\c", [~"d", ~"e"], "a\\b\\c\\d\\e");
1715 t!(v: b!("a\\b\\c"), [b!("d"), b!("e")], b!("a\\b\\c\\d\\e"));
1716 t!(v: b!("a\\b\\c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a\\b\\c\\d\\e"));
1720 fn test_with_helpers() {
1722 (s: $path:expr, $op:ident, $arg:expr, $res:expr) => (
1725 let path = Path::new(pstr);
1727 let res = path.$op(arg);
1729 assert!(res.as_str() == Some(exp),
1730 "`{}`.{}(\"{}\"): Expected `{}`, found `{}`",
1731 pstr, stringify!($op), arg, exp, res.as_str().unwrap());
1736 t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d");
1737 t!(s: ".", with_filename, "foo", "foo");
1738 t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d");
1739 t!(s: "\\", with_filename, "foo", "\\foo");
1740 t!(s: "\\a", with_filename, "foo", "\\foo");
1741 t!(s: "foo", with_filename, "bar", "bar");
1742 t!(s: "\\", with_filename, "foo\\", "\\foo");
1743 t!(s: "\\a", with_filename, "foo\\", "\\foo");
1744 t!(s: "a\\b\\c", with_filename, "", "a\\b");
1745 t!(s: "a\\b\\c", with_filename, ".", "a\\b");
1746 t!(s: "a\\b\\c", with_filename, "..", "a");
1747 t!(s: "\\a", with_filename, "", "\\");
1748 t!(s: "foo", with_filename, "", ".");
1749 t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e");
1750 t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d");
1751 t!(s: "..", with_filename, "foo", "..\\foo");
1752 t!(s: "..\\..", with_filename, "foo", "..\\..\\foo");
1753 t!(s: "..", with_filename, "", "..");
1754 t!(s: "..\\..", with_filename, "", "..\\..");
1755 t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz");
1756 t!(s: "C:\\foo", with_filename, "bar", "C:\\bar");
1757 t!(s: "C:\\", with_filename, "foo", "C:\\foo");
1758 t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz");
1759 t!(s: "C:foo", with_filename, "bar", "C:bar");
1760 t!(s: "C:", with_filename, "foo", "C:foo");
1761 t!(s: "C:\\foo", with_filename, "", "C:\\");
1762 t!(s: "C:foo", with_filename, "", "C:");
1763 t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\");
1764 t!(s: "C:\\foo", with_filename, "..", "C:\\");
1765 t!(s: "C:\\", with_filename, "..", "C:\\");
1766 t!(s: "C:foo\\bar", with_filename, "..", "C:");
1767 t!(s: "C:foo", with_filename, "..", "C:..");
1768 t!(s: "C:", with_filename, "..", "C:..");
1769 t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar");
1770 t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo");
1771 t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share");
1772 t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share");
1773 t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share");
1774 t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share");
1775 t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz");
1776 t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar");
1777 t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo");
1778 t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\..");
1779 t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz");
1780 t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar");
1781 t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo");
1782 t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\..");
1783 t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz");
1784 t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar");
1785 t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\..");
1787 t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe");
1788 t!(s: "hi\\there.txt", with_extension, "", "hi\\there");
1789 t!(s: "hi\\there.txt", with_extension, ".", "hi\\there..");
1790 t!(s: "hi\\there.txt", with_extension, "..", "hi\\there...");
1791 t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt");
1792 t!(s: "hi\\there", with_extension, ".", "hi\\there..");
1793 t!(s: "hi\\there", with_extension, "..", "hi\\there...");
1794 t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt");
1795 t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt");
1796 t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo");
1797 t!(s: "\\", with_extension, "txt", "\\");
1798 t!(s: "\\", with_extension, ".", "\\");
1799 t!(s: "\\", with_extension, "..", "\\");
1800 t!(s: ".", with_extension, "txt", ".");
1801 // extension setter calls filename setter internally, no need for extended tests
1807 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1811 let mut p1 = Path::new(path);
1813 let p2 = Path::new(path);
1814 assert!(p1 == p2.$with(arg));
1817 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1821 let mut p1 = Path::new(path);
1823 let p2 = Path::new(path);
1824 assert!(p1 == p2.$with(arg));
1829 t!(v: b!("a\\b\\c"), set_filename, with_filename, b!("d"));
1830 t!(v: b!("\\"), set_filename, with_filename, b!("foo"));
1831 t!(s: "a\\b\\c", set_filename, with_filename, "d");
1832 t!(s: "\\", set_filename, with_filename, "foo");
1833 t!(s: ".", set_filename, with_filename, "foo");
1834 t!(s: "a\\b", set_filename, with_filename, "");
1835 t!(s: "a", set_filename, with_filename, "");
1837 t!(v: b!("hi\\there.txt"), set_extension, with_extension, b!("exe"));
1838 t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
1839 t!(s: "hi\\there.", set_extension, with_extension, "txt");
1840 t!(s: "hi\\there", set_extension, with_extension, "txt");
1841 t!(s: "hi\\there.txt", set_extension, with_extension, "");
1842 t!(s: "hi\\there", set_extension, with_extension, "");
1843 t!(s: ".", set_extension, with_extension, "txt");
1845 // with_ helpers use the setter internally, so the tests for the with_ helpers
1846 // will suffice. No need for the full set of prefix tests.
1852 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1855 let filename = $filename;
1856 assert!(path.filename_str() == filename,
1857 "`{}`.filename_str(): Expected `{:?}`, found `{:?}`",
1858 path.as_str().unwrap(), filename, path.filename_str());
1859 let dirname = $dirname;
1860 assert!(path.dirname_str() == dirname,
1861 "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
1862 path.as_str().unwrap(), dirname, path.dirname_str());
1863 let filestem = $filestem;
1864 assert!(path.filestem_str() == filestem,
1865 "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
1866 path.as_str().unwrap(), filestem, path.filestem_str());
1868 assert!(path.extension_str() == ext,
1869 "`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
1870 path.as_str().unwrap(), ext, path.extension_str());
1873 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1876 assert!(path.filename() == $filename);
1877 assert!(path.dirname() == $dirname);
1878 assert!(path.filestem() == $filestem);
1879 assert!(path.extension() == $ext);
1884 t!(v: Path::new(b!("a\\b\\c")), Some(b!("c")), b!("a\\b"), Some(b!("c")), None);
1885 t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
1886 t!(s: Path::new("."), None, Some("."), None, 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("hi\\there.txt"), Some("there.txt"), Some("hi"),
1891 Some("there"), Some("txt"));
1892 t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None);
1893 t!(s: Path::new("hi\\there."), Some("there."), Some("hi"),
1894 Some("there"), Some(""));
1895 t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None);
1896 t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"),
1897 Some("."), Some("there"));
1899 // these are already tested in test_components, so no need for extended tests
1903 fn test_dir_path() {
1904 t!(s: Path::new("hi\\there").dir_path(), "hi");
1905 t!(s: Path::new("hi").dir_path(), ".");
1906 t!(s: Path::new("\\hi").dir_path(), "\\");
1907 t!(s: Path::new("\\").dir_path(), "\\");
1908 t!(s: Path::new("..").dir_path(), "..");
1909 t!(s: Path::new("..\\..").dir_path(), "..\\..");
1911 // dir_path is just dirname interpreted as a path.
1912 // No need for extended tests
1916 fn test_is_absolute() {
1918 ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => (
1920 let path = Path::new($path);
1921 let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel);
1922 let b = path.is_absolute();
1923 assert!(b == abs, "Path '{}'.is_absolute(): expected {:?}, found {:?}",
1924 path.as_str().unwrap(), abs, b);
1925 let b = is_vol_relative(&path);
1926 assert!(b == vol, "is_vol_relative('{}'): expected {:?}, found {:?}",
1927 path.as_str().unwrap(), vol, b);
1928 let b = is_cwd_relative(&path);
1929 assert!(b == cwd, "is_cwd_relative('{}'): expected {:?}, found {:?}",
1930 path.as_str().unwrap(), cwd, b);
1931 let b = path.is_relative();
1932 assert!(b == rel, "Path '{}'.is_relativf(): expected {:?}, found {:?}",
1933 path.as_str().unwrap(), rel, b);
1937 t!("a\\b\\c", false, false, false, true);
1938 t!("\\a\\b\\c", false, true, false, false);
1939 t!("a", false, false, false, true);
1940 t!("\\a", false, true, false, false);
1941 t!(".", false, false, false, true);
1942 t!("\\", false, true, false, false);
1943 t!("..", false, false, false, true);
1944 t!("..\\..", false, false, false, true);
1945 t!("C:a\\b.txt", false, false, true, false);
1946 t!("C:\\a\\b.txt", true, false, false, false);
1947 t!("\\\\server\\share\\a\\b.txt", true, false, false, false);
1948 t!("\\\\?\\a\\b\\c.txt", true, false, false, false);
1949 t!("\\\\?\\C:\\a\\b.txt", true, false, false, false);
1950 t!("\\\\?\\C:a\\b.txt", true, false, false, false); // NB: not equivalent to C:a\b.txt
1951 t!("\\\\?\\UNC\\server\\share\\a\\b.txt", true, false, false, false);
1952 t!("\\\\.\\a\\b", true, false, false, false);
1956 fn test_is_ancestor_of() {
1958 (s: $path:expr, $dest:expr, $exp:expr) => (
1960 let path = Path::new($path);
1961 let dest = Path::new($dest);
1963 let res = path.is_ancestor_of(&dest);
1965 "`{}`.is_ancestor_of(`{}`): Expected {:?}, found {:?}",
1966 path.as_str().unwrap(), dest.as_str().unwrap(), exp, res);
1971 t!(s: "a\\b\\c", "a\\b\\c\\d", true);
1972 t!(s: "a\\b\\c", "a\\b\\c", true);
1973 t!(s: "a\\b\\c", "a\\b", false);
1974 t!(s: "\\a\\b\\c", "\\a\\b\\c", true);
1975 t!(s: "\\a\\b", "\\a\\b\\c", true);
1976 t!(s: "\\a\\b\\c\\d", "\\a\\b\\c", false);
1977 t!(s: "\\a\\b", "a\\b\\c", false);
1978 t!(s: "a\\b", "\\a\\b\\c", false);
1979 t!(s: "a\\b\\c", "a\\b\\d", false);
1980 t!(s: "..\\a\\b\\c", "a\\b\\c", false);
1981 t!(s: "a\\b\\c", "..\\a\\b\\c", false);
1982 t!(s: "a\\b\\c", "a\\b\\cd", false);
1983 t!(s: "a\\b\\cd", "a\\b\\c", false);
1984 t!(s: "..\\a\\b", "..\\a\\b\\c", true);
1985 t!(s: ".", "a\\b", true);
1986 t!(s: ".", ".", true);
1987 t!(s: "\\", "\\", true);
1988 t!(s: "\\", "\\a\\b", true);
1989 t!(s: "..", "a\\b", true);
1990 t!(s: "..\\..", "a\\b", true);
1991 t!(s: "foo\\bar", "foobar", false);
1992 t!(s: "foobar", "foo\\bar", false);
1994 t!(s: "foo", "C:foo", false);
1995 t!(s: "C:foo", "foo", false);
1996 t!(s: "C:foo", "C:foo\\bar", true);
1997 t!(s: "C:foo\\bar", "C:foo", false);
1998 t!(s: "C:\\foo", "C:\\foo\\bar", true);
1999 t!(s: "C:", "C:", true);
2000 t!(s: "C:", "C:\\", false);
2001 t!(s: "C:\\", "C:", false);
2002 t!(s: "C:\\", "C:\\", true);
2003 t!(s: "C:\\foo\\bar", "C:\\foo", false);
2004 t!(s: "C:foo\\bar", "C:foo", false);
2005 t!(s: "C:\\foo", "\\foo", false);
2006 t!(s: "\\foo", "C:\\foo", false);
2007 t!(s: "\\\\server\\share\\foo", "\\\\server\\share\\foo\\bar", true);
2008 t!(s: "\\\\server\\share", "\\\\server\\share\\foo", true);
2009 t!(s: "\\\\server\\share\\foo", "\\\\server\\share", false);
2010 t!(s: "C:\\foo", "\\\\server\\share\\foo", false);
2011 t!(s: "\\\\server\\share\\foo", "C:\\foo", false);
2012 t!(s: "\\\\?\\foo\\bar", "\\\\?\\foo\\bar\\baz", true);
2013 t!(s: "\\\\?\\foo\\bar\\baz", "\\\\?\\foo\\bar", false);
2014 t!(s: "\\\\?\\foo\\bar", "\\foo\\bar\\baz", false);
2015 t!(s: "\\foo\\bar", "\\\\?\\foo\\bar\\baz", false);
2016 t!(s: "\\\\?\\C:\\foo\\bar", "\\\\?\\C:\\foo\\bar\\baz", true);
2017 t!(s: "\\\\?\\C:\\foo\\bar\\baz", "\\\\?\\C:\\foo\\bar", false);
2018 t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\foo", true);
2019 t!(s: "\\\\?\\C:", "\\\\?\\C:\\", false); // this is a weird one
2020 t!(s: "\\\\?\\C:\\", "\\\\?\\C:", false);
2021 t!(s: "\\\\?\\C:\\a", "\\\\?\\c:\\a\\b", true);
2022 t!(s: "\\\\?\\c:\\a", "\\\\?\\C:\\a\\b", true);
2023 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a\\b", false);
2024 t!(s: "\\\\?\\foo", "\\\\?\\foobar", false);
2025 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", true);
2026 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\", true);
2027 t!(s: "\\\\?\\a\\b\\", "\\\\?\\a\\b", true);
2028 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", false);
2029 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b\\", false);
2030 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c\\d", true);
2031 t!(s: "\\\\?\\UNC\\a\\b\\c\\d", "\\\\?\\UNC\\a\\b\\c", false);
2032 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", true);
2033 t!(s: "\\\\.\\foo\\bar", "\\\\.\\foo\\bar\\baz", true);
2034 t!(s: "\\\\.\\foo\\bar\\baz", "\\\\.\\foo\\bar", false);
2035 t!(s: "\\\\.\\foo", "\\\\.\\foo\\bar", true);
2036 t!(s: "\\\\.\\foo", "\\\\.\\foobar", false);
2038 t!(s: "\\a\\b", "\\\\?\\a\\b", false);
2039 t!(s: "\\\\?\\a\\b", "\\a\\b", false);
2040 t!(s: "\\a\\b", "\\\\?\\C:\\a\\b", false);
2041 t!(s: "\\\\?\\C:\\a\\b", "\\a\\b", false);
2042 t!(s: "Z:\\a\\b", "\\\\?\\z:\\a\\b", true);
2043 t!(s: "C:\\a\\b", "\\\\?\\D:\\a\\b", false);
2044 t!(s: "a\\b", "\\\\?\\a\\b", false);
2045 t!(s: "\\\\?\\a\\b", "a\\b", false);
2046 t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b", true);
2047 t!(s: "\\\\?\\C:\\a\\b", "C:\\a\\b", true);
2048 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", false);
2049 t!(s: "C:a\\b", "\\\\?\\C:a\\b", false);
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\\", true);
2053 t!(s: "\\\\?\\C:\\a\\b\\", "C:\\a\\b", true);
2054 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c", true);
2055 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b\\c", true);
2059 fn test_ends_with_path() {
2061 (s: $path:expr, $child:expr, $exp:expr) => (
2063 let path = Path::new($path);
2064 let child = Path::new($child);
2065 assert_eq!(path.ends_with_path(&child), $exp);
2070 t!(s: "a\\b\\c", "c", true);
2071 t!(s: "a\\b\\c", "d", false);
2072 t!(s: "foo\\bar\\quux", "bar", false);
2073 t!(s: "foo\\bar\\quux", "barquux", false);
2074 t!(s: "a\\b\\c", "b\\c", true);
2075 t!(s: "a\\b\\c", "a\\b\\c", true);
2076 t!(s: "a\\b\\c", "foo\\a\\b\\c", false);
2077 t!(s: "\\a\\b\\c", "a\\b\\c", true);
2078 t!(s: "\\a\\b\\c", "\\a\\b\\c", false); // child must be relative
2079 t!(s: "\\a\\b\\c", "foo\\a\\b\\c", false);
2080 t!(s: "a\\b\\c", "", false);
2081 t!(s: "", "", true);
2082 t!(s: "\\a\\b\\c", "d\\e\\f", false);
2083 t!(s: "a\\b\\c", "a\\b", false);
2084 t!(s: "a\\b\\c", "b", false);
2085 t!(s: "C:\\a\\b", "b", true);
2086 t!(s: "C:\\a\\b", "C:b", false);
2087 t!(s: "C:\\a\\b", "C:a\\b", false);
2091 fn test_path_relative_from() {
2093 (s: $path:expr, $other:expr, $exp:expr) => (
2095 let path = Path::new($path);
2096 let other = Path::new($other);
2097 let res = path.path_relative_from(&other);
2099 assert!(res.as_ref().and_then(|x| x.as_str()) == exp,
2100 "`{}`.path_relative_from(`{}`): Expected {:?}, got {:?}",
2101 path.as_str().unwrap(), other.as_str().unwrap(), exp,
2102 res.as_ref().and_then(|x| x.as_str()));
2107 t!(s: "a\\b\\c", "a\\b", Some("c"));
2108 t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c"));
2109 t!(s: "a\\b\\c", "a\\b\\c\\d", Some(".."));
2110 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2111 t!(s: "a\\b\\c", "a\\b\\c\\d\\e", Some("..\\.."));
2112 t!(s: "a\\b\\c", "a\\d\\e", Some("..\\..\\b\\c"));
2113 t!(s: "a\\b\\c", "d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2114 t!(s: "a\\b\\c", "\\a\\b\\c", None);
2115 t!(s: "\\a\\b\\c", "a\\b\\c", Some("\\a\\b\\c"));
2116 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d", Some(".."));
2117 t!(s: "\\a\\b\\c", "\\a\\b", Some("c"));
2118 t!(s: "\\a\\b\\c", "\\a\\b\\c\\d\\e", Some("..\\.."));
2119 t!(s: "\\a\\b\\c", "\\a\\d\\e", Some("..\\..\\b\\c"));
2120 t!(s: "\\a\\b\\c", "\\d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2121 t!(s: "hi\\there.txt", "hi\\there", Some("..\\there.txt"));
2122 t!(s: ".", "a", Some(".."));
2123 t!(s: ".", "a\\b", Some("..\\.."));
2124 t!(s: ".", ".", Some("."));
2125 t!(s: "a", ".", Some("a"));
2126 t!(s: "a\\b", ".", Some("a\\b"));
2127 t!(s: "..", ".", Some(".."));
2128 t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2129 t!(s: "\\a\\b\\c", "\\a\\b\\c", Some("."));
2130 t!(s: "\\", "\\", Some("."));
2131 t!(s: "\\", ".", Some("\\"));
2132 t!(s: "..\\..\\a", "b", Some("..\\..\\..\\a"));
2133 t!(s: "a", "..\\..\\b", None);
2134 t!(s: "..\\..\\a", "..\\..\\b", Some("..\\a"));
2135 t!(s: "..\\..\\a", "..\\..\\a\\b", Some(".."));
2136 t!(s: "..\\..\\a\\b", "..\\..\\a", Some("b"));
2138 t!(s: "C:a\\b\\c", "C:a\\b", Some("c"));
2139 t!(s: "C:a\\b", "C:a\\b\\c", Some(".."));
2140 t!(s: "C:" ,"C:a\\b", Some("..\\.."));
2141 t!(s: "C:a\\b", "C:c\\d", Some("..\\..\\a\\b"));
2142 t!(s: "C:a\\b", "D:c\\d", Some("C:a\\b"));
2143 t!(s: "C:a\\b", "C:..\\c", None);
2144 t!(s: "C:..\\a", "C:b\\c", Some("..\\..\\..\\a"));
2145 t!(s: "C:\\a\\b\\c", "C:\\a\\b", Some("c"));
2146 t!(s: "C:\\a\\b", "C:\\a\\b\\c", Some(".."));
2147 t!(s: "C:\\", "C:\\a\\b", Some("..\\.."));
2148 t!(s: "C:\\a\\b", "C:\\c\\d", Some("..\\..\\a\\b"));
2149 t!(s: "C:\\a\\b", "C:a\\b", Some("C:\\a\\b"));
2150 t!(s: "C:a\\b", "C:\\a\\b", None);
2151 t!(s: "\\a\\b", "C:\\a\\b", None);
2152 t!(s: "\\a\\b", "C:a\\b", None);
2153 t!(s: "a\\b", "C:\\a\\b", None);
2154 t!(s: "a\\b", "C:a\\b", None);
2156 t!(s: "\\\\a\\b\\c", "\\\\a\\b", Some("c"));
2157 t!(s: "\\\\a\\b", "\\\\a\\b\\c", Some(".."));
2158 t!(s: "\\\\a\\b\\c\\e", "\\\\a\\b\\c\\d", Some("..\\e"));
2159 t!(s: "\\\\a\\c\\d", "\\\\a\\b\\d", Some("\\\\a\\c\\d"));
2160 t!(s: "\\\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\b\\c\\d"));
2161 t!(s: "\\\\a\\b\\c", "\\d\\e", Some("\\\\a\\b\\c"));
2162 t!(s: "\\d\\e", "\\\\a\\b\\c", None);
2163 t!(s: "d\\e", "\\\\a\\b\\c", None);
2164 t!(s: "C:\\a\\b\\c", "\\\\a\\b\\c", Some("C:\\a\\b\\c"));
2165 t!(s: "C:\\c", "\\\\a\\b\\c", Some("C:\\c"));
2167 t!(s: "\\\\?\\a\\b", "\\a\\b", Some("\\\\?\\a\\b"));
2168 t!(s: "\\\\?\\a\\b", "a\\b", Some("\\\\?\\a\\b"));
2169 t!(s: "\\\\?\\a\\b", "\\b", Some("\\\\?\\a\\b"));
2170 t!(s: "\\\\?\\a\\b", "b", Some("\\\\?\\a\\b"));
2171 t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", Some(".."));
2172 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", Some("c"));
2173 t!(s: "\\\\?\\a\\b", "\\\\?\\c\\d", Some("\\\\?\\a\\b"));
2174 t!(s: "\\\\?\\a", "\\\\?\\b", Some("\\\\?\\a"));
2176 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2177 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2178 t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\b", Some("..\\a"));
2179 t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a", Some("\\\\?\\C:\\a"));
2180 t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\c:\\a", Some("b"));
2181 t!(s: "\\\\?\\C:\\a\\b", "C:\\a", Some("b"));
2182 t!(s: "\\\\?\\C:\\a", "C:\\a\\b", Some(".."));
2183 t!(s: "C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2184 t!(s: "C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2185 t!(s: "\\\\?\\C:\\a", "D:\\a", Some("\\\\?\\C:\\a"));
2186 t!(s: "\\\\?\\c:\\a\\b", "C:\\a", Some("b"));
2187 t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", Some("\\\\?\\C:\\a\\b"));
2188 t!(s: "\\\\?\\C:\\a\\.\\b", "C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2189 t!(s: "\\\\?\\C:\\a\\b/c", "C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2190 t!(s: "\\\\?\\C:\\a\\..\\b", "C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2191 t!(s: "C:a\\b", "\\\\?\\C:\\a\\b", None);
2192 t!(s: "\\\\?\\C:\\a\\.\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2193 t!(s: "\\\\?\\C:\\a\\b/c", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2194 t!(s: "\\\\?\\C:\\a\\..\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2195 t!(s: "\\\\?\\C:\\a\\b\\", "\\\\?\\C:\\a", Some("b"));
2196 t!(s: "\\\\?\\C:\\.\\b", "\\\\?\\C:\\.", Some("b"));
2197 t!(s: "C:\\b", "\\\\?\\C:\\.", Some("..\\b"));
2198 t!(s: "\\\\?\\a\\.\\b\\c", "\\\\?\\a\\.\\b", Some("c"));
2199 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\.\\d", Some("..\\..\\b\\c"));
2200 t!(s: "\\\\?\\a\\..\\b", "\\\\?\\a\\..", Some("b"));
2201 t!(s: "\\\\?\\a\\b\\..", "\\\\?\\a\\b", Some("\\\\?\\a\\b\\.."));
2202 t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\..\\b", Some("..\\..\\b\\c"));
2204 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2205 t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", Some(".."));
2206 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2207 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2208 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2209 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\C:\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2210 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2211 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2212 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2213 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b", Some("c"));
2214 t!(s: "\\\\?\\UNC\\a\\b", "\\\\a\\b\\c", Some(".."));
2215 t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2216 t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2217 t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2218 t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2219 t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2220 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2221 t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\a\\b\\c"));
2225 fn test_str_components() {
2227 (s: $path:expr, $exp:expr) => (
2229 let path = Path::new($path);
2230 let comps = path.str_components().map(|x|x.unwrap())
2231 .collect::<~[&str]>();
2232 let exp: &[&str] = $exp;
2233 assert!(comps.as_slice() == exp,
2234 "str_components: Expected {:?}, found {:?}",
2235 comps.as_slice(), exp);
2236 let comps = path.rev_str_components().map(|x|x.unwrap())
2237 .collect::<~[&str]>();
2238 let exp = exp.rev_iter().map(|&x|x).collect::<~[&str]>();
2239 assert!(comps.as_slice() == exp,
2240 "rev_str_components: Expected {:?}, found {:?}",
2241 comps.as_slice(), exp);
2244 (v: [$($arg:expr),+], $exp:expr) => (
2246 let path = Path::new(b!($($arg),+));
2247 let comps = path.str_components().map(|x|x.unwrap()).collect::<~[&str]>();
2248 let exp: &[&str] = $exp;
2249 assert!(comps.as_slice() == exp,
2250 "str_components: Expected {:?}, found {:?}",
2251 comps.as_slice(), exp);
2252 let comps = path.rev_str_components().map(|x|x.unwrap())
2253 .collect::<~[&str]>();
2254 let exp = exp.rev_iter().map(|&x|x).collect::<~[&str]>();
2255 assert!(comps.as_slice() == exp,
2256 "rev_str_components: Expected {:?}, found {:?}",
2257 comps.as_slice(), exp);
2262 t!(v: ["a\\b\\c"], ["a", "b", "c"]);
2263 t!(s: "a\\b\\c", ["a", "b", "c"]);
2264 t!(s: "a\\b\\d", ["a", "b", "d"]);
2265 t!(s: "a\\b\\cd", ["a", "b", "cd"]);
2266 t!(s: "\\a\\b\\c", ["a", "b", "c"]);
2268 t!(s: "\\a", ["a"]);
2271 t!(s: "..", [".."]);
2272 t!(s: "..\\..", ["..", ".."]);
2273 t!(s: "..\\..\\foo", ["..", "..", "foo"]);
2274 t!(s: "C:foo\\bar", ["foo", "bar"]);
2275 t!(s: "C:foo", ["foo"]);
2277 t!(s: "C:\\foo\\bar", ["foo", "bar"]);
2278 t!(s: "C:\\foo", ["foo"]);
2280 t!(s: "\\\\server\\share\\foo\\bar", ["foo", "bar"]);
2281 t!(s: "\\\\server\\share\\foo", ["foo"]);
2282 t!(s: "\\\\server\\share", []);
2283 t!(s: "\\\\?\\foo\\bar\\baz", ["bar", "baz"]);
2284 t!(s: "\\\\?\\foo\\bar", ["bar"]);
2285 t!(s: "\\\\?\\foo", []);
2286 t!(s: "\\\\?\\", []);
2287 t!(s: "\\\\?\\a\\b", ["b"]);
2288 t!(s: "\\\\?\\a\\b\\", ["b"]);
2289 t!(s: "\\\\?\\foo\\bar\\\\baz", ["bar", "", "baz"]);
2290 t!(s: "\\\\?\\C:\\foo\\bar", ["foo", "bar"]);
2291 t!(s: "\\\\?\\C:\\foo", ["foo"]);
2292 t!(s: "\\\\?\\C:\\", []);
2293 t!(s: "\\\\?\\C:\\foo\\", ["foo"]);
2294 t!(s: "\\\\?\\UNC\\server\\share\\foo\\bar", ["foo", "bar"]);
2295 t!(s: "\\\\?\\UNC\\server\\share\\foo", ["foo"]);
2296 t!(s: "\\\\?\\UNC\\server\\share", []);
2297 t!(s: "\\\\.\\foo\\bar\\baz", ["bar", "baz"]);
2298 t!(s: "\\\\.\\foo\\bar", ["bar"]);
2299 t!(s: "\\\\.\\foo", []);
2303 fn test_components_iter() {
2305 (s: $path:expr, $exp:expr) => (
2307 let path = Path::new($path);
2308 let comps = path.components().collect::<~[&[u8]]>();
2309 let exp: &[&[u8]] = $exp;
2310 assert!(comps.as_slice() == exp, "components: Expected {:?}, found {:?}",
2311 comps.as_slice(), exp);
2312 let comps = path.rev_components().collect::<~[&[u8]]>();
2313 let exp = exp.rev_iter().map(|&x|x).collect::<~[&[u8]]>();
2314 assert!(comps.as_slice() == exp,
2315 "rev_components: Expected {:?}, found {:?}",
2316 comps.as_slice(), exp);
2321 t!(s: "a\\b\\c", [b!("a"), b!("b"), b!("c")]);
2322 t!(s: ".", [b!(".")]);
2323 // since this is really a wrapper around str_components, those tests suffice
2327 fn test_make_non_verbatim() {
2329 ($path:expr, $exp:expr) => (
2331 let path = Path::new($path);
2332 let exp: Option<&str> = $exp;
2333 let exp = exp.map(|s| Path::new(s));
2334 assert!(make_non_verbatim(&path) == exp);
2339 t!(r"\a\b\c", Some(r"\a\b\c"));
2340 t!(r"a\b\c", Some(r"a\b\c"));
2341 t!(r"C:\a\b\c", Some(r"C:\a\b\c"));
2342 t!(r"C:a\b\c", Some(r"C:a\b\c"));
2343 t!(r"\\server\share\foo", Some(r"\\server\share\foo"));
2344 t!(r"\\.\foo", None);
2345 t!(r"\\?\foo", None);
2346 t!(r"\\?\C:", None);
2347 t!(r"\\?\C:foo", None);
2348 t!(r"\\?\C:\", Some(r"C:\"));
2349 t!(r"\\?\C:\foo", Some(r"C:\foo"));
2350 t!(r"\\?\C:\foo\bar\baz", Some(r"C:\foo\bar\baz"));
2351 t!(r"\\?\C:\foo\.\bar\baz", None);
2352 t!(r"\\?\C:\foo\bar\..\baz", None);
2353 t!(r"\\?\C:\foo\bar\..", None);
2354 t!(r"\\?\UNC\server\share\foo", Some(r"\\server\share\foo"));
2355 t!(r"\\?\UNC\server\share", Some(r"\\server\share"));
2356 t!(r"\\?\UNC\server", None);
2357 t!(r"\\?\UNC\server\", None);