Implement new methods vec.starts_with()/vec.ends_with()
This commit is contained in:
parent
d8f82c8e43
commit
2fcb53493d
@ -189,12 +189,9 @@ mod test {
|
||||
let mut d = Path::new(env!("CFG_PREFIX"));
|
||||
d.push("lib/rustc/triple/lib");
|
||||
debug2!("test_prefix_path: {} vs. {}",
|
||||
res.to_str(),
|
||||
res,
|
||||
d.display());
|
||||
assert!(ends_with(res.as_bytes(), d.as_vec()));
|
||||
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
|
||||
v.len() >= needle.len() && v.slice_from(v.len()-needle.len()) == needle
|
||||
}
|
||||
assert!(res.as_bytes().ends_with(d.as_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -217,10 +217,6 @@ fn is_read_only(p: &Path) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
|
||||
v.len() >= needle.len() && v.slice_from(v.len() - needle.len()) == needle
|
||||
}
|
||||
|
||||
fn test_sysroot() -> Path {
|
||||
// Totally gross hack but it's just for test cases.
|
||||
// Infer the sysroot from the exe name and pray that it's right.
|
||||
@ -747,7 +743,7 @@ fn test_package_version() {
|
||||
&ws) {
|
||||
Some(p) => {
|
||||
let suffix = format!("0.4{}", os::consts::DLL_SUFFIX);
|
||||
ends_with(p.as_vec(), suffix.as_bytes())
|
||||
p.as_vec().ends_with(suffix.as_bytes())
|
||||
}
|
||||
None => false
|
||||
});
|
||||
@ -785,7 +781,7 @@ fn test_package_request_version() {
|
||||
Some(p) => {
|
||||
debug2!("installed: {}", p.display());
|
||||
let suffix = format!("0.3{}", os::consts::DLL_SUFFIX);
|
||||
ends_with(p.as_vec(), suffix.as_bytes())
|
||||
p.as_vec().ends_with(suffix.as_bytes())
|
||||
}
|
||||
None => false
|
||||
});
|
||||
|
@ -1173,6 +1173,12 @@ pub trait ImmutableEqVector<T:Eq> {
|
||||
|
||||
/// Return true if a vector contains an element with the given value
|
||||
fn contains(&self, x: &T) -> bool;
|
||||
|
||||
/// Returns true if `needle` is a prefix of the vector.
|
||||
fn starts_with(&self, needle: &[T]) -> bool;
|
||||
|
||||
/// Returns true if `needle` is a suffix of the vector.
|
||||
fn ends_with(&self, needle: &[T]) -> bool;
|
||||
}
|
||||
|
||||
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
|
||||
@ -1190,6 +1196,18 @@ impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
|
||||
fn contains(&self, x: &T) -> bool {
|
||||
self.iter().any(|elt| *x == *elt)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn starts_with(&self, needle: &[T]) -> bool {
|
||||
let n = needle.len();
|
||||
self.len() >= n && needle == self.slice_to(n)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn ends_with(&self, needle: &[T]) -> bool {
|
||||
let (m, n) = (self.len(), needle.len());
|
||||
m >= n && needle == self.slice_from(m - n)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension methods for vectors containing `TotalOrd` elements.
|
||||
@ -3828,6 +3846,34 @@ mod tests {
|
||||
assert_eq!(xs.capacity(), 100);
|
||||
assert_eq!(xs, range(0, 100).to_owned_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_starts_with() {
|
||||
assert!(bytes!("foobar").starts_with(bytes!("foo")));
|
||||
assert!(!bytes!("foobar").starts_with(bytes!("oob")));
|
||||
assert!(!bytes!("foobar").starts_with(bytes!("bar")));
|
||||
assert!(!bytes!("foo").starts_with(bytes!("foobar")));
|
||||
assert!(!bytes!("bar").starts_with(bytes!("foobar")));
|
||||
assert!(bytes!("foobar").starts_with(bytes!("foobar")));
|
||||
let empty: &[u8] = [];
|
||||
assert!(empty.starts_with(empty));
|
||||
assert!(!empty.starts_with(bytes!("foo")));
|
||||
assert!(bytes!("foobar").starts_with(empty));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ends_with() {
|
||||
assert!(bytes!("foobar").ends_with(bytes!("bar")));
|
||||
assert!(!bytes!("foobar").ends_with(bytes!("oba")));
|
||||
assert!(!bytes!("foobar").ends_with(bytes!("foo")));
|
||||
assert!(!bytes!("foo").ends_with(bytes!("foobar")));
|
||||
assert!(!bytes!("bar").ends_with(bytes!("foobar")));
|
||||
assert!(bytes!("foobar").ends_with(bytes!("foobar")));
|
||||
let empty: &[u8] = [];
|
||||
assert!(empty.ends_with(empty));
|
||||
assert!(!empty.ends_with(bytes!("foo")));
|
||||
assert!(bytes!("foobar").ends_with(empty));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -30,13 +30,10 @@ fn test_tempdir() {
|
||||
let path = {
|
||||
let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
|
||||
let p = p.path();
|
||||
assert!(ends_with(p.as_vec(), bytes!("foobar")));
|
||||
assert!(p.as_vec().ends_with(bytes!("foobar")));
|
||||
p.clone()
|
||||
};
|
||||
assert!(!os::path_exists(&path));
|
||||
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
|
||||
v.len() >= needle.len() && v.slice_from(v.len()-needle.len()) == needle
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rm_tempdir() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user