Add a way to iterate over a str's chars to std::str

This commit is contained in:
Marijn Haverbeke 2011-10-31 14:52:08 +01:00
parent 7080ac15fb
commit b0d60a7108
2 changed files with 29 additions and 1 deletions

View File

@ -11,7 +11,7 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len, index,
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,
contains;
contains, chars;
native "c-stack-cdecl" mod rustrt {
fn rust_str_push(&s: str, ch: u8);
@ -299,6 +299,21 @@ Pluck a character out of a string
*/
fn char_at(s: str, i: uint) -> char { ret char_range_at(s, i).ch; }
/*
Function: chars
Iterate over the characters in a string
*/
fn chars(s: str, it: block(char)) {
let pos = 0u, len = byte_len(s);
while (pos < len) {
let {ch, next} = char_range_at(s, pos);
pos = next;
it(ch);
}
}
/*
Function: char_len

View File

@ -315,3 +315,16 @@ fn contains() {
assert !str::contains("abcde", "def");
assert !str::contains("", "a");
}
#[test]
fn chars() {
let i = 0;
str::chars("x\u03c0y") {|ch|
alt i {
0 { assert ch == 'x'; }
1 { assert ch == '\u03c0'; }
2 { assert ch == 'y'; }
}
i += 1;
}
}