core: Add str::each_char

This commit is contained in:
Brian Anderson 2012-03-29 22:28:26 -07:00
parent 9bff2f2545
commit 8641c95221
1 changed files with 26 additions and 0 deletions

View File

@ -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;
}
}