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};
22 use option::Option::{self, Some, None};
23 use slice::{AsSlice, Split, SliceExt, SliceConcatExt};
24 use str::{self, FromStr, StrExt};
27 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
29 /// Iterator that yields successive components of a Path as &[u8]
30 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
32 /// Iterator that yields successive components of a Path as Option<&str>
33 pub type StrComponents<'a> =
34 Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
36 /// Represents a POSIX file path
39 repr: Vec<u8>, // assumed to never be empty or contain NULs
40 sepidx: Option<uint> // index of the final separator in repr
43 /// The standard path separator character
44 pub const SEP: char = '/';
46 /// The standard path separator byte
47 pub const SEP_BYTE: u8 = SEP as u8;
49 /// Returns whether the given byte is a path separator
51 pub fn is_sep_byte(u: &u8) -> bool {
55 /// Returns whether the given char is a path separator
57 pub fn is_sep(c: char) -> bool {
61 impl fmt::Show for Path {
62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63 write!(f, "Path {{ {} }}", self.display())
67 impl PartialEq for Path {
69 fn eq(&self, other: &Path) -> bool {
70 self.repr == other.repr
76 impl PartialOrd for Path {
77 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
83 fn cmp(&self, other: &Path) -> Ordering {
84 self.repr.cmp(&other.repr)
88 impl FromStr for Path {
89 fn from_str(s: &str) -> Option<Path> {
94 impl<S: hash::Writer> hash::Hash<S> for Path {
96 fn hash(&self, state: &mut S) {
101 impl BytesContainer for Path {
103 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
108 impl GenericPathUnsafe for Path {
109 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
110 let path = Path::normalize(path.container_as_bytes());
111 assert!(!path.is_empty());
112 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
113 Path{ repr: path, sepidx: idx }
116 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
117 let filename = filename.container_as_bytes();
119 None if b".." == self.repr => {
120 let mut v = Vec::with_capacity(3 + filename.len());
121 v.push_all(dot_dot_static);
123 v.push_all(filename);
124 // FIXME: this is slow
125 self.repr = Path::normalize(v.as_slice());
128 self.repr = Path::normalize(filename);
130 Some(idx) if self.repr.index(&((idx+1)..)) == b".." => {
131 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
132 v.push_all(self.repr.as_slice());
134 v.push_all(filename);
135 // FIXME: this is slow
136 self.repr = Path::normalize(v.as_slice());
139 let mut v = Vec::with_capacity(idx + 1 + filename.len());
140 v.push_all(self.repr.index(&(0..(idx+1))));
141 v.push_all(filename);
142 // FIXME: this is slow
143 self.repr = Path::normalize(v.as_slice());
146 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
149 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
150 let path = path.container_as_bytes();
151 if !path.is_empty() {
152 if path[0] == SEP_BYTE {
153 self.repr = Path::normalize(path);
155 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
156 v.push_all(self.repr.as_slice());
159 // FIXME: this is slow
160 self.repr = Path::normalize(v.as_slice());
162 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
167 impl GenericPath for Path {
169 fn as_vec<'a>(&'a self) -> &'a [u8] {
173 fn into_vec(self) -> Vec<u8> {
177 fn dirname<'a>(&'a self) -> &'a [u8] {
179 None if b".." == self.repr => self.repr.as_slice(),
181 Some(0) => self.repr.index(&(0..1)),
182 Some(idx) if self.repr.index(&((idx+1)..)) == b".." => self.repr.as_slice(),
183 Some(idx) => self.repr.index(&(0..idx))
187 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
189 None if b"." == self.repr ||
190 b".." == self.repr => None,
191 None => Some(self.repr.as_slice()),
192 Some(idx) if self.repr.index(&((idx+1)..)) == b".." => None,
193 Some(0) if self.repr.index(&(1..)).is_empty() => None,
194 Some(idx) => Some(self.repr.index(&((idx+1)..)))
198 fn pop(&mut self) -> bool {
200 None if b"." == self.repr => false,
202 self.repr = vec![b'.'];
206 Some(0) if b"/" == self.repr => false,
209 self.repr.truncate(idx+1);
211 self.repr.truncate(idx);
213 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
219 fn root_path(&self) -> Option<Path> {
220 if self.is_absolute() {
228 fn is_absolute(&self) -> bool {
229 self.repr[0] == SEP_BYTE
232 fn is_ancestor_of(&self, other: &Path) -> bool {
233 if self.is_absolute() != other.is_absolute() {
236 let mut ita = self.components();
237 let mut itb = other.components();
238 if b"." == self.repr {
239 return match itb.next() {
241 Some(b) => b != b".."
245 match (ita.next(), itb.next()) {
247 (Some(a), Some(b)) if a == b => { continue },
248 (Some(a), _) if a == b".." => {
249 // if ita contains only .. components, it's an ancestor
250 return ita.all(|x| x == b"..");
259 fn path_relative_from(&self, base: &Path) -> Option<Path> {
260 if self.is_absolute() != base.is_absolute() {
261 if self.is_absolute() {
267 let mut ita = self.components();
268 let mut itb = base.components();
269 let mut comps = vec![];
271 match (ita.next(), itb.next()) {
272 (None, None) => break,
275 comps.extend(ita.by_ref());
278 (None, _) => comps.push(dot_dot_static),
279 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
280 (Some(a), Some(b)) if b == b"." => comps.push(a),
281 (Some(_), Some(b)) if b == b".." => return None,
282 (Some(a), Some(_)) => {
283 comps.push(dot_dot_static);
285 comps.push(dot_dot_static);
288 comps.extend(ita.by_ref());
293 Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
297 fn ends_with_path(&self, child: &Path) -> bool {
298 if !child.is_relative() { return false; }
299 let mut selfit = self.components().rev();
300 let mut childit = child.components().rev();
302 match (selfit.next(), childit.next()) {
303 (Some(a), Some(b)) => if a != b { return false; },
304 (Some(_), None) => break,
305 (None, Some(_)) => return false,
306 (None, None) => break
314 /// Returns a new Path from a byte vector or string
318 /// Panics the task if the vector contains a NUL.
320 pub fn new<T: BytesContainer>(path: T) -> Path {
321 GenericPath::new(path)
324 /// Returns a new Path from a byte vector or string, if possible
326 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
327 GenericPath::new_opt(path)
330 /// Returns a normalized byte vector representation of a path, by removing all empty
331 /// components, and unnecessary . and .. components.
332 fn normalize<V: ?Sized + AsSlice<u8>>(v: &V) -> Vec<u8> {
333 // borrowck is being very picky
335 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
336 let v_ = if is_abs { v.as_slice().index(&(1..)) } else { v.as_slice() };
337 let comps = normalize_helper(v_, is_abs);
341 if is_abs && comps.is_empty() {
344 let n = if is_abs { comps.len() } else { comps.len() - 1} +
345 comps.iter().map(|v| v.len()).sum();
346 let mut v = Vec::with_capacity(n);
347 let mut it = comps.into_iter();
351 Some(comp) => v.push_all(comp)
364 None => v.as_slice().to_vec(),
369 /// Returns an iterator that yields each component of the path in turn.
370 /// Does not distinguish between absolute and relative paths, e.g.
371 /// /a/b/c and a/b/c yield the same set of components.
372 /// A path of "/" yields no components. A path of "." yields one component.
373 pub fn components<'a>(&'a self) -> Components<'a> {
374 let v = if self.repr[0] == SEP_BYTE {
375 self.repr.index(&(1..))
376 } else { self.repr.as_slice() };
377 let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
378 let mut ret = v.split(is_sep_byte);
380 // consume the empty "" component
386 /// Returns an iterator that yields each component of the path as Option<&str>.
387 /// See components() for details.
388 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
389 fn from_utf8(s: &[u8]) -> Option<&str> {
390 str::from_utf8(s).ok()
392 let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
393 self.components().map(f)
397 // None result means the byte vector didn't need normalizing
398 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
399 if is_abs && v.is_empty() {
402 let mut comps: Vec<&'a [u8]> = vec![];
404 let mut changed = false;
405 for comp in v.split(is_sep_byte) {
406 if comp.is_empty() { changed = true }
407 else if comp == b"." { changed = true }
408 else if comp == b".." {
409 if is_abs && comps.is_empty() { changed = true }
410 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
411 else { comps.pop().unwrap(); changed = true }
412 } else { comps.push(comp) }
415 if comps.is_empty() && !is_abs {
419 comps.push(dot_static);
427 #[allow(non_upper_case_globals)]
428 static dot_static: &'static [u8] = b".";
429 #[allow(non_upper_case_globals)]
430 static dot_dot_static: &'static [u8] = b"..";
437 use iter::IteratorExt;
438 use option::Option::{self, Some, None};
439 use path::GenericPath;
440 use slice::{AsSlice, SliceExt};
441 use str::{self, Str, StrExt};
442 use string::ToString;
446 (s: $path:expr, $exp:expr) => (
449 assert_eq!(path.as_str(), Some($exp));
452 (v: $path:expr, $exp:expr) => (
455 assert_eq!(path.as_vec(), $exp);
462 let empty: &[u8] = &[];
463 t!(v: Path::new(empty), b".");
464 t!(v: Path::new(b"/"), b"/");
465 t!(v: Path::new(b"a/b/c"), b"a/b/c");
466 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
467 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
468 let p = Path::new(b"a/b/c\xFF");
469 assert!(p.as_str().is_none());
471 t!(s: Path::new(""), ".");
472 t!(s: Path::new("/"), "/");
473 t!(s: Path::new("hi"), "hi");
474 t!(s: Path::new("hi/"), "hi");
475 t!(s: Path::new("/lib"), "/lib");
476 t!(s: Path::new("/lib/"), "/lib");
477 t!(s: Path::new("hi/there"), "hi/there");
478 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
480 t!(s: Path::new("hi/there/"), "hi/there");
481 t!(s: Path::new("hi/../there"), "there");
482 t!(s: Path::new("../hi/there"), "../hi/there");
483 t!(s: Path::new("/../hi/there"), "/hi/there");
484 t!(s: Path::new("foo/.."), ".");
485 t!(s: Path::new("/foo/.."), "/");
486 t!(s: Path::new("/foo/../.."), "/");
487 t!(s: Path::new("/foo/../../bar"), "/bar");
488 t!(s: Path::new("/./hi/./there/."), "/hi/there");
489 t!(s: Path::new("/./hi/./there/./.."), "/hi");
490 t!(s: Path::new("foo/../.."), "..");
491 t!(s: Path::new("foo/../../.."), "../..");
492 t!(s: Path::new("foo/../../bar"), "../bar");
494 assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
495 assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
498 let p = Path::new(b"foo/bar\x80");
499 assert!(p.as_str().is_none());
503 fn test_opt_paths() {
504 assert!(Path::new_opt(b"foo/bar\0").is_none());
505 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
506 assert!(Path::new_opt("foo/bar\0").is_none());
507 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
511 fn test_null_byte() {
513 let result = Thread::scoped(move|| {
514 Path::new(b"foo/bar\0")
516 assert!(result.is_err());
518 let result = Thread::scoped(move|| {
519 Path::new("test").set_filename(b"f\0o")
521 assert!(result.is_err());
523 let result = Thread::scoped(move|| {
524 Path::new("test").push(b"f\0o");
526 assert!(result.is_err());
530 fn test_display_str() {
532 ($path:expr, $disp:ident, $exp:expr) => (
534 let path = Path::new($path);
535 assert_eq!(path.$disp().to_string(), $exp);
539 t!("foo", display, "foo");
540 t!(b"foo\x80", display, "foo\u{FFFD}");
541 t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
542 t!(b"foo\xFF/bar", filename_display, "bar");
543 t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
544 t!(b"/", filename_display, "");
547 ($path:expr, $exp:expr) => (
549 let path = Path::new($path);
550 let mo = path.display().as_cow();
551 assert_eq!(mo.as_slice(), $exp);
554 ($path:expr, $exp:expr, filename) => (
556 let path = Path::new($path);
557 let mo = path.filename_display().as_cow();
558 assert_eq!(mo.as_slice(), $exp);
564 t!(b"foo\x80", "foo\u{FFFD}");
565 t!(b"foo\xFFbar", "foo\u{FFFD}bar");
566 t!(b"foo\xFF/bar", "bar", filename);
567 t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
568 t!(b"/", "", filename);
574 ($path:expr, $exp:expr, $expf:expr) => (
576 let path = Path::new($path);
577 let f = format!("{}", path.display());
579 let f = format!("{}", path.filename_display());
580 assert_eq!(f, $expf);
585 t!(b"foo", "foo", "foo");
586 t!(b"foo/bar", "foo/bar", "bar");
588 t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
589 t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
590 t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
591 t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
595 fn test_components() {
597 (s: $path:expr, $op:ident, $exp:expr) => (
599 let path = Path::new($path);
600 assert_eq!(path.$op(), ($exp).as_bytes());
603 (s: $path:expr, $op:ident, $exp:expr, opt) => (
605 let path = Path::new($path);
606 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
607 assert_eq!(left, $exp);
610 (v: $path:expr, $op:ident, $exp:expr) => (
613 let path = Path::new(arg);
614 assert_eq!(path.$op(), $exp);
619 t!(v: b"a/b/c", filename, Some(b"c"));
620 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
621 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
622 t!(s: "a/b/c", filename, Some("c"), opt);
623 t!(s: "/a/b/c", filename, Some("c"), opt);
624 t!(s: "a", filename, Some("a"), opt);
625 t!(s: "/a", filename, Some("a"), opt);
626 t!(s: ".", filename, None, opt);
627 t!(s: "/", filename, None, opt);
628 t!(s: "..", filename, None, opt);
629 t!(s: "../..", filename, None, opt);
631 t!(v: b"a/b/c", dirname, b"a/b");
632 t!(v: b"a/b/c\xFF", dirname, b"a/b");
633 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
634 t!(s: "a/b/c", dirname, "a/b");
635 t!(s: "/a/b/c", dirname, "/a/b");
636 t!(s: "a", dirname, ".");
637 t!(s: "/a", dirname, "/");
638 t!(s: ".", dirname, ".");
639 t!(s: "/", dirname, "/");
640 t!(s: "..", dirname, "..");
641 t!(s: "../..", dirname, "../..");
643 t!(v: b"hi/there.txt", filestem, Some(b"there"));
644 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
645 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
646 t!(s: "hi/there.txt", filestem, Some("there"), opt);
647 t!(s: "hi/there", filestem, Some("there"), opt);
648 t!(s: "there.txt", filestem, Some("there"), opt);
649 t!(s: "there", filestem, Some("there"), opt);
650 t!(s: ".", filestem, None, opt);
651 t!(s: "/", filestem, None, opt);
652 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
653 t!(s: ".bar", filestem, Some(".bar"), opt);
654 t!(s: "..bar", filestem, Some("."), opt);
655 t!(s: "hi/there..txt", filestem, Some("there."), opt);
656 t!(s: "..", filestem, None, opt);
657 t!(s: "../..", filestem, None, opt);
659 t!(v: b"hi/there.txt", extension, Some(b"txt"));
660 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
661 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
662 t!(v: b"hi/there", extension, None);
663 t!(v: b"hi/there\x80", extension, None);
664 t!(s: "hi/there.txt", extension, Some("txt"), opt);
665 t!(s: "hi/there", extension, None, opt);
666 t!(s: "there.txt", extension, Some("txt"), opt);
667 t!(s: "there", extension, None, opt);
668 t!(s: ".", extension, None, opt);
669 t!(s: "/", extension, None, opt);
670 t!(s: "foo/.bar", extension, None, opt);
671 t!(s: ".bar", extension, None, opt);
672 t!(s: "..bar", extension, Some("bar"), opt);
673 t!(s: "hi/there..txt", extension, Some("txt"), opt);
674 t!(s: "..", extension, None, opt);
675 t!(s: "../..", extension, None, opt);
681 (s: $path:expr, $join:expr) => (
685 let mut p1 = Path::new(path);
688 assert_eq!(p1, p2.join(join));
693 t!(s: "a/b/c", "..");
694 t!(s: "/a/b/c", "d");
696 t!(s: "a/b", "/c/d");
700 fn test_push_path() {
702 (s: $path:expr, $push:expr, $exp:expr) => (
704 let mut p = Path::new($path);
705 let push = Path::new($push);
707 assert_eq!(p.as_str(), Some($exp));
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", "a/b/c/d");
715 t!(s: "a/b", "/c/d", "/c/d");
716 t!(s: "a/b", ".", "a/b");
717 t!(s: "a/b", "../c", "a/c");
721 fn test_push_many() {
723 (s: $path:expr, $push:expr, $exp:expr) => (
725 let mut p = Path::new($path);
727 assert_eq!(p.as_str(), Some($exp));
730 (v: $path:expr, $push:expr, $exp:expr) => (
732 let mut p = Path::new($path);
734 assert_eq!(p.as_vec(), $exp);
739 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
740 t!(s: "a/b/c", ["d", "/e"], "/e");
741 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
742 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
743 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
744 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
745 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
751 (s: $path:expr, $left:expr, $right:expr) => (
753 let mut p = Path::new($path);
754 let result = p.pop();
755 assert_eq!(p.as_str(), Some($left));
756 assert_eq!(result, $right);
759 (b: $path:expr, $left:expr, $right:expr) => (
761 let mut p = Path::new($path);
762 let result = p.pop();
763 assert_eq!(p.as_vec(), $left);
764 assert_eq!(result, $right);
769 t!(b: b"a/b/c", b"a/b", true);
770 t!(b: b"a", b".", true);
771 t!(b: b".", b".", false);
772 t!(b: b"/a", b"/", true);
773 t!(b: b"/", b"/", false);
774 t!(b: b"a/b/c\x80", b"a/b", true);
775 t!(b: b"a/b\x80/c", b"a/b\x80", true);
776 t!(b: b"\xFF", b".", true);
777 t!(b: b"/\xFF", b"/", true);
778 t!(s: "a/b/c", "a/b", true);
779 t!(s: "a", ".", true);
780 t!(s: ".", ".", false);
781 t!(s: "/a", "/", true);
782 t!(s: "/", "/", false);
786 fn test_root_path() {
787 assert_eq!(Path::new(b"a/b/c").root_path(), None);
788 assert_eq!(Path::new(b"/a/b/c").root_path(), Some(Path::new("/")));
793 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
794 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
795 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
796 t!(s: Path::new("a/b/c").join(".."), "a/b");
797 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
798 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
799 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
800 t!(s: Path::new(".").join("a/b"), "a/b");
801 t!(s: Path::new("/").join("a/b"), "/a/b");
805 fn test_join_path() {
807 (s: $path:expr, $join:expr, $exp:expr) => (
809 let path = Path::new($path);
810 let join = Path::new($join);
811 let res = path.join(&join);
812 assert_eq!(res.as_str(), Some($exp));
817 t!(s: "a/b/c", "..", "a/b");
818 t!(s: "/a/b/c", "d", "/a/b/c/d");
819 t!(s: "a/b", "c/d", "a/b/c/d");
820 t!(s: "a/b", "/c/d", "/c/d");
821 t!(s: ".", "a/b", "a/b");
822 t!(s: "/", "a/b", "/a/b");
826 fn test_join_many() {
828 (s: $path:expr, $join:expr, $exp:expr) => (
830 let path = Path::new($path);
831 let res = path.join_many(&$join);
832 assert_eq!(res.as_str(), Some($exp));
835 (v: $path:expr, $join:expr, $exp:expr) => (
837 let path = Path::new($path);
838 let res = path.join_many(&$join);
839 assert_eq!(res.as_vec(), $exp);
844 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
845 t!(s: "a/b/c", ["..", "d"], "a/b/d");
846 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
847 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
848 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
849 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
853 fn test_with_helpers() {
854 let empty: &[u8] = &[];
856 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
857 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
858 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
860 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
861 t!(s: Path::new(".").with_filename("foo"), "foo");
862 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
863 t!(s: Path::new("/").with_filename("foo"), "/foo");
864 t!(s: Path::new("/a").with_filename("foo"), "/foo");
865 t!(s: Path::new("foo").with_filename("bar"), "bar");
866 t!(s: Path::new("/").with_filename("foo/"), "/foo");
867 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
868 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
869 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
870 t!(s: Path::new("a/b/c").with_filename(".."), "a");
871 t!(s: Path::new("/a").with_filename(""), "/");
872 t!(s: Path::new("foo").with_filename(""), ".");
873 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
874 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
875 t!(s: Path::new("..").with_filename("foo"), "../foo");
876 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
877 t!(s: Path::new("..").with_filename(""), "..");
878 t!(s: Path::new("../..").with_filename(""), "../..");
880 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
881 b"hi/there\x80.exe");
882 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
884 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
885 b"hi/there\x80.\xFF");
886 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
887 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
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.txt").with_extension(".."), "hi/there...");
891 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
892 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
893 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
894 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
895 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
896 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
897 t!(s: Path::new("/").with_extension("txt"), "/");
898 t!(s: Path::new("/").with_extension("."), "/");
899 t!(s: Path::new("/").with_extension(".."), "/");
900 t!(s: Path::new(".").with_extension("txt"), ".");
906 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
910 let mut p1 = Path::new(path);
912 let p2 = Path::new(path);
913 assert_eq!(p1, p2.$with(arg));
916 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
920 let mut p1 = Path::new(path);
922 let p2 = Path::new(path);
923 assert_eq!(p1, p2.$with(arg));
928 t!(v: b"a/b/c", set_filename, with_filename, b"d");
929 t!(v: b"/", set_filename, with_filename, b"foo");
930 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
931 t!(s: "a/b/c", set_filename, with_filename, "d");
932 t!(s: "/", set_filename, with_filename, "foo");
933 t!(s: ".", set_filename, with_filename, "foo");
934 t!(s: "a/b", set_filename, with_filename, "");
935 t!(s: "a", set_filename, with_filename, "");
937 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
938 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
939 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
940 t!(s: "hi/there.", set_extension, with_extension, "txt");
941 t!(s: "hi/there", set_extension, with_extension, "txt");
942 t!(s: "hi/there.txt", set_extension, with_extension, "");
943 t!(s: "hi/there", set_extension, with_extension, "");
944 t!(s: ".", set_extension, with_extension, "txt");
950 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
953 assert_eq!(path.filename_str(), $filename);
954 assert_eq!(path.dirname_str(), $dirname);
955 assert_eq!(path.filestem_str(), $filestem);
956 assert_eq!(path.extension_str(), $ext);
959 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
962 assert_eq!(path.filename(), $filename);
963 assert_eq!(path.dirname(), $dirname);
964 assert_eq!(path.filestem(), $filestem);
965 assert_eq!(path.extension(), $ext);
970 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
971 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
972 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
973 Some(b"there"), Some(b"\xFF"));
974 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), 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("../.."), None, Some("../.."), None, None);
979 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
980 Some("there"), Some("txt"));
981 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
982 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
983 Some("there"), Some(""));
984 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
985 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
986 Some("."), Some("there"));
987 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
988 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
989 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
990 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
995 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
996 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
997 t!(s: Path::new("hi/there").dir_path(), "hi");
998 t!(s: Path::new("hi").dir_path(), ".");
999 t!(s: Path::new("/hi").dir_path(), "/");
1000 t!(s: Path::new("/").dir_path(), "/");
1001 t!(s: Path::new("..").dir_path(), "..");
1002 t!(s: Path::new("../..").dir_path(), "../..");
1006 fn test_is_absolute() {
1008 (s: $path:expr, $abs:expr, $rel:expr) => (
1010 let path = Path::new($path);
1011 assert_eq!(path.is_absolute(), $abs);
1012 assert_eq!(path.is_relative(), $rel);
1016 t!(s: "a/b/c", false, true);
1017 t!(s: "/a/b/c", true, false);
1018 t!(s: "a", false, true);
1019 t!(s: "/a", true, false);
1020 t!(s: ".", false, true);
1021 t!(s: "/", true, false);
1022 t!(s: "..", false, true);
1023 t!(s: "../..", false, true);
1027 fn test_is_ancestor_of() {
1029 (s: $path:expr, $dest:expr, $exp:expr) => (
1031 let path = Path::new($path);
1032 let dest = Path::new($dest);
1033 assert_eq!(path.is_ancestor_of(&dest), $exp);
1038 t!(s: "a/b/c", "a/b/c/d", true);
1039 t!(s: "a/b/c", "a/b/c", true);
1040 t!(s: "a/b/c", "a/b", false);
1041 t!(s: "/a/b/c", "/a/b/c", true);
1042 t!(s: "/a/b", "/a/b/c", true);
1043 t!(s: "/a/b/c/d", "/a/b/c", false);
1044 t!(s: "/a/b", "a/b/c", false);
1045 t!(s: "a/b", "/a/b/c", false);
1046 t!(s: "a/b/c", "a/b/d", false);
1047 t!(s: "../a/b/c", "a/b/c", false);
1048 t!(s: "a/b/c", "../a/b/c", false);
1049 t!(s: "a/b/c", "a/b/cd", false);
1050 t!(s: "a/b/cd", "a/b/c", false);
1051 t!(s: "../a/b", "../a/b/c", true);
1052 t!(s: ".", "a/b", true);
1053 t!(s: ".", ".", true);
1054 t!(s: "/", "/", true);
1055 t!(s: "/", "/a/b", true);
1056 t!(s: "..", "a/b", true);
1057 t!(s: "../..", "a/b", true);
1061 fn test_ends_with_path() {
1063 (s: $path:expr, $child:expr, $exp:expr) => (
1065 let path = Path::new($path);
1066 let child = Path::new($child);
1067 assert_eq!(path.ends_with_path(&child), $exp);
1070 (v: $path:expr, $child:expr, $exp:expr) => (
1072 let path = Path::new($path);
1073 let child = Path::new($child);
1074 assert_eq!(path.ends_with_path(&child), $exp);
1079 t!(s: "a/b/c", "c", true);
1080 t!(s: "a/b/c", "d", false);
1081 t!(s: "foo/bar/quux", "bar", false);
1082 t!(s: "foo/bar/quux", "barquux", false);
1083 t!(s: "a/b/c", "b/c", true);
1084 t!(s: "a/b/c", "a/b/c", true);
1085 t!(s: "a/b/c", "foo/a/b/c", false);
1086 t!(s: "/a/b/c", "a/b/c", true);
1087 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1088 t!(s: "/a/b/c", "foo/a/b/c", false);
1089 t!(s: "a/b/c", "", false);
1090 t!(s: "", "", true);
1091 t!(s: "/a/b/c", "d/e/f", false);
1092 t!(s: "a/b/c", "a/b", false);
1093 t!(s: "a/b/c", "b", false);
1094 t!(v: b"a/b/c", b"b/c", true);
1095 t!(v: b"a/b/\xFF", b"\xFF", true);
1096 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1100 fn test_path_relative_from() {
1102 (s: $path:expr, $other:expr, $exp:expr) => (
1104 let path = Path::new($path);
1105 let other = Path::new($other);
1106 let res = path.path_relative_from(&other);
1107 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1112 t!(s: "a/b/c", "a/b", Some("c"));
1113 t!(s: "a/b/c", "a/b/d", Some("../c"));
1114 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1115 t!(s: "a/b/c", "a/b/c", Some("."));
1116 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1117 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1118 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1119 t!(s: "a/b/c", "/a/b/c", None);
1120 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1121 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1122 t!(s: "/a/b/c", "/a/b", Some("c"));
1123 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1124 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1125 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1126 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1127 t!(s: ".", "a", Some(".."));
1128 t!(s: ".", "a/b", Some("../.."));
1129 t!(s: ".", ".", Some("."));
1130 t!(s: "a", ".", Some("a"));
1131 t!(s: "a/b", ".", Some("a/b"));
1132 t!(s: "..", ".", Some(".."));
1133 t!(s: "a/b/c", "a/b/c", Some("."));
1134 t!(s: "/a/b/c", "/a/b/c", Some("."));
1135 t!(s: "/", "/", Some("."));
1136 t!(s: "/", ".", Some("/"));
1137 t!(s: "../../a", "b", Some("../../../a"));
1138 t!(s: "a", "../../b", None);
1139 t!(s: "../../a", "../../b", Some("../a"));
1140 t!(s: "../../a", "../../a/b", Some(".."));
1141 t!(s: "../../a/b", "../../a", Some("b"));
1145 fn test_components_iter() {
1147 (s: $path:expr, $exp:expr) => (
1149 let path = Path::new($path);
1150 let comps = path.components().collect::<Vec<&[u8]>>();
1151 let exp: &[&str] = &$exp;
1152 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1153 assert_eq!(comps, exps);
1154 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1155 let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1156 assert_eq!(comps, exps);
1159 (b: $arg:expr, [$($exp:expr),*]) => (
1161 let path = Path::new($arg);
1162 let comps = path.components().collect::<Vec<&[u8]>>();
1163 let exp: &[&[u8]] = &[$($exp),*];
1164 assert_eq!(comps, exp);
1165 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1166 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1167 assert_eq!(comps, exp)
1172 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1173 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1174 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1175 t!(s: "a/b/c", ["a", "b", "c"]);
1176 t!(s: "a/b/d", ["a", "b", "d"]);
1177 t!(s: "a/b/cd", ["a", "b", "cd"]);
1178 t!(s: "/a/b/c", ["a", "b", "c"]);
1183 t!(s: "..", [".."]);
1184 t!(s: "../..", ["..", ".."]);
1185 t!(s: "../../foo", ["..", "..", "foo"]);
1189 fn test_str_components() {
1191 (b: $arg:expr, $exp:expr) => (
1193 let path = Path::new($arg);
1194 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1195 let exp: &[Option<&str>] = &$exp;
1196 assert_eq!(comps, exp);
1197 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1198 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1199 assert_eq!(comps, exp);
1204 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1205 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1206 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1207 // str_components is a wrapper around components, so no need to do
1208 // the full set of tests
1215 use self::test::Bencher;
1217 use prelude::v1::{Clone, GenericPath};
1220 fn join_home_dir(b: &mut Bencher) {
1221 let posix_path = Path::new("/");
1223 posix_path.join("home");
1228 fn join_abs_path_home_dir(b: &mut Bencher) {
1229 let posix_path = Path::new("/");
1231 posix_path.join("/home");
1236 fn join_many_home_dir(b: &mut Bencher) {
1237 let posix_path = Path::new("/");
1239 posix_path.join_many(&["home"]);
1244 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1245 let posix_path = Path::new("/");
1247 posix_path.join_many(&["/home"]);
1252 fn push_home_dir(b: &mut Bencher) {
1253 let mut posix_path = Path::new("/");
1255 posix_path.push("home");
1260 fn push_abs_path_home_dir(b: &mut Bencher) {
1261 let mut posix_path = Path::new("/");
1263 posix_path.push("/home");
1268 fn push_many_home_dir(b: &mut Bencher) {
1269 let mut posix_path = Path::new("/");
1271 posix_path.push_many(&["home"]);
1276 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1277 let mut posix_path = Path::new("/");
1279 posix_path.push_many(&["/home"]);
1284 fn ends_with_path_home_dir(b: &mut Bencher) {
1285 let posix_home_path = Path::new("/home");
1287 posix_home_path.ends_with_path(&Path::new("home"));
1292 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1293 let posix_home_path = Path::new("/home");
1295 posix_home_path.ends_with_path(&Path::new("jome"));
1300 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1301 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1302 let mut sub = path.clone();
1305 path.is_ancestor_of(&sub);
1310 fn path_relative_from_forward(b: &mut Bencher) {
1311 let path = Path::new("/a/b/c");
1312 let mut other = path.clone();
1315 path.path_relative_from(&other);
1320 fn path_relative_from_same_level(b: &mut Bencher) {
1321 let path = Path::new("/a/b/c");
1322 let mut other = path.clone();
1326 path.path_relative_from(&other);
1331 fn path_relative_from_backward(b: &mut Bencher) {
1332 let path = Path::new("/a/b");
1333 let mut other = path.clone();
1336 path.path_relative_from(&other);