Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement vec.starts_with()/ends_with() #9907

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/librustc/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 2 additions & 6 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down
25 changes: 6 additions & 19 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,17 +827,6 @@ pub fn eq(a: &~str, b: &~str) -> bool {
eq_slice(*a, *b)
}

/*
Section: Searching
*/

// Utility used by various searching functions
fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
let mut i = at;
for c in needle.byte_iter() { if haystack[i] != c { return false; } i += 1u; }
return true;
}

/*
Section: Misc
*/
Expand Down Expand Up @@ -2018,18 +2007,16 @@ impl<'self> StrSlice<'self> for &'self str {
}
}

#[inline]
fn starts_with<'a>(&self, needle: &'a str) -> bool {
let (self_len, needle_len) = (self.len(), needle.len());
if needle_len == 0u { true }
else if needle_len > self_len { false }
else { match_at(*self, needle, 0u) }
let n = needle.len();
self.len() >= n && needle == self.slice_to(n)
}

#[inline]
fn ends_with(&self, needle: &str) -> bool {
let (self_len, needle_len) = (self.len(), needle.len());
if needle_len == 0u { true }
else if needle_len > self_len { false }
else { match_at(*self, needle, self_len - needle_len) }
let (m, n) = (self.len(), needle.len());
m >= n && needle == self.slice_from(m - n)
}

fn escape_default(&self) -> ~str {
Expand Down
50 changes: 48 additions & 2 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -1186,9 +1192,21 @@ impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
self.iter().rposition(|x| *x == *t)
}

#[inline]
fn contains(&self, x: &T) -> bool {
for elt in self.iter() { if *x == *elt { return true; } }
false
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)
}
}

Expand Down Expand Up @@ -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)]
Expand Down
5 changes: 1 addition & 4 deletions src/test/run-pass/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down