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::{DoubleEndedIteratorExt, AdditiveIterator, Extend};
19 use iter::{Iterator, IteratorExt, Map};
21 use option::Option::{None, Some};
23 use str::{FromStr, Str};
25 use slice::{CloneSliceExt, Split, AsSlice, SliceConcatExt,
26 PartialEqSliceExt, SliceExt};
29 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
31 /// Iterator that yields successive components of a Path as &[u8]
32 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
34 /// Iterator that yields successive components of a Path as Option<&str>
35 pub type StrComponents<'a> =
36 Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
38 /// Represents a POSIX file path
41 repr: Vec<u8>, // assumed to never be empty or contain NULs
42 sepidx: Option<uint> // index of the final separator in repr
45 /// The standard path separator character
46 pub const SEP: char = '/';
48 /// The standard path separator byte
49 pub const SEP_BYTE: u8 = SEP as u8;
51 /// Returns whether the given byte is a path separator
53 pub fn is_sep_byte(u: &u8) -> bool {
57 /// Returns whether the given char is a path separator
59 pub fn is_sep(c: char) -> bool {
63 impl PartialEq for Path {
65 fn eq(&self, other: &Path) -> bool {
66 self.repr == other.repr
72 impl PartialOrd for Path {
73 fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
79 fn cmp(&self, other: &Path) -> Ordering {
80 self.repr.cmp(&other.repr)
84 impl FromStr for Path {
85 fn from_str(s: &str) -> Option<Path> {
90 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
91 // we cannot usefully take ToCStr arguments by reference (without forcing an
92 // additional & around &str). So we are instead temporarily adding an instance
93 // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
94 // instance should be removed, and arguments bound by ToCStr should be passed by
97 impl ToCStr for Path {
99 fn to_c_str(&self) -> CString {
100 // The Path impl guarantees no internal NUL
101 unsafe { self.to_c_str_unchecked() }
105 unsafe fn to_c_str_unchecked(&self) -> CString {
106 self.as_vec().to_c_str_unchecked()
110 impl<S: hash::Writer> hash::Hash<S> for Path {
112 fn hash(&self, state: &mut S) {
113 self.repr.hash(state)
117 impl BytesContainer for Path {
119 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
124 impl GenericPathUnsafe for Path {
125 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
126 let path = Path::normalize(path.container_as_bytes());
127 assert!(!path.is_empty());
128 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
129 Path{ repr: path, sepidx: idx }
132 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
133 let filename = filename.container_as_bytes();
135 None if b".." == self.repr => {
136 let mut v = Vec::with_capacity(3 + filename.len());
137 v.push_all(dot_dot_static);
139 v.push_all(filename);
140 // FIXME: this is slow
141 self.repr = Path::normalize(v.as_slice());
144 self.repr = Path::normalize(filename);
146 Some(idx) if self.repr[idx+1..] == b".." => {
147 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
148 v.push_all(self.repr.as_slice());
150 v.push_all(filename);
151 // FIXME: this is slow
152 self.repr = Path::normalize(v.as_slice());
155 let mut v = Vec::with_capacity(idx + 1 + filename.len());
156 v.push_all(self.repr[..idx+1]);
157 v.push_all(filename);
158 // FIXME: this is slow
159 self.repr = Path::normalize(v.as_slice());
162 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
165 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
166 let path = path.container_as_bytes();
167 if !path.is_empty() {
168 if path[0] == SEP_BYTE {
169 self.repr = Path::normalize(path);
171 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
172 v.push_all(self.repr.as_slice());
175 // FIXME: this is slow
176 self.repr = Path::normalize(v.as_slice());
178 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
183 impl GenericPath for Path {
185 fn as_vec<'a>(&'a self) -> &'a [u8] {
189 fn into_vec(self) -> Vec<u8> {
193 fn dirname<'a>(&'a self) -> &'a [u8] {
195 None if b".." == self.repr => self.repr.as_slice(),
197 Some(0) => self.repr[..1],
198 Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(),
199 Some(idx) => self.repr[..idx]
203 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
205 None if b"." == self.repr ||
206 b".." == self.repr => None,
207 None => Some(self.repr.as_slice()),
208 Some(idx) if self.repr[idx+1..] == b".." => None,
209 Some(0) if self.repr[1..].is_empty() => None,
210 Some(idx) => Some(self.repr[idx+1..])
214 fn pop(&mut self) -> bool {
216 None if b"." == self.repr => false,
218 self.repr = vec![b'.'];
222 Some(0) if b"/" == self.repr => false,
225 self.repr.truncate(idx+1);
227 self.repr.truncate(idx);
229 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
235 fn root_path(&self) -> Option<Path> {
236 if self.is_absolute() {
244 fn is_absolute(&self) -> bool {
245 self.repr[0] == SEP_BYTE
248 fn is_ancestor_of(&self, other: &Path) -> bool {
249 if self.is_absolute() != other.is_absolute() {
252 let mut ita = self.components();
253 let mut itb = other.components();
254 if b"." == self.repr {
255 return match itb.next() {
257 Some(b) => b != b".."
261 match (ita.next(), itb.next()) {
263 (Some(a), Some(b)) if a == b => { continue },
264 (Some(a), _) if a == b".." => {
265 // if ita contains only .. components, it's an ancestor
266 return ita.all(|x| x == b"..");
275 fn path_relative_from(&self, base: &Path) -> Option<Path> {
276 if self.is_absolute() != base.is_absolute() {
277 if self.is_absolute() {
283 let mut ita = self.components();
284 let mut itb = base.components();
285 let mut comps = vec![];
287 match (ita.next(), itb.next()) {
288 (None, None) => break,
291 comps.extend(ita.by_ref());
294 (None, _) => comps.push(dot_dot_static),
295 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
296 (Some(a), Some(b)) if b == b"." => comps.push(a),
297 (Some(_), Some(b)) if b == b".." => return None,
298 (Some(a), Some(_)) => {
299 comps.push(dot_dot_static);
301 comps.push(dot_dot_static);
304 comps.extend(ita.by_ref());
309 Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
313 fn ends_with_path(&self, child: &Path) -> bool {
314 if !child.is_relative() { return false; }
315 let mut selfit = self.components().rev();
316 let mut childit = child.components().rev();
318 match (selfit.next(), childit.next()) {
319 (Some(a), Some(b)) => if a != b { return false; },
320 (Some(_), None) => break,
321 (None, Some(_)) => return false,
322 (None, None) => break
330 /// Returns a new Path from a byte vector or string
334 /// Panics the task if the vector contains a NUL.
336 pub fn new<T: BytesContainer>(path: T) -> Path {
337 GenericPath::new(path)
340 /// Returns a new Path from a byte vector or string, if possible
342 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
343 GenericPath::new_opt(path)
346 /// Returns a normalized byte vector representation of a path, by removing all empty
347 /// components, and unnecessary . and .. components.
348 fn normalize<Sized? V: AsSlice<u8>>(v: &V) -> Vec<u8> {
349 // borrowck is being very picky
351 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
352 let v_ = if is_abs { v.as_slice()[1..] } else { v.as_slice() };
353 let comps = normalize_helper(v_, is_abs);
357 if is_abs && comps.is_empty() {
360 let n = if is_abs { comps.len() } else { comps.len() - 1} +
361 comps.iter().map(|v| v.len()).sum();
362 let mut v = Vec::with_capacity(n);
363 let mut it = comps.into_iter();
367 Some(comp) => v.push_all(comp)
380 None => v.as_slice().to_vec(),
385 /// Returns an iterator that yields each component of the path in turn.
386 /// Does not distinguish between absolute and relative paths, e.g.
387 /// /a/b/c and a/b/c yield the same set of components.
388 /// A path of "/" yields no components. A path of "." yields one component.
389 pub fn components<'a>(&'a self) -> Components<'a> {
390 let v = if self.repr[0] == SEP_BYTE {
392 } else { self.repr.as_slice() };
393 let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
394 let mut ret = v.split(is_sep_byte);
396 // consume the empty "" component
402 /// Returns an iterator that yields each component of the path as Option<&str>.
403 /// See components() for details.
404 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
405 fn from_utf8(s: &[u8]) -> Option<&str> {
406 str::from_utf8(s).ok()
408 let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
409 self.components().map(f)
413 // None result means the byte vector didn't need normalizing
414 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
415 if is_abs && v.is_empty() {
418 let mut comps: Vec<&'a [u8]> = vec![];
420 let mut changed = false;
421 for comp in v.split(is_sep_byte) {
422 if comp.is_empty() { changed = true }
423 else if comp == b"." { changed = true }
424 else if comp == b".." {
425 if is_abs && comps.is_empty() { changed = true }
426 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
427 else { comps.pop().unwrap(); changed = true }
428 } else { comps.push(comp) }
431 if comps.is_empty() && !is_abs {
435 comps.push(dot_static);
443 #[allow(non_upper_case_globals)]
444 static dot_static: &'static [u8] = b".";
445 #[allow(non_upper_case_globals)]
446 static dot_dot_static: &'static [u8] = b"..";
451 use prelude::Option::{mod, Some, None};
452 use prelude::{Vec, Clone, AsSlice, SliceExt, CloneSliceExt, IteratorExt};
453 use prelude::{DoubleEndedIteratorExt, Str, StrExt, ToString, GenericPath};
457 (s: $path:expr, $exp:expr) => (
460 assert!(path.as_str() == Some($exp));
463 (v: $path:expr, $exp:expr) => (
466 assert!(path.as_vec() == $exp);
473 let empty: &[u8] = &[];
474 t!(v: Path::new(empty), b".");
475 t!(v: Path::new(b"/"), b"/");
476 t!(v: Path::new(b"a/b/c"), b"a/b/c");
477 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
478 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
479 let p = Path::new(b"a/b/c\xFF");
480 assert!(p.as_str() == None);
482 t!(s: Path::new(""), ".");
483 t!(s: Path::new("/"), "/");
484 t!(s: Path::new("hi"), "hi");
485 t!(s: Path::new("hi/"), "hi");
486 t!(s: Path::new("/lib"), "/lib");
487 t!(s: Path::new("/lib/"), "/lib");
488 t!(s: Path::new("hi/there"), "hi/there");
489 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
491 t!(s: Path::new("hi/there/"), "hi/there");
492 t!(s: Path::new("hi/../there"), "there");
493 t!(s: Path::new("../hi/there"), "../hi/there");
494 t!(s: Path::new("/../hi/there"), "/hi/there");
495 t!(s: Path::new("foo/.."), ".");
496 t!(s: Path::new("/foo/.."), "/");
497 t!(s: Path::new("/foo/../.."), "/");
498 t!(s: Path::new("/foo/../../bar"), "/bar");
499 t!(s: Path::new("/./hi/./there/."), "/hi/there");
500 t!(s: Path::new("/./hi/./there/./.."), "/hi");
501 t!(s: Path::new("foo/../.."), "..");
502 t!(s: Path::new("foo/../../.."), "../..");
503 t!(s: Path::new("foo/../../bar"), "../bar");
505 assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
506 assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
509 let p = Path::new(b"foo/bar\x80");
510 assert!(p.as_str() == None);
514 fn test_opt_paths() {
515 assert!(Path::new_opt(b"foo/bar\0") == None);
516 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
517 assert!(Path::new_opt("foo/bar\0") == None);
518 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
522 fn test_null_byte() {
524 let result = Thread::spawn(move|| {
525 Path::new(b"foo/bar\0")
527 assert!(result.is_err());
529 let result = Thread::spawn(move|| {
530 Path::new("test").set_filename(b"f\0o")
532 assert!(result.is_err());
534 let result = Thread::spawn(move|| {
535 Path::new("test").push(b"f\0o");
537 assert!(result.is_err());
541 fn test_display_str() {
543 ($path:expr, $disp:ident, $exp:expr) => (
545 let path = Path::new($path);
546 assert!(path.$disp().to_string() == $exp);
550 t!("foo", display, "foo");
551 t!(b"foo\x80", display, "foo\u{FFFD}");
552 t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
553 t!(b"foo\xFF/bar", filename_display, "bar");
554 t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
555 t!(b"/", filename_display, "");
558 ($path:expr, $exp:expr) => (
560 let path = Path::new($path);
561 let mo = path.display().as_cow();
562 assert!(mo.as_slice() == $exp);
565 ($path:expr, $exp:expr, filename) => (
567 let path = Path::new($path);
568 let mo = path.filename_display().as_cow();
569 assert!(mo.as_slice() == $exp);
575 t!(b"foo\x80", "foo\u{FFFD}");
576 t!(b"foo\xFFbar", "foo\u{FFFD}bar");
577 t!(b"foo\xFF/bar", "bar", filename);
578 t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
579 t!(b"/", "", filename);
585 ($path:expr, $exp:expr, $expf:expr) => (
587 let path = Path::new($path);
588 let f = format!("{}", path.display());
590 let f = format!("{}", path.filename_display());
596 t!(b"foo", "foo", "foo");
597 t!(b"foo/bar", "foo/bar", "bar");
599 t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
600 t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
601 t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
602 t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
606 fn test_components() {
608 (s: $path:expr, $op:ident, $exp:expr) => (
610 let path = Path::new($path);
611 assert!(path.$op() == ($exp).as_bytes());
614 (s: $path:expr, $op:ident, $exp:expr, opt) => (
616 let path = Path::new($path);
617 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
618 assert!(left == $exp);
621 (v: $path:expr, $op:ident, $exp:expr) => (
624 let path = Path::new(arg);
625 assert!(path.$op() == $exp);
630 t!(v: b"a/b/c", filename, Some(b"c"));
631 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
632 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
633 t!(s: "a/b/c", filename, Some("c"), opt);
634 t!(s: "/a/b/c", filename, Some("c"), opt);
635 t!(s: "a", filename, Some("a"), opt);
636 t!(s: "/a", filename, Some("a"), opt);
637 t!(s: ".", filename, None, opt);
638 t!(s: "/", filename, None, opt);
639 t!(s: "..", filename, None, opt);
640 t!(s: "../..", filename, None, opt);
642 t!(v: b"a/b/c", dirname, b"a/b");
643 t!(v: b"a/b/c\xFF", dirname, b"a/b");
644 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
645 t!(s: "a/b/c", dirname, "a/b");
646 t!(s: "/a/b/c", dirname, "/a/b");
647 t!(s: "a", dirname, ".");
648 t!(s: "/a", dirname, "/");
649 t!(s: ".", dirname, ".");
650 t!(s: "/", dirname, "/");
651 t!(s: "..", dirname, "..");
652 t!(s: "../..", dirname, "../..");
654 t!(v: b"hi/there.txt", filestem, Some(b"there"));
655 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
656 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
657 t!(s: "hi/there.txt", filestem, Some("there"), opt);
658 t!(s: "hi/there", filestem, Some("there"), opt);
659 t!(s: "there.txt", filestem, Some("there"), opt);
660 t!(s: "there", filestem, Some("there"), opt);
661 t!(s: ".", filestem, None, opt);
662 t!(s: "/", filestem, None, opt);
663 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
664 t!(s: ".bar", filestem, Some(".bar"), opt);
665 t!(s: "..bar", filestem, Some("."), opt);
666 t!(s: "hi/there..txt", filestem, Some("there."), opt);
667 t!(s: "..", filestem, None, opt);
668 t!(s: "../..", filestem, None, opt);
670 t!(v: b"hi/there.txt", extension, Some(b"txt"));
671 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
672 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
673 t!(v: b"hi/there", extension, None);
674 t!(v: b"hi/there\x80", extension, None);
675 t!(s: "hi/there.txt", extension, Some("txt"), opt);
676 t!(s: "hi/there", extension, None, opt);
677 t!(s: "there.txt", extension, Some("txt"), opt);
678 t!(s: "there", extension, None, opt);
679 t!(s: ".", extension, None, opt);
680 t!(s: "/", extension, None, opt);
681 t!(s: "foo/.bar", extension, None, opt);
682 t!(s: ".bar", extension, None, opt);
683 t!(s: "..bar", extension, Some("bar"), opt);
684 t!(s: "hi/there..txt", extension, Some("txt"), opt);
685 t!(s: "..", extension, None, opt);
686 t!(s: "../..", extension, None, opt);
692 (s: $path:expr, $join:expr) => (
696 let mut p1 = Path::new(path);
699 assert!(p1 == p2.join(join));
704 t!(s: "a/b/c", "..");
705 t!(s: "/a/b/c", "d");
707 t!(s: "a/b", "/c/d");
711 fn test_push_path() {
713 (s: $path:expr, $push:expr, $exp:expr) => (
715 let mut p = Path::new($path);
716 let push = Path::new($push);
718 assert!(p.as_str() == Some($exp));
723 t!(s: "a/b/c", "d", "a/b/c/d");
724 t!(s: "/a/b/c", "d", "/a/b/c/d");
725 t!(s: "a/b", "c/d", "a/b/c/d");
726 t!(s: "a/b", "/c/d", "/c/d");
727 t!(s: "a/b", ".", "a/b");
728 t!(s: "a/b", "../c", "a/c");
732 fn test_push_many() {
734 (s: $path:expr, $push:expr, $exp:expr) => (
736 let mut p = Path::new($path);
738 assert!(p.as_str() == Some($exp));
741 (v: $path:expr, $push:expr, $exp:expr) => (
743 let mut p = Path::new($path);
745 assert!(p.as_vec() == $exp);
750 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
751 t!(s: "a/b/c", ["d", "/e"], "/e");
752 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
753 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
754 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
755 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
756 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
762 (s: $path:expr, $left:expr, $right:expr) => (
764 let mut p = Path::new($path);
765 let result = p.pop();
766 assert!(p.as_str() == Some($left));
767 assert!(result == $right);
770 (b: $path:expr, $left:expr, $right:expr) => (
772 let mut p = Path::new($path);
773 let result = p.pop();
774 assert!(p.as_vec() == $left);
775 assert!(result == $right);
780 t!(b: b"a/b/c", b"a/b", true);
781 t!(b: b"a", b".", true);
782 t!(b: b".", b".", false);
783 t!(b: b"/a", b"/", true);
784 t!(b: b"/", b"/", false);
785 t!(b: b"a/b/c\x80", b"a/b", true);
786 t!(b: b"a/b\x80/c", b"a/b\x80", true);
787 t!(b: b"\xFF", b".", true);
788 t!(b: b"/\xFF", b"/", true);
789 t!(s: "a/b/c", "a/b", true);
790 t!(s: "a", ".", true);
791 t!(s: ".", ".", false);
792 t!(s: "/a", "/", true);
793 t!(s: "/", "/", false);
797 fn test_root_path() {
798 assert!(Path::new(b"a/b/c").root_path() == None);
799 assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
804 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
805 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
806 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
807 t!(s: Path::new("a/b/c").join(".."), "a/b");
808 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
809 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
810 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
811 t!(s: Path::new(".").join("a/b"), "a/b");
812 t!(s: Path::new("/").join("a/b"), "/a/b");
816 fn test_join_path() {
818 (s: $path:expr, $join:expr, $exp:expr) => (
820 let path = Path::new($path);
821 let join = Path::new($join);
822 let res = path.join(&join);
823 assert!(res.as_str() == Some($exp));
828 t!(s: "a/b/c", "..", "a/b");
829 t!(s: "/a/b/c", "d", "/a/b/c/d");
830 t!(s: "a/b", "c/d", "a/b/c/d");
831 t!(s: "a/b", "/c/d", "/c/d");
832 t!(s: ".", "a/b", "a/b");
833 t!(s: "/", "a/b", "/a/b");
837 fn test_join_many() {
839 (s: $path:expr, $join:expr, $exp:expr) => (
841 let path = Path::new($path);
842 let res = path.join_many(&$join);
843 assert!(res.as_str() == Some($exp));
846 (v: $path:expr, $join:expr, $exp:expr) => (
848 let path = Path::new($path);
849 let res = path.join_many(&$join);
850 assert!(res.as_vec() == $exp);
855 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
856 t!(s: "a/b/c", ["..", "d"], "a/b/d");
857 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
858 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
859 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
860 t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
864 fn test_with_helpers() {
865 let empty: &[u8] = &[];
867 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
868 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
869 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
871 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
872 t!(s: Path::new(".").with_filename("foo"), "foo");
873 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
874 t!(s: Path::new("/").with_filename("foo"), "/foo");
875 t!(s: Path::new("/a").with_filename("foo"), "/foo");
876 t!(s: Path::new("foo").with_filename("bar"), "bar");
877 t!(s: Path::new("/").with_filename("foo/"), "/foo");
878 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
879 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
880 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
881 t!(s: Path::new("a/b/c").with_filename(".."), "a");
882 t!(s: Path::new("/a").with_filename(""), "/");
883 t!(s: Path::new("foo").with_filename(""), ".");
884 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
885 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
886 t!(s: Path::new("..").with_filename("foo"), "../foo");
887 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
888 t!(s: Path::new("..").with_filename(""), "..");
889 t!(s: Path::new("../..").with_filename(""), "../..");
891 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
892 b"hi/there\x80.exe");
893 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
895 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
896 b"hi/there\x80.\xFF");
897 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
898 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
899 t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
900 t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
901 t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
902 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
903 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
904 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
905 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
906 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
907 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
908 t!(s: Path::new("/").with_extension("txt"), "/");
909 t!(s: Path::new("/").with_extension("."), "/");
910 t!(s: Path::new("/").with_extension(".."), "/");
911 t!(s: Path::new(".").with_extension("txt"), ".");
917 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
921 let mut p1 = Path::new(path);
923 let p2 = Path::new(path);
924 assert!(p1 == p2.$with(arg));
927 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
931 let mut p1 = Path::new(path);
933 let p2 = Path::new(path);
934 assert!(p1 == p2.$with(arg));
939 t!(v: b"a/b/c", set_filename, with_filename, b"d");
940 t!(v: b"/", set_filename, with_filename, b"foo");
941 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
942 t!(s: "a/b/c", set_filename, with_filename, "d");
943 t!(s: "/", set_filename, with_filename, "foo");
944 t!(s: ".", set_filename, with_filename, "foo");
945 t!(s: "a/b", set_filename, with_filename, "");
946 t!(s: "a", set_filename, with_filename, "");
948 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
949 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
950 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
951 t!(s: "hi/there.", set_extension, with_extension, "txt");
952 t!(s: "hi/there", set_extension, with_extension, "txt");
953 t!(s: "hi/there.txt", set_extension, with_extension, "");
954 t!(s: "hi/there", set_extension, with_extension, "");
955 t!(s: ".", set_extension, with_extension, "txt");
961 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
964 let filename = $filename;
965 assert!(path.filename_str() == filename,
966 "{}.filename_str(): Expected `{}`, found {}",
967 path.as_str().unwrap(), filename, path.filename_str());
968 let dirname = $dirname;
969 assert!(path.dirname_str() == dirname,
970 "`{}`.dirname_str(): Expected `{}`, found `{}`",
971 path.as_str().unwrap(), dirname, path.dirname_str());
972 let filestem = $filestem;
973 assert!(path.filestem_str() == filestem,
974 "`{}`.filestem_str(): Expected `{}`, found `{}`",
975 path.as_str().unwrap(), filestem, path.filestem_str());
977 assert!(path.extension_str() == ext,
978 "`{}`.extension_str(): Expected `{}`, found `{}`",
979 path.as_str().unwrap(), ext, path.extension_str());
982 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
985 assert!(path.filename() == $filename);
986 assert!(path.dirname() == $dirname);
987 assert!(path.filestem() == $filestem);
988 assert!(path.extension() == $ext);
993 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
994 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
995 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
996 Some(b"there"), Some(b"\xFF"));
997 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
998 t!(s: Path::new("."), None, Some("."), None, None);
999 t!(s: Path::new("/"), None, Some("/"), None, None);
1000 t!(s: Path::new(".."), None, Some(".."), None, None);
1001 t!(s: Path::new("../.."), None, Some("../.."), None, None);
1002 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
1003 Some("there"), Some("txt"));
1004 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
1005 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
1006 Some("there"), Some(""));
1007 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
1008 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
1009 Some("."), Some("there"));
1010 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
1011 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
1012 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
1013 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
1017 fn test_dir_path() {
1018 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
1019 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
1020 t!(s: Path::new("hi/there").dir_path(), "hi");
1021 t!(s: Path::new("hi").dir_path(), ".");
1022 t!(s: Path::new("/hi").dir_path(), "/");
1023 t!(s: Path::new("/").dir_path(), "/");
1024 t!(s: Path::new("..").dir_path(), "..");
1025 t!(s: Path::new("../..").dir_path(), "../..");
1029 fn test_is_absolute() {
1031 (s: $path:expr, $abs:expr, $rel:expr) => (
1033 let path = Path::new($path);
1034 assert_eq!(path.is_absolute(), $abs);
1035 assert_eq!(path.is_relative(), $rel);
1039 t!(s: "a/b/c", false, true);
1040 t!(s: "/a/b/c", true, false);
1041 t!(s: "a", false, true);
1042 t!(s: "/a", true, false);
1043 t!(s: ".", false, true);
1044 t!(s: "/", true, false);
1045 t!(s: "..", false, true);
1046 t!(s: "../..", false, true);
1050 fn test_is_ancestor_of() {
1052 (s: $path:expr, $dest:expr, $exp:expr) => (
1054 let path = Path::new($path);
1055 let dest = Path::new($dest);
1056 assert_eq!(path.is_ancestor_of(&dest), $exp);
1061 t!(s: "a/b/c", "a/b/c/d", true);
1062 t!(s: "a/b/c", "a/b/c", true);
1063 t!(s: "a/b/c", "a/b", false);
1064 t!(s: "/a/b/c", "/a/b/c", true);
1065 t!(s: "/a/b", "/a/b/c", true);
1066 t!(s: "/a/b/c/d", "/a/b/c", false);
1067 t!(s: "/a/b", "a/b/c", false);
1068 t!(s: "a/b", "/a/b/c", false);
1069 t!(s: "a/b/c", "a/b/d", false);
1070 t!(s: "../a/b/c", "a/b/c", false);
1071 t!(s: "a/b/c", "../a/b/c", false);
1072 t!(s: "a/b/c", "a/b/cd", false);
1073 t!(s: "a/b/cd", "a/b/c", false);
1074 t!(s: "../a/b", "../a/b/c", true);
1075 t!(s: ".", "a/b", true);
1076 t!(s: ".", ".", true);
1077 t!(s: "/", "/", true);
1078 t!(s: "/", "/a/b", true);
1079 t!(s: "..", "a/b", true);
1080 t!(s: "../..", "a/b", true);
1084 fn test_ends_with_path() {
1086 (s: $path:expr, $child:expr, $exp:expr) => (
1088 let path = Path::new($path);
1089 let child = Path::new($child);
1090 assert_eq!(path.ends_with_path(&child), $exp);
1093 (v: $path:expr, $child:expr, $exp:expr) => (
1095 let path = Path::new($path);
1096 let child = Path::new($child);
1097 assert_eq!(path.ends_with_path(&child), $exp);
1102 t!(s: "a/b/c", "c", true);
1103 t!(s: "a/b/c", "d", false);
1104 t!(s: "foo/bar/quux", "bar", false);
1105 t!(s: "foo/bar/quux", "barquux", false);
1106 t!(s: "a/b/c", "b/c", true);
1107 t!(s: "a/b/c", "a/b/c", true);
1108 t!(s: "a/b/c", "foo/a/b/c", false);
1109 t!(s: "/a/b/c", "a/b/c", true);
1110 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1111 t!(s: "/a/b/c", "foo/a/b/c", false);
1112 t!(s: "a/b/c", "", false);
1113 t!(s: "", "", true);
1114 t!(s: "/a/b/c", "d/e/f", false);
1115 t!(s: "a/b/c", "a/b", false);
1116 t!(s: "a/b/c", "b", false);
1117 t!(v: b"a/b/c", b"b/c", true);
1118 t!(v: b"a/b/\xFF", b"\xFF", true);
1119 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1123 fn test_path_relative_from() {
1125 (s: $path:expr, $other:expr, $exp:expr) => (
1127 let path = Path::new($path);
1128 let other = Path::new($other);
1129 let res = path.path_relative_from(&other);
1130 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1135 t!(s: "a/b/c", "a/b", Some("c"));
1136 t!(s: "a/b/c", "a/b/d", Some("../c"));
1137 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1138 t!(s: "a/b/c", "a/b/c", Some("."));
1139 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1140 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1141 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1142 t!(s: "a/b/c", "/a/b/c", None);
1143 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1144 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1145 t!(s: "/a/b/c", "/a/b", Some("c"));
1146 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1147 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1148 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1149 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1150 t!(s: ".", "a", Some(".."));
1151 t!(s: ".", "a/b", Some("../.."));
1152 t!(s: ".", ".", Some("."));
1153 t!(s: "a", ".", Some("a"));
1154 t!(s: "a/b", ".", Some("a/b"));
1155 t!(s: "..", ".", Some(".."));
1156 t!(s: "a/b/c", "a/b/c", Some("."));
1157 t!(s: "/a/b/c", "/a/b/c", Some("."));
1158 t!(s: "/", "/", Some("."));
1159 t!(s: "/", ".", Some("/"));
1160 t!(s: "../../a", "b", Some("../../../a"));
1161 t!(s: "a", "../../b", None);
1162 t!(s: "../../a", "../../b", Some("../a"));
1163 t!(s: "../../a", "../../a/b", Some(".."));
1164 t!(s: "../../a/b", "../../a", Some("b"));
1168 fn test_components_iter() {
1170 (s: $path:expr, $exp:expr) => (
1172 let path = Path::new($path);
1173 let comps = path.components().collect::<Vec<&[u8]>>();
1174 let exp: &[&str] = &$exp;
1175 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1176 assert!(comps == exps, "components: Expected {}, found {}",
1178 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1179 let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1180 assert!(comps == exps, "rev_components: Expected {}, found {}",
1184 (b: $arg:expr, [$($exp:expr),*]) => (
1186 let path = Path::new($arg);
1187 let comps = path.components().collect::<Vec<&[u8]>>();
1188 let exp: &[&[u8]] = &[$($exp),*];
1189 assert_eq!(comps, exp);
1190 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1191 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1192 assert_eq!(comps, exp)
1197 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1198 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1199 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1200 t!(s: "a/b/c", ["a", "b", "c"]);
1201 t!(s: "a/b/d", ["a", "b", "d"]);
1202 t!(s: "a/b/cd", ["a", "b", "cd"]);
1203 t!(s: "/a/b/c", ["a", "b", "c"]);
1208 t!(s: "..", [".."]);
1209 t!(s: "../..", ["..", ".."]);
1210 t!(s: "../../foo", ["..", "..", "foo"]);
1214 fn test_str_components() {
1216 (b: $arg:expr, $exp:expr) => (
1218 let path = Path::new($arg);
1219 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1220 let exp: &[Option<&str>] = &$exp;
1221 assert_eq!(comps, exp);
1222 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1223 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1224 assert_eq!(comps, exp);
1229 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1230 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1231 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1232 // str_components is a wrapper around components, so no need to do
1233 // the full set of tests
1240 use self::test::Bencher;
1242 use prelude::{Clone, GenericPath};
1245 fn join_home_dir(b: &mut Bencher) {
1246 let posix_path = Path::new("/");
1248 posix_path.join("home");
1253 fn join_abs_path_home_dir(b: &mut Bencher) {
1254 let posix_path = Path::new("/");
1256 posix_path.join("/home");
1261 fn join_many_home_dir(b: &mut Bencher) {
1262 let posix_path = Path::new("/");
1264 posix_path.join_many(&["home"]);
1269 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1270 let posix_path = Path::new("/");
1272 posix_path.join_many(&["/home"]);
1277 fn push_home_dir(b: &mut Bencher) {
1278 let mut posix_path = Path::new("/");
1280 posix_path.push("home");
1285 fn push_abs_path_home_dir(b: &mut Bencher) {
1286 let mut posix_path = Path::new("/");
1288 posix_path.push("/home");
1293 fn push_many_home_dir(b: &mut Bencher) {
1294 let mut posix_path = Path::new("/");
1296 posix_path.push_many(&["home"]);
1301 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1302 let mut posix_path = Path::new("/");
1304 posix_path.push_many(&["/home"]);
1309 fn ends_with_path_home_dir(b: &mut Bencher) {
1310 let posix_home_path = Path::new("/home");
1312 posix_home_path.ends_with_path(&Path::new("home"));
1317 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1318 let posix_home_path = Path::new("/home");
1320 posix_home_path.ends_with_path(&Path::new("jome"));
1325 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1326 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1327 let mut sub = path.clone();
1330 path.is_ancestor_of(&sub);
1335 fn path_relative_from_forward(b: &mut Bencher) {
1336 let path = Path::new("/a/b/c");
1337 let mut other = path.clone();
1340 path.path_relative_from(&other);
1345 fn path_relative_from_same_level(b: &mut Bencher) {
1346 let path = Path::new("/a/b/c");
1347 let mut other = path.clone();
1351 path.path_relative_from(&other);
1356 fn path_relative_from_backward(b: &mut Bencher) {
1357 let path = Path::new("/a/b");
1358 let mut other = path.clone();
1361 path.path_relative_from(&other);