Skip to content

Commit d4a3238

Browse files
committed
auto merge of #9907 : kballard/rust/vec_ends_with, r=alexcrichton
2 parents 2cb96a4 + 87a2d03 commit d4a3238

File tree

5 files changed

+59
-36
lines changed

5 files changed

+59
-36
lines changed

src/librustc/back/rpath.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,9 @@ mod test {
189189
let mut d = Path::new(env!("CFG_PREFIX"));
190190
d.push("lib/rustc/triple/lib");
191191
debug2!("test_prefix_path: {} vs. {}",
192-
res.to_str(),
192+
res,
193193
d.display());
194-
assert!(ends_with(res.as_bytes(), d.as_vec()));
195-
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
196-
v.len() >= needle.len() && v.slice_from(v.len()-needle.len()) == needle
197-
}
194+
assert!(res.as_bytes().ends_with(d.as_vec()));
198195
}
199196
200197
#[test]

src/librustpkg/tests.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,6 @@ fn is_read_only(p: &Path) -> bool {
217217
}
218218
}
219219

220-
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
221-
v.len() >= needle.len() && v.slice_from(v.len() - needle.len()) == needle
222-
}
223-
224220
fn test_sysroot() -> Path {
225221
// Totally gross hack but it's just for test cases.
226222
// Infer the sysroot from the exe name and pray that it's right.
@@ -747,7 +743,7 @@ fn test_package_version() {
747743
&ws) {
748744
Some(p) => {
749745
let suffix = format!("0.4{}", os::consts::DLL_SUFFIX);
750-
ends_with(p.as_vec(), suffix.as_bytes())
746+
p.as_vec().ends_with(suffix.as_bytes())
751747
}
752748
None => false
753749
});
@@ -785,7 +781,7 @@ fn test_package_request_version() {
785781
Some(p) => {
786782
debug2!("installed: {}", p.display());
787783
let suffix = format!("0.3{}", os::consts::DLL_SUFFIX);
788-
ends_with(p.as_vec(), suffix.as_bytes())
784+
p.as_vec().ends_with(suffix.as_bytes())
789785
}
790786
None => false
791787
});

src/libstd/str.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -827,17 +827,6 @@ pub fn eq(a: &~str, b: &~str) -> bool {
827827
eq_slice(*a, *b)
828828
}
829829

830-
/*
831-
Section: Searching
832-
*/
833-
834-
// Utility used by various searching functions
835-
fn match_at<'a,'b>(haystack: &'a str, needle: &'b str, at: uint) -> bool {
836-
let mut i = at;
837-
for c in needle.byte_iter() { if haystack[i] != c { return false; } i += 1u; }
838-
return true;
839-
}
840-
841830
/*
842831
Section: Misc
843832
*/
@@ -2018,18 +2007,16 @@ impl<'self> StrSlice<'self> for &'self str {
20182007
}
20192008
}
20202009
2010+
#[inline]
20212011
fn starts_with<'a>(&self, needle: &'a str) -> bool {
2022-
let (self_len, needle_len) = (self.len(), needle.len());
2023-
if needle_len == 0u { true }
2024-
else if needle_len > self_len { false }
2025-
else { match_at(*self, needle, 0u) }
2012+
let n = needle.len();
2013+
self.len() >= n && needle == self.slice_to(n)
20262014
}
20272015
2016+
#[inline]
20282017
fn ends_with(&self, needle: &str) -> bool {
2029-
let (self_len, needle_len) = (self.len(), needle.len());
2030-
if needle_len == 0u { true }
2031-
else if needle_len > self_len { false }
2032-
else { match_at(*self, needle, self_len - needle_len) }
2018+
let (m, n) = (self.len(), needle.len());
2019+
m >= n && needle == self.slice_from(m - n)
20332020
}
20342021
20352022
fn escape_default(&self) -> ~str {

src/libstd/vec.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,12 @@ pub trait ImmutableEqVector<T:Eq> {
11731173

11741174
/// Return true if a vector contains an element with the given value
11751175
fn contains(&self, x: &T) -> bool;
1176+
1177+
/// Returns true if `needle` is a prefix of the vector.
1178+
fn starts_with(&self, needle: &[T]) -> bool;
1179+
1180+
/// Returns true if `needle` is a suffix of the vector.
1181+
fn ends_with(&self, needle: &[T]) -> bool;
11761182
}
11771183

11781184
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
@@ -1186,9 +1192,21 @@ impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
11861192
self.iter().rposition(|x| *x == *t)
11871193
}
11881194

1195+
#[inline]
11891196
fn contains(&self, x: &T) -> bool {
1190-
for elt in self.iter() { if *x == *elt { return true; } }
1191-
false
1197+
self.iter().any(|elt| *x == *elt)
1198+
}
1199+
1200+
#[inline]
1201+
fn starts_with(&self, needle: &[T]) -> bool {
1202+
let n = needle.len();
1203+
self.len() >= n && needle == self.slice_to(n)
1204+
}
1205+
1206+
#[inline]
1207+
fn ends_with(&self, needle: &[T]) -> bool {
1208+
let (m, n) = (self.len(), needle.len());
1209+
m >= n && needle == self.slice_from(m - n)
11921210
}
11931211
}
11941212

@@ -3828,6 +3846,34 @@ mod tests {
38283846
assert_eq!(xs.capacity(), 100);
38293847
assert_eq!(xs, range(0, 100).to_owned_vec());
38303848
}
3849+
3850+
#[test]
3851+
fn test_starts_with() {
3852+
assert!(bytes!("foobar").starts_with(bytes!("foo")));
3853+
assert!(!bytes!("foobar").starts_with(bytes!("oob")));
3854+
assert!(!bytes!("foobar").starts_with(bytes!("bar")));
3855+
assert!(!bytes!("foo").starts_with(bytes!("foobar")));
3856+
assert!(!bytes!("bar").starts_with(bytes!("foobar")));
3857+
assert!(bytes!("foobar").starts_with(bytes!("foobar")));
3858+
let empty: &[u8] = [];
3859+
assert!(empty.starts_with(empty));
3860+
assert!(!empty.starts_with(bytes!("foo")));
3861+
assert!(bytes!("foobar").starts_with(empty));
3862+
}
3863+
3864+
#[test]
3865+
fn test_ends_with() {
3866+
assert!(bytes!("foobar").ends_with(bytes!("bar")));
3867+
assert!(!bytes!("foobar").ends_with(bytes!("oba")));
3868+
assert!(!bytes!("foobar").ends_with(bytes!("foo")));
3869+
assert!(!bytes!("foo").ends_with(bytes!("foobar")));
3870+
assert!(!bytes!("bar").ends_with(bytes!("foobar")));
3871+
assert!(bytes!("foobar").ends_with(bytes!("foobar")));
3872+
let empty: &[u8] = [];
3873+
assert!(empty.ends_with(empty));
3874+
assert!(!empty.ends_with(bytes!("foo")));
3875+
assert!(bytes!("foobar").ends_with(empty));
3876+
}
38313877
}
38323878

38333879
#[cfg(test)]

src/test/run-pass/tempfile.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ fn test_tempdir() {
3030
let path = {
3131
let p = TempDir::new_in(&Path::new("."), "foobar").unwrap();
3232
let p = p.path();
33-
assert!(ends_with(p.as_vec(), bytes!("foobar")));
33+
assert!(p.as_vec().ends_with(bytes!("foobar")));
3434
p.clone()
3535
};
3636
assert!(!os::path_exists(&path));
37-
fn ends_with(v: &[u8], needle: &[u8]) -> bool {
38-
v.len() >= needle.len() && v.slice_from(v.len()-needle.len()) == needle
39-
}
4037
}
4138

4239
fn test_rm_tempdir() {

0 commit comments

Comments
 (0)