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
13 use c_str::{CString, ToCStr};
15 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
18 use iter::{AdditiveIterator, Extend};
19 use iter::{Iterator, IteratorExt, Map};
21 use option::Option::{None, Some};
23 use str::{FromStr, Str};
25 use slice::{Split, AsSlice, SliceConcatExt, SliceExt};
28 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
30 /// Iterator that yields successive components of a Path as &[u8]
31 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
33 /// Iterator that yields successive components of a Path as Option<&str>
34 pub type StrComponents<'a> =
35 Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
37 /// Represents a POSIX file path
40 repr: Vec<u8>, // assumed to never be empty or contain NULs
41 sepidx: Option<uint> // index of the final separator in repr
44 /// The standard path separator character
45 pub const SEP: char = '/';
47 /// The standard path separator byte
48 pub const SEP_BYTE: u8 = SEP as u8;
50 /// Returns whether the given byte is a path separator
52 pub fn is_sep_byte(u: &u8) -> bool {
56 /// Returns whether the given char is a path separator
58 pub fn is_sep(c: char) -> bool {
62 impl PartialEq for Path {
64 fn eq(&self, other: &Path) -> bool {
65 self.repr == other.repr
71 impl PartialOrd for Path {
72 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
78 fn cmp(&self, other: &Path) -> Ordering {
79 self.repr.cmp(&other.repr)
83 impl FromStr for Path {
84 fn from_str(s: &str) -> Option<Path> {
89 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
90 // we cannot usefully take ToCStr arguments by reference (without forcing an
91 // additional & around &str). So we are instead temporarily adding an instance
92 // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
93 // instance should be removed, and arguments bound by ToCStr should be passed by
96 impl ToCStr for Path {
98 fn to_c_str(&self) -> CString {
99 // The Path impl guarantees no internal NUL
100 unsafe { self.to_c_str_unchecked() }
104 unsafe fn to_c_str_unchecked(&self) -> CString {
105 self.as_vec().to_c_str_unchecked()
109 impl<S: hash::Writer> hash::Hash<S> for Path {
111 fn hash(&self, state: &mut S) {
112 self.repr.hash(state)
116 impl BytesContainer for Path {
118 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
123 impl GenericPathUnsafe for Path {
124 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
125 let path = Path::normalize(path.container_as_bytes());
126 assert!(!path.is_empty());
127 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
128 Path{ repr: path, sepidx: idx }
131 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
132 let filename = filename.container_as_bytes();
134 None if b".." == self.repr => {
135 let mut v = Vec::with_capacity(3 + filename.len());
136 v.push_all(dot_dot_static);
138 v.push_all(filename);
139 // FIXME: this is slow
140 self.repr = Path::normalize(v.as_slice());
143 self.repr = Path::normalize(filename);
145 Some(idx) if self.repr[idx+1..] == b".." => {
146 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
147 v.push_all(self.repr.as_slice());
149 v.push_all(filename);
150 // FIXME: this is slow
151 self.repr = Path::normalize(v.as_slice());
154 let mut v = Vec::with_capacity(idx + 1 + filename.len());
155 v.push_all(self.repr[..idx+1]);
156 v.push_all(filename);
157 // FIXME: this is slow
158 self.repr = Path::normalize(v.as_slice());
161 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
164 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
165 let path = path.container_as_bytes();
166 if !path.is_empty() {
167 if path[0] == SEP_BYTE {
168 self.repr = Path::normalize(path);
170 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
171 v.push_all(self.repr.as_slice());
174 // FIXME: this is slow
175 self.repr = Path::normalize(v.as_slice());
177 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
182 impl GenericPath for Path {
184 fn as_vec<'a>(&'a self) -> &'a [u8] {
188 fn into_vec(self) -> Vec<u8> {
192 fn dirname<'a>(&'a self) -> &'a [u8] {
194 None if b".." == self.repr => self.repr.as_slice(),
196 Some(0) => self.repr[..1],
197 Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(),
198 Some(idx) => self.repr[..idx]
202 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
204 None if b"." == self.repr ||
205 b".." == self.repr => None,
206 None => Some(self.repr.as_slice()),
207 Some(idx) if self.repr[idx+1..] == b".." => None,
208 Some(0) if self.repr[1..].is_empty() => None,
209 Some(idx) => Some(self.repr[idx+1..])
213 fn pop(&mut self) -> bool {
215 None if b"." == self.repr => false,
217 self.repr = vec![b'.'];
221 Some(0) if b"/" == self.repr => false,
224 self.repr.truncate(idx+1);
226 self.repr.truncate(idx);
228 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
234 fn root_path(&self) -> Option<Path> {
235 if self.is_absolute() {
243 fn is_absolute(&self) -> bool {
244 self.repr[0] == SEP_BYTE
247 fn is_ancestor_of(&self, other: &Path) -> bool {
248 if self.is_absolute() != other.is_absolute() {
251 let mut ita = self.components();
252 let mut itb = other.components();
253 if b"." == self.repr {
254 return match itb.next() {
256 Some(b) => b != b".."
260 match (ita.next(), itb.next()) {
262 (Some(a), Some(b)) if a == b => { continue },
263 (Some(a), _) if a == b".." => {
264 // if ita contains only .. components, it's an ancestor
265 return ita.all(|x| x == b"..");
274 fn path_relative_from(&self, base: &Path) -> Option<Path> {
275 if self.is_absolute() != base.is_absolute() {
276 if self.is_absolute() {
282 let mut ita = self.components();
283 let mut itb = base.components();
284 let mut comps = vec![];
286 match (ita.next(), itb.next()) {
287 (None, None) => break,
290 comps.extend(ita.by_ref());
293 (None, _) => comps.push(dot_dot_static),
294 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
295 (Some(a), Some(b)) if b == b"." => comps.push(a),
296 (Some(_), Some(b)) if b == b".." => return None,
297 (Some(a), Some(_)) => {
298 comps.push(dot_dot_static);
300 comps.push(dot_dot_static);
303 comps.extend(ita.by_ref());
308 Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
312 fn ends_with_path(&self, child: &Path) -> bool {
313 if !child.is_relative() { return false; }
314 let mut selfit = self.components().rev();
315 let mut childit = child.components().rev();
317 match (selfit.next(), childit.next()) {
318 (Some(a), Some(b)) => if a != b { return false; },
319 (Some(_), None) => break,
320 (None, Some(_)) => return false,
321 (None, None) => break
329 /// Returns a new Path from a byte vector or string
333 /// Panics the task if the vector contains a NUL.
335 pub fn new<T: BytesContainer>(path: T) -> Path {
336 GenericPath::new(path)
339 /// Returns a new Path from a byte vector or string, if possible
341 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
342 GenericPath::new_opt(path)
345 /// Returns a normalized byte vector representation of a path, by removing all empty
346 /// components, and unnecessary . and .. components.
347 fn normalize<Sized? V: AsSlice<u8>>(v: &V) -> Vec<u8> {
348 // borrowck is being very picky
350 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
351 let v_ = if is_abs { v.as_slice()[1..] } else { v.as_slice() };
352 let comps = normalize_helper(v_, is_abs);
356 if is_abs && comps.is_empty() {
359 let n = if is_abs { comps.len() } else { comps.len() - 1} +
360 comps.iter().map(|v| v.len()).sum();
361 let mut v = Vec::with_capacity(n);
362 let mut it = comps.into_iter();
366 Some(comp) => v.push_all(comp)
379 None => v.as_slice().to_vec(),
384 /// Returns an iterator that yields each component of the path in turn.
385 /// Does not distinguish between absolute and relative paths, e.g.
386 /// /a/b/c and a/b/c yield the same set of components.
387 /// A path of "/" yields no components. A path of "." yields one component.
388 pub fn components<'a>(&'a self) -> Components<'a> {
389 let v = if self.repr[0] == SEP_BYTE {
391 } else { self.repr.as_slice() };
392 let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
393 let mut ret = v.split(is_sep_byte);
395 // consume the empty "" component
401 /// Returns an iterator that yields each component of the path as Option<&str>.
402 /// See components() for details.
403 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
404 fn from_utf8(s: &[u8]) -> Option<&str> {
405 str::from_utf8(s).ok()
407 let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
408 self.components().map(f)
412 // None result means the byte vector didn't need normalizing
413 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
414 if is_abs && v.is_empty() {
417 let mut comps: Vec<&'a [u8]> = vec![];
419 let mut changed = false;
420 for comp in v.split(is_sep_byte) {
421 if comp.is_empty() { changed = true }
422 else if comp == b"." { changed = true }
423 else if comp == b".." {
424 if is_abs && comps.is_empty() { changed = true }
425 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
426 else { comps.pop().unwrap(); changed = true }
427 } else { comps.push(comp) }
430 if comps.is_empty() && !is_abs {
434 comps.push(dot_static);
442 #[allow(non_upper_case_globals)]
443 static dot_static: &'static [u8] = b".";
444 #[allow(non_upper_case_globals)]
445 static dot_dot_static: &'static [u8] = b"..";
452 use iter::IteratorExt;
453 use option::Option::{self, Some, None};
454 use path::GenericPath;
455 use slice::{AsSlice, SliceExt};
456 use str::{self, Str, StrExt};
457 use string::ToString;
461 (s: $path:expr, $exp:expr) => (
464 assert!(path.as_str() == Some($exp));
467 (v: $path:expr, $exp:expr) => (
470 assert!(path.as_vec() == $exp);
477 let empty: &[u8] = &[];
478 t!(v: Path::new(empty), b".");
479 t!(v: Path::new(b"/"), b"/");
480 t!(v: Path::new(b"a/b/c"), b"a/b/c");
481 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
482 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
483 let p = Path::new(b"a/b/c\xFF");
484 assert!(p.as_str() == None);
486 t!(s: Path::new(""), ".");
487 t!(s: Path::new("/"), "/");
488 t!(s: Path::new("hi"), "hi");
489 t!(s: Path::new("hi/"), "hi");
490 t!(s: Path::new("/lib"), "/lib");
491 t!(s: Path::new("/lib/"), "/lib");
492 t!(s: Path::new("hi/there"), "hi/there");
493 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
495 t!(s: Path::new("hi/there/"), "hi/there");
496 t!(s: Path::new("hi/../there"), "there");
497 t!(s: Path::new("../hi/there"), "../hi/there");
498 t!(s: Path::new("/../hi/there"), "/hi/there");
499 t!(s: Path::new("foo/.."), ".");
500 t!(s: Path::new("/foo/.."), "/");
501 t!(s: Path::new("/foo/../.."), "/");
502 t!(s: Path::new("/foo/../../bar"), "/bar");
503 t!(s: Path::new("/./hi/./there/."), "/hi/there");
504 t!(s: Path::new("/./hi/./there/./.."), "/hi");
505 t!(s: Path::new("foo/../.."), "..");
506 t!(s: Path::new("foo/../../.."), "../..");
507 t!(s: Path::new("foo/../../bar"), "../bar");
509 assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
510 assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
513 let p = Path::new(b"foo/bar\x80");
514 assert!(p.as_str() == None);
518 fn test_opt_paths() {
519 assert!(Path::new_opt(b"foo/bar\0") == None);
520 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
521 assert!(Path::new_opt("foo/bar\0") == None);
522 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
526 fn test_null_byte() {
528 let result = Thread::spawn(move|| {
529 Path::new(b"foo/bar\0")
531 assert!(result.is_err());
533 let result = Thread::spawn(move|| {
534 Path::new("test").set_filename(b"f\0o")
536 assert!(result.is_err());
538 let result = Thread::spawn(move|| {
539 Path::new("test").push(b"f\0o");
541 assert!(result.is_err());
545 fn test_display_str() {
547 ($path:expr, $disp:ident, $exp:expr) => (
549 let path = Path::new($path);
550 assert!(path.$disp().to_string() == $exp);
554 t!("foo", display, "foo");
555 t!(b"foo\x80", display, "foo\u{FFFD}");
556 t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
557 t!(b"foo\xFF/bar", filename_display, "bar");
558 t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
559 t!(b"/", filename_display, "");
562 ($path:expr, $exp:expr) => (
564 let path = Path::new($path);
565 let mo = path.display().as_cow();
566 assert!(mo.as_slice() == $exp);
569 ($path:expr, $exp:expr, filename) => (
571 let path = Path::new($path);
572 let mo = path.filename_display().as_cow();
573 assert!(mo.as_slice() == $exp);
579 t!(b"foo\x80", "foo\u{FFFD}");
580 t!(b"foo\xFFbar", "foo\u{FFFD}bar");
581 t!(b"foo\xFF/bar", "bar", filename);
582 t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
583 t!(b"/", "", filename);
589 ($path:expr, $exp:expr, $expf:expr) => (
591 let path = Path::new($path);
592 let f = format!("{}", path.display());
594 let f = format!("{}", path.filename_display());
600 t!(b"foo", "foo", "foo");
601 t!(b"foo/bar", "foo/bar", "bar");
603 t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
604 t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
605 t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
606 t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
610 fn test_components() {
612 (s: $path:expr, $op:ident, $exp:expr) => (
614 let path = Path::new($path);
615 assert!(path.$op() == ($exp).as_bytes());
618 (s: $path:expr, $op:ident, $exp:expr, opt) => (
620 let path = Path::new($path);
621 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
622 assert!(left == $exp);
625 (v: $path:expr, $op:ident, $exp:expr) => (
628 let path = Path::new(arg);
629 assert!(path.$op() == $exp);
634 t!(v: b"a/b/c", filename, Some(b"c"));
635 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
636 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
637 t!(s: "a/b/c", filename, Some("c"), opt);
638 t!(s: "/a/b/c", filename, Some("c"), opt);
639 t!(s: "a", filename, Some("a"), opt);
640 t!(s: "/a", filename, Some("a"), opt);
641 t!(s: ".", filename, None, opt);
642 t!(s: "/", filename, None, opt);
643 t!(s: "..", filename, None, opt);
644 t!(s: "../..", filename, None, opt);
646 t!(v: b"a/b/c", dirname, b"a/b");
647 t!(v: b"a/b/c\xFF", dirname, b"a/b");
648 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
649 t!(s: "a/b/c", dirname, "a/b");
650 t!(s: "/a/b/c", dirname, "/a/b");
651 t!(s: "a", dirname, ".");
652 t!(s: "/a", dirname, "/");
653 t!(s: ".", dirname, ".");
654 t!(s: "/", dirname, "/");
655 t!(s: "..", dirname, "..");
656 t!(s: "../..", dirname, "../..");
658 t!(v: b"hi/there.txt", filestem, Some(b"there"));
659 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
660 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
661 t!(s: "hi/there.txt", filestem, Some("there"), opt);
662 t!(s: "hi/there", filestem, Some("there"), opt);
663 t!(s: "there.txt", filestem, Some("there"), opt);
664 t!(s: "there", filestem, Some("there"), opt);
665 t!(s: ".", filestem, None, opt);
666 t!(s: "/", filestem, None, opt);
667 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
668 t!(s: ".bar", filestem, Some(".bar"), opt);
669 t!(s: "..bar", filestem, Some("."), opt);
670 t!(s: "hi/there..txt", filestem, Some("there."), opt);
671 t!(s: "..", filestem, None, opt);
672 t!(s: "../..", filestem, None, opt);
674 t!(v: b"hi/there.txt", extension, Some(b"txt"));
675 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
676 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
677 t!(v: b"hi/there", extension, None);
678 t!(v: b"hi/there\x80", extension, None);
679 t!(s: "hi/there.txt", extension, Some("txt"), opt);
680 t!(s: "hi/there", extension, None, opt);
681 t!(s: "there.txt", extension, Some("txt"), opt);
682 t!(s: "there", extension, None, opt);
683 t!(s: ".", extension, None, opt);
684 t!(s: "/", extension, None, opt);
685 t!(s: "foo/.bar", extension, None, opt);
686 t!(s: ".bar", extension, None, opt);
687 t!(s: "..bar", extension, Some("bar"), opt);
688 t!(s: "hi/there..txt", extension, Some("txt"), opt);
689 t!(s: "..", extension, None, opt);
690 t!(s: "../..", extension, None, opt);
696 (s: $path:expr, $join:expr) => (
700 let mut p1 = Path::new(path);
703 assert!(p1 == p2.join(join));
708 t!(s: "a/b/c", "..");
709 t!(s: "/a/b/c", "d");
711 t!(s: "a/b", "/c/d");
715 fn test_push_path() {
717 (s: $path:expr, $push:expr, $exp:expr) => (
719 let mut p = Path::new($path);
720 let push = Path::new($push);
722 assert!(p.as_str() == Some($exp));
727 t!(s: "a/b/c", "d", "a/b/c/d");
728 t!(s: "/a/b/c", "d", "/a/b/c/d");
729 t!(s: "a/b", "c/d", "a/b/c/d");
730 t!(s: "a/b", "/c/d", "/c/d");
731 t!(s: "a/b", ".", "a/b");
732 t!(s: "a/b", "../c", "a/c");
736 fn test_push_many() {
738 (s: $path:expr, $push:expr, $exp:expr) => (
740 let mut p = Path::new($path);
742 assert!(p.as_str() == Some($exp));
745 (v: $path:expr, $push:expr, $exp:expr) => (
747 let mut p = Path::new($path);
749 assert!(p.as_vec() == $exp);
754 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
755 t!(s: "a/b/c", ["d", "/e"], "/e");
756 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
757 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
758 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
759 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
760 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
766 (s: $path:expr, $left:expr, $right:expr) => (
768 let mut p = Path::new($path);
769 let result = p.pop();
770 assert!(p.as_str() == Some($left));
771 assert!(result == $right);
774 (b: $path:expr, $left:expr, $right:expr) => (
776 let mut p = Path::new($path);
777 let result = p.pop();
778 assert!(p.as_vec() == $left);
779 assert!(result == $right);
784 t!(b: b"a/b/c", b"a/b", true);
785 t!(b: b"a", b".", true);
786 t!(b: b".", b".", false);
787 t!(b: b"/a", b"/", true);
788 t!(b: b"/", b"/", false);
789 t!(b: b"a/b/c\x80", b"a/b", true);
790 t!(b: b"a/b\x80/c", b"a/b\x80", true);
791 t!(b: b"\xFF", b".", true);
792 t!(b: b"/\xFF", b"/", true);
793 t!(s: "a/b/c", "a/b", true);
794 t!(s: "a", ".", true);
795 t!(s: ".", ".", false);
796 t!(s: "/a", "/", true);
797 t!(s: "/", "/", false);
801 fn test_root_path() {
802 assert!(Path::new(b"a/b/c").root_path() == None);
803 assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
808 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
809 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
810 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
811 t!(s: Path::new("a/b/c").join(".."), "a/b");
812 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
813 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
814 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
815 t!(s: Path::new(".").join("a/b"), "a/b");
816 t!(s: Path::new("/").join("a/b"), "/a/b");
820 fn test_join_path() {
822 (s: $path:expr, $join:expr, $exp:expr) => (
824 let path = Path::new($path);
825 let join = Path::new($join);
826 let res = path.join(&join);
827 assert!(res.as_str() == Some($exp));
832 t!(s: "a/b/c", "..", "a/b");
833 t!(s: "/a/b/c", "d", "/a/b/c/d");
834 t!(s: "a/b", "c/d", "a/b/c/d");
835 t!(s: "a/b", "/c/d", "/c/d");
836 t!(s: ".", "a/b", "a/b");
837 t!(s: "/", "a/b", "/a/b");
841 fn test_join_many() {
843 (s: $path:expr, $join:expr, $exp:expr) => (
845 let path = Path::new($path);
846 let res = path.join_many(&$join);
847 assert!(res.as_str() == Some($exp));
850 (v: $path:expr, $join:expr, $exp:expr) => (
852 let path = Path::new($path);
853 let res = path.join_many(&$join);
854 assert!(res.as_vec() == $exp);
859 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
860 t!(s: "a/b/c", ["..", "d"], "a/b/d");
861 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
862 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
863 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
864 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
868 fn test_with_helpers() {
869 let empty: &[u8] = &[];
871 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
872 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
873 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
875 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
876 t!(s: Path::new(".").with_filename("foo"), "foo");
877 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
878 t!(s: Path::new("/").with_filename("foo"), "/foo");
879 t!(s: Path::new("/a").with_filename("foo"), "/foo");
880 t!(s: Path::new("foo").with_filename("bar"), "bar");
881 t!(s: Path::new("/").with_filename("foo/"), "/foo");
882 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
883 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
884 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
885 t!(s: Path::new("a/b/c").with_filename(".."), "a");
886 t!(s: Path::new("/a").with_filename(""), "/");
887 t!(s: Path::new("foo").with_filename(""), ".");
888 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
889 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
890 t!(s: Path::new("..").with_filename("foo"), "../foo");
891 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
892 t!(s: Path::new("..").with_filename(""), "..");
893 t!(s: Path::new("../..").with_filename(""), "../..");
895 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
896 b"hi/there\x80.exe");
897 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
899 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
900 b"hi/there\x80.\xFF");
901 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
902 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
903 t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
904 t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
905 t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
906 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
907 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
908 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
909 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
910 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
911 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
912 t!(s: Path::new("/").with_extension("txt"), "/");
913 t!(s: Path::new("/").with_extension("."), "/");
914 t!(s: Path::new("/").with_extension(".."), "/");
915 t!(s: Path::new(".").with_extension("txt"), ".");
921 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
925 let mut p1 = Path::new(path);
927 let p2 = Path::new(path);
928 assert!(p1 == p2.$with(arg));
931 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
935 let mut p1 = Path::new(path);
937 let p2 = Path::new(path);
938 assert!(p1 == p2.$with(arg));
943 t!(v: b"a/b/c", set_filename, with_filename, b"d");
944 t!(v: b"/", set_filename, with_filename, b"foo");
945 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
946 t!(s: "a/b/c", set_filename, with_filename, "d");
947 t!(s: "/", set_filename, with_filename, "foo");
948 t!(s: ".", set_filename, with_filename, "foo");
949 t!(s: "a/b", set_filename, with_filename, "");
950 t!(s: "a", set_filename, with_filename, "");
952 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
953 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
954 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
955 t!(s: "hi/there.", set_extension, with_extension, "txt");
956 t!(s: "hi/there", set_extension, with_extension, "txt");
957 t!(s: "hi/there.txt", set_extension, with_extension, "");
958 t!(s: "hi/there", set_extension, with_extension, "");
959 t!(s: ".", set_extension, with_extension, "txt");
965 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
968 let filename = $filename;
969 assert!(path.filename_str() == filename,
970 "{}.filename_str(): Expected `{}`, found {}",
971 path.as_str().unwrap(), filename, path.filename_str());
972 let dirname = $dirname;
973 assert!(path.dirname_str() == dirname,
974 "`{}`.dirname_str(): Expected `{}`, found `{}`",
975 path.as_str().unwrap(), dirname, path.dirname_str());
976 let filestem = $filestem;
977 assert!(path.filestem_str() == filestem,
978 "`{}`.filestem_str(): Expected `{}`, found `{}`",
979 path.as_str().unwrap(), filestem, path.filestem_str());
981 assert!(path.extension_str() == ext,
982 "`{}`.extension_str(): Expected `{}`, found `{}`",
983 path.as_str().unwrap(), ext, path.extension_str());
986 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
989 assert!(path.filename() == $filename);
990 assert!(path.dirname() == $dirname);
991 assert!(path.filestem() == $filestem);
992 assert!(path.extension() == $ext);
997 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
998 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
999 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
1000 Some(b"there"), Some(b"\xFF"));
1001 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
1002 t!(s: Path::new("."), None, Some("."), None, None);
1003 t!(s: Path::new("/"), None, Some("/"), None, None);
1004 t!(s: Path::new(".."), None, Some(".."), None, None);
1005 t!(s: Path::new("../.."), None, Some("../.."), None, None);
1006 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
1007 Some("there"), Some("txt"));
1008 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
1009 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
1010 Some("there"), Some(""));
1011 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
1012 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
1013 Some("."), Some("there"));
1014 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
1015 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
1016 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
1017 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
1021 fn test_dir_path() {
1022 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
1023 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
1024 t!(s: Path::new("hi/there").dir_path(), "hi");
1025 t!(s: Path::new("hi").dir_path(), ".");
1026 t!(s: Path::new("/hi").dir_path(), "/");
1027 t!(s: Path::new("/").dir_path(), "/");
1028 t!(s: Path::new("..").dir_path(), "..");
1029 t!(s: Path::new("../..").dir_path(), "../..");
1033 fn test_is_absolute() {
1035 (s: $path:expr, $abs:expr, $rel:expr) => (
1037 let path = Path::new($path);
1038 assert_eq!(path.is_absolute(), $abs);
1039 assert_eq!(path.is_relative(), $rel);
1043 t!(s: "a/b/c", false, true);
1044 t!(s: "/a/b/c", true, false);
1045 t!(s: "a", false, true);
1046 t!(s: "/a", true, false);
1047 t!(s: ".", false, true);
1048 t!(s: "/", true, false);
1049 t!(s: "..", false, true);
1050 t!(s: "../..", false, true);
1054 fn test_is_ancestor_of() {
1056 (s: $path:expr, $dest:expr, $exp:expr) => (
1058 let path = Path::new($path);
1059 let dest = Path::new($dest);
1060 assert_eq!(path.is_ancestor_of(&dest), $exp);
1065 t!(s: "a/b/c", "a/b/c/d", true);
1066 t!(s: "a/b/c", "a/b/c", true);
1067 t!(s: "a/b/c", "a/b", false);
1068 t!(s: "/a/b/c", "/a/b/c", true);
1069 t!(s: "/a/b", "/a/b/c", true);
1070 t!(s: "/a/b/c/d", "/a/b/c", false);
1071 t!(s: "/a/b", "a/b/c", false);
1072 t!(s: "a/b", "/a/b/c", false);
1073 t!(s: "a/b/c", "a/b/d", false);
1074 t!(s: "../a/b/c", "a/b/c", false);
1075 t!(s: "a/b/c", "../a/b/c", false);
1076 t!(s: "a/b/c", "a/b/cd", false);
1077 t!(s: "a/b/cd", "a/b/c", false);
1078 t!(s: "../a/b", "../a/b/c", true);
1079 t!(s: ".", "a/b", true);
1080 t!(s: ".", ".", true);
1081 t!(s: "/", "/", true);
1082 t!(s: "/", "/a/b", true);
1083 t!(s: "..", "a/b", true);
1084 t!(s: "../..", "a/b", true);
1088 fn test_ends_with_path() {
1090 (s: $path:expr, $child:expr, $exp:expr) => (
1092 let path = Path::new($path);
1093 let child = Path::new($child);
1094 assert_eq!(path.ends_with_path(&child), $exp);
1097 (v: $path:expr, $child:expr, $exp:expr) => (
1099 let path = Path::new($path);
1100 let child = Path::new($child);
1101 assert_eq!(path.ends_with_path(&child), $exp);
1106 t!(s: "a/b/c", "c", true);
1107 t!(s: "a/b/c", "d", false);
1108 t!(s: "foo/bar/quux", "bar", false);
1109 t!(s: "foo/bar/quux", "barquux", false);
1110 t!(s: "a/b/c", "b/c", true);
1111 t!(s: "a/b/c", "a/b/c", true);
1112 t!(s: "a/b/c", "foo/a/b/c", false);
1113 t!(s: "/a/b/c", "a/b/c", true);
1114 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1115 t!(s: "/a/b/c", "foo/a/b/c", false);
1116 t!(s: "a/b/c", "", false);
1117 t!(s: "", "", true);
1118 t!(s: "/a/b/c", "d/e/f", false);
1119 t!(s: "a/b/c", "a/b", false);
1120 t!(s: "a/b/c", "b", false);
1121 t!(v: b"a/b/c", b"b/c", true);
1122 t!(v: b"a/b/\xFF", b"\xFF", true);
1123 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1127 fn test_path_relative_from() {
1129 (s: $path:expr, $other:expr, $exp:expr) => (
1131 let path = Path::new($path);
1132 let other = Path::new($other);
1133 let res = path.path_relative_from(&other);
1134 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1139 t!(s: "a/b/c", "a/b", Some("c"));
1140 t!(s: "a/b/c", "a/b/d", Some("../c"));
1141 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1142 t!(s: "a/b/c", "a/b/c", Some("."));
1143 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1144 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1145 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1146 t!(s: "a/b/c", "/a/b/c", None);
1147 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1148 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1149 t!(s: "/a/b/c", "/a/b", Some("c"));
1150 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1151 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1152 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1153 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1154 t!(s: ".", "a", Some(".."));
1155 t!(s: ".", "a/b", Some("../.."));
1156 t!(s: ".", ".", Some("."));
1157 t!(s: "a", ".", Some("a"));
1158 t!(s: "a/b", ".", Some("a/b"));
1159 t!(s: "..", ".", Some(".."));
1160 t!(s: "a/b/c", "a/b/c", Some("."));
1161 t!(s: "/a/b/c", "/a/b/c", Some("."));
1162 t!(s: "/", "/", Some("."));
1163 t!(s: "/", ".", Some("/"));
1164 t!(s: "../../a", "b", Some("../../../a"));
1165 t!(s: "a", "../../b", None);
1166 t!(s: "../../a", "../../b", Some("../a"));
1167 t!(s: "../../a", "../../a/b", Some(".."));
1168 t!(s: "../../a/b", "../../a", Some("b"));
1172 fn test_components_iter() {
1174 (s: $path:expr, $exp:expr) => (
1176 let path = Path::new($path);
1177 let comps = path.components().collect::<Vec<&[u8]>>();
1178 let exp: &[&str] = &$exp;
1179 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1180 assert!(comps == exps, "components: Expected {}, found {}",
1182 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1183 let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1184 assert!(comps == exps, "rev_components: Expected {}, found {}",
1188 (b: $arg:expr, [$($exp:expr),*]) => (
1190 let path = Path::new($arg);
1191 let comps = path.components().collect::<Vec<&[u8]>>();
1192 let exp: &[&[u8]] = &[$($exp),*];
1193 assert_eq!(comps, exp);
1194 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1195 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1196 assert_eq!(comps, exp)
1201 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1202 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1203 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1204 t!(s: "a/b/c", ["a", "b", "c"]);
1205 t!(s: "a/b/d", ["a", "b", "d"]);
1206 t!(s: "a/b/cd", ["a", "b", "cd"]);
1207 t!(s: "/a/b/c", ["a", "b", "c"]);
1212 t!(s: "..", [".."]);
1213 t!(s: "../..", ["..", ".."]);
1214 t!(s: "../../foo", ["..", "..", "foo"]);
1218 fn test_str_components() {
1220 (b: $arg:expr, $exp:expr) => (
1222 let path = Path::new($arg);
1223 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1224 let exp: &[Option<&str>] = &$exp;
1225 assert_eq!(comps, exp);
1226 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1227 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1228 assert_eq!(comps, exp);
1233 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1234 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1235 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1236 // str_components is a wrapper around components, so no need to do
1237 // the full set of tests
1244 use self::test::Bencher;
1246 use prelude::v1::{Clone, GenericPath};
1249 fn join_home_dir(b: &mut Bencher) {
1250 let posix_path = Path::new("/");
1252 posix_path.join("home");
1257 fn join_abs_path_home_dir(b: &mut Bencher) {
1258 let posix_path = Path::new("/");
1260 posix_path.join("/home");
1265 fn join_many_home_dir(b: &mut Bencher) {
1266 let posix_path = Path::new("/");
1268 posix_path.join_many(&["home"]);
1273 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1274 let posix_path = Path::new("/");
1276 posix_path.join_many(&["/home"]);
1281 fn push_home_dir(b: &mut Bencher) {
1282 let mut posix_path = Path::new("/");
1284 posix_path.push("home");
1289 fn push_abs_path_home_dir(b: &mut Bencher) {
1290 let mut posix_path = Path::new("/");
1292 posix_path.push("/home");
1297 fn push_many_home_dir(b: &mut Bencher) {
1298 let mut posix_path = Path::new("/");
1300 posix_path.push_many(&["home"]);
1305 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1306 let mut posix_path = Path::new("/");
1308 posix_path.push_many(&["/home"]);
1313 fn ends_with_path_home_dir(b: &mut Bencher) {
1314 let posix_home_path = Path::new("/home");
1316 posix_home_path.ends_with_path(&Path::new("home"));
1321 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1322 let posix_home_path = Path::new("/home");
1324 posix_home_path.ends_with_path(&Path::new("jome"));
1329 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1330 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1331 let mut sub = path.clone();
1334 path.is_ancestor_of(&sub);
1339 fn path_relative_from_forward(b: &mut Bencher) {
1340 let path = Path::new("/a/b/c");
1341 let mut other = path.clone();
1344 path.path_relative_from(&other);
1349 fn path_relative_from_same_level(b: &mut Bencher) {
1350 let path = Path::new("/a/b/c");
1351 let mut other = path.clone();
1355 path.path_relative_from(&other);
1360 fn path_relative_from_backward(b: &mut Bencher) {
1361 let path = Path::new("/a/b");
1362 let mut other = path.clone();
1365 path.path_relative_from(&other);