Skip to content

Commit 2fcb534

Browse files
committed
Implement new methods vec.starts_with()/vec.ends_with()
1 parent d8f82c8 commit 2fcb534

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

src/librustc/back/rpath.rs

Lines changed: 2 additions & 5 deletions
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

Lines changed: 2 additions & 6 deletions
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/vec.rs

Lines changed: 46 additions & 0 deletions
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] {
@@ -1190,6 +1196,18 @@ impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
11901196
fn contains(&self, x: &T) -> bool {
11911197
self.iter().any(|elt| *x == *elt)
11921198
}
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)
1210+
}
11931211
}
11941212

11951213
/// Extension methods for vectors containing `TotalOrd` elements.
@@ -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

Lines changed: 1 addition & 4 deletions
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)