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