mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
core: Add str::each_char
This commit is contained in:
parent
9bff2f2545
commit
8641c95221
@ -55,6 +55,7 @@ export
|
||||
all_between, any_between,
|
||||
map,
|
||||
each,
|
||||
each_char,
|
||||
bytes_iter,
|
||||
chars_iter,
|
||||
split_char_iter,
|
||||
@ -635,6 +636,18 @@ fn each(s: str, it: fn(u8) -> bool) {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = "Iterates over the chars in a string"]
|
||||
#[inline(always)]
|
||||
fn each_char(s: str, it: fn(char) -> bool) {
|
||||
let mut pos = 0u;
|
||||
let len = len(s);
|
||||
while pos < len {
|
||||
let {ch, next} = char_range_at(s, pos);
|
||||
pos = next;
|
||||
if !it(ch) { break; }
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = "Iterate over the characters in a string"]
|
||||
fn chars_iter(s: str, it: fn(char)) {
|
||||
let mut pos = 0u;
|
||||
@ -2669,4 +2682,17 @@ mod tests {
|
||||
assert to_utf16(from_utf16(u)) == u;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_char() {
|
||||
let s = "abc";
|
||||
let mut found_b = false;
|
||||
for each_char(s) {|ch|
|
||||
if ch == 'b' {
|
||||
found_b = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert found_b;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user