diff options
| author | bors <bors@rust-lang.org> | 2013-07-20 02:16:41 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-20 02:16:41 -0700 |
| commit | fdbd56ca38c4a58377056c63e0b03b0d4e131f2c (patch) | |
| tree | 7c6cad3f140361d7939bc3d45c8c4ff6a14aefcb /src/libstd | |
| parent | 098709aa638bb29998b5e6f2700997b13c2c6712 (diff) | |
| parent | e39ea2a5c880b252abbf37dade7a9b7ce7b2cb40 (diff) | |
| download | rust-fdbd56ca38c4a58377056c63e0b03b0d4e131f2c.tar.gz rust-fdbd56ca38c4a58377056c63e0b03b0d4e131f2c.zip | |
auto merge of #7419 : catamorphism/rust/default-package, r=graydon
r? @brson `rustpkg build`, if executed in a package source directory inside a workspace, will now build that package. By "inside a workspace" I mean that the parent directory has to be called `src`, and rustpkg will create a `build` directory in .. if there isn't already one. Same goes for `rustpkg install` and `rustpkg clean`. For the time being, `rustpkg build` (etc.) will still error out if you run it inside a directory whose parent isn't called `src`. I'm not sure whether or not it's desirable to have it do something in a non-workspace directory.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index fe298931d42..3ec3407b1cc 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -122,6 +122,9 @@ pub trait GenericPath { /// Returns `true` if `self` is an absolute path. fn is_absolute(&self) -> bool; + + /// True if `self` is an ancestor of `other`. See `test_is_ancestor_of` for examples + fn is_ancestor_of(&self, (&Self)) -> bool; } #[cfg(target_os = "linux")] @@ -698,6 +701,15 @@ impl GenericPath for PosixPath { fn is_absolute(&self) -> bool { self.is_absolute } + + fn is_ancestor_of(&self, other: &PosixPath) -> bool { + debug!("%s / %s %? %?", self.to_str(), other.to_str(), self.is_absolute, + self.components.len()); + self == other || + (!other.components.is_empty() && !(self.components.is_empty() && !self.is_absolute) && + self.is_ancestor_of(&other.pop())) + } + } @@ -974,8 +986,13 @@ impl GenericPath for WindowsPath { fn is_absolute(&self) -> bool { self.is_absolute } -} + fn is_ancestor_of(&self, other: &WindowsPath) -> bool { + self == other || + (!other.components.is_empty() && !(self.components.is_empty() && !self.is_absolute) && + self.is_ancestor_of(&other.pop())) + } +} pub fn normalize(components: &[~str]) -> ~[~str] { let mut cs = ~[]; @@ -1290,4 +1307,27 @@ mod tests { assert_eq!(WindowsPath("C:\\COM1.TXT").is_restricted(), true); assert_eq!(WindowsPath("c:\\prn.exe").is_restricted(), true); } + + #[test] + fn test_is_ancestor_of() { + assert!(&PosixPath("/a/b").is_ancestor_of(&PosixPath("/a/b/c/d"))); + assert!(!&PosixPath("/a/b/c/d").is_ancestor_of(&PosixPath("/a/b"))); + assert!(!&PosixPath("/a/b").is_ancestor_of(&PosixPath("/c/d"))); + assert!(&PosixPath("/a/b").is_ancestor_of(&PosixPath("/a/b/c/d"))); + assert!(&PosixPath("/").is_ancestor_of(&PosixPath("/a/b/c"))); + assert!(!&PosixPath("/").is_ancestor_of(&PosixPath(""))); + assert!(!&PosixPath("/a/b/c").is_ancestor_of(&PosixPath(""))); + assert!(!&PosixPath("").is_ancestor_of(&PosixPath("/a/b/c"))); + + assert!(&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\a\\b\\c\\d"))); + assert!(!&WindowsPath("C:\\a\\b\\c\\d").is_ancestor_of(&WindowsPath("C:\\a\\b"))); + assert!(!&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\c\\d"))); + assert!(&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\a\\b\\c\\d"))); + assert!(&WindowsPath("C:\\").is_ancestor_of(&WindowsPath("C:\\a\\b\\c"))); + assert!(!&WindowsPath("C:\\").is_ancestor_of(&WindowsPath(""))); + assert!(!&WindowsPath("C:\\a\\b\\c").is_ancestor_of(&WindowsPath(""))); + assert!(!&WindowsPath("").is_ancestor_of(&WindowsPath("C:\\a\\b\\c"))); + + } + } |
