|
58 | 58 | all, any,
|
59 | 59 | all_between, any_between,
|
60 | 60 | map,
|
61 |
| - each, |
62 |
| - each_char, |
| 61 | + each, eachi, |
| 62 | + each_char, each_chari, |
63 | 63 | bytes_iter,
|
64 | 64 | chars_iter,
|
65 | 65 | split_char_iter,
|
|
73 | 73 | find_char, find_char_from, find_char_between,
|
74 | 74 | rfind_char, rfind_char_from, rfind_char_between,
|
75 | 75 | find_str, find_str_from, find_str_between,
|
76 |
| - contains, |
| 76 | + contains, contains_char, |
77 | 77 | starts_with,
|
78 | 78 | ends_with,
|
79 | 79 |
|
@@ -672,22 +672,35 @@ pure fn bytes_iter(ss: str/&, it: fn(u8)) {
|
672 | 672 | #[doc = "Iterate over the bytes in a string"]
|
673 | 673 | #[inline(always)]
|
674 | 674 | pure fn each(s: str/&, it: fn(u8) -> bool) {
|
| 675 | + eachi(s, {|_i, b| it(b)}) |
| 676 | +} |
| 677 | + |
| 678 | +#[doc = "Iterate over the bytes in a string, with indices"] |
| 679 | +#[inline(always)] |
| 680 | +pure fn eachi(s: str/&, it: fn(uint, u8) -> bool) { |
675 | 681 | let mut i = 0u, l = len(s);
|
676 | 682 | while (i < l) {
|
677 |
| - if !it(s[i]) { break; } |
| 683 | + if !it(i, s[i]) { break; } |
678 | 684 | i += 1u;
|
679 | 685 | }
|
680 | 686 | }
|
681 | 687 |
|
682 | 688 | #[doc = "Iterates over the chars in a string"]
|
683 | 689 | #[inline(always)]
|
684 | 690 | pure fn each_char(s: str/&, it: fn(char) -> bool) {
|
685 |
| - let mut pos = 0u; |
| 691 | + each_chari(s, {|_i, c| it(c)}) |
| 692 | +} |
| 693 | + |
| 694 | +#[doc = "Iterates over the chars in a string, with indices"] |
| 695 | +#[inline(always)] |
| 696 | +pure fn each_chari(s: str/&, it: fn(uint, char) -> bool) { |
| 697 | + let mut pos = 0u, ch_pos = 0u; |
686 | 698 | let len = len(s);
|
687 | 699 | while pos < len {
|
688 | 700 | let {ch, next} = char_range_at(s, pos);
|
689 | 701 | pos = next;
|
690 |
| - if !it(ch) { break; } |
| 702 | + if !it(ch_pos, ch) { break; } |
| 703 | + ch_pos += 1u; |
691 | 704 | }
|
692 | 705 | }
|
693 | 706 |
|
@@ -1146,6 +1159,18 @@ pure fn contains(haystack: str/&a, needle: str/&b) -> bool {
|
1146 | 1159 | option::is_some(find_str(haystack, needle))
|
1147 | 1160 | }
|
1148 | 1161 |
|
| 1162 | +#[doc = " |
| 1163 | +Returns true if a string contains a char. |
| 1164 | +
|
| 1165 | +# Arguments |
| 1166 | +
|
| 1167 | +* haystack - The string to look in |
| 1168 | +* needle - The char to look for |
| 1169 | +"] |
| 1170 | +pure fn contains_char(haystack: str/&, needle: char) -> bool { |
| 1171 | + option::is_some(find_char(haystack, needle)) |
| 1172 | +} |
| 1173 | + |
1149 | 1174 | #[doc = "
|
1150 | 1175 | Returns true if one string starts with another
|
1151 | 1176 |
|
@@ -1879,12 +1904,21 @@ impl extensions/& for str/& {
|
1879 | 1904 | #[doc = "Returns true if one string contains another"]
|
1880 | 1905 | #[inline]
|
1881 | 1906 | fn contains(needle: str/&a) -> bool { contains(self, needle) }
|
| 1907 | + #[doc = "Returns true if a string contains a char"] |
| 1908 | + #[inline] |
| 1909 | + fn contains_char(needle: char) -> bool { contains_char(self, needle) } |
1882 | 1910 | #[doc = "Iterate over the bytes in a string"]
|
1883 | 1911 | #[inline]
|
1884 | 1912 | fn each(it: fn(u8) -> bool) { each(self, it) }
|
| 1913 | + #[doc = "Iterate over the bytes in a string, with indices"] |
| 1914 | + #[inline] |
| 1915 | + fn eachi(it: fn(uint, u8) -> bool) { eachi(self, it) } |
1885 | 1916 | #[doc = "Iterate over the chars in a string"]
|
1886 | 1917 | #[inline]
|
1887 | 1918 | fn each_char(it: fn(char) -> bool) { each_char(self, it) }
|
| 1919 | + #[doc = "Iterate over the chars in a string, with indices"] |
| 1920 | + #[inline] |
| 1921 | + fn each_chari(it: fn(uint, char) -> bool) { each_chari(self, it) } |
1888 | 1922 | #[doc = "Returns true if one string ends with another"]
|
1889 | 1923 | #[inline]
|
1890 | 1924 | fn ends_with(needle: str/&) -> bool { ends_with(self, needle) }
|
@@ -2644,6 +2678,14 @@ mod tests {
|
2644 | 2678 | assert !contains(data, "ไท华");
|
2645 | 2679 | }
|
2646 | 2680 |
|
| 2681 | + #[test] |
| 2682 | + fn test_contains_char() { |
| 2683 | + assert contains_char("abc", 'b'); |
| 2684 | + assert contains_char("a", 'a'); |
| 2685 | + assert !contains_char("abc", 'd'); |
| 2686 | + assert !contains_char("", 'a'); |
| 2687 | + } |
| 2688 | + |
2647 | 2689 | #[test]
|
2648 | 2690 | fn test_chars_iter() {
|
2649 | 2691 | let mut i = 0;
|
|
0 commit comments