Added str::words_iter
This commit is contained in:
parent
d4b287e852
commit
ff95029171
|
@ -14,7 +14,7 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
|
|||
char_at, bytes, is_ascii, shift_byte, pop_byte,
|
||||
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
||||
from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
||||
contains, iter_chars, chars_iter, bytes_iter,
|
||||
contains, iter_chars, chars_iter, bytes_iter, words_iter,
|
||||
loop_chars, loop_chars_sub, escape, any, all, map, windowed;
|
||||
|
||||
#[abi = "cdecl"]
|
||||
|
@ -914,6 +914,15 @@ fn words(ss: str) -> [str] {
|
|||
{|w| 0u < str::char_len(w)});
|
||||
}
|
||||
|
||||
/*
|
||||
Function: words_iter
|
||||
|
||||
Apply a function to each word
|
||||
*/
|
||||
fn words_iter(ss: str, ff: fn&(&&str)) {
|
||||
vec::iter(words(ss), ff)
|
||||
}
|
||||
|
||||
/*
|
||||
Function: concat
|
||||
|
||||
|
@ -1649,7 +1658,7 @@ mod tests {
|
|||
i += 1;
|
||||
}
|
||||
|
||||
iter_chars("") {|ch| fail; } // should not fail
|
||||
iter_chars("") {|_ch| fail; } // should not fail
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1683,6 +1692,29 @@ mod tests {
|
|||
bytes_iter("") {|bb| assert bb == 0u8; }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_words_iter() {
|
||||
let data = "\nMary had a little lamb\nLittle lamb\n";
|
||||
|
||||
let ii = 0;
|
||||
|
||||
words_iter(data) {|ww|
|
||||
alt ii {
|
||||
0 { assert "Mary" == ww; }
|
||||
1 { assert "had" == ww; }
|
||||
2 { assert "a" == ww; }
|
||||
3 { assert "little" == ww; }
|
||||
_ { () }
|
||||
}
|
||||
ii += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_words_iter_() {
|
||||
words_iter("") {|_ww| fail; } // should not fail
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escape() {
|
||||
assert(escape("abcdef") == "abcdef");
|
||||
|
|
Loading…
Reference in New Issue