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