mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-08 01:28:18 +03:00
Add _str.starts_with and ends_with.
This commit is contained in:
@@ -188,6 +188,35 @@ fn match_at(&str haystack,
|
||||
ret -1;
|
||||
}
|
||||
|
||||
fn starts_with(str haystack, str needle) -> bool {
|
||||
let uint haystack_len = byte_len(haystack);
|
||||
let uint needle_len = byte_len(needle);
|
||||
if (needle_len == 0u) {
|
||||
ret true;
|
||||
}
|
||||
if (needle_len > haystack_len) {
|
||||
ret false;
|
||||
}
|
||||
ret eq(substr(haystack, 0u, needle_len), needle);
|
||||
}
|
||||
|
||||
|
||||
fn ends_with(str haystack, str needle) -> bool {
|
||||
let uint haystack_len = byte_len(haystack);
|
||||
let uint needle_len = byte_len(needle);
|
||||
if (needle_len == 0u) {
|
||||
ret true;
|
||||
}
|
||||
if (needle_len > haystack_len) {
|
||||
ret false;
|
||||
}
|
||||
ret eq(substr(haystack,
|
||||
haystack_len - needle_len,
|
||||
needle_len),
|
||||
needle);
|
||||
}
|
||||
|
||||
|
||||
fn substr(str s, uint begin, uint len) -> str {
|
||||
let str accum = "";
|
||||
let uint i = begin;
|
||||
|
||||
Reference in New Issue
Block a user