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.
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.
11 //! POSIX file path handling
14 use cmp::{Ordering, Eq, Ord, PartialEq, PartialOrd};
18 use iter::{AdditiveIterator, Extend};
19 use iter::{Iterator, IteratorExt, Map};
21 use option::Option::{self, Some, None};
22 use slice::{AsSlice, Split, SliceExt, SliceConcatExt};
23 use str::{self, FromStr, StrExt};
26 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
28 /// Iterator that yields successive components of a Path as &[u8]
29 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
31 /// Iterator that yields successive components of a Path as Option<&str>
32 pub type StrComponents<'a> =
33 Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
35 /// Represents a POSIX file path
38 repr: Vec<u8>, // assumed to never be empty or contain NULs
39 sepidx: Option<uint> // index of the final separator in repr
42 /// The standard path separator character
43 pub const SEP: char = '/';
45 /// The standard path separator byte
46 pub const SEP_BYTE: u8 = SEP as u8;
48 /// Returns whether the given byte is a path separator
50 pub fn is_sep_byte(u: &u8) -> bool {
54 /// Returns whether the given char is a path separator
56 pub fn is_sep(c: char) -> bool {
60 impl fmt::Show for Path {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 fmt::Show::fmt(&self.display(), f)
66 impl PartialEq for Path {
68 fn eq(&self, other: &Path) -> bool {
69 self.repr == other.repr
75 impl PartialOrd for Path {
76 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
82 fn cmp(&self, other: &Path) -> Ordering {
83 self.repr.cmp(&other.repr)
87 impl FromStr for Path {
88 fn from_str(s: &str) -> Option<Path> {
93 impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for Path {
95 fn hash(&self, state: &mut S) {
100 impl BytesContainer for Path {
102 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
107 impl GenericPathUnsafe for Path {
108 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
109 let path = Path::normalize(path.container_as_bytes());
110 assert!(!path.is_empty());
111 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
112 Path{ repr: path, sepidx: idx }
115 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
116 let filename = filename.container_as_bytes();
118 None if b".." == self.repr => {
119 let mut v = Vec::with_capacity(3 + filename.len());
120 v.push_all(dot_dot_static);
122 v.push_all(filename);
123 // FIXME: this is slow
124 self.repr = Path::normalize(v.as_slice());
127 self.repr = Path::normalize(filename);
129 Some(idx) if &self.repr[idx+1..] == b".." => {
130 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
131 v.push_all(self.repr.as_slice());
133 v.push_all(filename);
134 // FIXME: this is slow
135 self.repr = Path::normalize(v.as_slice());
138 let mut v = Vec::with_capacity(idx + 1 + filename.len());
139 v.push_all(&self.repr[..idx+1]);
140 v.push_all(filename);
141 // FIXME: this is slow
142 self.repr = Path::normalize(v.as_slice());
145 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
148 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
149 let path = path.container_as_bytes();
150 if !path.is_empty() {
151 if path[0] == SEP_BYTE {
152 self.repr = Path::normalize(path);
154 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
155 v.push_all(self.repr.as_slice());
158 // FIXME: this is slow
159 self.repr = Path::normalize(v.as_slice());
161 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
166 impl GenericPath for Path {
168 fn as_vec<'a>(&'a self) -> &'a [u8] {
172 fn into_vec(self) -> Vec<u8> {
176 fn dirname<'a>(&'a self) -> &'a [u8] {
178 None if b".." == self.repr => self.repr.as_slice(),
180 Some(0) => &self.repr[..1],
181 Some(idx) if &self.repr[idx+1..] == b".." => self.repr.as_slice(),
182 Some(idx) => &self.repr[..idx]
186 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
188 None if b"." == self.repr ||
189 b".." == self.repr => None,
190 None => Some(self.repr.as_slice()),
191 Some(idx) if &self.repr[idx+1..] == b".." => None,
192 Some(0) if self.repr[1..].is_empty() => None,
193 Some(idx) => Some(&self.repr[idx+1..])
197 fn pop(&mut self) -> bool {
199 None if b"." == self.repr => false,
201 self.repr = vec![b'.'];
205 Some(0) if b"/" == self.repr => false,
208 self.repr.truncate(idx+1);
210 self.repr.truncate(idx);
212 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
218 fn root_path(&self) -> Option<Path> {
219 if self.is_absolute() {
227 fn is_absolute(&self) -> bool {
228 self.repr[0] == SEP_BYTE
231 fn is_ancestor_of(&self, other: &Path) -> bool {
232 if self.is_absolute() != other.is_absolute() {
235 let mut ita = self.components();
236 let mut itb = other.components();
237 if b"." == self.repr {
238 return match itb.next() {
240 Some(b) => b != b".."
244 match (ita.next(), itb.next()) {
246 (Some(a), Some(b)) if a == b => { continue },
247 (Some(a), _) if a == b".." => {
248 // if ita contains only .. components, it's an ancestor
249 return ita.all(|x| x == b"..");
258 fn path_relative_from(&self, base: &Path) -> Option<Path> {
259 if self.is_absolute() != base.is_absolute() {
260 if self.is_absolute() {
266 let mut ita = self.components();
267 let mut itb = base.components();
268 let mut comps = vec![];
270 match (ita.next(), itb.next()) {
271 (None, None) => break,
274 comps.extend(ita.by_ref());
277 (None, _) => comps.push(dot_dot_static),
278 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
279 (Some(a), Some(b)) if b == b"." => comps.push(a),
280 (Some(_), Some(b)) if b == b".." => return None,
281 (Some(a), Some(_)) => {
282 comps.push(dot_dot_static);
284 comps.push(dot_dot_static);
287 comps.extend(ita.by_ref());
292 Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
296 fn ends_with_path(&self, child: &Path) -> bool {
297 if !child.is_relative() { return false; }
298 let mut selfit = self.components().rev();
299 let mut childit = child.components().rev();
301 match (selfit.next(), childit.next()) {
302 (Some(a), Some(b)) => if a != b { return false; },
303 (Some(_), None) => break,
304 (None, Some(_)) => return false,
305 (None, None) => break
313 /// Returns a new Path from a byte vector or string
317 /// Panics the task if the vector contains a NUL.
319 pub fn new<T: BytesContainer>(path: T) -> Path {
320 GenericPath::new(path)
323 /// Returns a new Path from a byte vector or string, if possible
325 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
326 GenericPath::new_opt(path)
329 /// Returns a normalized byte vector representation of a path, by removing all empty
330 /// components, and unnecessary . and .. components.
331 fn normalize<V: ?Sized + AsSlice<u8>>(v: &V) -> Vec<u8> {
332 // borrowck is being very picky
334 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
335 let v_ = if is_abs { &v.as_slice()[1..] } else { v.as_slice() };
336 let comps = normalize_helper(v_, is_abs);
340 if is_abs && comps.is_empty() {
343 let n = if is_abs { comps.len() } else { comps.len() - 1} +
344 comps.iter().map(|v| v.len()).sum();
345 let mut v = Vec::with_capacity(n);
346 let mut it = comps.into_iter();
350 Some(comp) => v.push_all(comp)
363 None => v.as_slice().to_vec(),
368 /// Returns an iterator that yields each component of the path in turn.
369 /// Does not distinguish between absolute and relative paths, e.g.
370 /// /a/b/c and a/b/c yield the same set of components.
371 /// A path of "/" yields no components. A path of "." yields one component.
372 pub fn components<'a>(&'a self) -> Components<'a> {
373 let v = if self.repr[0] == SEP_BYTE {
375 } else { self.repr.as_slice() };
376 let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
377 let mut ret = v.split(is_sep_byte);
379 // consume the empty "" component
385 /// Returns an iterator that yields each component of the path as Option<&str>.
386 /// See components() for details.
387 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
388 fn from_utf8(s: &[u8]) -> Option<&str> {
389 str::from_utf8(s).ok()
391 let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
392 self.components().map(f)
396 // None result means the byte vector didn't need normalizing
397 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
398 if is_abs && v.is_empty() {
401 let mut comps: Vec<&'a [u8]> = vec![];
403 let mut changed = false;
404 for comp in v.split(is_sep_byte) {
405 if comp.is_empty() { changed = true }
406 else if comp == b"." { changed = true }
407 else if comp == b".." {
408 if is_abs && comps.is_empty() { changed = true }
409 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
410 else { comps.pop().unwrap(); changed = true }
411 } else { comps.push(comp) }
414 if comps.is_empty() && !is_abs {
418 comps.push(dot_static);
426 #[allow(non_upper_case_globals)]
427 static dot_static: &'static [u8] = b".";
428 #[allow(non_upper_case_globals)]
429 static dot_dot_static: &'static [u8] = b"..";
436 use iter::IteratorExt;
437 use option::Option::{self, Some, None};
438 use path::GenericPath;
439 use slice::{AsSlice, SliceExt};
440 use str::{self, Str, StrExt};
441 use string::ToString;
445 (s: $path:expr, $exp:expr) => (
448 assert_eq!(path.as_str(), Some($exp));
451 (v: $path:expr, $exp:expr) => (
454 assert_eq!(path.as_vec(), $exp);
461 let empty: &[u8] = &[];
462 t!(v: Path::new(empty), b".");
463 t!(v: Path::new(b"/"), b"/");
464 t!(v: Path::new(b"a/b/c"), b"a/b/c");
465 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
466 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
467 let p = Path::new(b"a/b/c\xFF");
468 assert!(p.as_str().is_none());
470 t!(s: Path::new(""), ".");
471 t!(s: Path::new("/"), "/");
472 t!(s: Path::new("hi"), "hi");
473 t!(s: Path::new("hi/"), "hi");
474 t!(s: Path::new("/lib"), "/lib");
475 t!(s: Path::new("/lib/"), "/lib");
476 t!(s: Path::new("hi/there"), "hi/there");
477 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
479 t!(s: Path::new("hi/there/"), "hi/there");
480 t!(s: Path::new("hi/../there"), "there");
481 t!(s: Path::new("../hi/there"), "../hi/there");
482 t!(s: Path::new("/../hi/there"), "/hi/there");
483 t!(s: Path::new("foo/.."), ".");
484 t!(s: Path::new("/foo/.."), "/");
485 t!(s: Path::new("/foo/../.."), "/");
486 t!(s: Path::new("/foo/../../bar"), "/bar");
487 t!(s: Path::new("/./hi/./there/."), "/hi/there");
488 t!(s: Path::new("/./hi/./there/./.."), "/hi");
489 t!(s: Path::new("foo/../.."), "..");
490 t!(s: Path::new("foo/../../.."), "../..");
491 t!(s: Path::new("foo/../../bar"), "../bar");
493 assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
494 assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
497 let p = Path::new(b"foo/bar\x80");
498 assert!(p.as_str().is_none());
502 fn test_opt_paths() {
503 assert!(Path::new_opt(b"foo/bar\0").is_none());
504 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
505 assert!(Path::new_opt("foo/bar\0").is_none());
506 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
510 fn test_null_byte() {
512 let result = Thread::scoped(move|| {
513 Path::new(b"foo/bar\0")
515 assert!(result.is_err());
517 let result = Thread::scoped(move|| {
518 Path::new("test").set_filename(b"f\0o")
520 assert!(result.is_err());
522 let result = Thread::scoped(move|| {
523 Path::new("test").push(b"f\0o");
525 assert!(result.is_err());
529 fn test_display_str() {
531 ($path:expr, $disp:ident, $exp:expr) => (
533 let path = Path::new($path);
534 assert_eq!(path.$disp().to_string(), $exp);
538 t!("foo", display, "foo");
539 t!(b"foo\x80", display, "foo\u{FFFD}");
540 t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
541 t!(b"foo\xFF/bar", filename_display, "bar");
542 t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
543 t!(b"/", filename_display, "");
546 ($path:expr, $exp:expr) => (
548 let path = Path::new($path);
549 let mo = path.display().as_cow();
550 assert_eq!(mo.as_slice(), $exp);
553 ($path:expr, $exp:expr, filename) => (
555 let path = Path::new($path);
556 let mo = path.filename_display().as_cow();
557 assert_eq!(mo.as_slice(), $exp);
563 t!(b"foo\x80", "foo\u{FFFD}");
564 t!(b"foo\xFFbar", "foo\u{FFFD}bar");
565 t!(b"foo\xFF/bar", "bar", filename);
566 t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
567 t!(b"/", "", filename);
573 ($path:expr, $exp:expr, $expf:expr) => (
575 let path = Path::new($path);
576 let f = format!("{}", path.display());
578 let f = format!("{}", path.filename_display());
579 assert_eq!(f, $expf);
584 t!(b"foo", "foo", "foo");
585 t!(b"foo/bar", "foo/bar", "bar");
587 t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
588 t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
589 t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
590 t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
594 fn test_components() {
596 (s: $path:expr, $op:ident, $exp:expr) => (
598 let path = Path::new($path);
599 assert_eq!(path.$op(), ($exp).as_bytes());
602 (s: $path:expr, $op:ident, $exp:expr, opt) => (
604 let path = Path::new($path);
605 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
606 assert_eq!(left, $exp);
609 (v: $path:expr, $op:ident, $exp:expr) => (
612 let path = Path::new(arg);
613 assert_eq!(path.$op(), $exp);
618 t!(v: b"a/b/c", filename, Some(b"c"));
619 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
620 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
621 t!(s: "a/b/c", filename, Some("c"), opt);
622 t!(s: "/a/b/c", filename, Some("c"), opt);
623 t!(s: "a", filename, Some("a"), opt);
624 t!(s: "/a", filename, Some("a"), opt);
625 t!(s: ".", filename, None, opt);
626 t!(s: "/", filename, None, opt);
627 t!(s: "..", filename, None, opt);
628 t!(s: "../..", filename, None, opt);
630 t!(v: b"a/b/c", dirname, b"a/b");
631 t!(v: b"a/b/c\xFF", dirname, b"a/b");
632 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
633 t!(s: "a/b/c", dirname, "a/b");
634 t!(s: "/a/b/c", dirname, "/a/b");
635 t!(s: "a", dirname, ".");
636 t!(s: "/a", dirname, "/");
637 t!(s: ".", dirname, ".");
638 t!(s: "/", dirname, "/");
639 t!(s: "..", dirname, "..");
640 t!(s: "../..", dirname, "../..");
642 t!(v: b"hi/there.txt", filestem, Some(b"there"));
643 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
644 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
645 t!(s: "hi/there.txt", filestem, Some("there"), opt);
646 t!(s: "hi/there", filestem, Some("there"), opt);
647 t!(s: "there.txt", filestem, Some("there"), opt);
648 t!(s: "there", filestem, Some("there"), opt);
649 t!(s: ".", filestem, None, opt);
650 t!(s: "/", filestem, None, opt);
651 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
652 t!(s: ".bar", filestem, Some(".bar"), opt);
653 t!(s: "..bar", filestem, Some("."), opt);
654 t!(s: "hi/there..txt", filestem, Some("there."), opt);
655 t!(s: "..", filestem, None, opt);
656 t!(s: "../..", filestem, None, opt);
658 t!(v: b"hi/there.txt", extension, Some(b"txt"));
659 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
660 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
661 t!(v: b"hi/there", extension, None);
662 t!(v: b"hi/there\x80", extension, None);
663 t!(s: "hi/there.txt", extension, Some("txt"), opt);
664 t!(s: "hi/there", extension, None, opt);
665 t!(s: "there.txt", extension, Some("txt"), opt);
666 t!(s: "there", extension, None, opt);
667 t!(s: ".", extension, None, opt);
668 t!(s: "/", extension, None, opt);
669 t!(s: "foo/.bar", extension, None, opt);
670 t!(s: ".bar", extension, None, opt);
671 t!(s: "..bar", extension, Some("bar"), opt);
672 t!(s: "hi/there..txt", extension, Some("txt"), opt);
673 t!(s: "..", extension, None, opt);
674 t!(s: "../..", extension, None, opt);
680 (s: $path:expr, $join:expr) => (
684 let mut p1 = Path::new(path);
687 assert_eq!(p1, p2.join(join));
692 t!(s: "a/b/c", "..");
693 t!(s: "/a/b/c", "d");
695 t!(s: "a/b", "/c/d");
699 fn test_push_path() {
701 (s: $path:expr, $push:expr, $exp:expr) => (
703 let mut p = Path::new($path);
704 let push = Path::new($push);
706 assert_eq!(p.as_str(), Some($exp));
711 t!(s: "a/b/c", "d", "a/b/c/d");
712 t!(s: "/a/b/c", "d", "/a/b/c/d");
713 t!(s: "a/b", "c/d", "a/b/c/d");
714 t!(s: "a/b", "/c/d", "/c/d");
715 t!(s: "a/b", ".", "a/b");
716 t!(s: "a/b", "../c", "a/c");
720 fn test_push_many() {
722 (s: $path:expr, $push:expr, $exp:expr) => (
724 let mut p = Path::new($path);
726 assert_eq!(p.as_str(), Some($exp));
729 (v: $path:expr, $push:expr, $exp:expr) => (
731 let mut p = Path::new($path);
733 assert_eq!(p.as_vec(), $exp);
738 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
739 t!(s: "a/b/c", ["d", "/e"], "/e");
740 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
741 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
742 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
743 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
744 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
750 (s: $path:expr, $left:expr, $right:expr) => (
752 let mut p = Path::new($path);
753 let result = p.pop();
754 assert_eq!(p.as_str(), Some($left));
755 assert_eq!(result, $right);
758 (b: $path:expr, $left:expr, $right:expr) => (
760 let mut p = Path::new($path);
761 let result = p.pop();
762 assert_eq!(p.as_vec(), $left);
763 assert_eq!(result, $right);
768 t!(b: b"a/b/c", b"a/b", true);
769 t!(b: b"a", b".", true);
770 t!(b: b".", b".", false);
771 t!(b: b"/a", b"/", true);
772 t!(b: b"/", b"/", false);
773 t!(b: b"a/b/c\x80", b"a/b", true);
774 t!(b: b"a/b\x80/c", b"a/b\x80", true);
775 t!(b: b"\xFF", b".", true);
776 t!(b: b"/\xFF", b"/", true);
777 t!(s: "a/b/c", "a/b", true);
778 t!(s: "a", ".", true);
779 t!(s: ".", ".", false);
780 t!(s: "/a", "/", true);
781 t!(s: "/", "/", false);
785 fn test_root_path() {
786 assert_eq!(Path::new(b"a/b/c").root_path(), None);
787 assert_eq!(Path::new(b"/a/b/c").root_path(), Some(Path::new("/")));
792 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
793 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
794 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
795 t!(s: Path::new("a/b/c").join(".."), "a/b");
796 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
797 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
798 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
799 t!(s: Path::new(".").join("a/b"), "a/b");
800 t!(s: Path::new("/").join("a/b"), "/a/b");
804 fn test_join_path() {
806 (s: $path:expr, $join:expr, $exp:expr) => (
808 let path = Path::new($path);
809 let join = Path::new($join);
810 let res = path.join(&join);
811 assert_eq!(res.as_str(), Some($exp));
816 t!(s: "a/b/c", "..", "a/b");
817 t!(s: "/a/b/c", "d", "/a/b/c/d");
818 t!(s: "a/b", "c/d", "a/b/c/d");
819 t!(s: "a/b", "/c/d", "/c/d");
820 t!(s: ".", "a/b", "a/b");
821 t!(s: "/", "a/b", "/a/b");
825 fn test_join_many() {
827 (s: $path:expr, $join:expr, $exp:expr) => (
829 let path = Path::new($path);
830 let res = path.join_many(&$join);
831 assert_eq!(res.as_str(), Some($exp));
834 (v: $path:expr, $join:expr, $exp:expr) => (
836 let path = Path::new($path);
837 let res = path.join_many(&$join);
838 assert_eq!(res.as_vec(), $exp);
843 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
844 t!(s: "a/b/c", ["..", "d"], "a/b/d");
845 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
846 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
847 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
848 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
852 fn test_with_helpers() {
853 let empty: &[u8] = &[];
855 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
856 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
857 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
859 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
860 t!(s: Path::new(".").with_filename("foo"), "foo");
861 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
862 t!(s: Path::new("/").with_filename("foo"), "/foo");
863 t!(s: Path::new("/a").with_filename("foo"), "/foo");
864 t!(s: Path::new("foo").with_filename("bar"), "bar");
865 t!(s: Path::new("/").with_filename("foo/"), "/foo");
866 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
867 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
868 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
869 t!(s: Path::new("a/b/c").with_filename(".."), "a");
870 t!(s: Path::new("/a").with_filename(""), "/");
871 t!(s: Path::new("foo").with_filename(""), ".");
872 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
873 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
874 t!(s: Path::new("..").with_filename("foo"), "../foo");
875 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
876 t!(s: Path::new("..").with_filename(""), "..");
877 t!(s: Path::new("../..").with_filename(""), "../..");
879 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
880 b"hi/there\x80.exe");
881 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
883 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
884 b"hi/there\x80.\xFF");
885 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
886 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
887 t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
888 t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
889 t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
890 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
891 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
892 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
893 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
894 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
895 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
896 t!(s: Path::new("/").with_extension("txt"), "/");
897 t!(s: Path::new("/").with_extension("."), "/");
898 t!(s: Path::new("/").with_extension(".."), "/");
899 t!(s: Path::new(".").with_extension("txt"), ".");
905 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
909 let mut p1 = Path::new(path);
911 let p2 = Path::new(path);
912 assert_eq!(p1, p2.$with(arg));
915 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
919 let mut p1 = Path::new(path);
921 let p2 = Path::new(path);
922 assert_eq!(p1, p2.$with(arg));
927 t!(v: b"a/b/c", set_filename, with_filename, b"d");
928 t!(v: b"/", set_filename, with_filename, b"foo");
929 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
930 t!(s: "a/b/c", set_filename, with_filename, "d");
931 t!(s: "/", set_filename, with_filename, "foo");
932 t!(s: ".", set_filename, with_filename, "foo");
933 t!(s: "a/b", set_filename, with_filename, "");
934 t!(s: "a", set_filename, with_filename, "");
936 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
937 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
938 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
939 t!(s: "hi/there.", set_extension, with_extension, "txt");
940 t!(s: "hi/there", set_extension, with_extension, "txt");
941 t!(s: "hi/there.txt", set_extension, with_extension, "");
942 t!(s: "hi/there", set_extension, with_extension, "");
943 t!(s: ".", set_extension, with_extension, "txt");
949 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
952 assert_eq!(path.filename_str(), $filename);
953 assert_eq!(path.dirname_str(), $dirname);
954 assert_eq!(path.filestem_str(), $filestem);
955 assert_eq!(path.extension_str(), $ext);
958 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
961 assert_eq!(path.filename(), $filename);
962 assert_eq!(path.dirname(), $dirname);
963 assert_eq!(path.filestem(), $filestem);
964 assert_eq!(path.extension(), $ext);
969 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
970 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
971 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
972 Some(b"there"), Some(b"\xFF"));
973 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
974 t!(s: Path::new("."), None, Some("."), None, None);
975 t!(s: Path::new("/"), None, Some("/"), None, None);
976 t!(s: Path::new(".."), None, Some(".."), None, None);
977 t!(s: Path::new("../.."), None, Some("../.."), None, None);
978 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
979 Some("there"), Some("txt"));
980 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
981 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
982 Some("there"), Some(""));
983 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
984 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
985 Some("."), Some("there"));
986 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
987 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
988 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
989 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
994 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
995 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
996 t!(s: Path::new("hi/there").dir_path(), "hi");
997 t!(s: Path::new("hi").dir_path(), ".");
998 t!(s: Path::new("/hi").dir_path(), "/");
999 t!(s: Path::new("/").dir_path(), "/");
1000 t!(s: Path::new("..").dir_path(), "..");
1001 t!(s: Path::new("../..").dir_path(), "../..");
1005 fn test_is_absolute() {
1007 (s: $path:expr, $abs:expr, $rel:expr) => (
1009 let path = Path::new($path);
1010 assert_eq!(path.is_absolute(), $abs);
1011 assert_eq!(path.is_relative(), $rel);
1015 t!(s: "a/b/c", false, true);
1016 t!(s: "/a/b/c", true, false);
1017 t!(s: "a", false, true);
1018 t!(s: "/a", true, false);
1019 t!(s: ".", false, true);
1020 t!(s: "/", true, false);
1021 t!(s: "..", false, true);
1022 t!(s: "../..", false, true);
1026 fn test_is_ancestor_of() {
1028 (s: $path:expr, $dest:expr, $exp:expr) => (
1030 let path = Path::new($path);
1031 let dest = Path::new($dest);
1032 assert_eq!(path.is_ancestor_of(&dest), $exp);
1037 t!(s: "a/b/c", "a/b/c/d", true);
1038 t!(s: "a/b/c", "a/b/c", true);
1039 t!(s: "a/b/c", "a/b", false);
1040 t!(s: "/a/b/c", "/a/b/c", true);
1041 t!(s: "/a/b", "/a/b/c", true);
1042 t!(s: "/a/b/c/d", "/a/b/c", false);
1043 t!(s: "/a/b", "a/b/c", false);
1044 t!(s: "a/b", "/a/b/c", false);
1045 t!(s: "a/b/c", "a/b/d", false);
1046 t!(s: "../a/b/c", "a/b/c", false);
1047 t!(s: "a/b/c", "../a/b/c", false);
1048 t!(s: "a/b/c", "a/b/cd", false);
1049 t!(s: "a/b/cd", "a/b/c", false);
1050 t!(s: "../a/b", "../a/b/c", true);
1051 t!(s: ".", "a/b", true);
1052 t!(s: ".", ".", true);
1053 t!(s: "/", "/", true);
1054 t!(s: "/", "/a/b", true);
1055 t!(s: "..", "a/b", true);
1056 t!(s: "../..", "a/b", true);
1060 fn test_ends_with_path() {
1062 (s: $path:expr, $child:expr, $exp:expr) => (
1064 let path = Path::new($path);
1065 let child = Path::new($child);
1066 assert_eq!(path.ends_with_path(&child), $exp);
1069 (v: $path:expr, $child:expr, $exp:expr) => (
1071 let path = Path::new($path);
1072 let child = Path::new($child);
1073 assert_eq!(path.ends_with_path(&child), $exp);
1078 t!(s: "a/b/c", "c", true);
1079 t!(s: "a/b/c", "d", false);
1080 t!(s: "foo/bar/quux", "bar", false);
1081 t!(s: "foo/bar/quux", "barquux", false);
1082 t!(s: "a/b/c", "b/c", true);
1083 t!(s: "a/b/c", "a/b/c", true);
1084 t!(s: "a/b/c", "foo/a/b/c", false);
1085 t!(s: "/a/b/c", "a/b/c", true);
1086 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1087 t!(s: "/a/b/c", "foo/a/b/c", false);
1088 t!(s: "a/b/c", "", false);
1089 t!(s: "", "", true);
1090 t!(s: "/a/b/c", "d/e/f", false);
1091 t!(s: "a/b/c", "a/b", false);
1092 t!(s: "a/b/c", "b", false);
1093 t!(v: b"a/b/c", b"b/c", true);
1094 t!(v: b"a/b/\xFF", b"\xFF", true);
1095 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1099 fn test_path_relative_from() {
1101 (s: $path:expr, $other:expr, $exp:expr) => (
1103 let path = Path::new($path);
1104 let other = Path::new($other);
1105 let res = path.path_relative_from(&other);
1106 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1111 t!(s: "a/b/c", "a/b", Some("c"));
1112 t!(s: "a/b/c", "a/b/d", Some("../c"));
1113 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1114 t!(s: "a/b/c", "a/b/c", Some("."));
1115 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1116 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1117 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1118 t!(s: "a/b/c", "/a/b/c", None);
1119 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1120 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1121 t!(s: "/a/b/c", "/a/b", Some("c"));
1122 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1123 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1124 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1125 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1126 t!(s: ".", "a", Some(".."));
1127 t!(s: ".", "a/b", Some("../.."));
1128 t!(s: ".", ".", Some("."));
1129 t!(s: "a", ".", Some("a"));
1130 t!(s: "a/b", ".", Some("a/b"));
1131 t!(s: "..", ".", Some(".."));
1132 t!(s: "a/b/c", "a/b/c", Some("."));
1133 t!(s: "/a/b/c", "/a/b/c", Some("."));
1134 t!(s: "/", "/", Some("."));
1135 t!(s: "/", ".", Some("/"));
1136 t!(s: "../../a", "b", Some("../../../a"));
1137 t!(s: "a", "../../b", None);
1138 t!(s: "../../a", "../../b", Some("../a"));
1139 t!(s: "../../a", "../../a/b", Some(".."));
1140 t!(s: "../../a/b", "../../a", Some("b"));
1144 fn test_components_iter() {
1146 (s: $path:expr, $exp:expr) => (
1148 let path = Path::new($path);
1149 let comps = path.components().collect::<Vec<&[u8]>>();
1150 let exp: &[&str] = &$exp;
1151 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1152 assert_eq!(comps, exps);
1153 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1154 let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1155 assert_eq!(comps, exps);
1158 (b: $arg:expr, [$($exp:expr),*]) => (
1160 let path = Path::new($arg);
1161 let comps = path.components().collect::<Vec<&[u8]>>();
1162 let exp: &[&[u8]] = &[$($exp),*];
1163 assert_eq!(comps, exp);
1164 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1165 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1166 assert_eq!(comps, exp)
1171 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1172 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1173 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1174 t!(s: "a/b/c", ["a", "b", "c"]);
1175 t!(s: "a/b/d", ["a", "b", "d"]);
1176 t!(s: "a/b/cd", ["a", "b", "cd"]);
1177 t!(s: "/a/b/c", ["a", "b", "c"]);
1182 t!(s: "..", [".."]);
1183 t!(s: "../..", ["..", ".."]);
1184 t!(s: "../../foo", ["..", "..", "foo"]);
1188 fn test_str_components() {
1190 (b: $arg:expr, $exp:expr) => (
1192 let path = Path::new($arg);
1193 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1194 let exp: &[Option<&str>] = &$exp;
1195 assert_eq!(comps, exp);
1196 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1197 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1198 assert_eq!(comps, exp);
1203 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1204 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1205 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1206 // str_components is a wrapper around components, so no need to do
1207 // the full set of tests
1214 use self::test::Bencher;
1216 use prelude::v1::{Clone, GenericPath};
1219 fn join_home_dir(b: &mut Bencher) {
1220 let posix_path = Path::new("/");
1222 posix_path.join("home");
1227 fn join_abs_path_home_dir(b: &mut Bencher) {
1228 let posix_path = Path::new("/");
1230 posix_path.join("/home");
1235 fn join_many_home_dir(b: &mut Bencher) {
1236 let posix_path = Path::new("/");
1238 posix_path.join_many(&["home"]);
1243 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1244 let posix_path = Path::new("/");
1246 posix_path.join_many(&["/home"]);
1251 fn push_home_dir(b: &mut Bencher) {
1252 let mut posix_path = Path::new("/");
1254 posix_path.push("home");
1259 fn push_abs_path_home_dir(b: &mut Bencher) {
1260 let mut posix_path = Path::new("/");
1262 posix_path.push("/home");
1267 fn push_many_home_dir(b: &mut Bencher) {
1268 let mut posix_path = Path::new("/");
1270 posix_path.push_many(&["home"]);
1275 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1276 let mut posix_path = Path::new("/");
1278 posix_path.push_many(&["/home"]);
1283 fn ends_with_path_home_dir(b: &mut Bencher) {
1284 let posix_home_path = Path::new("/home");
1286 posix_home_path.ends_with_path(&Path::new("home"));
1291 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1292 let posix_home_path = Path::new("/home");
1294 posix_home_path.ends_with_path(&Path::new("jome"));
1299 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1300 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1301 let mut sub = path.clone();
1304 path.is_ancestor_of(&sub);
1309 fn path_relative_from_forward(b: &mut Bencher) {
1310 let path = Path::new("/a/b/c");
1311 let mut other = path.clone();
1314 path.path_relative_from(&other);
1319 fn path_relative_from_same_level(b: &mut Bencher) {
1320 let path = Path::new("/a/b/c");
1321 let mut other = path.clone();
1325 path.path_relative_from(&other);
1330 fn path_relative_from_backward(b: &mut Bencher) {
1331 let path = Path::new("/a/b");
1332 let mut other = path.clone();
1335 path.path_relative_from(&other);