Skip to content

Commit 04e89af

Browse files
committed
Add std::str::contains
1 parent 4b58071 commit 04e89af

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/lib/str.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len, index,
44
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars,
55
to_chars, char_len, char_at, bytes, is_ascii, shift_byte, pop_byte,
66
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
7-
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice;
7+
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
8+
contains;
89

910
native "rust" mod rustrt {
1011
fn rust_str_push(&s: str, ch: u8);
@@ -254,6 +255,10 @@ fn find(haystack: str, needle: str) -> int {
254255
ret -1;
255256
}
256257

258+
fn contains(haystack: str, needle: str) -> bool {
259+
0 <= find(haystack, needle)
260+
}
261+
257262
fn starts_with(haystack: str, needle: str) -> bool {
258263
let haystack_len: uint = byte_len(haystack);
259264
let needle_len: uint = byte_len(needle);

src/test/stdtest/str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,14 @@ fn vec_str_conversions() {
304304
i += 1u;
305305
}
306306
}
307+
308+
#[test]
309+
fn contains() {
310+
assert str::contains("abcde", "bcd");
311+
assert str::contains("abcde", "abcd");
312+
assert str::contains("abcde", "bcde");
313+
assert str::contains("abcde", "");
314+
assert str::contains("", "");
315+
assert !str::contains("abcde", "def");
316+
assert !str::contains("", "a");
317+
}

0 commit comments

Comments
 (0)