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