diff options
| author | bors <bors@rust-lang.org> | 2013-09-25 12:45:54 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-09-25 12:45:54 -0700 |
| commit | af25f58ac3da45899ed65b3af965150c8a90dcda (patch) | |
| tree | eca071107edf532e600606f68f602e7e3d8f56d4 /src/libstd | |
| parent | 0186473bd2ec34a56496c7b513ee380cbf30a6a3 (diff) | |
| parent | 667adad26f9cc1cfa4eeba8aee15035da7544f8c (diff) | |
| download | rust-af25f58ac3da45899ed65b3af965150c8a90dcda.tar.gz rust-af25f58ac3da45899ed65b3af965150c8a90dcda.zip | |
auto merge of #9498 : catamorphism/rust/rust-path-hack-fix, r=cmr,metajack
r? @metajack
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 336284963a2..af2565ec67a 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -233,6 +233,21 @@ pub trait GenericPath : Clone + Eq + ToStr { result } + + /// Returns `true` iff `child` is a suffix of `parent`. See the test + /// case for examples. + fn is_parent_of(&self, child: &Self) -> bool { + if !self.is_absolute() || child.is_absolute() + || self.components().len() < child.components().len() + || self.components().is_empty() { + return false; + } + let child_components = child.components().len(); + let parent_components = self.components().len(); + let to_drop = self.components().len() - child_components; + self.components().slice(to_drop, parent_components) == child.components() + } + fn components<'a>(&'a self) -> &'a [~str]; } @@ -1450,4 +1465,43 @@ mod tests { } + + #[test] + fn test_is_parent_of() { + fn is_parent_of_pp(p: &PosixPath, q: &PosixPath) -> bool { + p.is_parent_of(q) + } + + assert!(is_parent_of_pp(&PosixPath("/a/b/c/d/e"), &PosixPath("c/d/e"))); + assert!(!is_parent_of_pp(&PosixPath("a/b/c/d/e"), &PosixPath("c/d/e"))); + assert!(!is_parent_of_pp(&PosixPath("/a/b/c/d/e"), &PosixPath("/c/d/e"))); + assert!(!is_parent_of_pp(&PosixPath(""), &PosixPath(""))); + assert!(!is_parent_of_pp(&PosixPath(""), &PosixPath("a/b/c"))); + assert!(is_parent_of_pp(&PosixPath("/a/b/c"), &PosixPath(""))); + assert!(is_parent_of_pp(&PosixPath("/a/b/c"), &PosixPath("a/b/c"))); + assert!(!is_parent_of_pp(&PosixPath("/a/b/c"), &PosixPath("d/e/f"))); + + fn is_parent_of_wp(p: &WindowsPath, q: &WindowsPath) -> bool { + p.is_parent_of(q) + } + + let abcde = WindowsPath("C:\\a\\b\\c\\d\\e"); + let rel_abcde = WindowsPath("a\\b\\c\\d\\e"); + let cde = WindowsPath("c\\d\\e"); + let slashcde = WindowsPath("C:\\c\\d\\e"); + let empty = WindowsPath(""); + let abc = WindowsPath("C:\\a\\b\\c"); + let rel_abc = WindowsPath("a\\b\\c"); + let def = WindowsPath("d\\e\\f"); + + assert!(is_parent_of_wp(&abcde, &cde)); + assert!(!is_parent_of_wp(&rel_abcde, &cde)); + assert!(!is_parent_of_wp(&abcde, &slashcde)); + assert!(!is_parent_of_wp(&empty, &empty)); + assert!(!is_parent_of_wp(&empty, &rel_abc)); + assert!(is_parent_of_wp(&abc, &empty)); + assert!(is_parent_of_wp(&abc, &rel_abc)); + assert!(!is_parent_of_wp(&abc, &def)); + } + } |
