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};
16 use collections::{Collection, MutableSeq};
17 use from_str::FromStr;
20 use iter::{DoubleEndedIterator, AdditiveIterator, Extendable, Iterator, Map};
21 use option::{Option, None, Some};
24 use slice::{CloneableVector, Splits, Slice, VectorVector,
25 ImmutablePartialEqSlice, ImmutableSlice};
28 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
30 /// Iterator that yields successive components of a Path as &[u8]
31 pub type Components<'a> = Splits<'a, u8>;
33 /// Iterator that yields successive components of a Path as Option<&str>
34 pub type StrComponents<'a> = Map<'a, &'a [u8], Option<&'a 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 static SEP: char = '/';
47 /// The standard path separator byte
48 pub static 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<'a> ToCStr for &'a Path {
111 fn to_c_str(&self) -> CString {
116 unsafe fn to_c_str_unchecked(&self) -> CString {
117 (*self).to_c_str_unchecked()
121 impl<S: hash::Writer> hash::Hash<S> for Path {
123 fn hash(&self, state: &mut S) {
124 self.repr.hash(state)
128 impl BytesContainer for Path {
130 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
134 fn container_into_owned_bytes(self) -> Vec<u8> {
139 impl<'a> BytesContainer for &'a Path {
141 fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
146 impl GenericPathUnsafe for Path {
147 unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
148 let path = Path::normalize(path.container_as_bytes());
149 assert!(!path.is_empty());
150 let idx = path.as_slice().rposition_elem(&SEP_BYTE);
151 Path{ repr: path, sepidx: idx }
154 unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
155 let filename = filename.container_as_bytes();
157 None if b".." == self.repr.as_slice() => {
158 let mut v = Vec::with_capacity(3 + filename.len());
159 v.push_all(dot_dot_static);
161 v.push_all(filename);
162 // FIXME: this is slow
163 self.repr = Path::normalize(v.as_slice());
166 self.repr = Path::normalize(filename);
168 Some(idx) if self.repr.slice_from(idx+1) == b".." => {
169 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
170 v.push_all(self.repr.as_slice());
172 v.push_all(filename);
173 // FIXME: this is slow
174 self.repr = Path::normalize(v.as_slice());
177 let mut v = Vec::with_capacity(idx + 1 + filename.len());
178 v.push_all(self.repr.slice_to(idx+1));
179 v.push_all(filename);
180 // FIXME: this is slow
181 self.repr = Path::normalize(v.as_slice());
184 self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
187 unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
188 let path = path.container_as_bytes();
189 if !path.is_empty() {
190 if path[0] == SEP_BYTE {
191 self.repr = Path::normalize(path);
193 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
194 v.push_all(self.repr.as_slice());
197 // FIXME: this is slow
198 self.repr = Path::normalize(v.as_slice());
200 self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
205 impl GenericPath for Path {
207 fn as_vec<'a>(&'a self) -> &'a [u8] {
211 fn into_vec(self) -> Vec<u8> {
215 fn dirname<'a>(&'a self) -> &'a [u8] {
217 None if b".." == self.repr.as_slice() => self.repr.as_slice(),
219 Some(0) => self.repr.slice_to(1),
220 Some(idx) if self.repr.slice_from(idx+1) == b".." => self.repr.as_slice(),
221 Some(idx) => self.repr.slice_to(idx)
225 fn filename<'a>(&'a self) -> Option<&'a [u8]> {
227 None if b"." == self.repr.as_slice() ||
228 b".." == self.repr.as_slice() => None,
229 None => Some(self.repr.as_slice()),
230 Some(idx) if self.repr.slice_from(idx+1) == b".." => None,
231 Some(0) if self.repr.slice_from(1).is_empty() => None,
232 Some(idx) => Some(self.repr.slice_from(idx+1))
236 fn pop(&mut self) -> bool {
238 None if b"." == self.repr.as_slice() => false,
240 self.repr = vec![b'.'];
244 Some(0) if b"/" == self.repr.as_slice() => false,
247 self.repr.truncate(idx+1);
249 self.repr.truncate(idx);
251 self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
257 fn root_path(&self) -> Option<Path> {
258 if self.is_absolute() {
266 fn is_absolute(&self) -> bool {
267 *self.repr.get(0) == SEP_BYTE
270 fn is_ancestor_of(&self, other: &Path) -> bool {
271 if self.is_absolute() != other.is_absolute() {
274 let mut ita = self.components();
275 let mut itb = other.components();
276 if b"." == self.repr.as_slice() {
277 return match itb.next() {
279 Some(b) => b != b".."
283 match (ita.next(), itb.next()) {
285 (Some(a), Some(b)) if a == b => { continue },
286 (Some(a), _) if a == b".." => {
287 // if ita contains only .. components, it's an ancestor
288 return ita.all(|x| x == b"..");
297 fn path_relative_from(&self, base: &Path) -> Option<Path> {
298 if self.is_absolute() != base.is_absolute() {
299 if self.is_absolute() {
305 let mut ita = self.components();
306 let mut itb = base.components();
307 let mut comps = vec![];
309 match (ita.next(), itb.next()) {
310 (None, None) => break,
313 comps.extend(ita.by_ref());
316 (None, _) => comps.push(dot_dot_static),
317 (Some(a), Some(b)) if comps.is_empty() && a == b => (),
318 (Some(a), Some(b)) if b == b"." => comps.push(a),
319 (Some(_), Some(b)) if b == b".." => return None,
320 (Some(a), Some(_)) => {
321 comps.push(dot_dot_static);
323 comps.push(dot_dot_static);
326 comps.extend(ita.by_ref());
331 Some(Path::new(comps.as_slice().connect_vec(&SEP_BYTE)))
335 fn ends_with_path(&self, child: &Path) -> bool {
336 if !child.is_relative() { return false; }
337 let mut selfit = self.components().rev();
338 let mut childit = child.components().rev();
340 match (selfit.next(), childit.next()) {
341 (Some(a), Some(b)) => if a != b { return false; },
342 (Some(_), None) => break,
343 (None, Some(_)) => return false,
344 (None, None) => break
352 /// Returns a new Path from a byte vector or string
356 /// Fails the task if the vector contains a NUL.
358 pub fn new<T: BytesContainer>(path: T) -> Path {
359 GenericPath::new(path)
362 /// Returns a new Path from a byte vector or string, if possible
364 pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
365 GenericPath::new_opt(path)
368 /// Returns a normalized byte vector representation of a path, by removing all empty
369 /// components, and unnecessary . and .. components.
370 fn normalize<V: Slice<u8>+CloneableVector<u8>>(v: V) -> Vec<u8> {
371 // borrowck is being very picky
373 let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
374 let v_ = if is_abs { v.as_slice().slice_from(1) } else { v.as_slice() };
375 let comps = normalize_helper(v_, is_abs);
379 if is_abs && comps.is_empty() {
382 let n = if is_abs { comps.len() } else { comps.len() - 1} +
383 comps.iter().map(|v| v.len()).sum();
384 let mut v = Vec::with_capacity(n);
385 let mut it = comps.move_iter();
389 Some(comp) => v.push_all(comp)
402 None => Vec::from_slice(v.as_slice()),
407 /// Returns an iterator that yields each component of the path in turn.
408 /// Does not distinguish between absolute and relative paths, e.g.
409 /// /a/b/c and a/b/c yield the same set of components.
410 /// A path of "/" yields no components. A path of "." yields one component.
411 pub fn components<'a>(&'a self) -> Components<'a> {
412 let v = if *self.repr.get(0) == SEP_BYTE {
413 self.repr.slice_from(1)
414 } else { self.repr.as_slice() };
415 let mut ret = v.split(is_sep_byte);
417 // consume the empty "" component
423 /// Returns an iterator that yields each component of the path as Option<&str>.
424 /// See components() for details.
425 pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
426 self.components().map(str::from_utf8)
430 // None result means the byte vector didn't need normalizing
431 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
432 if is_abs && v.as_slice().is_empty() {
435 let mut comps: Vec<&'a [u8]> = vec![];
437 let mut changed = false;
438 for comp in v.split(is_sep_byte) {
439 if comp.is_empty() { changed = true }
440 else if comp == b"." { changed = true }
441 else if comp == b".." {
442 if is_abs && comps.is_empty() { changed = true }
443 else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
444 else { comps.pop().unwrap(); changed = true }
445 } else { comps.push(comp) }
448 if comps.is_empty() && !is_abs {
452 comps.push(dot_static);
460 static dot_static: &'static [u8] = b".";
461 static dot_dot_static: &'static [u8] = b"..";
472 (s: $path:expr, $exp:expr) => (
475 assert!(path.as_str() == Some($exp));
478 (v: $path:expr, $exp:expr) => (
481 assert!(path.as_vec() == $exp);
488 let empty: &[u8] = [];
489 t!(v: Path::new(empty), b".");
490 t!(v: Path::new(b"/"), b"/");
491 t!(v: Path::new(b"a/b/c"), b"a/b/c");
492 t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
493 t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
494 let p = Path::new(b"a/b/c\xFF");
495 assert!(p.as_str() == None);
497 t!(s: Path::new(""), ".");
498 t!(s: Path::new("/"), "/");
499 t!(s: Path::new("hi"), "hi");
500 t!(s: Path::new("hi/"), "hi");
501 t!(s: Path::new("/lib"), "/lib");
502 t!(s: Path::new("/lib/"), "/lib");
503 t!(s: Path::new("hi/there"), "hi/there");
504 t!(s: Path::new("hi/there.txt"), "hi/there.txt");
506 t!(s: Path::new("hi/there/"), "hi/there");
507 t!(s: Path::new("hi/../there"), "there");
508 t!(s: Path::new("../hi/there"), "../hi/there");
509 t!(s: Path::new("/../hi/there"), "/hi/there");
510 t!(s: Path::new("foo/.."), ".");
511 t!(s: Path::new("/foo/.."), "/");
512 t!(s: Path::new("/foo/../.."), "/");
513 t!(s: Path::new("/foo/../../bar"), "/bar");
514 t!(s: Path::new("/./hi/./there/."), "/hi/there");
515 t!(s: Path::new("/./hi/./there/./.."), "/hi");
516 t!(s: Path::new("foo/../.."), "..");
517 t!(s: Path::new("foo/../../.."), "../..");
518 t!(s: Path::new("foo/../../bar"), "../bar");
520 assert_eq!(Path::new(b"foo/bar").into_vec().as_slice(), b"foo/bar");
521 assert_eq!(Path::new(b"/foo/../../bar").into_vec().as_slice(),
524 let p = Path::new(b"foo/bar\x80");
525 assert!(p.as_str() == None);
529 fn test_opt_paths() {
530 assert!(Path::new_opt(b"foo/bar\0") == None);
531 t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
532 assert!(Path::new_opt("foo/bar\0") == None);
533 t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
537 fn test_null_byte() {
539 let result = task::try(proc() {
540 Path::new(b"foo/bar\0")
542 assert!(result.is_err());
544 let result = task::try(proc() {
545 Path::new("test").set_filename(b"f\0o")
547 assert!(result.is_err());
549 let result = task::try(proc() {
550 Path::new("test").push(b"f\0o");
552 assert!(result.is_err());
556 fn test_display_str() {
558 ($path:expr, $disp:ident, $exp:expr) => (
560 let path = Path::new($path);
561 assert!(path.$disp().to_string().as_slice() == $exp);
565 t!("foo", display, "foo");
566 t!(b"foo\x80", display, "foo\uFFFD");
567 t!(b"foo\xFFbar", display, "foo\uFFFDbar");
568 t!(b"foo\xFF/bar", filename_display, "bar");
569 t!(b"foo/\xFFbar", filename_display, "\uFFFDbar");
570 t!(b"/", filename_display, "");
573 ($path:expr, $exp:expr) => (
575 let path = Path::new($path);
576 let mo = path.display().as_maybe_owned();
577 assert!(mo.as_slice() == $exp);
580 ($path:expr, $exp:expr, filename) => (
582 let path = Path::new($path);
583 let mo = path.filename_display().as_maybe_owned();
584 assert!(mo.as_slice() == $exp);
590 t!(b"foo\x80", "foo\uFFFD");
591 t!(b"foo\xFFbar", "foo\uFFFDbar");
592 t!(b"foo\xFF/bar", "bar", filename);
593 t!(b"foo/\xFFbar", "\uFFFDbar", filename);
594 t!(b"/", "", filename);
600 ($path:expr, $exp:expr, $expf:expr) => (
602 let path = Path::new($path);
603 let f = format!("{}", path.display());
604 assert!(f.as_slice() == $exp);
605 let f = format!("{}", path.filename_display());
606 assert!(f.as_slice() == $expf);
611 t!(b"foo", "foo", "foo");
612 t!(b"foo/bar", "foo/bar", "bar");
614 t!(b"foo\xFF", "foo\uFFFD", "foo\uFFFD");
615 t!(b"foo\xFF/bar", "foo\uFFFD/bar", "bar");
616 t!(b"foo/\xFFbar", "foo/\uFFFDbar", "\uFFFDbar");
617 t!(b"\xFFfoo/bar\xFF", "\uFFFDfoo/bar\uFFFD", "bar\uFFFD");
621 fn test_components() {
623 (s: $path:expr, $op:ident, $exp:expr) => (
626 let path = Path::new($path);
627 assert!(path.$op() == mem::transmute(($exp).as_bytes()));
631 (s: $path:expr, $op:ident, $exp:expr, opt) => (
633 let path = Path::new($path);
634 let left = path.$op().map(|x| str::from_utf8(x).unwrap());
635 assert!(left == $exp);
638 (v: $path:expr, $op:ident, $exp:expr) => (
642 let path = Path::new(arg);
643 assert!(path.$op() == mem::transmute($exp));
649 t!(v: b"a/b/c", filename, Some(b"c"));
650 t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
651 t!(v: b"a/b\xFF/c", filename, Some(b"c"));
652 t!(s: "a/b/c", filename, Some("c"), opt);
653 t!(s: "/a/b/c", filename, Some("c"), opt);
654 t!(s: "a", filename, Some("a"), opt);
655 t!(s: "/a", filename, Some("a"), opt);
656 t!(s: ".", filename, None, opt);
657 t!(s: "/", filename, None, opt);
658 t!(s: "..", filename, None, opt);
659 t!(s: "../..", filename, None, opt);
661 t!(v: b"a/b/c", dirname, b"a/b");
662 t!(v: b"a/b/c\xFF", dirname, b"a/b");
663 t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
664 t!(s: "a/b/c", dirname, "a/b");
665 t!(s: "/a/b/c", dirname, "/a/b");
666 t!(s: "a", dirname, ".");
667 t!(s: "/a", dirname, "/");
668 t!(s: ".", dirname, ".");
669 t!(s: "/", dirname, "/");
670 t!(s: "..", dirname, "..");
671 t!(s: "../..", dirname, "../..");
673 t!(v: b"hi/there.txt", filestem, Some(b"there"));
674 t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
675 t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
676 t!(s: "hi/there.txt", filestem, Some("there"), opt);
677 t!(s: "hi/there", filestem, Some("there"), opt);
678 t!(s: "there.txt", filestem, Some("there"), opt);
679 t!(s: "there", filestem, Some("there"), opt);
680 t!(s: ".", filestem, None, opt);
681 t!(s: "/", filestem, None, opt);
682 t!(s: "foo/.bar", filestem, Some(".bar"), opt);
683 t!(s: ".bar", filestem, Some(".bar"), opt);
684 t!(s: "..bar", filestem, Some("."), opt);
685 t!(s: "hi/there..txt", filestem, Some("there."), opt);
686 t!(s: "..", filestem, None, opt);
687 t!(s: "../..", filestem, None, opt);
689 t!(v: b"hi/there.txt", extension, Some(b"txt"));
690 t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
691 t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
692 let no: Option<&'static [u8]> = None;
693 t!(v: b"hi/there", extension, no);
694 t!(v: b"hi/there\x80", extension, no);
695 t!(s: "hi/there.txt", extension, Some("txt"), opt);
696 t!(s: "hi/there", extension, None, opt);
697 t!(s: "there.txt", extension, Some("txt"), opt);
698 t!(s: "there", extension, None, opt);
699 t!(s: ".", extension, None, opt);
700 t!(s: "/", extension, None, opt);
701 t!(s: "foo/.bar", extension, None, opt);
702 t!(s: ".bar", extension, None, opt);
703 t!(s: "..bar", extension, Some("bar"), opt);
704 t!(s: "hi/there..txt", extension, Some("txt"), opt);
705 t!(s: "..", extension, None, opt);
706 t!(s: "../..", extension, None, opt);
712 (s: $path:expr, $join:expr) => (
716 let mut p1 = Path::new(path);
719 assert!(p1 == p2.join(join));
724 t!(s: "a/b/c", "..");
725 t!(s: "/a/b/c", "d");
727 t!(s: "a/b", "/c/d");
731 fn test_push_path() {
733 (s: $path:expr, $push:expr, $exp:expr) => (
735 let mut p = Path::new($path);
736 let push = Path::new($push);
738 assert!(p.as_str() == Some($exp));
743 t!(s: "a/b/c", "d", "a/b/c/d");
744 t!(s: "/a/b/c", "d", "/a/b/c/d");
745 t!(s: "a/b", "c/d", "a/b/c/d");
746 t!(s: "a/b", "/c/d", "/c/d");
747 t!(s: "a/b", ".", "a/b");
748 t!(s: "a/b", "../c", "a/c");
752 fn test_push_many() {
754 (s: $path:expr, $push:expr, $exp:expr) => (
756 let mut p = Path::new($path);
758 assert!(p.as_str() == Some($exp));
761 (v: $path:expr, $push:expr, $exp:expr) => (
763 let mut p = Path::new($path);
765 assert!(p.as_vec() == $exp);
770 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
771 t!(s: "a/b/c", ["d", "/e"], "/e");
772 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
773 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
774 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
775 t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
776 t!(v: b"a/b/c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")], b"a/b/c/d/e");
782 (s: $path:expr, $left:expr, $right:expr) => (
784 let mut p = Path::new($path);
785 let result = p.pop();
786 assert!(p.as_str() == Some($left));
787 assert!(result == $right);
790 (b: $path:expr, $left:expr, $right:expr) => (
792 let mut p = Path::new($path);
793 let result = p.pop();
794 assert!(p.as_vec() == $left);
795 assert!(result == $right);
800 t!(b: b"a/b/c", b"a/b", true);
801 t!(b: b"a", b".", true);
802 t!(b: b".", b".", false);
803 t!(b: b"/a", b"/", true);
804 t!(b: b"/", b"/", false);
805 t!(b: b"a/b/c\x80", b"a/b", true);
806 t!(b: b"a/b\x80/c", b"a/b\x80", true);
807 t!(b: b"\xFF", b".", true);
808 t!(b: b"/\xFF", b"/", true);
809 t!(s: "a/b/c", "a/b", true);
810 t!(s: "a", ".", true);
811 t!(s: ".", ".", false);
812 t!(s: "/a", "/", true);
813 t!(s: "/", "/", false);
817 fn test_root_path() {
818 assert!(Path::new(b"a/b/c").root_path() == None);
819 assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
824 t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
825 t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
826 t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
827 t!(s: Path::new("a/b/c").join(".."), "a/b");
828 t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
829 t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
830 t!(s: Path::new("a/b").join("/c/d"), "/c/d");
831 t!(s: Path::new(".").join("a/b"), "a/b");
832 t!(s: Path::new("/").join("a/b"), "/a/b");
836 fn test_join_path() {
838 (s: $path:expr, $join:expr, $exp:expr) => (
840 let path = Path::new($path);
841 let join = Path::new($join);
842 let res = path.join(&join);
843 assert!(res.as_str() == Some($exp));
848 t!(s: "a/b/c", "..", "a/b");
849 t!(s: "/a/b/c", "d", "/a/b/c/d");
850 t!(s: "a/b", "c/d", "a/b/c/d");
851 t!(s: "a/b", "/c/d", "/c/d");
852 t!(s: ".", "a/b", "a/b");
853 t!(s: "/", "a/b", "/a/b");
857 fn test_join_many() {
859 (s: $path:expr, $join:expr, $exp:expr) => (
861 let path = Path::new($path);
862 let res = path.join_many($join);
863 assert!(res.as_str() == Some($exp));
866 (v: $path:expr, $join:expr, $exp:expr) => (
868 let path = Path::new($path);
869 let res = path.join_many($join);
870 assert!(res.as_vec() == $exp);
875 t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
876 t!(s: "a/b/c", ["..", "d"], "a/b/d");
877 t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
878 t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
879 t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
880 t!(v: b"a/b/c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")], b"a/b/c/d/e");
884 fn test_with_helpers() {
885 let empty: &[u8] = [];
887 t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
888 t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
889 t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
891 t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
892 t!(s: Path::new(".").with_filename("foo"), "foo");
893 t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
894 t!(s: Path::new("/").with_filename("foo"), "/foo");
895 t!(s: Path::new("/a").with_filename("foo"), "/foo");
896 t!(s: Path::new("foo").with_filename("bar"), "bar");
897 t!(s: Path::new("/").with_filename("foo/"), "/foo");
898 t!(s: Path::new("/a").with_filename("foo/"), "/foo");
899 t!(s: Path::new("a/b/c").with_filename(""), "a/b");
900 t!(s: Path::new("a/b/c").with_filename("."), "a/b");
901 t!(s: Path::new("a/b/c").with_filename(".."), "a");
902 t!(s: Path::new("/a").with_filename(""), "/");
903 t!(s: Path::new("foo").with_filename(""), ".");
904 t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
905 t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
906 t!(s: Path::new("..").with_filename("foo"), "../foo");
907 t!(s: Path::new("../..").with_filename("foo"), "../../foo");
908 t!(s: Path::new("..").with_filename(""), "..");
909 t!(s: Path::new("../..").with_filename(""), "../..");
911 t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
912 b"hi/there\x80.exe");
913 t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
915 t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
916 b"hi/there\x80.\xFF");
917 t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
918 t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
919 t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
920 t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
921 t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
922 t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
923 t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
924 t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
925 t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
926 t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
927 t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
928 t!(s: Path::new("/").with_extension("txt"), "/");
929 t!(s: Path::new("/").with_extension("."), "/");
930 t!(s: Path::new("/").with_extension(".."), "/");
931 t!(s: Path::new(".").with_extension("txt"), ".");
937 (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
941 let mut p1 = Path::new(path);
943 let p2 = Path::new(path);
944 assert!(p1 == p2.$with(arg));
947 (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
951 let mut p1 = Path::new(path);
953 let p2 = Path::new(path);
954 assert!(p1 == p2.$with(arg));
959 t!(v: b"a/b/c", set_filename, with_filename, b"d");
960 t!(v: b"/", set_filename, with_filename, b"foo");
961 t!(v: b"\x80", set_filename, with_filename, b"\xFF");
962 t!(s: "a/b/c", set_filename, with_filename, "d");
963 t!(s: "/", set_filename, with_filename, "foo");
964 t!(s: ".", set_filename, with_filename, "foo");
965 t!(s: "a/b", set_filename, with_filename, "");
966 t!(s: "a", set_filename, with_filename, "");
968 t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
969 t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
970 t!(s: "hi/there.txt", set_extension, with_extension, "exe");
971 t!(s: "hi/there.", set_extension, with_extension, "txt");
972 t!(s: "hi/there", set_extension, with_extension, "txt");
973 t!(s: "hi/there.txt", set_extension, with_extension, "");
974 t!(s: "hi/there", set_extension, with_extension, "");
975 t!(s: ".", set_extension, with_extension, "txt");
981 (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
985 let filename = $filename;
986 assert!(path.filename_str() == filename,
987 "{}.filename_str(): Expected `{:?}`, found {:?}",
988 path.as_str().unwrap(), filename, path.filename_str());
989 let dirname = $dirname;
990 assert!(path.dirname_str() == dirname,
991 "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
992 path.as_str().unwrap(), dirname, path.dirname_str());
993 let filestem = $filestem;
994 assert!(path.filestem_str() == filestem,
995 "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
996 path.as_str().unwrap(), filestem, path.filestem_str());
998 assert!(path.extension_str() == mem::transmute(ext),
999 "`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
1000 path.as_str().unwrap(), ext, path.extension_str());
1004 (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
1008 assert!(path.filename() == mem::transmute($filename));
1009 assert!(path.dirname() == mem::transmute($dirname));
1010 assert!(path.filestem() == mem::transmute($filestem));
1011 assert!(path.extension() == mem::transmute($ext));
1017 let no: Option<&'static str> = None;
1018 t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), no);
1019 t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), no);
1020 t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
1021 Some(b"there"), Some(b"\xFF"));
1022 t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), no);
1023 t!(s: Path::new("."), None, Some("."), None, no);
1024 t!(s: Path::new("/"), None, Some("/"), None, no);
1025 t!(s: Path::new(".."), None, Some(".."), None, no);
1026 t!(s: Path::new("../.."), None, Some("../.."), None, no);
1027 t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
1028 Some("there"), Some("txt"));
1029 t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), no);
1030 t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
1031 Some("there"), Some(""));
1032 t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), no);
1033 t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
1034 Some("."), Some("there"));
1035 t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, no);
1036 t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
1037 t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), no);
1038 t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), no);
1042 fn test_dir_path() {
1043 t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
1044 t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
1045 t!(s: Path::new("hi/there").dir_path(), "hi");
1046 t!(s: Path::new("hi").dir_path(), ".");
1047 t!(s: Path::new("/hi").dir_path(), "/");
1048 t!(s: Path::new("/").dir_path(), "/");
1049 t!(s: Path::new("..").dir_path(), "..");
1050 t!(s: Path::new("../..").dir_path(), "../..");
1054 fn test_is_absolute() {
1056 (s: $path:expr, $abs:expr, $rel:expr) => (
1058 let path = Path::new($path);
1059 assert_eq!(path.is_absolute(), $abs);
1060 assert_eq!(path.is_relative(), $rel);
1064 t!(s: "a/b/c", false, true);
1065 t!(s: "/a/b/c", true, false);
1066 t!(s: "a", false, true);
1067 t!(s: "/a", true, false);
1068 t!(s: ".", false, true);
1069 t!(s: "/", true, false);
1070 t!(s: "..", false, true);
1071 t!(s: "../..", false, true);
1075 fn test_is_ancestor_of() {
1077 (s: $path:expr, $dest:expr, $exp:expr) => (
1079 let path = Path::new($path);
1080 let dest = Path::new($dest);
1081 assert_eq!(path.is_ancestor_of(&dest), $exp);
1086 t!(s: "a/b/c", "a/b/c/d", true);
1087 t!(s: "a/b/c", "a/b/c", true);
1088 t!(s: "a/b/c", "a/b", false);
1089 t!(s: "/a/b/c", "/a/b/c", true);
1090 t!(s: "/a/b", "/a/b/c", true);
1091 t!(s: "/a/b/c/d", "/a/b/c", false);
1092 t!(s: "/a/b", "a/b/c", false);
1093 t!(s: "a/b", "/a/b/c", false);
1094 t!(s: "a/b/c", "a/b/d", false);
1095 t!(s: "../a/b/c", "a/b/c", false);
1096 t!(s: "a/b/c", "../a/b/c", false);
1097 t!(s: "a/b/c", "a/b/cd", false);
1098 t!(s: "a/b/cd", "a/b/c", false);
1099 t!(s: "../a/b", "../a/b/c", true);
1100 t!(s: ".", "a/b", true);
1101 t!(s: ".", ".", true);
1102 t!(s: "/", "/", true);
1103 t!(s: "/", "/a/b", true);
1104 t!(s: "..", "a/b", true);
1105 t!(s: "../..", "a/b", true);
1109 fn test_ends_with_path() {
1111 (s: $path:expr, $child:expr, $exp:expr) => (
1113 let path = Path::new($path);
1114 let child = Path::new($child);
1115 assert_eq!(path.ends_with_path(&child), $exp);
1118 (v: $path:expr, $child:expr, $exp:expr) => (
1120 let path = Path::new($path);
1121 let child = Path::new($child);
1122 assert_eq!(path.ends_with_path(&child), $exp);
1127 t!(s: "a/b/c", "c", true);
1128 t!(s: "a/b/c", "d", false);
1129 t!(s: "foo/bar/quux", "bar", false);
1130 t!(s: "foo/bar/quux", "barquux", false);
1131 t!(s: "a/b/c", "b/c", true);
1132 t!(s: "a/b/c", "a/b/c", true);
1133 t!(s: "a/b/c", "foo/a/b/c", false);
1134 t!(s: "/a/b/c", "a/b/c", true);
1135 t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1136 t!(s: "/a/b/c", "foo/a/b/c", false);
1137 t!(s: "a/b/c", "", false);
1138 t!(s: "", "", true);
1139 t!(s: "/a/b/c", "d/e/f", false);
1140 t!(s: "a/b/c", "a/b", false);
1141 t!(s: "a/b/c", "b", false);
1142 t!(v: b"a/b/c", b"b/c", true);
1143 t!(v: b"a/b/\xFF", b"\xFF", true);
1144 t!(v: b"a/b/\xFF", b"b/\xFF", true);
1148 fn test_path_relative_from() {
1150 (s: $path:expr, $other:expr, $exp:expr) => (
1152 let path = Path::new($path);
1153 let other = Path::new($other);
1154 let res = path.path_relative_from(&other);
1155 assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1160 t!(s: "a/b/c", "a/b", Some("c"));
1161 t!(s: "a/b/c", "a/b/d", Some("../c"));
1162 t!(s: "a/b/c", "a/b/c/d", Some(".."));
1163 t!(s: "a/b/c", "a/b/c", Some("."));
1164 t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1165 t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1166 t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1167 t!(s: "a/b/c", "/a/b/c", None);
1168 t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1169 t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1170 t!(s: "/a/b/c", "/a/b", Some("c"));
1171 t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1172 t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1173 t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1174 t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1175 t!(s: ".", "a", Some(".."));
1176 t!(s: ".", "a/b", Some("../.."));
1177 t!(s: ".", ".", Some("."));
1178 t!(s: "a", ".", Some("a"));
1179 t!(s: "a/b", ".", Some("a/b"));
1180 t!(s: "..", ".", Some(".."));
1181 t!(s: "a/b/c", "a/b/c", Some("."));
1182 t!(s: "/a/b/c", "/a/b/c", Some("."));
1183 t!(s: "/", "/", Some("."));
1184 t!(s: "/", ".", Some("/"));
1185 t!(s: "../../a", "b", Some("../../../a"));
1186 t!(s: "a", "../../b", None);
1187 t!(s: "../../a", "../../b", Some("../a"));
1188 t!(s: "../../a", "../../a/b", Some(".."));
1189 t!(s: "../../a/b", "../../a", Some("b"));
1193 fn test_components_iter() {
1195 (s: $path:expr, $exp:expr) => (
1197 let path = Path::new($path);
1198 let comps = path.components().collect::<Vec<&[u8]>>();
1199 let exp: &[&str] = $exp;
1200 let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1201 assert!(comps == exps, "components: Expected {:?}, found {:?}",
1203 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1204 let exps = exps.move_iter().rev().collect::<Vec<&[u8]>>();
1205 assert!(comps == exps, "rev_components: Expected {:?}, found {:?}",
1209 (b: $arg:expr, [$($exp:expr),*]) => (
1211 let path = Path::new($arg);
1212 let comps = path.components().collect::<Vec<&[u8]>>();
1213 let exp: &[&[u8]] = [$($exp),*];
1214 assert_eq!(comps.as_slice(), exp);
1215 let comps = path.components().rev().collect::<Vec<&[u8]>>();
1216 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1217 assert_eq!(comps, exp)
1222 t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1223 t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1224 t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1225 t!(s: "a/b/c", ["a", "b", "c"]);
1226 t!(s: "a/b/d", ["a", "b", "d"]);
1227 t!(s: "a/b/cd", ["a", "b", "cd"]);
1228 t!(s: "/a/b/c", ["a", "b", "c"]);
1233 t!(s: "..", [".."]);
1234 t!(s: "../..", ["..", ".."]);
1235 t!(s: "../../foo", ["..", "..", "foo"]);
1239 fn test_str_components() {
1241 (b: $arg:expr, $exp:expr) => (
1243 let path = Path::new($arg);
1244 let comps = path.str_components().collect::<Vec<Option<&str>>>();
1245 let exp: &[Option<&str>] = $exp;
1246 assert_eq!(comps.as_slice(), exp);
1247 let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1248 let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1249 assert_eq!(comps, exp);
1254 t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1255 t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1256 t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1257 // str_components is a wrapper around components, so no need to do
1258 // the full set of tests
1265 use self::test::Bencher;
1270 fn join_home_dir(b: &mut Bencher) {
1271 let posix_path = Path::new("/");
1273 posix_path.join("home");
1278 fn join_abs_path_home_dir(b: &mut Bencher) {
1279 let posix_path = Path::new("/");
1281 posix_path.join("/home");
1286 fn join_many_home_dir(b: &mut Bencher) {
1287 let posix_path = Path::new("/");
1289 posix_path.join_many(&["home"]);
1294 fn join_many_abs_path_home_dir(b: &mut Bencher) {
1295 let posix_path = Path::new("/");
1297 posix_path.join_many(&["/home"]);
1302 fn push_home_dir(b: &mut Bencher) {
1303 let mut posix_path = Path::new("/");
1305 posix_path.push("home");
1310 fn push_abs_path_home_dir(b: &mut Bencher) {
1311 let mut posix_path = Path::new("/");
1313 posix_path.push("/home");
1318 fn push_many_home_dir(b: &mut Bencher) {
1319 let mut posix_path = Path::new("/");
1321 posix_path.push_many(&["home"]);
1326 fn push_many_abs_path_home_dir(b: &mut Bencher) {
1327 let mut posix_path = Path::new("/");
1329 posix_path.push_many(&["/home"]);
1334 fn ends_with_path_home_dir(b: &mut Bencher) {
1335 let posix_home_path = Path::new("/home");
1337 posix_home_path.ends_with_path(&Path::new("home"));
1342 fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1343 let posix_home_path = Path::new("/home");
1345 posix_home_path.ends_with_path(&Path::new("jome"));
1350 fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1351 let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1352 let mut sub = path.clone();
1355 path.is_ancestor_of(&sub);
1360 fn path_relative_from_forward(b: &mut Bencher) {
1361 let path = Path::new("/a/b/c");
1362 let mut other = path.clone();
1365 path.path_relative_from(&other);
1370 fn path_relative_from_same_level(b: &mut Bencher) {
1371 let path = Path::new("/a/b/c");
1372 let mut other = path.clone();
1376 path.path_relative_from(&other);
1381 fn path_relative_from_backward(b: &mut Bencher) {
1382 let path = Path::new("/a/b");
1383 let mut other = path.clone();
1386 path.path_relative_from(&other);