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};
17 use iter::{AdditiveIterator, Extend};
18 use iter::{Iterator, IteratorExt, Map};
20 use option::Option::{self, Some, None};
21 use slice::{AsSlice, Split, SliceExt, SliceConcatExt};
22 use str::{self, FromStr, StrExt};
25 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
27 /// Iterator that yields successive components of a Path as &[u8]
28 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
30 /// Iterator that yields successive components of a Path as Option<&str>
31 pub type StrComponents<'a> =
32 Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
34 /// Represents a POSIX file path
37 repr: Vec<u8>, // assumed to never be empty or contain NULs
38 sepidx: Option<uint> // index of the final separator in repr
41 /// The standard path separator character
42 pub const SEP: char = '/';
44 /// The standard path separator byte
45 pub const SEP_BYTE: u8 = SEP as u8;
47 /// Returns whether the given byte is a path separator
49 pub fn is_sep_byte(u: &u8) -> bool {
53 /// Returns whether the given char is a path separator
55 pub fn is_sep(c: char) -> bool {
59 impl PartialEq for Path {
61 fn eq(&self, other: &Path) -> bool {
62 self.repr == other.repr
68 impl PartialOrd for Path {
69 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
75 fn cmp(&self, other: &Path) -> Ordering {
76 self.repr.cmp(&other.repr)
80 impl FromStr for Path {
81 fn from_str(s: &str) -> Option<Path> {
86 impl<S: hash::Writer> hash::Hash<S> for Path {
88 fn hash(&self, state: &mut S) {
93 impl BytesContainer for Path {
95 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
100 impl GenericPathUnsafe for Path {
101 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
102 let path = Path::normalize(path.container_as_bytes());
103 assert!(!path.is_empty());
104 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
105 Path{ repr: path, sepidx: idx }
108 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
109 let filename = filename.container_as_bytes();
111 None if b".." == self.repr => {
112 let mut v = Vec::with_capacity(3 + filename.len());
113 v.push_all(dot_dot_static);
115 v.push_all(filename);
116 // FIXME: this is slow
117 self.repr = Path::normalize(v.as_slice());
120 self.repr = Path::normalize(filename);
122 Some(idx) if self.repr[idx+1..] == b".." => {
123 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
124 v.push_all(self.repr.as_slice());
126 v.push_all(filename);
127 // FIXME: this is slow
128 self.repr = Path::normalize(v.as_slice());
131 let mut v = Vec::with_capacity(idx + 1 + filename.len());
132 v.push_all(self.repr[..idx+1]);
133 v.push_all(filename);
134 // FIXME: this is slow
135 self.repr = Path::normalize(v.as_slice());
138 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
141 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
142 let path = path.container_as_bytes();
143 if !path.is_empty() {
144 if path[0] == SEP_BYTE {
145 self.repr = Path::normalize(path);
147 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
148 v.push_all(self.repr.as_slice());
151 // FIXME: this is slow
152 self.repr = Path::normalize(v.as_slice());
154 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
159 impl GenericPath for Path {
161 fn as_vec<'a>(&'a self) -> &'a [u8] {
165 fn into_vec(self) -> Vec<u8> {
169 fn dirname<'a>(&'a self) -> &'a [u8] {
171 None if b".." == self.repr => self.repr.as_slice(),
173 Some(0) => self.repr[..1],
174 Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(),
175 Some(idx) => self.repr[..idx]
179 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
181 None if b"." == self.repr ||
182 b".." == self.repr => None,
183 None => Some(self.repr.as_slice()),
184 Some(idx) if self.repr[idx+1..] == b".." => None,
185 Some(0) if self.repr[1..].is_empty() => None,
186 Some(idx) => Some(self.repr[idx+1..])
190 fn pop(&mut self) -> bool {
192 None if b"." == self.repr => false,
194 self.repr = vec![b'.'];
198 Some(0) if b"/" == self.repr => false,
201 self.repr.truncate(idx+1);
203 self.repr.truncate(idx);
205 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
211 fn root_path(&self) -> Option<Path> {
212 if self.is_absolute() {
220 fn is_absolute(&self) -> bool {
221 self.repr[0] == SEP_BYTE
224 fn is_ancestor_of(&self, other: &Path) -> bool {
225 if self.is_absolute() != other.is_absolute() {
228 let mut ita = self.components();
229 let mut itb = other.components();
230 if b"." == self.repr {
231 return match itb.next() {
233 Some(b) => b != b".."
237 match (ita.next(), itb.next()) {
239 (Some(a), Some(b)) if a == b => { continue },
240 (Some(a), _) if a == b".." => {
241 // if ita contains only .. components, it's an ancestor
242 return ita.all(|x| x == b"..");
251 fn path_relative_from(&self, base: &Path) -> Option<Path> {
252 if self.is_absolute() != base.is_absolute() {
253 if self.is_absolute() {
259 let mut ita = self.components();
260 let mut itb = base.components();
261 let mut comps = vec![];
263 match (ita.next(), itb.next()) {
264 (None, None) => break,
267 comps.extend(ita.by_ref());
270 (None, _) => comps.push(dot_dot_static),
271 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
272 (Some(a), Some(b)) if b == b"." => comps.push(a),
273 (Some(_), Some(b)) if b == b".." => return None,
274 (Some(a), Some(_)) => {
275 comps.push(dot_dot_static);
277 comps.push(dot_dot_static);
280 comps.extend(ita.by_ref());
285 Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
289 fn ends_with_path(&self, child: &Path) -> bool {
290 if !child.is_relative() { return false; }
291 let mut selfit = self.components().rev();
292 let mut childit = child.components().rev();
294 match (selfit.next(), childit.next()) {
295 (Some(a), Some(b)) => if a != b { return false; },
296 (Some(_), None) => break,
297 (None, Some(_)) => return false,
298 (None, None) => break
306 /// Returns a new Path from a byte vector or string
310 /// Panics the task if the vector contains a NUL.
312 pub fn new<T: BytesContainer>(path: T) -> Path {
313 GenericPath::new(path)
316 /// Returns a new Path from a byte vector or string, if possible
318 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
319 GenericPath::new_opt(path)
322 /// Returns a normalized byte vector representation of a path, by removing all empty
323 /// components, and unnecessary . and .. components.
324 fn normalize<V: ?Sized + AsSlice<u8>>(v: &V) -> Vec<u8> {
325 // borrowck is being very picky
327 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
328 let v_ = if is_abs { v.as_slice()[1..] } else { v.as_slice() };
329 let comps = normalize_helper(v_, is_abs);
333 if is_abs && comps.is_empty() {
336 let n = if is_abs { comps.len() } else { comps.len() - 1} +
337 comps.iter().map(|v| v.len()).sum();
338 let mut v = Vec::with_capacity(n);
339 let mut it = comps.into_iter();
343 Some(comp) => v.push_all(comp)
356 None => v.as_slice().to_vec(),
361 /// Returns an iterator that yields each component of the path in turn.
362 /// Does not distinguish between absolute and relative paths, e.g.
363 /// /a/b/c and a/b/c yield the same set of components.
364 /// A path of "/" yields no components. A path of "." yields one component.
365 pub fn components<'a>(&'a self) -> Components<'a> {
366 let v = if self.repr[0] == SEP_BYTE {
368 } else { self.repr.as_slice() };
369 let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
370 let mut ret = v.split(is_sep_byte);
372 // consume the empty "" component
378 /// Returns an iterator that yields each component of the path as Option<&str>.
379 /// See components() for details.
380 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
381 fn from_utf8(s: &[u8]) -> Option<&str> {
382 str::from_utf8(s).ok()
384 let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
385 self.components().map(f)
389 // None result means the byte vector didn't need normalizing
390 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
391 if is_abs && v.is_empty() {
394 let mut comps: Vec<&'a [u8]> = vec![];
396 let mut changed = false;
397 for comp in v.split(is_sep_byte) {
398 if comp.is_empty() { changed = true }
399 else if comp == b"." { changed = true }
400 else if comp == b".." {
401 if is_abs && comps.is_empty() { changed = true }
402 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
403 else { comps.pop().unwrap(); changed = true }
404 } else { comps.push(comp) }
407 if comps.is_empty() && !is_abs {
411 comps.push(dot_static);
419 #[allow(non_upper_case_globals)]
420 static dot_static: &'static [u8] = b".";
421 #[allow(non_upper_case_globals)]
422 static dot_dot_static: &'static [u8] = b"..";
429 use iter::IteratorExt;
430 use option::Option::{self, Some, None};
431 use path::GenericPath;
432 use slice::{AsSlice, SliceExt};
433 use str::{self, Str, StrExt};
434 use string::ToString;
438 (s: $path:expr, $exp:expr) => (
441 assert!(path.as_str() == Some($exp));
444 (v: $path:expr, $exp:expr) => (
447 assert!(path.as_vec() == $exp);
454 let empty: &[u8] = &[];
455 t!(v: Path::new(empty), b".");
456 t!(v: Path::new(b"/"), b"/");
457 t!(v: Path::new(b"a/b/c"), b"a/b/c");
458 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
459 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
460 let p = Path::new(b"a/b/c\xFF");
461 assert!(p.as_str() == None);
463 t!(s: Path::new(""), ".");
464 t!(s: Path::new("/"), "/");
465 t!(s: Path::new("hi"), "hi");
466 t!(s: Path::new("hi/"), "hi");
467 t!(s: Path::new("/lib"), "/lib");
468 t!(s: Path::new("/lib/"), "/lib");
469 t!(s: Path::new("hi/there"), "hi/there");
470 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
472 t!(s: Path::new("hi/there/"), "hi/there");
473 t!(s: Path::new("hi/../there"), "there");
474 t!(s: Path::new("../hi/there"), "../hi/there");
475 t!(s: Path::new("/../hi/there"), "/hi/there");
476 t!(s: Path::new("foo/.."), ".");
477 t!(s: Path::new("/foo/.."), "/");
478 t!(s: Path::new("/foo/../.."), "/");
479 t!(s: Path::new("/foo/../../bar"), "/bar");
480 t!(s: Path::new("/./hi/./there/."), "/hi/there");
481 t!(s: Path::new("/./hi/./there/./.."), "/hi");
482 t!(s: Path::new("foo/../.."), "..");
483 t!(s: Path::new("foo/../../.."), "../..");
484 t!(s: Path::new("foo/../../bar"), "../bar");
486 assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
487 assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
490 let p = Path::new(b"foo/bar\x80");
491 assert!(p.as_str() == None);
495 fn test_opt_paths() {
496 assert!(Path::new_opt(b"foo/bar\0") == None);
497 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
498 assert!(Path::new_opt("foo/bar\0") == None);
499 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
503 fn test_null_byte() {
505 let result = Thread::spawn(move|| {
506 Path::new(b"foo/bar\0")
508 assert!(result.is_err());
510 let result = Thread::spawn(move|| {
511 Path::new("test").set_filename(b"f\0o")
513 assert!(result.is_err());
515 let result = Thread::spawn(move|| {
516 Path::new("test").push(b"f\0o");
518 assert!(result.is_err());
522 fn test_display_str() {
524 ($path:expr, $disp:ident, $exp:expr) => (
526 let path = Path::new($path);
527 assert!(path.$disp().to_string() == $exp);
531 t!("foo", display, "foo");
532 t!(b"foo\x80", display, "foo\u{FFFD}");
533 t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
534 t!(b"foo\xFF/bar", filename_display, "bar");
535 t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
536 t!(b"/", filename_display, "");
539 ($path:expr, $exp:expr) => (
541 let path = Path::new($path);
542 let mo = path.display().as_cow();
543 assert!(mo.as_slice() == $exp);
546 ($path:expr, $exp:expr, filename) => (
548 let path = Path::new($path);
549 let mo = path.filename_display().as_cow();
550 assert!(mo.as_slice() == $exp);
556 t!(b"foo\x80", "foo\u{FFFD}");
557 t!(b"foo\xFFbar", "foo\u{FFFD}bar");
558 t!(b"foo\xFF/bar", "bar", filename);
559 t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
560 t!(b"/", "", filename);
566 ($path:expr, $exp:expr, $expf:expr) => (
568 let path = Path::new($path);
569 let f = format!("{}", path.display());
571 let f = format!("{}", path.filename_display());
577 t!(b"foo", "foo", "foo");
578 t!(b"foo/bar", "foo/bar", "bar");
580 t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
581 t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
582 t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
583 t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
587 fn test_components() {
589 (s: $path:expr, $op:ident, $exp:expr) => (
591 let path = Path::new($path);
592 assert!(path.$op() == ($exp).as_bytes());
595 (s: $path:expr, $op:ident, $exp:expr, opt) => (
597 let path = Path::new($path);
598 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
599 assert!(left == $exp);
602 (v: $path:expr, $op:ident, $exp:expr) => (
605 let path = Path::new(arg);
606 assert!(path.$op() == $exp);
611 t!(v: b"a/b/c", filename, Some(b"c"));
612 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
613 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
614 t!(s: "a/b/c", filename, Some("c"), opt);
615 t!(s: "/a/b/c", filename, Some("c"), opt);
616 t!(s: "a", filename, Some("a"), opt);
617 t!(s: "/a", filename, Some("a"), opt);
618 t!(s: ".", filename, None, opt);
619 t!(s: "/", filename, None, opt);
620 t!(s: "..", filename, None, opt);
621 t!(s: "../..", filename, None, opt);
623 t!(v: b"a/b/c", dirname, b"a/b");
624 t!(v: b"a/b/c\xFF", dirname, b"a/b");
625 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
626 t!(s: "a/b/c", dirname, "a/b");
627 t!(s: "/a/b/c", dirname, "/a/b");
628 t!(s: "a", dirname, ".");
629 t!(s: "/a", dirname, "/");
630 t!(s: ".", dirname, ".");
631 t!(s: "/", dirname, "/");
632 t!(s: "..", dirname, "..");
633 t!(s: "../..", dirname, "../..");
635 t!(v: b"hi/there.txt", filestem, Some(b"there"));
636 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
637 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
638 t!(s: "hi/there.txt", filestem, Some("there"), opt);
639 t!(s: "hi/there", filestem, Some("there"), opt);
640 t!(s: "there.txt", filestem, Some("there"), opt);
641 t!(s: "there", filestem, Some("there"), opt);
642 t!(s: ".", filestem, None, opt);
643 t!(s: "/", filestem, None, opt);
644 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
645 t!(s: ".bar", filestem, Some(".bar"), opt);
646 t!(s: "..bar", filestem, Some("."), opt);
647 t!(s: "hi/there..txt", filestem, Some("there."), opt);
648 t!(s: "..", filestem, None, opt);
649 t!(s: "../..", filestem, None, opt);
651 t!(v: b"hi/there.txt", extension, Some(b"txt"));
652 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
653 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
654 t!(v: b"hi/there", extension, None);
655 t!(v: b"hi/there\x80", extension, None);
656 t!(s: "hi/there.txt", extension, Some("txt"), opt);
657 t!(s: "hi/there", extension, None, opt);
658 t!(s: "there.txt", extension, Some("txt"), opt);
659 t!(s: "there", extension, None, opt);
660 t!(s: ".", extension, None, opt);
661 t!(s: "/", extension, None, opt);
662 t!(s: "foo/.bar", extension, None, opt);
663 t!(s: ".bar", extension, None, opt);
664 t!(s: "..bar", extension, Some("bar"), opt);
665 t!(s: "hi/there..txt", extension, Some("txt"), opt);
666 t!(s: "..", extension, None, opt);
667 t!(s: "../..", extension, None, opt);
673 (s: $path:expr, $join:expr) => (
677 let mut p1 = Path::new(path);
680 assert!(p1 == p2.join(join));
685 t!(s: "a/b/c", "..");
686 t!(s: "/a/b/c", "d");
688 t!(s: "a/b", "/c/d");
692 fn test_push_path() {
694 (s: $path:expr, $push:expr, $exp:expr) => (
696 let mut p = Path::new($path);
697 let push = Path::new($push);
699 assert!(p.as_str() == Some($exp));
704 t!(s: "a/b/c", "d", "a/b/c/d");
705 t!(s: "/a/b/c", "d", "/a/b/c/d");
706 t!(s: "a/b", "c/d", "a/b/c/d");
707 t!(s: "a/b", "/c/d", "/c/d");
708 t!(s: "a/b", ".", "a/b");
709 t!(s: "a/b", "../c", "a/c");
713 fn test_push_many() {
715 (s: $path:expr, $push:expr, $exp:expr) => (
717 let mut p = Path::new($path);
719 assert!(p.as_str() == Some($exp));
722 (v: $path:expr, $push:expr, $exp:expr) => (
724 let mut p = Path::new($path);
726 assert!(p.as_vec() == $exp);
731 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
732 t!(s: "a/b/c", ["d", "/e"], "/e");
733 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
734 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
735 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
736 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
737 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
743 (s: $path:expr, $left:expr, $right:expr) => (
745 let mut p = Path::new($path);
746 let result = p.pop();
747 assert!(p.as_str() == Some($left));
748 assert!(result == $right);
751 (b: $path:expr, $left:expr, $right:expr) => (
753 let mut p = Path::new($path);
754 let result = p.pop();
755 assert!(p.as_vec() == $left);
756 assert!(result == $right);
761 t!(b: b"a/b/c", b"a/b", true);
762 t!(b: b"a", b".", true);
763 t!(b: b".", b".", false);
764 t!(b: b"/a", b"/", true);
765 t!(b: b"/", b"/", false);
766 t!(b: b"a/b/c\x80", b"a/b", true);
767 t!(b: b"a/b\x80/c", b"a/b\x80", true);
768 t!(b: b"\xFF", b".", true);
769 t!(b: b"/\xFF", b"/", true);
770 t!(s: "a/b/c", "a/b", true);
771 t!(s: "a", ".", true);
772 t!(s: ".", ".", false);
773 t!(s: "/a", "/", true);
774 t!(s: "/", "/", false);
778 fn test_root_path() {
779 assert!(Path::new(b"a/b/c").root_path() == None);
780 assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
785 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
786 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
787 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
788 t!(s: Path::new("a/b/c").join(".."), "a/b");
789 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
790 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
791 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
792 t!(s: Path::new(".").join("a/b"), "a/b");
793 t!(s: Path::new("/").join("a/b"), "/a/b");
797 fn test_join_path() {
799 (s: $path:expr, $join:expr, $exp:expr) => (
801 let path = Path::new($path);
802 let join = Path::new($join);
803 let res = path.join(&join);
804 assert!(res.as_str() == Some($exp));
809 t!(s: "a/b/c", "..", "a/b");
810 t!(s: "/a/b/c", "d", "/a/b/c/d");
811 t!(s: "a/b", "c/d", "a/b/c/d");
812 t!(s: "a/b", "/c/d", "/c/d");
813 t!(s: ".", "a/b", "a/b");
814 t!(s: "/", "a/b", "/a/b");
818 fn test_join_many() {
820 (s: $path:expr, $join:expr, $exp:expr) => (
822 let path = Path::new($path);
823 let res = path.join_many(&$join);
824 assert!(res.as_str() == Some($exp));
827 (v: $path:expr, $join:expr, $exp:expr) => (
829 let path = Path::new($path);
830 let res = path.join_many(&$join);
831 assert!(res.as_vec() == $exp);
836 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
837 t!(s: "a/b/c", ["..", "d"], "a/b/d");
838 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
839 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
840 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
841 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
845 fn test_with_helpers() {
846 let empty: &[u8] = &[];
848 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
849 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
850 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
852 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
853 t!(s: Path::new(".").with_filename("foo"), "foo");
854 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
855 t!(s: Path::new("/").with_filename("foo"), "/foo");
856 t!(s: Path::new("/a").with_filename("foo"), "/foo");
857 t!(s: Path::new("foo").with_filename("bar"), "bar");
858 t!(s: Path::new("/").with_filename("foo/"), "/foo");
859 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
860 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
861 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
862 t!(s: Path::new("a/b/c").with_filename(".."), "a");
863 t!(s: Path::new("/a").with_filename(""), "/");
864 t!(s: Path::new("foo").with_filename(""), ".");
865 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
866 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
867 t!(s: Path::new("..").with_filename("foo"), "../foo");
868 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
869 t!(s: Path::new("..").with_filename(""), "..");
870 t!(s: Path::new("../..").with_filename(""), "../..");
872 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
873 b"hi/there\x80.exe");
874 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
876 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
877 b"hi/there\x80.\xFF");
878 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
879 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
880 t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
881 t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
882 t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
883 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
884 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
885 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
886 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
887 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
888 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
889 t!(s: Path::new("/").with_extension("txt"), "/");
890 t!(s: Path::new("/").with_extension("."), "/");
891 t!(s: Path::new("/").with_extension(".."), "/");
892 t!(s: Path::new(".").with_extension("txt"), ".");
898 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
902 let mut p1 = Path::new(path);
904 let p2 = Path::new(path);
905 assert!(p1 == p2.$with(arg));
908 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
912 let mut p1 = Path::new(path);
914 let p2 = Path::new(path);
915 assert!(p1 == p2.$with(arg));
920 t!(v: b"a/b/c", set_filename, with_filename, b"d");
921 t!(v: b"/", set_filename, with_filename, b"foo");
922 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
923 t!(s: "a/b/c", set_filename, with_filename, "d");
924 t!(s: "/", set_filename, with_filename, "foo");
925 t!(s: ".", set_filename, with_filename, "foo");
926 t!(s: "a/b", set_filename, with_filename, "");
927 t!(s: "a", set_filename, with_filename, "");
929 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
930 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
931 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
932 t!(s: "hi/there.", set_extension, with_extension, "txt");
933 t!(s: "hi/there", set_extension, with_extension, "txt");
934 t!(s: "hi/there.txt", set_extension, with_extension, "");
935 t!(s: "hi/there", set_extension, with_extension, "");
936 t!(s: ".", set_extension, with_extension, "txt");
942 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
945 let filename = $filename;
946 assert!(path.filename_str() == filename,
947 "{}.filename_str(): Expected `{}`, found {}",
948 path.as_str().unwrap(), filename, path.filename_str());
949 let dirname = $dirname;
950 assert!(path.dirname_str() == dirname,
951 "`{}`.dirname_str(): Expected `{}`, found `{}`",
952 path.as_str().unwrap(), dirname, path.dirname_str());
953 let filestem = $filestem;
954 assert!(path.filestem_str() == filestem,
955 "`{}`.filestem_str(): Expected `{}`, found `{}`",
956 path.as_str().unwrap(), filestem, path.filestem_str());
958 assert!(path.extension_str() == ext,
959 "`{}`.extension_str(): Expected `{}`, found `{}`",
960 path.as_str().unwrap(), ext, path.extension_str());
963 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
966 assert!(path.filename() == $filename);
967 assert!(path.dirname() == $dirname);
968 assert!(path.filestem() == $filestem);
969 assert!(path.extension() == $ext);
974 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
975 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
976 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
977 Some(b"there"), Some(b"\xFF"));
978 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
979 t!(s: Path::new("."), None, Some("."), None, None);
980 t!(s: Path::new("/"), None, Some("/"), None, None);
981 t!(s: Path::new(".."), None, Some(".."), None, None);
982 t!(s: Path::new("../.."), None, Some("../.."), None, None);
983 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
984 Some("there"), Some("txt"));
985 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
986 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
987 Some("there"), Some(""));
988 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
989 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
990 Some("."), Some("there"));
991 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
992 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
993 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
994 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
999 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
1000 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
1001 t!(s: Path::new("hi/there").dir_path(), "hi");
1002 t!(s: Path::new("hi").dir_path(), ".");
1003 t!(s: Path::new("/hi").dir_path(), "/");
1004 t!(s: Path::new("/").dir_path(), "/");
1005 t!(s: Path::new("..").dir_path(), "..");
1006 t!(s: Path::new("../..").dir_path(), "../..");
1010 fn test_is_absolute() {
1012 (s: $path:expr, $abs:expr, $rel:expr) => (
1014 let path = Path::new($path);
1015 assert_eq!(path.is_absolute(), $abs);
1016 assert_eq!(path.is_relative(), $rel);
1020 t!(s: "a/b/c", false, true);
1021 t!(s: "/a/b/c", true, false);
1022 t!(s: "a", false, true);
1023 t!(s: "/a", true, false);
1024 t!(s: ".", false, true);
1025 t!(s: "/", true, false);
1026 t!(s: "..", false, true);
1027 t!(s: "../..", false, true);
1031 fn test_is_ancestor_of() {
1033 (s: $path:expr, $dest:expr, $exp:expr) => (
1035 let path = Path::new($path);
1036 let dest = Path::new($dest);
1037 assert_eq!(path.is_ancestor_of(&dest), $exp);
1042 t!(s: "a/b/c", "a/b/c/d", true);
1043 t!(s: "a/b/c", "a/b/c", true);
1044 t!(s: "a/b/c", "a/b", false);
1045 t!(s: "/a/b/c", "/a/b/c", true);
1046 t!(s: "/a/b", "/a/b/c", true);
1047 t!(s: "/a/b/c/d", "/a/b/c", false);
1048 t!(s: "/a/b", "a/b/c", false);
1049 t!(s: "a/b", "/a/b/c", false);
1050 t!(s: "a/b/c", "a/b/d", false);
1051 t!(s: "../a/b/c", "a/b/c", false);
1052 t!(s: "a/b/c", "../a/b/c", false);
1053 t!(s: "a/b/c", "a/b/cd", false);
1054 t!(s: "a/b/cd", "a/b/c", false);
1055 t!(s: "../a/b", "../a/b/c", true);
1056 t!(s: ".", "a/b", true);
1057 t!(s: ".", ".", true);
1058 t!(s: "/", "/", true);
1059 t!(s: "/", "/a/b", true);
1060 t!(s: "..", "a/b", true);
1061 t!(s: "../..", "a/b", true);
1065 fn test_ends_with_path() {
1067 (s: $path:expr, $child:expr, $exp:expr) => (
1069 let path = Path::new($path);
1070 let child = Path::new($child);
1071 assert_eq!(path.ends_with_path(&child), $exp);
1074 (v: $path:expr, $child:expr, $exp:expr) => (
1076 let path = Path::new($path);
1077 let child = Path::new($child);
1078 assert_eq!(path.ends_with_path(&child), $exp);
1083 t!(s: "a/b/c", "c", true);
1084 t!(s: "a/b/c", "d", false);
1085 t!(s: "foo/bar/quux", "bar", false);
1086 t!(s: "foo/bar/quux", "barquux", false);
1087 t!(s: "a/b/c", "b/c", true);
1088 t!(s: "a/b/c", "a/b/c", true);
1089 t!(s: "a/b/c", "foo/a/b/c", false);
1090 t!(s: "/a/b/c", "a/b/c", true);
1091 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1092 t!(s: "/a/b/c", "foo/a/b/c", false);
1093 t!(s: "a/b/c", "", false);
1094 t!(s: "", "", true);
1095 t!(s: "/a/b/c", "d/e/f", false);
1096 t!(s: "a/b/c", "a/b", false);
1097 t!(s: "a/b/c", "b", false);
1098 t!(v: b"a/b/c", b"b/c", true);
1099 t!(v: b"a/b/\xFF", b"\xFF", true);
1100 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1104 fn test_path_relative_from() {
1106 (s: $path:expr, $other:expr, $exp:expr) => (
1108 let path = Path::new($path);
1109 let other = Path::new($other);
1110 let res = path.path_relative_from(&other);
1111 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1116 t!(s: "a/b/c", "a/b", Some("c"));
1117 t!(s: "a/b/c", "a/b/d", Some("../c"));
1118 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1119 t!(s: "a/b/c", "a/b/c", Some("."));
1120 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1121 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1122 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1123 t!(s: "a/b/c", "/a/b/c", None);
1124 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1125 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1126 t!(s: "/a/b/c", "/a/b", Some("c"));
1127 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1128 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1129 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1130 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1131 t!(s: ".", "a", Some(".."));
1132 t!(s: ".", "a/b", Some("../.."));
1133 t!(s: ".", ".", Some("."));
1134 t!(s: "a", ".", Some("a"));
1135 t!(s: "a/b", ".", Some("a/b"));
1136 t!(s: "..", ".", Some(".."));
1137 t!(s: "a/b/c", "a/b/c", Some("."));
1138 t!(s: "/a/b/c", "/a/b/c", Some("."));
1139 t!(s: "/", "/", Some("."));
1140 t!(s: "/", ".", Some("/"));
1141 t!(s: "../../a", "b", Some("../../../a"));
1142 t!(s: "a", "../../b", None);
1143 t!(s: "../../a", "../../b", Some("../a"));
1144 t!(s: "../../a", "../../a/b", Some(".."));
1145 t!(s: "../../a/b", "../../a", Some("b"));
1149 fn test_components_iter() {
1151 (s: $path:expr, $exp:expr) => (
1153 let path = Path::new($path);
1154 let comps = path.components().collect::<Vec<&[u8]>>();
1155 let exp: &[&str] = &$exp;
1156 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1157 assert!(comps == exps, "components: Expected {}, found {}",
1159 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1160 let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1161 assert!(comps == exps, "rev_components: Expected {}, found {}",
1165 (b: $arg:expr, [$($exp:expr),*]) => (
1167 let path = Path::new($arg);
1168 let comps = path.components().collect::<Vec<&[u8]>>();
1169 let exp: &[&[u8]] = &[$($exp),*];
1170 assert_eq!(comps, exp);
1171 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1172 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1173 assert_eq!(comps, exp)
1178 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1179 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1180 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1181 t!(s: "a/b/c", ["a", "b", "c"]);
1182 t!(s: "a/b/d", ["a", "b", "d"]);
1183 t!(s: "a/b/cd", ["a", "b", "cd"]);
1184 t!(s: "/a/b/c", ["a", "b", "c"]);
1189 t!(s: "..", [".."]);
1190 t!(s: "../..", ["..", ".."]);
1191 t!(s: "../../foo", ["..", "..", "foo"]);
1195 fn test_str_components() {
1197 (b: $arg:expr, $exp:expr) => (
1199 let path = Path::new($arg);
1200 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1201 let exp: &[Option<&str>] = &$exp;
1202 assert_eq!(comps, exp);
1203 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1204 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1205 assert_eq!(comps, exp);
1210 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1211 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1212 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1213 // str_components is a wrapper around components, so no need to do
1214 // the full set of tests
1221 use self::test::Bencher;
1223 use prelude::v1::{Clone, GenericPath};
1226 fn join_home_dir(b: &mut Bencher) {
1227 let posix_path = Path::new("/");
1229 posix_path.join("home");
1234 fn join_abs_path_home_dir(b: &mut Bencher) {
1235 let posix_path = Path::new("/");
1237 posix_path.join("/home");
1242 fn join_many_home_dir(b: &mut Bencher) {
1243 let posix_path = Path::new("/");
1245 posix_path.join_many(&["home"]);
1250 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1251 let posix_path = Path::new("/");
1253 posix_path.join_many(&["/home"]);
1258 fn push_home_dir(b: &mut Bencher) {
1259 let mut posix_path = Path::new("/");
1261 posix_path.push("home");
1266 fn push_abs_path_home_dir(b: &mut Bencher) {
1267 let mut posix_path = Path::new("/");
1269 posix_path.push("/home");
1274 fn push_many_home_dir(b: &mut Bencher) {
1275 let mut posix_path = Path::new("/");
1277 posix_path.push_many(&["home"]);
1282 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1283 let mut posix_path = Path::new("/");
1285 posix_path.push_many(&["/home"]);
1290 fn ends_with_path_home_dir(b: &mut Bencher) {
1291 let posix_home_path = Path::new("/home");
1293 posix_home_path.ends_with_path(&Path::new("home"));
1298 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1299 let posix_home_path = Path::new("/home");
1301 posix_home_path.ends_with_path(&Path::new("jome"));
1306 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1307 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1308 let mut sub = path.clone();
1311 path.is_ancestor_of(&sub);
1316 fn path_relative_from_forward(b: &mut Bencher) {
1317 let path = Path::new("/a/b/c");
1318 let mut other = path.clone();
1321 path.path_relative_from(&other);
1326 fn path_relative_from_same_level(b: &mut Bencher) {
1327 let path = Path::new("/a/b/c");
1328 let mut other = path.clone();
1332 path.path_relative_from(&other);
1337 fn path_relative_from_backward(b: &mut Bencher) {
1338 let path = Path::new("/a/b");
1339 let mut other = path.clone();
1342 path.path_relative_from(&other);