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