]> git.lizzy.rs Git - rust.git/blob - src/libstd/path/windows.rs
ec225a588fcc18bfb24616b9da80b6cf69eb6907
[rust.git] / src / libstd / path / windows.rs
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.
4 //
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.
10
11 //! Windows file path handling
12
13 use ascii::AsciiCast;
14 use c_str::{CString, ToCStr};
15 use clone::Clone;
16 use cmp::{PartialEq, Eq};
17 use collections::Collection;
18 use from_str::FromStr;
19 use hash;
20 use io::Writer;
21 use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Iterator, Map};
22 use mem;
23 use option::{Option, Some, None};
24 use slice::{Vector, ImmutableVector};
25 use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
26 use string::String;
27 use vec::Vec;
28
29 use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe};
30
31 /// Iterator that yields successive components of a Path as &str
32 ///
33 /// Each component is yielded as Option<&str> for compatibility with PosixPath, but
34 /// every component in WindowsPath is guaranteed to be Some.
35 pub type StrComponents<'a> = Map<'a, &'a str, Option<&'a str>,
36                                        CharSplits<'a, char>>;
37
38 /// Iterator that yields successive components of a Path as &[u8]
39 pub type Components<'a> = Map<'a, Option<&'a str>, &'a [u8],
40                                     StrComponents<'a>>;
41
42 /// Represents a Windows path
43 // Notes for Windows path impl:
44 // The MAX_PATH is 260, but 253 is the practical limit due to some API bugs
45 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for good information
46 // about windows paths.
47 // That same page puts a bunch of restrictions on allowed characters in a path.
48 // `\foo.txt` means "relative to current drive", but will not be considered to be absolute here
49 // as `∃P | P.join("\foo.txt") != "\foo.txt"`.
50 // `C:` is interesting, that means "the current directory on drive C".
51 // Long absolute paths need to have \\?\ prefix (or, for UNC, \\?\UNC\). I think that can be
52 // ignored for now, though, and only added in a hypothetical .to_pwstr() function.
53 // However, if a path is parsed that has \\?\, this needs to be preserved as it disables the
54 // processing of "." and ".." components and / as a separator.
55 // Experimentally, \\?\foo is not the same thing as \foo.
56 // Also, \\foo is not valid either (certainly not equivalent to \foo).
57 // Similarly, C:\\Users is not equivalent to C:\Users, although C:\Users\\foo is equivalent
58 // to C:\Users\foo. In fact the command prompt treats C:\\foo\bar as UNC path. But it might be
59 // best to just ignore that and normalize it to C:\foo\bar.
60 //
61 // Based on all this, I think the right approach is to do the following:
62 // * Require valid utf-8 paths. Windows API may use WCHARs, but we don't, and utf-8 is convertible
63 // to UTF-16 anyway (though does Windows use UTF-16 or UCS-2? Not sure).
64 // * Parse the prefixes \\?\UNC\, \\?\, and \\.\ explicitly.
65 // * If \\?\UNC\, treat following two path components as server\share. Don't error for missing
66 //   server\share.
67 // * If \\?\, parse disk from following component, if present. Don't error for missing disk.
68 // * If \\.\, treat rest of path as just regular components. I don't know how . and .. are handled
69 //   here, they probably aren't, but I'm not going to worry about that.
70 // * Else if starts with \\, treat following two components as server\share. Don't error for missing
71 //   server\share.
72 // * Otherwise, attempt to parse drive from start of path.
73 //
74 // The only error condition imposed here is valid utf-8. All other invalid paths are simply
75 // preserved by the data structure; let the Windows API error out on them.
76 #[deriving(Clone)]
77 pub struct Path {
78     repr: String, // assumed to never be empty
79     prefix: Option<PathPrefix>,
80     sepidx: Option<uint> // index of the final separator in the non-prefix portion of repr
81 }
82
83 impl PartialEq for Path {
84     #[inline]
85     fn eq(&self, other: &Path) -> bool {
86         self.repr == other.repr
87     }
88 }
89
90 impl Eq for Path {}
91
92 impl FromStr for Path {
93     fn from_str(s: &str) -> Option<Path> {
94         Path::new_opt(s)
95     }
96 }
97
98 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
99 // we cannot usefully take ToCStr arguments by reference (without forcing an
100 // additional & around &str). So we are instead temporarily adding an instance
101 // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
102 // instance should be removed, and arguments bound by ToCStr should be passed by
103 // reference.
104
105 impl ToCStr for Path {
106     #[inline]
107     fn to_c_str(&self) -> CString {
108         // The Path impl guarantees no internal NUL
109         unsafe { self.to_c_str_unchecked() }
110     }
111
112     #[inline]
113     unsafe fn to_c_str_unchecked(&self) -> CString {
114         self.as_vec().to_c_str_unchecked()
115     }
116 }
117
118 impl<'a> ToCStr for &'a Path {
119     #[inline]
120     fn to_c_str(&self) -> CString {
121         (*self).to_c_str()
122     }
123
124     #[inline]
125     unsafe fn to_c_str_unchecked(&self) -> CString {
126         (*self).to_c_str_unchecked()
127     }
128 }
129
130 impl<S: hash::Writer> hash::Hash<S> for Path {
131     #[cfg(not(test))]
132     #[inline]
133     fn hash(&self, state: &mut S) {
134         self.repr.hash(state)
135     }
136
137     #[cfg(test)]
138     #[inline]
139     fn hash(&self, _: &mut S) {
140         // No-op because the `hash` implementation will be wrong.
141     }
142 }
143
144 impl BytesContainer for Path {
145     #[inline]
146     fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
147         self.as_vec()
148     }
149     #[inline]
150     fn container_into_owned_bytes(self) -> Vec<u8> {
151         self.into_vec()
152     }
153     #[inline]
154     fn container_as_str<'a>(&'a self) -> Option<&'a str> {
155         self.as_str()
156     }
157     #[inline]
158     fn is_str(_: Option<Path>) -> bool { true }
159 }
160
161 impl<'a> BytesContainer for &'a Path {
162     #[inline]
163     fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
164         self.as_vec()
165     }
166     #[inline]
167     fn container_as_str<'a>(&'a self) -> Option<&'a str> {
168         self.as_str()
169     }
170     #[inline]
171     fn is_str(_: Option<&'a Path>) -> bool { true }
172 }
173
174 impl GenericPathUnsafe for Path {
175     /// See `GenericPathUnsafe::from_vec_unchecked`.
176     ///
177     /// # Failure
178     ///
179     /// Fails if not valid UTF-8.
180     #[inline]
181     unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
182         let (prefix, path) = Path::normalize_(path.container_as_str().unwrap());
183         assert!(!path.is_empty());
184         let mut ret = Path{ repr: path, prefix: prefix, sepidx: None };
185         ret.update_sepidx();
186         ret
187     }
188
189     /// See `GenericPathUnsafe::set_filename_unchecked`.
190     ///
191     /// # Failure
192     ///
193     /// Fails if not valid UTF-8.
194     unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
195         let filename = filename.container_as_str().unwrap();
196         match self.sepidx_or_prefix_len() {
197             None if ".." == self.repr.as_slice() => {
198                 let mut s = String::with_capacity(3 + filename.len());
199                 s.push_str("..");
200                 s.push_char(SEP);
201                 s.push_str(filename);
202                 self.update_normalized(s);
203             }
204             None => {
205                 self.update_normalized(filename);
206             }
207             Some((_,idxa,end)) if self.repr.as_slice().slice(idxa,end) == ".." => {
208                 let mut s = String::with_capacity(end + 1 + filename.len());
209                 s.push_str(self.repr.as_slice().slice_to(end));
210                 s.push_char(SEP);
211                 s.push_str(filename);
212                 self.update_normalized(s);
213             }
214             Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => {
215                 let mut s = String::with_capacity(idxb + filename.len());
216                 s.push_str(self.repr.as_slice().slice_to(idxb));
217                 s.push_str(filename);
218                 self.update_normalized(s);
219             }
220             Some((idxb,_,_)) => {
221                 let mut s = String::with_capacity(idxb + 1 + filename.len());
222                 s.push_str(self.repr.as_slice().slice_to(idxb));
223                 s.push_char(SEP);
224                 s.push_str(filename);
225                 self.update_normalized(s);
226             }
227         }
228     }
229
230     /// See `GenericPathUnsafe::push_unchecked`.
231     ///
232     /// Concatenating two Windows Paths is rather complicated.
233     /// For the most part, it will behave as expected, except in the case of
234     /// pushing a volume-relative path, e.g. `C:foo.txt`. Because we have no
235     /// concept of per-volume cwds like Windows does, we can't behave exactly
236     /// like Windows will. Instead, if the receiver is an absolute path on
237     /// the same volume as the new path, it will be treated as the cwd that
238     /// the new path is relative to. Otherwise, the new path will be treated
239     /// as if it were absolute and will replace the receiver outright.
240     unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
241         let path = path.container_as_str().unwrap();
242         fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
243             // assume prefix is Some(DiskPrefix)
244             let rest = path.slice_from(prefix_len(prefix));
245             !rest.is_empty() && rest[0].is_ascii() && is_sep(rest[0] as char)
246         }
247         fn shares_volume(me: &Path, path: &str) -> bool {
248             // path is assumed to have a prefix of Some(DiskPrefix)
249             let repr = me.repr.as_slice();
250             match me.prefix {
251                 Some(DiskPrefix) => repr[0] == path[0].to_ascii().to_upper().to_byte(),
252                 Some(VerbatimDiskPrefix) => repr[4] == path[0].to_ascii().to_upper().to_byte(),
253                 _ => false
254             }
255         }
256         fn is_sep_(prefix: Option<PathPrefix>, u: u8) -> bool {
257             if prefix_is_verbatim(prefix) { is_sep_verbatim(u as char) }
258             else { is_sep(u as char) }
259         }
260
261         fn replace_path(me: &mut Path, path: &str, prefix: Option<PathPrefix>) {
262             let newpath = Path::normalize__(path, prefix);
263             me.repr = match newpath {
264                 Some(p) => p,
265                 None => String::from_str(path)
266             };
267             me.prefix = prefix;
268             me.update_sepidx();
269         }
270         fn append_path(me: &mut Path, path: &str) {
271             // appends a path that has no prefix
272             // if me is verbatim, we need to pre-normalize the new path
273             let path_ = if is_verbatim(me) { Path::normalize__(path, None) }
274                         else { None };
275             let pathlen = path_.as_ref().map_or(path.len(), |p| p.len());
276             let mut s = String::with_capacity(me.repr.len() + 1 + pathlen);
277             s.push_str(me.repr.as_slice());
278             let plen = me.prefix_len();
279             // if me is "C:" we don't want to add a path separator
280             match me.prefix {
281                 Some(DiskPrefix) if me.repr.len() == plen => (),
282                 _ if !(me.repr.len() > plen && me.repr.as_slice()[me.repr.len()-1] == SEP_BYTE) => {
283                     s.push_char(SEP);
284                 }
285                 _ => ()
286             }
287             match path_ {
288                 None => s.push_str(path),
289                 Some(p) => s.push_str(p.as_slice())
290             };
291             me.update_normalized(s)
292         }
293
294         if !path.is_empty() {
295             let prefix = parse_prefix(path);
296             match prefix {
297                 Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => {
298                     // cwd-relative path, self is on the same volume
299                     append_path(self, path.slice_from(prefix_len(prefix)));
300                 }
301                 Some(_) => {
302                     // absolute path, or cwd-relative and self is not same volume
303                     replace_path(self, path, prefix);
304                 }
305                 None if !path.is_empty() && is_sep_(self.prefix, path[0]) => {
306                     // volume-relative path
307                     if self.prefix.is_some() {
308                         // truncate self down to the prefix, then append
309                         let n = self.prefix_len();
310                         self.repr.truncate(n);
311                         append_path(self, path);
312                     } else {
313                         // we have no prefix, so nothing to be relative to
314                         replace_path(self, path, prefix);
315                     }
316                 }
317                 None => {
318                     // relative path
319                     append_path(self, path);
320                 }
321             }
322         }
323     }
324 }
325
326 impl GenericPath for Path {
327     #[inline]
328     fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
329         match path.container_as_str() {
330             None => None,
331             Some(ref s) => {
332                 if contains_nul(s) {
333                     None
334                 } else {
335                     Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
336                 }
337             }
338         }
339     }
340
341     /// See `GenericPath::as_str` for info.
342     /// Always returns a `Some` value.
343     #[inline]
344     fn as_str<'a>(&'a self) -> Option<&'a str> {
345         Some(self.repr.as_slice())
346     }
347
348     #[inline]
349     fn as_vec<'a>(&'a self) -> &'a [u8] {
350         self.repr.as_bytes()
351     }
352
353     #[inline]
354     fn into_vec(self) -> Vec<u8> {
355         Vec::from_slice(self.repr.as_bytes())
356     }
357
358     #[inline]
359     fn dirname<'a>(&'a self) -> &'a [u8] {
360         self.dirname_str().unwrap().as_bytes()
361     }
362
363     /// See `GenericPath::dirname_str` for info.
364     /// Always returns a `Some` value.
365     fn dirname_str<'a>(&'a self) -> Option<&'a str> {
366         Some(match self.sepidx_or_prefix_len() {
367             None if ".." == self.repr.as_slice() => self.repr.as_slice(),
368             None => ".",
369             Some((_,idxa,end)) if self.repr.as_slice().slice(idxa, end) == ".." => {
370                 self.repr.as_slice()
371             }
372             Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => {
373                 self.repr.as_slice()
374             }
375             Some((0,idxa,_)) => self.repr.as_slice().slice_to(idxa),
376             Some((idxb,idxa,_)) => {
377                 match self.prefix {
378                     Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => {
379                         self.repr.as_slice().slice_to(idxa)
380                     }
381                     _ => self.repr.as_slice().slice_to(idxb)
382                 }
383             }
384         })
385     }
386
387     #[inline]
388     fn filename<'a>(&'a self) -> Option<&'a [u8]> {
389         self.filename_str().map(|x| x.as_bytes())
390     }
391
392     /// See `GenericPath::filename_str` for info.
393     /// Always returns a `Some` value if `filename` returns a `Some` value.
394     fn filename_str<'a>(&'a self) -> Option<&'a str> {
395         let repr = self.repr.as_slice();
396         match self.sepidx_or_prefix_len() {
397             None if "." == repr || ".." == repr => None,
398             None => Some(repr),
399             Some((_,idxa,end)) if repr.slice(idxa, end) == ".." => None,
400             Some((_,idxa,end)) if idxa == end => None,
401             Some((_,idxa,end)) => Some(repr.slice(idxa, end))
402         }
403     }
404
405     /// See `GenericPath::filestem_str` for info.
406     /// Always returns a `Some` value if `filestem` returns a `Some` value.
407     #[inline]
408     fn filestem_str<'a>(&'a self) -> Option<&'a str> {
409         // filestem() returns a byte vector that's guaranteed valid UTF-8
410         self.filestem().map(|t| unsafe { mem::transmute(t) })
411     }
412
413     #[inline]
414     fn extension_str<'a>(&'a self) -> Option<&'a str> {
415         // extension() returns a byte vector that's guaranteed valid UTF-8
416         self.extension().map(|t| unsafe { mem::transmute(t) })
417     }
418
419     fn dir_path(&self) -> Path {
420         unsafe { GenericPathUnsafe::new_unchecked(self.dirname_str().unwrap()) }
421     }
422
423     #[inline]
424     fn pop(&mut self) -> bool {
425         match self.sepidx_or_prefix_len() {
426             None if "." == self.repr.as_slice() => false,
427             None => {
428                 self.repr = String::from_str(".");
429                 self.sepidx = None;
430                 true
431             }
432             Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false,
433             Some((idxb,_,end)) if self.repr.as_slice().slice(idxb, end) == "\\" => false,
434             Some((idxb,idxa,_)) => {
435                 let trunc = match self.prefix {
436                     Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => {
437                         let plen = self.prefix_len();
438                         if idxb == plen { idxa } else { idxb }
439                     }
440                     _ => idxb
441                 };
442                 self.repr.truncate(trunc);
443                 self.update_sepidx();
444                 true
445             }
446         }
447     }
448
449     fn root_path(&self) -> Option<Path> {
450         if self.prefix.is_some() {
451             Some(Path::new(match self.prefix {
452                 Some(DiskPrefix) if self.is_absolute() => {
453                     self.repr.as_slice().slice_to(self.prefix_len()+1)
454                 }
455                 Some(VerbatimDiskPrefix) => {
456                     self.repr.as_slice().slice_to(self.prefix_len()+1)
457                 }
458                 _ => self.repr.as_slice().slice_to(self.prefix_len())
459             }))
460         } else if is_vol_relative(self) {
461             Some(Path::new(self.repr.as_slice().slice_to(1)))
462         } else {
463             None
464         }
465     }
466
467     /// See `GenericPath::is_absolute` for info.
468     ///
469     /// A Windows Path is considered absolute only if it has a non-volume prefix,
470     /// or if it has a volume prefix and the path starts with '\'.
471     /// A path of `\foo` is not considered absolute because it's actually
472     /// relative to the "current volume". A separate method `Path::is_vol_relative`
473     /// is provided to indicate this case. Similarly a path of `C:foo` is not
474     /// considered absolute because it's relative to the cwd on volume C:. A
475     /// separate method `Path::is_cwd_relative` is provided to indicate this case.
476     #[inline]
477     fn is_absolute(&self) -> bool {
478         match self.prefix {
479             Some(DiskPrefix) => {
480                 let rest = self.repr.as_slice().slice_from(self.prefix_len());
481                 rest.len() > 0 && rest[0] == SEP_BYTE
482             }
483             Some(_) => true,
484             None => false
485         }
486     }
487
488     #[inline]
489     fn is_relative(&self) -> bool {
490         self.prefix.is_none() && !is_vol_relative(self)
491     }
492
493     fn is_ancestor_of(&self, other: &Path) -> bool {
494         if !self.equiv_prefix(other) {
495             false
496         } else if self.is_absolute() != other.is_absolute() ||
497                   is_vol_relative(self) != is_vol_relative(other) {
498             false
499         } else {
500             let mut ita = self.str_components().map(|x|x.unwrap());
501             let mut itb = other.str_components().map(|x|x.unwrap());
502             if "." == self.repr.as_slice() {
503                 return itb.next() != Some("..");
504             }
505             loop {
506                 match (ita.next(), itb.next()) {
507                     (None, _) => break,
508                     (Some(a), Some(b)) if a == b => { continue },
509                     (Some(a), _) if a == ".." => {
510                         // if ita contains only .. components, it's an ancestor
511                         return ita.all(|x| x == "..");
512                     }
513                     _ => return false
514                 }
515             }
516             true
517         }
518     }
519
520     fn path_relative_from(&self, base: &Path) -> Option<Path> {
521         fn comp_requires_verbatim(s: &str) -> bool {
522             s == "." || s == ".." || s.contains_char(SEP2)
523         }
524
525         if !self.equiv_prefix(base) {
526             // prefixes differ
527             if self.is_absolute() {
528                 Some(self.clone())
529             } else if self.prefix == Some(DiskPrefix) && base.prefix == Some(DiskPrefix) {
530                 // both drives, drive letters must differ or they'd be equiv
531                 Some(self.clone())
532             } else {
533                 None
534             }
535         } else if self.is_absolute() != base.is_absolute() {
536             if self.is_absolute() {
537                 Some(self.clone())
538             } else {
539                 None
540             }
541         } else if is_vol_relative(self) != is_vol_relative(base) {
542             if is_vol_relative(self) {
543                 Some(self.clone())
544             } else {
545                 None
546             }
547         } else {
548             let mut ita = self.str_components().map(|x|x.unwrap());
549             let mut itb = base.str_components().map(|x|x.unwrap());
550             let mut comps = vec![];
551
552             let a_verb = is_verbatim(self);
553             let b_verb = is_verbatim(base);
554             loop {
555                 match (ita.next(), itb.next()) {
556                     (None, None) => break,
557                     (Some(a), None) if a_verb && comp_requires_verbatim(a) => {
558                         return Some(self.clone())
559                     }
560                     (Some(a), None) => {
561                         comps.push(a);
562                         if !a_verb {
563                             comps.extend(ita.by_ref());
564                             break;
565                         }
566                     }
567                     (None, _) => comps.push(".."),
568                     (Some(a), Some(b)) if comps.is_empty() && a == b => (),
569                     (Some(a), Some(b)) if !b_verb && b == "." => {
570                         if a_verb && comp_requires_verbatim(a) {
571                             return Some(self.clone())
572                         } else { comps.push(a) }
573                     }
574                     (Some(_), Some(b)) if !b_verb && b == ".." => return None,
575                     (Some(a), Some(_)) if a_verb && comp_requires_verbatim(a) => {
576                         return Some(self.clone())
577                     }
578                     (Some(a), Some(_)) => {
579                         comps.push("..");
580                         for _ in itb {
581                             comps.push("..");
582                         }
583                         comps.push(a);
584                         if !a_verb {
585                             comps.extend(ita.by_ref());
586                             break;
587                         }
588                     }
589                 }
590             }
591             Some(Path::new(comps.connect("\\")))
592         }
593     }
594
595     fn ends_with_path(&self, child: &Path) -> bool {
596         if !child.is_relative() { return false; }
597         let mut selfit = self.str_components().rev();
598         let mut childit = child.str_components().rev();
599         loop {
600             match (selfit.next(), childit.next()) {
601                 (Some(a), Some(b)) => if a != b { return false; },
602                 (Some(_), None) => break,
603                 (None, Some(_)) => return false,
604                 (None, None) => break
605             }
606         }
607         true
608     }
609 }
610
611 impl Path {
612     /// Returns a new Path from a byte vector or string
613     ///
614     /// # Failure
615     ///
616     /// Fails the task if the vector contains a NUL.
617     /// Fails if invalid UTF-8.
618     #[inline]
619     pub fn new<T: BytesContainer>(path: T) -> Path {
620         GenericPath::new(path)
621     }
622
623     /// Returns a new Path from a byte vector or string, if possible
624     #[inline]
625     pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
626         GenericPath::new_opt(path)
627     }
628
629     /// Returns an iterator that yields each component of the path in turn as a Option<&str>.
630     /// Every component is guaranteed to be Some.
631     /// Does not yield the path prefix (including server/share components in UNC paths).
632     /// Does not distinguish between volume-relative and relative paths, e.g.
633     /// \a\b\c and a\b\c.
634     /// Does not distinguish between absolute and cwd-relative paths, e.g.
635     /// C:\foo and C:foo.
636     pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
637         let repr = self.repr.as_slice();
638         let s = match self.prefix {
639             Some(_) => {
640                 let plen = self.prefix_len();
641                 if repr.len() > plen && repr[plen] == SEP_BYTE {
642                     repr.slice_from(plen+1)
643                 } else { repr.slice_from(plen) }
644             }
645             None if repr[0] == SEP_BYTE => repr.slice_from(1),
646             None => repr
647         };
648         let ret = s.split_terminator(SEP).map(Some);
649         ret
650     }
651
652     /// Returns an iterator that yields each component of the path in turn as a &[u8].
653     /// See str_components() for details.
654     pub fn components<'a>(&'a self) -> Components<'a> {
655         fn convert<'a>(x: Option<&'a str>) -> &'a [u8] {
656             #![inline]
657             x.unwrap().as_bytes()
658         }
659         self.str_components().map(convert)
660     }
661
662     fn equiv_prefix(&self, other: &Path) -> bool {
663         let s_repr = self.repr.as_slice();
664         let o_repr = other.repr.as_slice();
665         match (self.prefix, other.prefix) {
666             (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
667                 self.is_absolute() &&
668                     s_repr[0].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
669             }
670             (Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
671                 other.is_absolute() &&
672                     s_repr[4].to_ascii().eq_ignore_case(o_repr[0].to_ascii())
673             }
674             (Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
675                 s_repr[4].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
676             }
677             (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
678                 s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len())
679             }
680             (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => {
681                 s_repr.slice(8, self.prefix_len()) == o_repr.slice(2, other.prefix_len())
682             }
683             (None, None) => true,
684             (a, b) if a == b => {
685                 s_repr.slice_to(self.prefix_len()) == o_repr.slice_to(other.prefix_len())
686             }
687             _ => false
688         }
689     }
690
691     fn normalize_<S: StrAllocating>(s: S) -> (Option<PathPrefix>, String) {
692         // make borrowck happy
693         let (prefix, val) = {
694             let prefix = parse_prefix(s.as_slice());
695             let path = Path::normalize__(s.as_slice(), prefix);
696             (prefix, path)
697         };
698         (prefix, match val {
699             None => s.into_string(),
700             Some(val) => val
701         })
702     }
703
704     fn normalize__(s: &str, prefix: Option<PathPrefix>) -> Option<String> {
705         if prefix_is_verbatim(prefix) {
706             // don't do any normalization
707             match prefix {
708                 Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
709                     // the server component has no trailing '\'
710                     let mut s = String::from_str(s);
711                     s.push_char(SEP);
712                     Some(s)
713                 }
714                 _ => None
715             }
716         } else {
717             let (is_abs, comps) = normalize_helper(s, prefix);
718             let mut comps = comps;
719             match (comps.is_some(),prefix) {
720                 (false, Some(DiskPrefix)) => {
721                     if s[0] >= 'a' as u8 && s[0] <= 'z' as u8 {
722                         comps = Some(vec![]);
723                     }
724                 }
725                 (false, Some(VerbatimDiskPrefix)) => {
726                     if s[4] >= 'a' as u8 && s[0] <= 'z' as u8 {
727                         comps = Some(vec![]);
728                     }
729                 }
730                 _ => ()
731             }
732             match comps {
733                 None => None,
734                 Some(comps) => {
735                     if prefix.is_some() && comps.is_empty() {
736                         match prefix.unwrap() {
737                             DiskPrefix => {
738                                 let len = prefix_len(prefix) + is_abs as uint;
739                                 let mut s = String::from_str(s.slice_to(len));
740                                 unsafe {
741                                     let v = s.as_mut_vec();
742                                     *v.get_mut(0) = v.get(0)
743                                                      .to_ascii()
744                                                      .to_upper()
745                                                      .to_byte();
746                                 }
747                                 if is_abs {
748                                     // normalize C:/ to C:\
749                                     unsafe {
750                                         *s.as_mut_vec().get_mut(2) = SEP_BYTE;
751                                     }
752                                 }
753                                 Some(s)
754                             }
755                             VerbatimDiskPrefix => {
756                                 let len = prefix_len(prefix) + is_abs as uint;
757                                 let mut s = String::from_str(s.slice_to(len));
758                                 unsafe {
759                                     let v = s.as_mut_vec();
760                                     *v.get_mut(4) = v.get(4).to_ascii().to_upper().to_byte();
761                                 }
762                                 Some(s)
763                             }
764                             _ => {
765                                 let plen = prefix_len(prefix);
766                                 if s.len() > plen {
767                                     Some(String::from_str(s.slice_to(plen)))
768                                 } else { None }
769                             }
770                         }
771                     } else if is_abs && comps.is_empty() {
772                         Some(String::from_char(1, SEP))
773                     } else {
774                         let prefix_ = s.slice_to(prefix_len(prefix));
775                         let n = prefix_.len() +
776                                 if is_abs { comps.len() } else { comps.len() - 1} +
777                                 comps.iter().map(|v| v.len()).sum();
778                         let mut s = String::with_capacity(n);
779                         match prefix {
780                             Some(DiskPrefix) => {
781                                 s.push_char(prefix_[0].to_ascii().to_upper().to_char());
782                                 s.push_char(':');
783                             }
784                             Some(VerbatimDiskPrefix) => {
785                                 s.push_str(prefix_.slice_to(4));
786                                 s.push_char(prefix_[4].to_ascii().to_upper().to_char());
787                                 s.push_str(prefix_.slice_from(5));
788                             }
789                             Some(UNCPrefix(a,b)) => {
790                                 s.push_str("\\\\");
791                                 s.push_str(prefix_.slice(2, a+2));
792                                 s.push_char(SEP);
793                                 s.push_str(prefix_.slice(3+a, 3+a+b));
794                             }
795                             Some(_) => s.push_str(prefix_),
796                             None => ()
797                         }
798                         let mut it = comps.move_iter();
799                         if !is_abs {
800                             match it.next() {
801                                 None => (),
802                                 Some(comp) => s.push_str(comp)
803                             }
804                         }
805                         for comp in it {
806                             s.push_char(SEP);
807                             s.push_str(comp);
808                         }
809                         Some(s)
810                     }
811                 }
812             }
813         }
814     }
815
816     fn update_sepidx(&mut self) {
817         let s = if self.has_nonsemantic_trailing_slash() {
818                     self.repr.as_slice().slice_to(self.repr.len()-1)
819                 } else { self.repr.as_slice() };
820         let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep }
821                           else { is_sep_verbatim });
822         let prefixlen = self.prefix_len();
823         self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
824     }
825
826     fn prefix_len(&self) -> uint {
827         prefix_len(self.prefix)
828     }
829
830     // Returns a tuple (before, after, end) where before is the index of the separator
831     // and after is the index just after the separator.
832     // end is the length of the string, normally, or the index of the final character if it is
833     // a non-semantic trailing separator in a verbatim string.
834     // If the prefix is considered the separator, before and after are the same.
835     fn sepidx_or_prefix_len(&self) -> Option<(uint,uint,uint)> {
836         match self.sepidx {
837             None => match self.prefix_len() { 0 => None, x => Some((x,x,self.repr.len())) },
838             Some(x) => {
839                 if self.has_nonsemantic_trailing_slash() {
840                     Some((x,x+1,self.repr.len()-1))
841                 } else { Some((x,x+1,self.repr.len())) }
842             }
843         }
844     }
845
846     fn has_nonsemantic_trailing_slash(&self) -> bool {
847         is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
848             self.repr.as_slice()[self.repr.len()-1] == SEP_BYTE
849     }
850
851     fn update_normalized<S: Str>(&mut self, s: S) {
852         let (prefix, path) = Path::normalize_(s.as_slice());
853         self.repr = path;
854         self.prefix = prefix;
855         self.update_sepidx();
856     }
857 }
858
859 /// Returns whether the path is considered "volume-relative", which means a path
860 /// that looks like "\foo". Paths of this form are relative to the current volume,
861 /// but absolute within that volume.
862 #[inline]
863 pub fn is_vol_relative(path: &Path) -> bool {
864     path.prefix.is_none() && is_sep_byte(&path.repr.as_slice()[0])
865 }
866
867 /// Returns whether the path is considered "cwd-relative", which means a path
868 /// with a volume prefix that is not absolute. This look like "C:foo.txt". Paths
869 /// of this form are relative to the cwd on the given volume.
870 #[inline]
871 pub fn is_cwd_relative(path: &Path) -> bool {
872     path.prefix == Some(DiskPrefix) && !path.is_absolute()
873 }
874
875 /// Returns the PathPrefix for this Path
876 #[inline]
877 pub fn prefix(path: &Path) -> Option<PathPrefix> {
878     path.prefix
879 }
880
881 /// Returns whether the Path's prefix is a verbatim prefix, i.e. `\\?\`
882 #[inline]
883 pub fn is_verbatim(path: &Path) -> bool {
884     prefix_is_verbatim(path.prefix)
885 }
886
887 /// Returns the non-verbatim equivalent of the input path, if possible.
888 /// If the input path is a device namespace path, None is returned.
889 /// If the input path is not verbatim, it is returned as-is.
890 /// If the input path is verbatim, but the same path can be expressed as
891 /// non-verbatim, the non-verbatim version is returned.
892 /// Otherwise, None is returned.
893 pub fn make_non_verbatim(path: &Path) -> Option<Path> {
894     let repr = path.repr.as_slice();
895     let new_path = match path.prefix {
896         Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None,
897         Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()),
898         Some(VerbatimDiskPrefix) => {
899             // \\?\D:\
900             Path::new(repr.slice_from(4))
901         }
902         Some(VerbatimUNCPrefix(_,_)) => {
903             // \\?\UNC\server\share
904             Path::new(format!(r"\{}", repr.slice_from(7)))
905         }
906     };
907     if new_path.prefix.is_none() {
908         // \\?\UNC\server is a VerbatimUNCPrefix
909         // but \\server is nothing
910         return None;
911     }
912     // now ensure normalization didn't change anything
913     if repr.slice_from(path.prefix_len()) ==
914         new_path.repr.as_slice().slice_from(new_path.prefix_len()) {
915         Some(new_path)
916     } else {
917         None
918     }
919 }
920
921 /// The standard path separator character
922 pub static SEP: char = '\\';
923 /// The standard path separator byte
924 pub static SEP_BYTE: u8 = SEP as u8;
925
926 /// The alternative path separator character
927 pub static SEP2: char = '/';
928 /// The alternative path separator character
929 pub static SEP2_BYTE: u8 = SEP2 as u8;
930
931 /// Returns whether the given char is a path separator.
932 /// Allows both the primary separator '\' and the alternative separator '/'.
933 #[inline]
934 pub fn is_sep(c: char) -> bool {
935     c == SEP || c == SEP2
936 }
937
938 /// Returns whether the given char is a path separator.
939 /// Only allows the primary separator '\'; use is_sep to allow '/'.
940 #[inline]
941 pub fn is_sep_verbatim(c: char) -> bool {
942     c == SEP
943 }
944
945 /// Returns whether the given byte is a path separator.
946 /// Allows both the primary separator '\' and the alternative separator '/'.
947 #[inline]
948 pub fn is_sep_byte(u: &u8) -> bool {
949     *u == SEP_BYTE || *u == SEP2_BYTE
950 }
951
952 /// Returns whether the given byte is a path separator.
953 /// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
954 #[inline]
955 pub fn is_sep_byte_verbatim(u: &u8) -> bool {
956     *u == SEP_BYTE
957 }
958
959 /// Prefix types for Path
960 #[deriving(PartialEq, Clone)]
961 pub enum PathPrefix {
962     /// Prefix `\\?\`, uint is the length of the following component
963     VerbatimPrefix(uint),
964     /// Prefix `\\?\UNC\`, uints are the lengths of the UNC components
965     VerbatimUNCPrefix(uint, uint),
966     /// Prefix `\\?\C:\` (for any alphabetic character)
967     VerbatimDiskPrefix,
968     /// Prefix `\\.\`, uint is the length of the following component
969     DeviceNSPrefix(uint),
970     /// UNC prefix `\\server\share`, uints are the lengths of the server/share
971     UNCPrefix(uint, uint),
972     /// Prefix `C:` for any alphabetic character
973     DiskPrefix
974 }
975
976 fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
977     if path.starts_with("\\\\") {
978         // \\
979         path = path.slice_from(2);
980         if path.starts_with("?\\") {
981             // \\?\
982             path = path.slice_from(2);
983             if path.starts_with("UNC\\") {
984                 // \\?\UNC\server\share
985                 path = path.slice_from(4);
986                 let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) {
987                     Some(x) => x,
988                     None => (path.len(), 0)
989                 };
990                 return Some(VerbatimUNCPrefix(idx_a, idx_b));
991             } else {
992                 // \\?\path
993                 let idx = path.find('\\');
994                 if idx == Some(2) && path[1] == ':' as u8 {
995                     let c = path[0];
996                     if c.is_ascii() && ::char::is_alphabetic(c as char) {
997                         // \\?\C:\ path
998                         return Some(VerbatimDiskPrefix);
999                     }
1000                 }
1001                 let idx = idx.unwrap_or(path.len());
1002                 return Some(VerbatimPrefix(idx));
1003             }
1004         } else if path.starts_with(".\\") {
1005             // \\.\path
1006             path = path.slice_from(2);
1007             let idx = path.find('\\').unwrap_or(path.len());
1008             return Some(DeviceNSPrefix(idx));
1009         }
1010         match parse_two_comps(path, is_sep) {
1011             Some((idx_a, idx_b)) if idx_a > 0 && idx_b > 0 => {
1012                 // \\server\share
1013                 return Some(UNCPrefix(idx_a, idx_b));
1014             }
1015             _ => ()
1016         }
1017     } else if path.len() > 1 && path[1] == ':' as u8 {
1018         // C:
1019         let c = path[0];
1020         if c.is_ascii() && ::char::is_alphabetic(c as char) {
1021             return Some(DiskPrefix);
1022         }
1023     }
1024     return None;
1025
1026     fn parse_two_comps<'a>(mut path: &'a str, f: |char| -> bool)
1027                        -> Option<(uint, uint)> {
1028         let idx_a = match path.find(|x| f(x)) {
1029             None => return None,
1030             Some(x) => x
1031         };
1032         path = path.slice_from(idx_a+1);
1033         let idx_b = path.find(f).unwrap_or(path.len());
1034         Some((idx_a, idx_b))
1035     }
1036 }
1037
1038 // None result means the string didn't need normalizing
1039 fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) {
1040     let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim };
1041     let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
1042     let s_ = s.slice_from(prefix_len(prefix));
1043     let s_ = if is_abs { s_.slice_from(1) } else { s_ };
1044
1045     if is_abs && s_.is_empty() {
1046         return (is_abs, match prefix {
1047             Some(DiskPrefix) | None => (if is_sep_verbatim(s.char_at(prefix_len(prefix))) { None }
1048                                         else { Some(vec![]) }),
1049             Some(_) => Some(vec![]), // need to trim the trailing separator
1050         });
1051     }
1052     let mut comps: Vec<&'a str> = vec![];
1053     let mut n_up = 0u;
1054     let mut changed = false;
1055     for comp in s_.split(f) {
1056         if comp.is_empty() { changed = true }
1057         else if comp == "." { changed = true }
1058         else if comp == ".." {
1059             let has_abs_prefix = match prefix {
1060                 Some(DiskPrefix) => false,
1061                 Some(_) => true,
1062                 None => false
1063             };
1064             if (is_abs || has_abs_prefix) && comps.is_empty() { changed = true }
1065             else if comps.len() == n_up { comps.push(".."); n_up += 1 }
1066             else { comps.pop().unwrap(); changed = true }
1067         } else { comps.push(comp) }
1068     }
1069     if !changed && !prefix_is_verbatim(prefix) {
1070         changed = s.find(is_sep).is_some();
1071     }
1072     if changed {
1073         if comps.is_empty() && !is_abs && prefix.is_none() {
1074             if s == "." {
1075                 return (is_abs, None);
1076             }
1077             comps.push(".");
1078         }
1079         (is_abs, Some(comps))
1080     } else {
1081         (is_abs, None)
1082     }
1083 }
1084
1085 fn prefix_is_verbatim(p: Option<PathPrefix>) -> bool {
1086     match p {
1087         Some(VerbatimPrefix(_)) | Some(VerbatimUNCPrefix(_,_)) | Some(VerbatimDiskPrefix) => true,
1088         Some(DeviceNSPrefix(_)) => true, // not really sure, but I think so
1089         _ => false
1090     }
1091 }
1092
1093 fn prefix_len(p: Option<PathPrefix>) -> uint {
1094     match p {
1095         None => 0,
1096         Some(VerbatimPrefix(x)) => 4 + x,
1097         Some(VerbatimUNCPrefix(x,y)) => 8 + x + 1 + y,
1098         Some(VerbatimDiskPrefix) => 6,
1099         Some(UNCPrefix(x,y)) => 2 + x + 1 + y,
1100         Some(DeviceNSPrefix(x)) => 4 + x,
1101         Some(DiskPrefix) => 2
1102     }
1103 }
1104
1105 #[cfg(test)]
1106 mod tests {
1107     use prelude::*;
1108     use super::*;
1109     use super::parse_prefix;
1110
1111     macro_rules! t(
1112         (s: $path:expr, $exp:expr) => (
1113             {
1114                 let path = $path;
1115                 assert!(path.as_str() == Some($exp));
1116             }
1117         );
1118         (v: $path:expr, $exp:expr) => (
1119             {
1120                 let path = $path;
1121                 assert!(path.as_vec() == $exp);
1122             }
1123         )
1124     )
1125
1126     #[test]
1127     fn test_parse_prefix() {
1128         macro_rules! t(
1129             ($path:expr, $exp:expr) => (
1130                 {
1131                     let path = $path;
1132                     let exp = $exp;
1133                     let res = parse_prefix(path);
1134                     assert!(res == exp,
1135                             "parse_prefix(\"{}\"): expected {:?}, found {:?}", path, exp, res);
1136                 }
1137             )
1138         )
1139
1140         t!("\\\\SERVER\\share\\foo", Some(UNCPrefix(6,5)));
1141         t!("\\\\", None);
1142         t!("\\\\SERVER", None);
1143         t!("\\\\SERVER\\", None);
1144         t!("\\\\SERVER\\\\", None);
1145         t!("\\\\SERVER\\\\foo", None);
1146         t!("\\\\SERVER\\share", Some(UNCPrefix(6,5)));
1147         t!("\\\\SERVER/share/foo", Some(UNCPrefix(6,5)));
1148         t!("\\\\SERVER\\share/foo", Some(UNCPrefix(6,5)));
1149         t!("//SERVER/share/foo", None);
1150         t!("\\\\\\a\\b\\c", None);
1151         t!("\\\\?\\a\\b\\c", Some(VerbatimPrefix(1)));
1152         t!("\\\\?\\a/b/c", Some(VerbatimPrefix(5)));
1153         t!("//?/a/b/c", None);
1154         t!("\\\\.\\a\\b", Some(DeviceNSPrefix(1)));
1155         t!("\\\\.\\a/b", Some(DeviceNSPrefix(3)));
1156         t!("//./a/b", None);
1157         t!("\\\\?\\UNC\\server\\share\\foo", Some(VerbatimUNCPrefix(6,5)));
1158         t!("\\\\?\\UNC\\\\share\\foo", Some(VerbatimUNCPrefix(0,5)));
1159         t!("\\\\?\\UNC\\", Some(VerbatimUNCPrefix(0,0)));
1160         t!("\\\\?\\UNC\\server/share/foo", Some(VerbatimUNCPrefix(16,0)));
1161         t!("\\\\?\\UNC\\server", Some(VerbatimUNCPrefix(6,0)));
1162         t!("\\\\?\\UNC\\server\\", Some(VerbatimUNCPrefix(6,0)));
1163         t!("\\\\?\\UNC/server/share", Some(VerbatimPrefix(16)));
1164         t!("\\\\?\\UNC", Some(VerbatimPrefix(3)));
1165         t!("\\\\?\\C:\\a\\b.txt", Some(VerbatimDiskPrefix));
1166         t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1167         t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1168         t!("\\\\?\\C:a.txt", Some(VerbatimPrefix(7)));
1169         t!("\\\\?\\C:a\\b.txt", Some(VerbatimPrefix(3)));
1170         t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1171         t!("C:\\foo", Some(DiskPrefix));
1172         t!("z:/foo", Some(DiskPrefix));
1173         t!("d:", Some(DiskPrefix));
1174         t!("ab:", None);
1175         t!("ü:\\foo", None);
1176         t!("3:\\foo", None);
1177         t!(" :\\foo", None);
1178         t!("::\\foo", None);
1179         t!("\\\\?\\C:", Some(VerbatimPrefix(2)));
1180         t!("\\\\?\\z:\\", Some(VerbatimDiskPrefix));
1181         t!("\\\\?\\ab:\\", Some(VerbatimPrefix(3)));
1182         t!("\\\\?\\C:\\a", Some(VerbatimDiskPrefix));
1183         t!("\\\\?\\C:/a", Some(VerbatimPrefix(4)));
1184         t!("\\\\?\\C:\\a/b", Some(VerbatimDiskPrefix));
1185     }
1186
1187     #[test]
1188     fn test_paths() {
1189         let empty: &[u8] = [];
1190         t!(v: Path::new(empty), b".");
1191         t!(v: Path::new(b"\\"), b"\\");
1192         t!(v: Path::new(b"a\\b\\c"), b"a\\b\\c");
1193
1194         t!(s: Path::new(""), ".");
1195         t!(s: Path::new("\\"), "\\");
1196         t!(s: Path::new("hi"), "hi");
1197         t!(s: Path::new("hi\\"), "hi");
1198         t!(s: Path::new("\\lib"), "\\lib");
1199         t!(s: Path::new("\\lib\\"), "\\lib");
1200         t!(s: Path::new("hi\\there"), "hi\\there");
1201         t!(s: Path::new("hi\\there.txt"), "hi\\there.txt");
1202         t!(s: Path::new("/"), "\\");
1203         t!(s: Path::new("hi/"), "hi");
1204         t!(s: Path::new("/lib"), "\\lib");
1205         t!(s: Path::new("/lib/"), "\\lib");
1206         t!(s: Path::new("hi/there"), "hi\\there");
1207
1208         t!(s: Path::new("hi\\there\\"), "hi\\there");
1209         t!(s: Path::new("hi\\..\\there"), "there");
1210         t!(s: Path::new("hi/../there"), "there");
1211         t!(s: Path::new("..\\hi\\there"), "..\\hi\\there");
1212         t!(s: Path::new("\\..\\hi\\there"), "\\hi\\there");
1213         t!(s: Path::new("/../hi/there"), "\\hi\\there");
1214         t!(s: Path::new("foo\\.."), ".");
1215         t!(s: Path::new("\\foo\\.."), "\\");
1216         t!(s: Path::new("\\foo\\..\\.."), "\\");
1217         t!(s: Path::new("\\foo\\..\\..\\bar"), "\\bar");
1218         t!(s: Path::new("\\.\\hi\\.\\there\\."), "\\hi\\there");
1219         t!(s: Path::new("\\.\\hi\\.\\there\\.\\.."), "\\hi");
1220         t!(s: Path::new("foo\\..\\.."), "..");
1221         t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
1222         t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
1223
1224         assert_eq!(Path::new(b"foo\\bar").into_vec().as_slice(), b"foo\\bar");
1225         assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec().as_slice(), b"\\bar");
1226
1227         t!(s: Path::new("\\\\a"), "\\a");
1228         t!(s: Path::new("\\\\a\\"), "\\a");
1229         t!(s: Path::new("\\\\a\\b"), "\\\\a\\b");
1230         t!(s: Path::new("\\\\a\\b\\"), "\\\\a\\b");
1231         t!(s: Path::new("\\\\a\\b/"), "\\\\a\\b");
1232         t!(s: Path::new("\\\\\\b"), "\\b");
1233         t!(s: Path::new("\\\\a\\\\b"), "\\a\\b");
1234         t!(s: Path::new("\\\\a\\b\\c"), "\\\\a\\b\\c");
1235         t!(s: Path::new("\\\\server\\share/path"), "\\\\server\\share\\path");
1236         t!(s: Path::new("\\\\server/share/path"), "\\\\server\\share\\path");
1237         t!(s: Path::new("C:a\\b.txt"), "C:a\\b.txt");
1238         t!(s: Path::new("C:a/b.txt"), "C:a\\b.txt");
1239         t!(s: Path::new("z:\\a\\b.txt"), "Z:\\a\\b.txt");
1240         t!(s: Path::new("z:/a/b.txt"), "Z:\\a\\b.txt");
1241         t!(s: Path::new("ab:/a/b.txt"), "ab:\\a\\b.txt");
1242         t!(s: Path::new("C:\\"), "C:\\");
1243         t!(s: Path::new("C:"), "C:");
1244         t!(s: Path::new("q:"), "Q:");
1245         t!(s: Path::new("C:/"), "C:\\");
1246         t!(s: Path::new("C:\\foo\\.."), "C:\\");
1247         t!(s: Path::new("C:foo\\.."), "C:");
1248         t!(s: Path::new("C:\\a\\"), "C:\\a");
1249         t!(s: Path::new("C:\\a/"), "C:\\a");
1250         t!(s: Path::new("C:\\a\\b\\"), "C:\\a\\b");
1251         t!(s: Path::new("C:\\a\\b/"), "C:\\a\\b");
1252         t!(s: Path::new("C:a\\"), "C:a");
1253         t!(s: Path::new("C:a/"), "C:a");
1254         t!(s: Path::new("C:a\\b\\"), "C:a\\b");
1255         t!(s: Path::new("C:a\\b/"), "C:a\\b");
1256         t!(s: Path::new("\\\\?\\z:\\a\\b.txt"), "\\\\?\\z:\\a\\b.txt");
1257         t!(s: Path::new("\\\\?\\C:/a/b.txt"), "\\\\?\\C:/a/b.txt");
1258         t!(s: Path::new("\\\\?\\C:\\a/b.txt"), "\\\\?\\C:\\a/b.txt");
1259         t!(s: Path::new("\\\\?\\test\\a\\b.txt"), "\\\\?\\test\\a\\b.txt");
1260         t!(s: Path::new("\\\\?\\foo\\bar\\"), "\\\\?\\foo\\bar\\");
1261         t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1262         t!(s: Path::new("\\\\.\\"), "\\\\.\\");
1263         t!(s: Path::new("\\\\?\\UNC\\server\\share\\foo"), "\\\\?\\UNC\\server\\share\\foo");
1264         t!(s: Path::new("\\\\?\\UNC\\server/share"), "\\\\?\\UNC\\server/share\\");
1265         t!(s: Path::new("\\\\?\\UNC\\server"), "\\\\?\\UNC\\server\\");
1266         t!(s: Path::new("\\\\?\\UNC\\"), "\\\\?\\UNC\\\\");
1267         t!(s: Path::new("\\\\?\\UNC"), "\\\\?\\UNC");
1268
1269         // I'm not sure whether \\.\foo/bar should normalize to \\.\foo\bar
1270         // as information is sparse and this isn't really googleable.
1271         // I'm going to err on the side of not normalizing it, as this skips the filesystem
1272         t!(s: Path::new("\\\\.\\foo/bar"), "\\\\.\\foo/bar");
1273         t!(s: Path::new("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
1274     }
1275
1276     #[test]
1277     fn test_opt_paths() {
1278         assert!(Path::new_opt(b"foo\\bar\0") == None);
1279         assert!(Path::new_opt(b"foo\\bar\x80") == None);
1280         t!(v: Path::new_opt(b"foo\\bar").unwrap(), b"foo\\bar");
1281         assert!(Path::new_opt("foo\\bar\0") == None);
1282         t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar");
1283     }
1284
1285     #[test]
1286     fn test_null_byte() {
1287         use task;
1288         let result = task::try(proc() {
1289             Path::new(b"foo/bar\0")
1290         });
1291         assert!(result.is_err());
1292
1293         let result = task::try(proc() {
1294             Path::new("test").set_filename(b"f\0o")
1295         });
1296         assert!(result.is_err());
1297
1298         let result = task::try(proc() {
1299             Path::new("test").push(b"f\0o");
1300         });
1301         assert!(result.is_err());
1302     }
1303
1304     #[test]
1305     #[should_fail]
1306     fn test_not_utf8_fail() {
1307         Path::new(b"hello\x80.txt");
1308     }
1309
1310     #[test]
1311     fn test_display_str() {
1312         let path = Path::new("foo");
1313         assert_eq!(path.display().to_str(), "foo".to_string());
1314         let path = Path::new(b"\\");
1315         assert_eq!(path.filename_display().to_str(), "".to_string());
1316
1317         let path = Path::new("foo");
1318         let mo = path.display().as_maybe_owned();
1319         assert_eq!(mo.as_slice(), "foo");
1320         let path = Path::new(b"\\");
1321         let mo = path.filename_display().as_maybe_owned();
1322         assert_eq!(mo.as_slice(), "");
1323     }
1324
1325     #[test]
1326     fn test_display() {
1327         macro_rules! t(
1328             ($path:expr, $exp:expr, $expf:expr) => (
1329                 {
1330                     let path = Path::new($path);
1331                     let f = format!("{}", path.display());
1332                     assert_eq!(f.as_slice(), $exp);
1333                     let f = format!("{}", path.filename_display());
1334                     assert_eq!(f.as_slice(), $expf);
1335                 }
1336             )
1337         )
1338
1339         t!("foo", "foo", "foo");
1340         t!("foo\\bar", "foo\\bar", "bar");
1341         t!("\\", "\\", "");
1342     }
1343
1344     #[test]
1345     fn test_components() {
1346         macro_rules! t(
1347             (s: $path:expr, $op:ident, $exp:expr) => (
1348                 {
1349                     let path = $path;
1350                     let path = Path::new(path);
1351                     assert!(path.$op() == Some($exp));
1352                 }
1353             );
1354             (s: $path:expr, $op:ident, $exp:expr, opt) => (
1355                 {
1356                     let path = $path;
1357                     let path = Path::new(path);
1358                     let left = path.$op();
1359                     assert!(left == $exp);
1360                 }
1361             );
1362             (v: $path:expr, $op:ident, $exp:expr) => (
1363                 {
1364                     let path = $path;
1365                     let path = Path::new(path);
1366                     assert!(path.$op() == $exp);
1367                 }
1368             )
1369         )
1370
1371         t!(v: b"a\\b\\c", filename, Some(b"c"));
1372         t!(s: "a\\b\\c", filename_str, "c");
1373         t!(s: "\\a\\b\\c", filename_str, "c");
1374         t!(s: "a", filename_str, "a");
1375         t!(s: "\\a", filename_str, "a");
1376         t!(s: ".", filename_str, None, opt);
1377         t!(s: "\\", filename_str, None, opt);
1378         t!(s: "..", filename_str, None, opt);
1379         t!(s: "..\\..", filename_str, None, opt);
1380         t!(s: "c:\\foo.txt", filename_str, "foo.txt");
1381         t!(s: "C:\\", filename_str, None, opt);
1382         t!(s: "C:", filename_str, None, opt);
1383         t!(s: "\\\\server\\share\\foo.txt", filename_str, "foo.txt");
1384         t!(s: "\\\\server\\share", filename_str, None, opt);
1385         t!(s: "\\\\server", filename_str, "server");
1386         t!(s: "\\\\?\\bar\\foo.txt", filename_str, "foo.txt");
1387         t!(s: "\\\\?\\bar", filename_str, None, opt);
1388         t!(s: "\\\\?\\", filename_str, None, opt);
1389         t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", filename_str, "foo.txt");
1390         t!(s: "\\\\?\\UNC\\server", filename_str, None, opt);
1391         t!(s: "\\\\?\\UNC\\", filename_str, None, opt);
1392         t!(s: "\\\\?\\C:\\foo.txt", filename_str, "foo.txt");
1393         t!(s: "\\\\?\\C:\\", filename_str, None, opt);
1394         t!(s: "\\\\?\\C:", filename_str, None, opt);
1395         t!(s: "\\\\?\\foo/bar", filename_str, None, opt);
1396         t!(s: "\\\\?\\C:/foo", filename_str, None, opt);
1397         t!(s: "\\\\.\\foo\\bar", filename_str, "bar");
1398         t!(s: "\\\\.\\foo", filename_str, None, opt);
1399         t!(s: "\\\\.\\foo/bar", filename_str, None, opt);
1400         t!(s: "\\\\.\\foo\\bar/baz", filename_str, "bar/baz");
1401         t!(s: "\\\\.\\", filename_str, None, opt);
1402         t!(s: "\\\\?\\a\\b\\", filename_str, "b");
1403
1404         t!(v: b"a\\b\\c", dirname, b"a\\b");
1405         t!(s: "a\\b\\c", dirname_str, "a\\b");
1406         t!(s: "\\a\\b\\c", dirname_str, "\\a\\b");
1407         t!(s: "a", dirname_str, ".");
1408         t!(s: "\\a", dirname_str, "\\");
1409         t!(s: ".", dirname_str, ".");
1410         t!(s: "\\", dirname_str, "\\");
1411         t!(s: "..", dirname_str, "..");
1412         t!(s: "..\\..", dirname_str, "..\\..");
1413         t!(s: "c:\\foo.txt", dirname_str, "C:\\");
1414         t!(s: "C:\\", dirname_str, "C:\\");
1415         t!(s: "C:", dirname_str, "C:");
1416         t!(s: "C:foo.txt", dirname_str, "C:");
1417         t!(s: "\\\\server\\share\\foo.txt", dirname_str, "\\\\server\\share");
1418         t!(s: "\\\\server\\share", dirname_str, "\\\\server\\share");
1419         t!(s: "\\\\server", dirname_str, "\\");
1420         t!(s: "\\\\?\\bar\\foo.txt", dirname_str, "\\\\?\\bar");
1421         t!(s: "\\\\?\\bar", dirname_str, "\\\\?\\bar");
1422         t!(s: "\\\\?\\", dirname_str, "\\\\?\\");
1423         t!(s: "\\\\?\\UNC\\server\\share\\foo.txt", dirname_str, "\\\\?\\UNC\\server\\share");
1424         t!(s: "\\\\?\\UNC\\server", dirname_str, "\\\\?\\UNC\\server\\");
1425         t!(s: "\\\\?\\UNC\\", dirname_str, "\\\\?\\UNC\\\\");
1426         t!(s: "\\\\?\\C:\\foo.txt", dirname_str, "\\\\?\\C:\\");
1427         t!(s: "\\\\?\\C:\\", dirname_str, "\\\\?\\C:\\");
1428         t!(s: "\\\\?\\C:", dirname_str, "\\\\?\\C:");
1429         t!(s: "\\\\?\\C:/foo/bar", dirname_str, "\\\\?\\C:/foo/bar");
1430         t!(s: "\\\\?\\foo/bar", dirname_str, "\\\\?\\foo/bar");
1431         t!(s: "\\\\.\\foo\\bar", dirname_str, "\\\\.\\foo");
1432         t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
1433         t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
1434
1435         t!(v: b"hi\\there.txt", filestem, Some(b"there"));
1436         t!(s: "hi\\there.txt", filestem_str, "there");
1437         t!(s: "hi\\there", filestem_str, "there");
1438         t!(s: "there.txt", filestem_str, "there");
1439         t!(s: "there", filestem_str, "there");
1440         t!(s: ".", filestem_str, None, opt);
1441         t!(s: "\\", filestem_str, None, opt);
1442         t!(s: "foo\\.bar", filestem_str, ".bar");
1443         t!(s: ".bar", filestem_str, ".bar");
1444         t!(s: "..bar", filestem_str, ".");
1445         t!(s: "hi\\there..txt", filestem_str, "there.");
1446         t!(s: "..", filestem_str, None, opt);
1447         t!(s: "..\\..", filestem_str, None, opt);
1448         // filestem is based on filename, so we don't need the full set of prefix tests
1449
1450         t!(v: b"hi\\there.txt", extension, Some(b"txt"));
1451         t!(v: b"hi\\there", extension, None);
1452         t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
1453         t!(s: "hi\\there", extension_str, None, opt);
1454         t!(s: "there.txt", extension_str, Some("txt"), opt);
1455         t!(s: "there", extension_str, None, opt);
1456         t!(s: ".", extension_str, None, opt);
1457         t!(s: "\\", extension_str, None, opt);
1458         t!(s: "foo\\.bar", extension_str, None, opt);
1459         t!(s: ".bar", extension_str, None, opt);
1460         t!(s: "..bar", extension_str, Some("bar"), opt);
1461         t!(s: "hi\\there..txt", extension_str, Some("txt"), opt);
1462         t!(s: "..", extension_str, None, opt);
1463         t!(s: "..\\..", extension_str, None, opt);
1464         // extension is based on filename, so we don't need the full set of prefix tests
1465     }
1466
1467     #[test]
1468     fn test_push() {
1469         macro_rules! t(
1470             (s: $path:expr, $join:expr) => (
1471                 {
1472                     let path = $path;
1473                     let join = $join;
1474                     let mut p1 = Path::new(path);
1475                     let p2 = p1.clone();
1476                     p1.push(join);
1477                     assert!(p1 == p2.join(join));
1478                 }
1479             )
1480         )
1481
1482         t!(s: "a\\b\\c", "..");
1483         t!(s: "\\a\\b\\c", "d");
1484         t!(s: "a\\b", "c\\d");
1485         t!(s: "a\\b", "\\c\\d");
1486         // this is just a sanity-check test. push and join share an implementation,
1487         // so there's no need for the full set of prefix tests
1488
1489         // we do want to check one odd case though to ensure the prefix is re-parsed
1490         let mut p = Path::new("\\\\?\\C:");
1491         assert!(prefix(&p) == Some(VerbatimPrefix(2)));
1492         p.push("foo");
1493         assert!(prefix(&p) == Some(VerbatimDiskPrefix));
1494         assert_eq!(p.as_str(), Some("\\\\?\\C:\\foo"));
1495
1496         // and another with verbatim non-normalized paths
1497         let mut p = Path::new("\\\\?\\C:\\a\\");
1498         p.push("foo");
1499         assert_eq!(p.as_str(), Some("\\\\?\\C:\\a\\foo"));
1500     }
1501
1502     #[test]
1503     fn test_push_path() {
1504         macro_rules! t(
1505             (s: $path:expr, $push:expr, $exp:expr) => (
1506                 {
1507                     let mut p = Path::new($path);
1508                     let push = Path::new($push);
1509                     p.push(&push);
1510                     assert_eq!(p.as_str(), Some($exp));
1511                 }
1512             )
1513         )
1514
1515         t!(s: "a\\b\\c", "d", "a\\b\\c\\d");
1516         t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1517         t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1518         t!(s: "a\\b", "\\c\\d", "\\c\\d");
1519         t!(s: "a\\b", ".", "a\\b");
1520         t!(s: "a\\b", "..\\c", "a\\c");
1521         t!(s: "a\\b", "C:a.txt", "C:a.txt");
1522         t!(s: "a\\b", "..\\..\\..\\c", "..\\c");
1523         t!(s: "a\\b", "C:\\a.txt", "C:\\a.txt");
1524         t!(s: "C:\\a", "C:\\b.txt", "C:\\b.txt");
1525         t!(s: "C:\\a\\b\\c", "C:d", "C:\\a\\b\\c\\d");
1526         t!(s: "C:a\\b\\c", "C:d", "C:a\\b\\c\\d");
1527         t!(s: "C:a\\b", "..\\..\\..\\c", "C:..\\c");
1528         t!(s: "C:\\a\\b", "..\\..\\..\\c", "C:\\c");
1529         t!(s: "C:", r"a\b\c", r"C:a\b\c");
1530         t!(s: "C:", r"..\a", r"C:..\a");
1531         t!(s: "\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
1532         t!(s: "\\\\server\\share\\foo", "..\\..\\bar", "\\\\server\\share\\bar");
1533         t!(s: "\\\\server\\share\\foo", "C:baz", "C:baz");
1534         t!(s: "\\\\?\\C:\\a\\b", "C:c\\d", "\\\\?\\C:\\a\\b\\c\\d");
1535         t!(s: "\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
1536         t!(s: "\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
1537         t!(s: "\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
1538         t!(s: "\\\\?\\C:\\a\\b", "..\\..\\..\\c", "\\\\?\\C:\\a\\b\\..\\..\\..\\c");
1539         t!(s: "\\\\?\\foo\\bar", "..\\..\\c", "\\\\?\\foo\\bar\\..\\..\\c");
1540         t!(s: "\\\\?\\", "foo", "\\\\?\\\\foo");
1541         t!(s: "\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
1542         t!(s: "\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
1543         t!(s: "\\\\?\\UNC\\server\\share", "C:a", "C:a");
1544         t!(s: "\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\\\foo");
1545         t!(s: "C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
1546         t!(s: "\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
1547         t!(s: "\\\\.\\foo\\bar", "C:a", "C:a");
1548         // again, not sure about the following, but I'm assuming \\.\ should be verbatim
1549         t!(s: "\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
1550
1551         t!(s: "\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
1552     }
1553
1554     #[test]
1555     fn test_push_many() {
1556         macro_rules! t(
1557             (s: $path:expr, $push:expr, $exp:expr) => (
1558                 {
1559                     let mut p = Path::new($path);
1560                     p.push_many($push);
1561                     assert_eq!(p.as_str(), Some($exp));
1562                 }
1563             );
1564             (v: $path:expr, $push:expr, $exp:expr) => (
1565                 {
1566                     let mut p = Path::new($path);
1567                     p.push_many($push);
1568                     assert_eq!(p.as_vec(), $exp);
1569                 }
1570             )
1571         )
1572
1573         t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1574         t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
1575         t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1576         t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1577         t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1578         t!(v: b"a\\b\\c", [b"d", b"\\e", b"f"], b"\\e\\f");
1579         t!(v: b"a\\b\\c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")],
1580            b"a\\b\\c\\d\\e");
1581     }
1582
1583     #[test]
1584     fn test_pop() {
1585         macro_rules! t(
1586             (s: $path:expr, $left:expr, $right:expr) => (
1587                 {
1588                     let pstr = $path;
1589                     let mut p = Path::new(pstr);
1590                     let result = p.pop();
1591                     let left = $left;
1592                     assert!(p.as_str() == Some(left),
1593                         "`{}`.pop() failed; expected remainder `{}`, found `{}`",
1594                         pstr, left, p.as_str().unwrap());
1595                     assert!(result == $right);
1596                 }
1597             );
1598             (b: $path:expr, $left:expr, $right:expr) => (
1599                 {
1600                     let mut p = Path::new($path);
1601                     let result = p.pop();
1602                     assert_eq!(p.as_vec(), $left);
1603                     assert!(result == $right);
1604                 }
1605             )
1606         )
1607
1608         t!(s: "a\\b\\c", "a\\b", true);
1609         t!(s: "a", ".", true);
1610         t!(s: ".", ".", false);
1611         t!(s: "\\a", "\\", true);
1612         t!(s: "\\", "\\", false);
1613         t!(b: b"a\\b\\c", b"a\\b", true);
1614         t!(b: b"a", b".", true);
1615         t!(b: b".", b".", false);
1616         t!(b: b"\\a", b"\\", true);
1617         t!(b: b"\\", b"\\", false);
1618
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: "C:a\\b", "C:a", true);
1623         t!(s: "C:a", "C:", true);
1624         t!(s: "C:", "C:", false);
1625         t!(s: "\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1626         t!(s: "\\\\server\\share\\a", "\\\\server\\share", true);
1627         t!(s: "\\\\server\\share", "\\\\server\\share", false);
1628         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1629         t!(s: "\\\\?\\a\\b", "\\\\?\\a", true);
1630         t!(s: "\\\\?\\a", "\\\\?\\a", false);
1631         t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1632         t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1633         t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\", false);
1634         t!(s: "\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1635         t!(s: "\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share", true);
1636         t!(s: "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1637         t!(s: "\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1638         t!(s: "\\\\.\\a\\b", "\\\\.\\a", true);
1639         t!(s: "\\\\.\\a", "\\\\.\\a", false);
1640
1641         t!(s: "\\\\?\\a\\b\\", "\\\\?\\a", true);
1642     }
1643
1644     #[test]
1645     fn test_root_path() {
1646         assert!(Path::new("a\\b\\c").root_path() == None);
1647         assert!(Path::new("\\a\\b\\c").root_path() == Some(Path::new("\\")));
1648         assert!(Path::new("C:a").root_path() == Some(Path::new("C:")));
1649         assert!(Path::new("C:\\a").root_path() == Some(Path::new("C:\\")));
1650         assert!(Path::new("\\\\a\\b\\c").root_path() == Some(Path::new("\\\\a\\b")));
1651         assert!(Path::new("\\\\?\\a\\b").root_path() == Some(Path::new("\\\\?\\a")));
1652         assert!(Path::new("\\\\?\\C:\\a").root_path() == Some(Path::new("\\\\?\\C:\\")));
1653         assert!(Path::new("\\\\?\\UNC\\a\\b\\c").root_path() ==
1654                 Some(Path::new("\\\\?\\UNC\\a\\b")));
1655         assert!(Path::new("\\\\.\\a\\b").root_path() == Some(Path::new("\\\\.\\a")));
1656     }
1657
1658     #[test]
1659     fn test_join() {
1660         t!(s: Path::new("a\\b\\c").join(".."), "a\\b");
1661         t!(s: Path::new("\\a\\b\\c").join("d"), "\\a\\b\\c\\d");
1662         t!(s: Path::new("a\\b").join("c\\d"), "a\\b\\c\\d");
1663         t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
1664         t!(s: Path::new(".").join("a\\b"), "a\\b");
1665         t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
1666         t!(v: Path::new(b"a\\b\\c").join(b".."), b"a\\b");
1667         t!(v: Path::new(b"\\a\\b\\c").join(b"d"), b"\\a\\b\\c\\d");
1668         // full join testing is covered under test_push_path, so no need for
1669         // the full set of prefix tests
1670     }
1671
1672     #[test]
1673     fn test_join_path() {
1674         macro_rules! t(
1675             (s: $path:expr, $join:expr, $exp:expr) => (
1676                 {
1677                     let path = Path::new($path);
1678                     let join = Path::new($join);
1679                     let res = path.join(&join);
1680                     assert_eq!(res.as_str(), Some($exp));
1681                 }
1682             )
1683         )
1684
1685         t!(s: "a\\b\\c", "..", "a\\b");
1686         t!(s: "\\a\\b\\c", "d", "\\a\\b\\c\\d");
1687         t!(s: "a\\b", "c\\d", "a\\b\\c\\d");
1688         t!(s: "a\\b", "\\c\\d", "\\c\\d");
1689         t!(s: ".", "a\\b", "a\\b");
1690         t!(s: "\\", "a\\b", "\\a\\b");
1691         // join is implemented using push, so there's no need for
1692         // the full set of prefix tests
1693     }
1694
1695     #[test]
1696     fn test_join_many() {
1697         macro_rules! t(
1698             (s: $path:expr, $join:expr, $exp:expr) => (
1699                 {
1700                     let path = Path::new($path);
1701                     let res = path.join_many($join);
1702                     assert_eq!(res.as_str(), Some($exp));
1703                 }
1704             );
1705             (v: $path:expr, $join:expr, $exp:expr) => (
1706                 {
1707                     let path = Path::new($path);
1708                     let res = path.join_many($join);
1709                     assert_eq!(res.as_vec(), $exp);
1710                 }
1711             )
1712         )
1713
1714         t!(s: "a\\b\\c", ["d", "e"], "a\\b\\c\\d\\e");
1715         t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
1716         t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
1717         t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1718         t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1719         t!(v: b"a\\b\\c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")],
1720            b"a\\b\\c\\d\\e");
1721     }
1722
1723     #[test]
1724     fn test_with_helpers() {
1725         macro_rules! t(
1726             (s: $path:expr, $op:ident, $arg:expr, $res:expr) => (
1727                 {
1728                     let pstr = $path;
1729                     let path = Path::new(pstr);
1730                     let arg = $arg;
1731                     let res = path.$op(arg);
1732                     let exp = $res;
1733                     assert!(res.as_str() == Some(exp),
1734                             "`{}`.{}(\"{}\"): Expected `{}`, found `{}`",
1735                             pstr, stringify!($op), arg, exp, res.as_str().unwrap());
1736                 }
1737             )
1738         )
1739
1740         t!(s: "a\\b\\c", with_filename, "d", "a\\b\\d");
1741         t!(s: ".", with_filename, "foo", "foo");
1742         t!(s: "\\a\\b\\c", with_filename, "d", "\\a\\b\\d");
1743         t!(s: "\\", with_filename, "foo", "\\foo");
1744         t!(s: "\\a", with_filename, "foo", "\\foo");
1745         t!(s: "foo", with_filename, "bar", "bar");
1746         t!(s: "\\", with_filename, "foo\\", "\\foo");
1747         t!(s: "\\a", with_filename, "foo\\", "\\foo");
1748         t!(s: "a\\b\\c", with_filename, "", "a\\b");
1749         t!(s: "a\\b\\c", with_filename, ".", "a\\b");
1750         t!(s: "a\\b\\c", with_filename, "..", "a");
1751         t!(s: "\\a", with_filename, "", "\\");
1752         t!(s: "foo", with_filename, "", ".");
1753         t!(s: "a\\b\\c", with_filename, "d\\e", "a\\b\\d\\e");
1754         t!(s: "a\\b\\c", with_filename, "\\d", "a\\b\\d");
1755         t!(s: "..", with_filename, "foo", "..\\foo");
1756         t!(s: "..\\..", with_filename, "foo", "..\\..\\foo");
1757         t!(s: "..", with_filename, "", "..");
1758         t!(s: "..\\..", with_filename, "", "..\\..");
1759         t!(s: "C:\\foo\\bar", with_filename, "baz", "C:\\foo\\baz");
1760         t!(s: "C:\\foo", with_filename, "bar", "C:\\bar");
1761         t!(s: "C:\\", with_filename, "foo", "C:\\foo");
1762         t!(s: "C:foo\\bar", with_filename, "baz", "C:foo\\baz");
1763         t!(s: "C:foo", with_filename, "bar", "C:bar");
1764         t!(s: "C:", with_filename, "foo", "C:foo");
1765         t!(s: "C:\\foo", with_filename, "", "C:\\");
1766         t!(s: "C:foo", with_filename, "", "C:");
1767         t!(s: "C:\\foo\\bar", with_filename, "..", "C:\\");
1768         t!(s: "C:\\foo", with_filename, "..", "C:\\");
1769         t!(s: "C:\\", with_filename, "..", "C:\\");
1770         t!(s: "C:foo\\bar", with_filename, "..", "C:");
1771         t!(s: "C:foo", with_filename, "..", "C:..");
1772         t!(s: "C:", with_filename, "..", "C:..");
1773         t!(s: "\\\\server\\share\\foo", with_filename, "bar", "\\\\server\\share\\bar");
1774         t!(s: "\\\\server\\share", with_filename, "foo", "\\\\server\\share\\foo");
1775         t!(s: "\\\\server\\share\\foo", with_filename, "", "\\\\server\\share");
1776         t!(s: "\\\\server\\share", with_filename, "", "\\\\server\\share");
1777         t!(s: "\\\\server\\share\\foo", with_filename, "..", "\\\\server\\share");
1778         t!(s: "\\\\server\\share", with_filename, "..", "\\\\server\\share");
1779         t!(s: "\\\\?\\C:\\foo\\bar", with_filename, "baz", "\\\\?\\C:\\foo\\baz");
1780         t!(s: "\\\\?\\C:\\foo", with_filename, "bar", "\\\\?\\C:\\bar");
1781         t!(s: "\\\\?\\C:\\", with_filename, "foo", "\\\\?\\C:\\foo");
1782         t!(s: "\\\\?\\C:\\foo", with_filename, "..", "\\\\?\\C:\\..");
1783         t!(s: "\\\\?\\foo\\bar", with_filename, "baz", "\\\\?\\foo\\baz");
1784         t!(s: "\\\\?\\foo", with_filename, "bar", "\\\\?\\foo\\bar");
1785         t!(s: "\\\\?\\", with_filename, "foo", "\\\\?\\\\foo");
1786         t!(s: "\\\\?\\foo\\bar", with_filename, "..", "\\\\?\\foo\\..");
1787         t!(s: "\\\\.\\foo\\bar", with_filename, "baz", "\\\\.\\foo\\baz");
1788         t!(s: "\\\\.\\foo", with_filename, "bar", "\\\\.\\foo\\bar");
1789         t!(s: "\\\\.\\foo\\bar", with_filename, "..", "\\\\.\\foo\\..");
1790
1791         t!(s: "hi\\there.txt", with_extension, "exe", "hi\\there.exe");
1792         t!(s: "hi\\there.txt", with_extension, "", "hi\\there");
1793         t!(s: "hi\\there.txt", with_extension, ".", "hi\\there..");
1794         t!(s: "hi\\there.txt", with_extension, "..", "hi\\there...");
1795         t!(s: "hi\\there", with_extension, "txt", "hi\\there.txt");
1796         t!(s: "hi\\there", with_extension, ".", "hi\\there..");
1797         t!(s: "hi\\there", with_extension, "..", "hi\\there...");
1798         t!(s: "hi\\there.", with_extension, "txt", "hi\\there.txt");
1799         t!(s: "hi\\.foo", with_extension, "txt", "hi\\.foo.txt");
1800         t!(s: "hi\\there.txt", with_extension, ".foo", "hi\\there..foo");
1801         t!(s: "\\", with_extension, "txt", "\\");
1802         t!(s: "\\", with_extension, ".", "\\");
1803         t!(s: "\\", with_extension, "..", "\\");
1804         t!(s: ".", with_extension, "txt", ".");
1805         // extension setter calls filename setter internally, no need for extended tests
1806     }
1807
1808     #[test]
1809     fn test_setters() {
1810         macro_rules! t(
1811             (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1812                 {
1813                     let path = $path;
1814                     let arg = $arg;
1815                     let mut p1 = Path::new(path);
1816                     p1.$set(arg);
1817                     let p2 = Path::new(path);
1818                     assert!(p1 == p2.$with(arg));
1819                 }
1820             );
1821             (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
1822                 {
1823                     let path = $path;
1824                     let arg = $arg;
1825                     let mut p1 = Path::new(path);
1826                     p1.$set(arg);
1827                     let p2 = Path::new(path);
1828                     assert!(p1 == p2.$with(arg));
1829                 }
1830             )
1831         )
1832
1833         t!(v: b"a\\b\\c", set_filename, with_filename, b"d");
1834         t!(v: b"\\", set_filename, with_filename, b"foo");
1835         t!(s: "a\\b\\c", set_filename, with_filename, "d");
1836         t!(s: "\\", set_filename, with_filename, "foo");
1837         t!(s: ".", set_filename, with_filename, "foo");
1838         t!(s: "a\\b", set_filename, with_filename, "");
1839         t!(s: "a", set_filename, with_filename, "");
1840
1841         t!(v: b"hi\\there.txt", set_extension, with_extension, b"exe");
1842         t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
1843         t!(s: "hi\\there.", set_extension, with_extension, "txt");
1844         t!(s: "hi\\there", set_extension, with_extension, "txt");
1845         t!(s: "hi\\there.txt", set_extension, with_extension, "");
1846         t!(s: "hi\\there", set_extension, with_extension, "");
1847         t!(s: ".", set_extension, with_extension, "txt");
1848
1849         // with_ helpers use the setter internally, so the tests for the with_ helpers
1850         // will suffice. No need for the full set of prefix tests.
1851     }
1852
1853     #[test]
1854     fn test_getters() {
1855         macro_rules! t(
1856             (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1857                 {
1858                     let path = $path;
1859                     let filename = $filename;
1860                     assert!(path.filename_str() == filename,
1861                             "`{}`.filename_str(): Expected `{:?}`, found `{:?}`",
1862                             path.as_str().unwrap(), filename, path.filename_str());
1863                     let dirname = $dirname;
1864                     assert!(path.dirname_str() == dirname,
1865                             "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
1866                             path.as_str().unwrap(), dirname, path.dirname_str());
1867                     let filestem = $filestem;
1868                     assert!(path.filestem_str() == filestem,
1869                             "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
1870                             path.as_str().unwrap(), filestem, path.filestem_str());
1871                     let ext = $ext;
1872                     assert!(path.extension_str() == ext,
1873                             "`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
1874                             path.as_str().unwrap(), ext, path.extension_str());
1875                 }
1876             );
1877             (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1878                 {
1879                     let path = $path;
1880                     assert!(path.filename() == $filename);
1881                     assert!(path.dirname() == $dirname);
1882                     assert!(path.filestem() == $filestem);
1883                     assert!(path.extension() == $ext);
1884                 }
1885             )
1886         )
1887
1888         t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None);
1889         t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
1890         t!(s: Path::new("."), None, Some("."), None, None);
1891         t!(s: Path::new("\\"), None, Some("\\"), None, None);
1892         t!(s: Path::new(".."), None, Some(".."), None, None);
1893         t!(s: Path::new("..\\.."), None, Some("..\\.."), None, None);
1894         t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"),
1895               Some("there"), Some("txt"));
1896         t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None);
1897         t!(s: Path::new("hi\\there."), Some("there."), Some("hi"),
1898               Some("there"), Some(""));
1899         t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None);
1900         t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"),
1901               Some("."), Some("there"));
1902
1903         // these are already tested in test_components, so no need for extended tests
1904     }
1905
1906     #[test]
1907     fn test_dir_path() {
1908         t!(s: Path::new("hi\\there").dir_path(), "hi");
1909         t!(s: Path::new("hi").dir_path(), ".");
1910         t!(s: Path::new("\\hi").dir_path(), "\\");
1911         t!(s: Path::new("\\").dir_path(), "\\");
1912         t!(s: Path::new("..").dir_path(), "..");
1913         t!(s: Path::new("..\\..").dir_path(), "..\\..");
1914
1915         // dir_path is just dirname interpreted as a path.
1916         // No need for extended tests
1917     }
1918
1919     #[test]
1920     fn test_is_absolute() {
1921         macro_rules! t(
1922             ($path:expr, $abs:expr, $vol:expr, $cwd:expr, $rel:expr) => (
1923                 {
1924                     let path = Path::new($path);
1925                     let (abs, vol, cwd, rel) = ($abs, $vol, $cwd, $rel);
1926                     let b = path.is_absolute();
1927                     assert!(b == abs, "Path '{}'.is_absolute(): expected {:?}, found {:?}",
1928                             path.as_str().unwrap(), abs, b);
1929                     let b = is_vol_relative(&path);
1930                     assert!(b == vol, "is_vol_relative('{}'): expected {:?}, found {:?}",
1931                             path.as_str().unwrap(), vol, b);
1932                     let b = is_cwd_relative(&path);
1933                     assert!(b == cwd, "is_cwd_relative('{}'): expected {:?}, found {:?}",
1934                             path.as_str().unwrap(), cwd, b);
1935                     let b = path.is_relative();
1936                     assert!(b == rel, "Path '{}'.is_relativf(): expected {:?}, found {:?}",
1937                             path.as_str().unwrap(), rel, b);
1938                 }
1939             )
1940         )
1941         t!("a\\b\\c", false, false, false, true);
1942         t!("\\a\\b\\c", false, true, false, false);
1943         t!("a", false, false, false, true);
1944         t!("\\a", false, true, false, false);
1945         t!(".", false, false, false, true);
1946         t!("\\", false, true, false, false);
1947         t!("..", false, false, false, true);
1948         t!("..\\..", false, false, false, true);
1949         t!("C:a\\b.txt", false, false, true, false);
1950         t!("C:\\a\\b.txt", true, false, false, false);
1951         t!("\\\\server\\share\\a\\b.txt", true, false, false, false);
1952         t!("\\\\?\\a\\b\\c.txt", true, false, false, false);
1953         t!("\\\\?\\C:\\a\\b.txt", true, false, false, false);
1954         t!("\\\\?\\C:a\\b.txt", true, false, false, false); // NB: not equivalent to C:a\b.txt
1955         t!("\\\\?\\UNC\\server\\share\\a\\b.txt", true, false, false, false);
1956         t!("\\\\.\\a\\b", true, false, false, false);
1957     }
1958
1959     #[test]
1960     fn test_is_ancestor_of() {
1961         macro_rules! t(
1962             (s: $path:expr, $dest:expr, $exp:expr) => (
1963                 {
1964                     let path = Path::new($path);
1965                     let dest = Path::new($dest);
1966                     let exp = $exp;
1967                     let res = path.is_ancestor_of(&dest);
1968                     assert!(res == exp,
1969                             "`{}`.is_ancestor_of(`{}`): Expected {:?}, found {:?}",
1970                             path.as_str().unwrap(), dest.as_str().unwrap(), exp, res);
1971                 }
1972             )
1973         )
1974
1975         t!(s: "a\\b\\c", "a\\b\\c\\d", true);
1976         t!(s: "a\\b\\c", "a\\b\\c", true);
1977         t!(s: "a\\b\\c", "a\\b", false);
1978         t!(s: "\\a\\b\\c", "\\a\\b\\c", true);
1979         t!(s: "\\a\\b", "\\a\\b\\c", true);
1980         t!(s: "\\a\\b\\c\\d", "\\a\\b\\c", false);
1981         t!(s: "\\a\\b", "a\\b\\c", false);
1982         t!(s: "a\\b", "\\a\\b\\c", false);
1983         t!(s: "a\\b\\c", "a\\b\\d", false);
1984         t!(s: "..\\a\\b\\c", "a\\b\\c", false);
1985         t!(s: "a\\b\\c", "..\\a\\b\\c", false);
1986         t!(s: "a\\b\\c", "a\\b\\cd", false);
1987         t!(s: "a\\b\\cd", "a\\b\\c", false);
1988         t!(s: "..\\a\\b", "..\\a\\b\\c", true);
1989         t!(s: ".", "a\\b", true);
1990         t!(s: ".", ".", true);
1991         t!(s: "\\", "\\", true);
1992         t!(s: "\\", "\\a\\b", true);
1993         t!(s: "..", "a\\b", true);
1994         t!(s: "..\\..", "a\\b", true);
1995         t!(s: "foo\\bar", "foobar", false);
1996         t!(s: "foobar", "foo\\bar", false);
1997
1998         t!(s: "foo", "C:foo", false);
1999         t!(s: "C:foo", "foo", false);
2000         t!(s: "C:foo", "C:foo\\bar", true);
2001         t!(s: "C:foo\\bar", "C:foo", false);
2002         t!(s: "C:\\foo", "C:\\foo\\bar", true);
2003         t!(s: "C:", "C:", true);
2004         t!(s: "C:", "C:\\", false);
2005         t!(s: "C:\\", "C:", false);
2006         t!(s: "C:\\", "C:\\", true);
2007         t!(s: "C:\\foo\\bar", "C:\\foo", false);
2008         t!(s: "C:foo\\bar", "C:foo", false);
2009         t!(s: "C:\\foo", "\\foo", false);
2010         t!(s: "\\foo", "C:\\foo", false);
2011         t!(s: "\\\\server\\share\\foo", "\\\\server\\share\\foo\\bar", true);
2012         t!(s: "\\\\server\\share", "\\\\server\\share\\foo", true);
2013         t!(s: "\\\\server\\share\\foo", "\\\\server\\share", false);
2014         t!(s: "C:\\foo", "\\\\server\\share\\foo", false);
2015         t!(s: "\\\\server\\share\\foo", "C:\\foo", false);
2016         t!(s: "\\\\?\\foo\\bar", "\\\\?\\foo\\bar\\baz", true);
2017         t!(s: "\\\\?\\foo\\bar\\baz", "\\\\?\\foo\\bar", false);
2018         t!(s: "\\\\?\\foo\\bar", "\\foo\\bar\\baz", false);
2019         t!(s: "\\foo\\bar", "\\\\?\\foo\\bar\\baz", false);
2020         t!(s: "\\\\?\\C:\\foo\\bar", "\\\\?\\C:\\foo\\bar\\baz", true);
2021         t!(s: "\\\\?\\C:\\foo\\bar\\baz", "\\\\?\\C:\\foo\\bar", false);
2022         t!(s: "\\\\?\\C:\\", "\\\\?\\C:\\foo", true);
2023         t!(s: "\\\\?\\C:", "\\\\?\\C:\\", false); // this is a weird one
2024         t!(s: "\\\\?\\C:\\", "\\\\?\\C:", false);
2025         t!(s: "\\\\?\\C:\\a", "\\\\?\\c:\\a\\b", true);
2026         t!(s: "\\\\?\\c:\\a", "\\\\?\\C:\\a\\b", true);
2027         t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a\\b", false);
2028         t!(s: "\\\\?\\foo", "\\\\?\\foobar", false);
2029         t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", true);
2030         t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\", true);
2031         t!(s: "\\\\?\\a\\b\\", "\\\\?\\a\\b", true);
2032         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", false);
2033         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b\\", false);
2034         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c\\d", true);
2035         t!(s: "\\\\?\\UNC\\a\\b\\c\\d", "\\\\?\\UNC\\a\\b\\c", false);
2036         t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", true);
2037         t!(s: "\\\\.\\foo\\bar", "\\\\.\\foo\\bar\\baz", true);
2038         t!(s: "\\\\.\\foo\\bar\\baz", "\\\\.\\foo\\bar", false);
2039         t!(s: "\\\\.\\foo", "\\\\.\\foo\\bar", true);
2040         t!(s: "\\\\.\\foo", "\\\\.\\foobar", false);
2041
2042         t!(s: "\\a\\b", "\\\\?\\a\\b", false);
2043         t!(s: "\\\\?\\a\\b", "\\a\\b", false);
2044         t!(s: "\\a\\b", "\\\\?\\C:\\a\\b", false);
2045         t!(s: "\\\\?\\C:\\a\\b", "\\a\\b", false);
2046         t!(s: "Z:\\a\\b", "\\\\?\\z:\\a\\b", true);
2047         t!(s: "C:\\a\\b", "\\\\?\\D:\\a\\b", false);
2048         t!(s: "a\\b", "\\\\?\\a\\b", false);
2049         t!(s: "\\\\?\\a\\b", "a\\b", false);
2050         t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b", true);
2051         t!(s: "\\\\?\\C:\\a\\b", "C:\\a\\b", true);
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", false);
2055         t!(s: "\\\\?\\C:a\\b", "C:a\\b", false);
2056         t!(s: "C:\\a\\b", "\\\\?\\C:\\a\\b\\", true);
2057         t!(s: "\\\\?\\C:\\a\\b\\", "C:\\a\\b", true);
2058         t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b\\c", true);
2059         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b\\c", true);
2060     }
2061
2062     #[test]
2063     fn test_ends_with_path() {
2064         macro_rules! t(
2065             (s: $path:expr, $child:expr, $exp:expr) => (
2066                 {
2067                     let path = Path::new($path);
2068                     let child = Path::new($child);
2069                     assert_eq!(path.ends_with_path(&child), $exp);
2070                 }
2071             );
2072         )
2073
2074         t!(s: "a\\b\\c", "c", true);
2075         t!(s: "a\\b\\c", "d", false);
2076         t!(s: "foo\\bar\\quux", "bar", false);
2077         t!(s: "foo\\bar\\quux", "barquux", false);
2078         t!(s: "a\\b\\c", "b\\c", true);
2079         t!(s: "a\\b\\c", "a\\b\\c", true);
2080         t!(s: "a\\b\\c", "foo\\a\\b\\c", false);
2081         t!(s: "\\a\\b\\c", "a\\b\\c", true);
2082         t!(s: "\\a\\b\\c", "\\a\\b\\c", false); // child must be relative
2083         t!(s: "\\a\\b\\c", "foo\\a\\b\\c", false);
2084         t!(s: "a\\b\\c", "", false);
2085         t!(s: "", "", true);
2086         t!(s: "\\a\\b\\c", "d\\e\\f", false);
2087         t!(s: "a\\b\\c", "a\\b", false);
2088         t!(s: "a\\b\\c", "b", false);
2089         t!(s: "C:\\a\\b", "b", true);
2090         t!(s: "C:\\a\\b", "C:b", false);
2091         t!(s: "C:\\a\\b", "C:a\\b", false);
2092     }
2093
2094     #[test]
2095     fn test_path_relative_from() {
2096         macro_rules! t(
2097             (s: $path:expr, $other:expr, $exp:expr) => (
2098                 {
2099                     let path = Path::new($path);
2100                     let other = Path::new($other);
2101                     let res = path.path_relative_from(&other);
2102                     let exp = $exp;
2103                     assert!(res.as_ref().and_then(|x| x.as_str()) == exp,
2104                             "`{}`.path_relative_from(`{}`): Expected {:?}, got {:?}",
2105                             path.as_str().unwrap(), other.as_str().unwrap(), exp,
2106                             res.as_ref().and_then(|x| x.as_str()));
2107                 }
2108             )
2109         )
2110
2111         t!(s: "a\\b\\c", "a\\b", Some("c"));
2112         t!(s: "a\\b\\c", "a\\b\\d", Some("..\\c"));
2113         t!(s: "a\\b\\c", "a\\b\\c\\d", Some(".."));
2114         t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2115         t!(s: "a\\b\\c", "a\\b\\c\\d\\e", Some("..\\.."));
2116         t!(s: "a\\b\\c", "a\\d\\e", Some("..\\..\\b\\c"));
2117         t!(s: "a\\b\\c", "d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2118         t!(s: "a\\b\\c", "\\a\\b\\c", None);
2119         t!(s: "\\a\\b\\c", "a\\b\\c", Some("\\a\\b\\c"));
2120         t!(s: "\\a\\b\\c", "\\a\\b\\c\\d", Some(".."));
2121         t!(s: "\\a\\b\\c", "\\a\\b", Some("c"));
2122         t!(s: "\\a\\b\\c", "\\a\\b\\c\\d\\e", Some("..\\.."));
2123         t!(s: "\\a\\b\\c", "\\a\\d\\e", Some("..\\..\\b\\c"));
2124         t!(s: "\\a\\b\\c", "\\d\\e\\f", Some("..\\..\\..\\a\\b\\c"));
2125         t!(s: "hi\\there.txt", "hi\\there", Some("..\\there.txt"));
2126         t!(s: ".", "a", Some(".."));
2127         t!(s: ".", "a\\b", Some("..\\.."));
2128         t!(s: ".", ".", Some("."));
2129         t!(s: "a", ".", Some("a"));
2130         t!(s: "a\\b", ".", Some("a\\b"));
2131         t!(s: "..", ".", Some(".."));
2132         t!(s: "a\\b\\c", "a\\b\\c", Some("."));
2133         t!(s: "\\a\\b\\c", "\\a\\b\\c", Some("."));
2134         t!(s: "\\", "\\", Some("."));
2135         t!(s: "\\", ".", Some("\\"));
2136         t!(s: "..\\..\\a", "b", Some("..\\..\\..\\a"));
2137         t!(s: "a", "..\\..\\b", None);
2138         t!(s: "..\\..\\a", "..\\..\\b", Some("..\\a"));
2139         t!(s: "..\\..\\a", "..\\..\\a\\b", Some(".."));
2140         t!(s: "..\\..\\a\\b", "..\\..\\a", Some("b"));
2141
2142         t!(s: "C:a\\b\\c", "C:a\\b", Some("c"));
2143         t!(s: "C:a\\b", "C:a\\b\\c", Some(".."));
2144         t!(s: "C:" ,"C:a\\b", Some("..\\.."));
2145         t!(s: "C:a\\b", "C:c\\d", Some("..\\..\\a\\b"));
2146         t!(s: "C:a\\b", "D:c\\d", Some("C:a\\b"));
2147         t!(s: "C:a\\b", "C:..\\c", None);
2148         t!(s: "C:..\\a", "C:b\\c", Some("..\\..\\..\\a"));
2149         t!(s: "C:\\a\\b\\c", "C:\\a\\b", Some("c"));
2150         t!(s: "C:\\a\\b", "C:\\a\\b\\c", Some(".."));
2151         t!(s: "C:\\", "C:\\a\\b", Some("..\\.."));
2152         t!(s: "C:\\a\\b", "C:\\c\\d", Some("..\\..\\a\\b"));
2153         t!(s: "C:\\a\\b", "C:a\\b", Some("C:\\a\\b"));
2154         t!(s: "C:a\\b", "C:\\a\\b", None);
2155         t!(s: "\\a\\b", "C:\\a\\b", None);
2156         t!(s: "\\a\\b", "C:a\\b", None);
2157         t!(s: "a\\b", "C:\\a\\b", None);
2158         t!(s: "a\\b", "C:a\\b", None);
2159
2160         t!(s: "\\\\a\\b\\c", "\\\\a\\b", Some("c"));
2161         t!(s: "\\\\a\\b", "\\\\a\\b\\c", Some(".."));
2162         t!(s: "\\\\a\\b\\c\\e", "\\\\a\\b\\c\\d", Some("..\\e"));
2163         t!(s: "\\\\a\\c\\d", "\\\\a\\b\\d", Some("\\\\a\\c\\d"));
2164         t!(s: "\\\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\b\\c\\d"));
2165         t!(s: "\\\\a\\b\\c", "\\d\\e", Some("\\\\a\\b\\c"));
2166         t!(s: "\\d\\e", "\\\\a\\b\\c", None);
2167         t!(s: "d\\e", "\\\\a\\b\\c", None);
2168         t!(s: "C:\\a\\b\\c", "\\\\a\\b\\c", Some("C:\\a\\b\\c"));
2169         t!(s: "C:\\c", "\\\\a\\b\\c", Some("C:\\c"));
2170
2171         t!(s: "\\\\?\\a\\b", "\\a\\b", Some("\\\\?\\a\\b"));
2172         t!(s: "\\\\?\\a\\b", "a\\b", Some("\\\\?\\a\\b"));
2173         t!(s: "\\\\?\\a\\b", "\\b", Some("\\\\?\\a\\b"));
2174         t!(s: "\\\\?\\a\\b", "b", Some("\\\\?\\a\\b"));
2175         t!(s: "\\\\?\\a\\b", "\\\\?\\a\\b\\c", Some(".."));
2176         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\b", Some("c"));
2177         t!(s: "\\\\?\\a\\b", "\\\\?\\c\\d", Some("\\\\?\\a\\b"));
2178         t!(s: "\\\\?\\a", "\\\\?\\b", Some("\\\\?\\a"));
2179
2180         t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2181         t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2182         t!(s: "\\\\?\\C:\\a", "\\\\?\\C:\\b", Some("..\\a"));
2183         t!(s: "\\\\?\\C:\\a", "\\\\?\\D:\\a", Some("\\\\?\\C:\\a"));
2184         t!(s: "\\\\?\\C:\\a\\b", "\\\\?\\c:\\a", Some("b"));
2185         t!(s: "\\\\?\\C:\\a\\b", "C:\\a", Some("b"));
2186         t!(s: "\\\\?\\C:\\a", "C:\\a\\b", Some(".."));
2187         t!(s: "C:\\a\\b", "\\\\?\\C:\\a", Some("b"));
2188         t!(s: "C:\\a", "\\\\?\\C:\\a\\b", Some(".."));
2189         t!(s: "\\\\?\\C:\\a", "D:\\a", Some("\\\\?\\C:\\a"));
2190         t!(s: "\\\\?\\c:\\a\\b", "C:\\a", Some("b"));
2191         t!(s: "\\\\?\\C:\\a\\b", "C:a\\b", Some("\\\\?\\C:\\a\\b"));
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\\b", None);
2196         t!(s: "\\\\?\\C:\\a\\.\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\.\\b"));
2197         t!(s: "\\\\?\\C:\\a\\b/c", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\b/c"));
2198         t!(s: "\\\\?\\C:\\a\\..\\b", "\\\\?\\C:\\a", Some("\\\\?\\C:\\a\\..\\b"));
2199         t!(s: "\\\\?\\C:\\a\\b\\", "\\\\?\\C:\\a", Some("b"));
2200         t!(s: "\\\\?\\C:\\.\\b", "\\\\?\\C:\\.", Some("b"));
2201         t!(s: "C:\\b", "\\\\?\\C:\\.", Some("..\\b"));
2202         t!(s: "\\\\?\\a\\.\\b\\c", "\\\\?\\a\\.\\b", Some("c"));
2203         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\.\\d", Some("..\\..\\b\\c"));
2204         t!(s: "\\\\?\\a\\..\\b", "\\\\?\\a\\..", Some("b"));
2205         t!(s: "\\\\?\\a\\b\\..", "\\\\?\\a\\b", Some("\\\\?\\a\\b\\.."));
2206         t!(s: "\\\\?\\a\\b\\c", "\\\\?\\a\\..\\b", Some("..\\..\\b\\c"));
2207
2208         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2209         t!(s: "\\\\?\\UNC\\a\\b", "\\\\?\\UNC\\a\\b\\c", Some(".."));
2210         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2211         t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\?\\UNC\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2212         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2213         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\?\\C:\\a\\b\\c", Some("\\\\?\\UNC\\a\\b\\c"));
2214         t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2215         t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2216         t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\?\\UNC\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2217         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\b", Some("c"));
2218         t!(s: "\\\\?\\UNC\\a\\b", "\\\\a\\b\\c", Some(".."));
2219         t!(s: "\\\\?\\UNC\\a\\b\\c", "\\\\a\\c\\d", Some("\\\\?\\UNC\\a\\b\\c"));
2220         t!(s: "\\\\?\\UNC\\b\\c\\d", "\\\\a\\c\\d", Some("\\\\?\\UNC\\b\\c\\d"));
2221         t!(s: "\\\\?\\UNC\\a\\b\\.", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\."));
2222         t!(s: "\\\\?\\UNC\\a\\b\\c/d", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\c/d"));
2223         t!(s: "\\\\?\\UNC\\a\\b\\..", "\\\\a\\b", Some("\\\\?\\UNC\\a\\b\\.."));
2224         t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\b", Some("c"));
2225         t!(s: "\\\\a\\b\\c", "\\\\?\\UNC\\a\\c\\d", Some("\\\\a\\b\\c"));
2226     }
2227
2228     #[test]
2229     fn test_str_components() {
2230         macro_rules! t(
2231             (s: $path:expr, $exp:expr) => (
2232                 {
2233                     let path = Path::new($path);
2234                     let comps = path.str_components().map(|x|x.unwrap())
2235                                 .collect::<Vec<&str>>();
2236                     let exp: &[&str] = $exp;
2237                     assert_eq!(comps.as_slice(), exp);
2238                     let comps = path.str_components().rev().map(|x|x.unwrap())
2239                                 .collect::<Vec<&str>>();
2240                     let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&str>>();
2241                     assert_eq!(comps, exp);
2242                 }
2243             );
2244         )
2245
2246         t!(s: b"a\\b\\c", ["a", "b", "c"]);
2247         t!(s: "a\\b\\c", ["a", "b", "c"]);
2248         t!(s: "a\\b\\d", ["a", "b", "d"]);
2249         t!(s: "a\\b\\cd", ["a", "b", "cd"]);
2250         t!(s: "\\a\\b\\c", ["a", "b", "c"]);
2251         t!(s: "a", ["a"]);
2252         t!(s: "\\a", ["a"]);
2253         t!(s: "\\", []);
2254         t!(s: ".", ["."]);
2255         t!(s: "..", [".."]);
2256         t!(s: "..\\..", ["..", ".."]);
2257         t!(s: "..\\..\\foo", ["..", "..", "foo"]);
2258         t!(s: "C:foo\\bar", ["foo", "bar"]);
2259         t!(s: "C:foo", ["foo"]);
2260         t!(s: "C:", []);
2261         t!(s: "C:\\foo\\bar", ["foo", "bar"]);
2262         t!(s: "C:\\foo", ["foo"]);
2263         t!(s: "C:\\", []);
2264         t!(s: "\\\\server\\share\\foo\\bar", ["foo", "bar"]);
2265         t!(s: "\\\\server\\share\\foo", ["foo"]);
2266         t!(s: "\\\\server\\share", []);
2267         t!(s: "\\\\?\\foo\\bar\\baz", ["bar", "baz"]);
2268         t!(s: "\\\\?\\foo\\bar", ["bar"]);
2269         t!(s: "\\\\?\\foo", []);
2270         t!(s: "\\\\?\\", []);
2271         t!(s: "\\\\?\\a\\b", ["b"]);
2272         t!(s: "\\\\?\\a\\b\\", ["b"]);
2273         t!(s: "\\\\?\\foo\\bar\\\\baz", ["bar", "", "baz"]);
2274         t!(s: "\\\\?\\C:\\foo\\bar", ["foo", "bar"]);
2275         t!(s: "\\\\?\\C:\\foo", ["foo"]);
2276         t!(s: "\\\\?\\C:\\", []);
2277         t!(s: "\\\\?\\C:\\foo\\", ["foo"]);
2278         t!(s: "\\\\?\\UNC\\server\\share\\foo\\bar", ["foo", "bar"]);
2279         t!(s: "\\\\?\\UNC\\server\\share\\foo", ["foo"]);
2280         t!(s: "\\\\?\\UNC\\server\\share", []);
2281         t!(s: "\\\\.\\foo\\bar\\baz", ["bar", "baz"]);
2282         t!(s: "\\\\.\\foo\\bar", ["bar"]);
2283         t!(s: "\\\\.\\foo", []);
2284     }
2285
2286     #[test]
2287     fn test_components_iter() {
2288         macro_rules! t(
2289             (s: $path:expr, $exp:expr) => (
2290                 {
2291                     let path = Path::new($path);
2292                     let comps = path.components().collect::<Vec<&[u8]>>();
2293                     let exp: &[&[u8]] = $exp;
2294                     assert_eq!(comps.as_slice(), exp);
2295                     let comps = path.components().rev().collect::<Vec<&[u8]>>();
2296                     let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
2297                     assert_eq!(comps, exp);
2298                 }
2299             )
2300         )
2301
2302         t!(s: "a\\b\\c", [b"a", b"b", b"c"]);
2303         t!(s: ".", [b"."]);
2304         // since this is really a wrapper around str_components, those tests suffice
2305     }
2306
2307     #[test]
2308     fn test_make_non_verbatim() {
2309         macro_rules! t(
2310             ($path:expr, $exp:expr) => (
2311                 {
2312                     let path = Path::new($path);
2313                     let exp: Option<&str> = $exp;
2314                     let exp = exp.map(|s| Path::new(s));
2315                     assert!(make_non_verbatim(&path) == exp);
2316                 }
2317             )
2318         )
2319
2320         t!(r"\a\b\c", Some(r"\a\b\c"));
2321         t!(r"a\b\c", Some(r"a\b\c"));
2322         t!(r"C:\a\b\c", Some(r"C:\a\b\c"));
2323         t!(r"C:a\b\c", Some(r"C:a\b\c"));
2324         t!(r"\\server\share\foo", Some(r"\\server\share\foo"));
2325         t!(r"\\.\foo", None);
2326         t!(r"\\?\foo", None);
2327         t!(r"\\?\C:", None);
2328         t!(r"\\?\C:foo", None);
2329         t!(r"\\?\C:\", Some(r"C:\"));
2330         t!(r"\\?\C:\foo", Some(r"C:\foo"));
2331         t!(r"\\?\C:\foo\bar\baz", Some(r"C:\foo\bar\baz"));
2332         t!(r"\\?\C:\foo\.\bar\baz", None);
2333         t!(r"\\?\C:\foo\bar\..\baz", None);
2334         t!(r"\\?\C:\foo\bar\..", None);
2335         t!(r"\\?\UNC\server\share\foo", Some(r"\\server\share\foo"));
2336         t!(r"\\?\UNC\server\share", Some(r"\\server\share"));
2337         t!(r"\\?\UNC\server", None);
2338         t!(r"\\?\UNC\server\", None);
2339     }
2340 }