Add std::str::contains
This commit is contained in:
parent
4b58071f96
commit
04e89af6db
2 changed files with 17 additions and 1 deletions
|
|
@ -4,7 +4,8 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len, index,
|
|||
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars,
|
||||
to_chars, char_len, char_at, bytes, is_ascii, shift_byte, pop_byte,
|
||||
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
||||
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice;
|
||||
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
||||
contains;
|
||||
|
||||
native "rust" mod rustrt {
|
||||
fn rust_str_push(&s: str, ch: u8);
|
||||
|
|
@ -254,6 +255,10 @@ fn find(haystack: str, needle: str) -> int {
|
|||
ret -1;
|
||||
}
|
||||
|
||||
fn contains(haystack: str, needle: str) -> bool {
|
||||
0 <= find(haystack, needle)
|
||||
}
|
||||
|
||||
fn starts_with(haystack: str, needle: str) -> bool {
|
||||
let haystack_len: uint = byte_len(haystack);
|
||||
let needle_len: uint = byte_len(needle);
|
||||
|
|
|
|||
|
|
@ -304,3 +304,14 @@ fn vec_str_conversions() {
|
|||
i += 1u;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains() {
|
||||
assert str::contains("abcde", "bcd");
|
||||
assert str::contains("abcde", "abcd");
|
||||
assert str::contains("abcde", "bcde");
|
||||
assert str::contains("abcde", "");
|
||||
assert str::contains("", "");
|
||||
assert !str::contains("abcde", "def");
|
||||
assert !str::contains("", "a");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue