Skip to content

Commit 9315176

Browse files
committed
---
yaml --- r: 95458 b: refs/heads/dist-snap c: d773a02 h: refs/heads/master v: v3
1 parent 767a885 commit 9315176

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: 0adb41d0eb44ef74e897c22d9f1fcd8f97ef3458
9+
refs/heads/dist-snap: d773a024a2976f2759235551a52101cd08b37cce
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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]

branches/dist-snap/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
});

branches/dist-snap/src/libstd/str.rs

Lines changed: 6 additions & 19 deletions
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 {

branches/dist-snap/src/libstd/vec.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,12 @@ pub trait ImmutableEqVector<T:Eq> {
11491149

11501150
/// Return true if a vector contains an element with the given value
11511151
fn contains(&self, x: &T) -> bool;
1152+
1153+
/// Returns true if `needle` is a prefix of the vector.
1154+
fn starts_with(&self, needle: &[T]) -> bool;
1155+
1156+
/// Returns true if `needle` is a suffix of the vector.
1157+
fn ends_with(&self, needle: &[T]) -> bool;
11521158
}
11531159

11541160
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
@@ -1162,9 +1168,21 @@ impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
11621168
self.iter().rposition(|x| *x == *t)
11631169
}
11641170

1171+
#[inline]
11651172
fn contains(&self, x: &T) -> bool {
1166-
for elt in self.iter() { if *x == *elt { return true; } }
1167-
false
1173+
self.iter().any(|elt| *x == *elt)
1174+
}
1175+
1176+
#[inline]
1177+
fn starts_with(&self, needle: &[T]) -> bool {
1178+
let n = needle.len();
1179+
self.len() >= n && needle == self.slice_to(n)
1180+
}
1181+
1182+
#[inline]
1183+
fn ends_with(&self, needle: &[T]) -> bool {
1184+
let (m, n) = (self.len(), needle.len());
1185+
m >= n && needle == self.slice_from(m - n)
11681186
}
11691187
}
11701188

@@ -3748,6 +3766,34 @@ mod tests {
37483766
assert_eq!(xs.capacity(), 100);
37493767
assert_eq!(xs, range(0, 100).to_owned_vec());
37503768
}
3769+
3770+
#[test]
3771+
fn test_starts_with() {
3772+
assert!(bytes!("foobar").starts_with(bytes!("foo")));
3773+
assert!(!bytes!("foobar").starts_with(bytes!("oob")));
3774+
assert!(!bytes!("foobar").starts_with(bytes!("bar")));
3775+
assert!(!bytes!("foo").starts_with(bytes!("foobar")));
3776+
assert!(!bytes!("bar").starts_with(bytes!("foobar")));
3777+
assert!(bytes!("foobar").starts_with(bytes!("foobar")));
3778+
let empty: &[u8] = [];
3779+
assert!(empty.starts_with(empty));
3780+
assert!(!empty.starts_with(bytes!("foo")));
3781+
assert!(bytes!("foobar").starts_with(empty));
3782+
}
3783+
3784+
#[test]
3785+
fn test_ends_with() {
3786+
assert!(bytes!("foobar").ends_with(bytes!("bar")));
3787+
assert!(!bytes!("foobar").ends_with(bytes!("oba")));
3788+
assert!(!bytes!("foobar").ends_with(bytes!("foo")));
3789+
assert!(!bytes!("foo").ends_with(bytes!("foobar")));
3790+
assert!(!bytes!("bar").ends_with(bytes!("foobar")));
3791+
assert!(bytes!("foobar").ends_with(bytes!("foobar")));
3792+
let empty: &[u8] = [];
3793+
assert!(empty.ends_with(empty));
3794+
assert!(!empty.ends_with(bytes!("foo")));
3795+
assert!(bytes!("foobar").ends_with(empty));
3796+
}
37513797
}
37523798

37533799
#[cfg(test)]

branches/dist-snap/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)