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