Skip to content

Commit ff95029

Browse files
killerswannikomatsakis
authored andcommitted
Added str::words_iter
1 parent d4b287e commit ff95029

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/libcore/str.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
1414
char_at, bytes, is_ascii, shift_byte, pop_byte,
1515
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
1616
from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
17-
contains, iter_chars, chars_iter, bytes_iter,
17+
contains, iter_chars, chars_iter, bytes_iter, words_iter,
1818
loop_chars, loop_chars_sub, escape, any, all, map, windowed;
1919

2020
#[abi = "cdecl"]
@@ -914,6 +914,15 @@ fn words(ss: str) -> [str] {
914914
{|w| 0u < str::char_len(w)});
915915
}
916916

917+
/*
918+
Function: words_iter
919+
920+
Apply a function to each word
921+
*/
922+
fn words_iter(ss: str, ff: fn&(&&str)) {
923+
vec::iter(words(ss), ff)
924+
}
925+
917926
/*
918927
Function: concat
919928

@@ -1649,7 +1658,7 @@ mod tests {
16491658
i += 1;
16501659
}
16511660

1652-
iter_chars("") {|ch| fail; } // should not fail
1661+
iter_chars("") {|_ch| fail; } // should not fail
16531662
}
16541663

16551664
#[test]
@@ -1683,6 +1692,29 @@ mod tests {
16831692
bytes_iter("") {|bb| assert bb == 0u8; }
16841693
}
16851694

1695+
#[test]
1696+
fn test_words_iter() {
1697+
let data = "\nMary had a little lamb\nLittle lamb\n";
1698+
1699+
let ii = 0;
1700+
1701+
words_iter(data) {|ww|
1702+
alt ii {
1703+
0 { assert "Mary" == ww; }
1704+
1 { assert "had" == ww; }
1705+
2 { assert "a" == ww; }
1706+
3 { assert "little" == ww; }
1707+
_ { () }
1708+
}
1709+
ii += 1;
1710+
}
1711+
}
1712+
1713+
#[test]
1714+
fn test_words_iter_() {
1715+
words_iter("") {|_ww| fail; } // should not fail
1716+
}
1717+
16861718
#[test]
16871719
fn test_escape() {
16881720
assert(escape("abcdef") == "abcdef");

0 commit comments

Comments
 (0)