Skip to content

Commit 536dd2f

Browse files
killerswannikomatsakis
authored andcommitted
Added str::lines_iter
1 parent 1c54744 commit 536dd2f

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/libcore/str.rs

Lines changed: 27 additions & 3 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, words_iter,
17+
contains, iter_chars, chars_iter, bytes_iter, words_iter, lines_iter,
1818
loop_chars, loop_chars_sub, escape, any, all, map, windowed;
1919

2020
#[abi = "cdecl"]
@@ -923,6 +923,15 @@ fn words_iter(ss: str, ff: fn&(&&str)) {
923923
vec::iter(words(ss), ff)
924924
}
925925

926+
/*
927+
Function: lines_iter
928+
929+
Apply a function to each lines (by '\n')
930+
*/
931+
fn lines_iter(ss: str, ff: fn&(&&str)) {
932+
vec::iter(lines(ss), ff)
933+
}
934+
926935
/*
927936
Function: concat
928937
@@ -1708,11 +1717,26 @@ mod tests {
17081717
}
17091718
ii += 1;
17101719
}
1720+
1721+
words_iter("") {|_x| fail; } // should not fail
17111722
}
17121723

17131724
#[test]
1714-
fn test_words_iter_() {
1715-
words_iter("") {|_ww| fail; } // should not fail
1725+
fn test_lines_iter () {
1726+
let lf = "\nMary had a little lamb\nLittle lamb\n";
1727+
1728+
let ii = 0;
1729+
1730+
lines_iter(lf) {|x|
1731+
alt ii {
1732+
0 { assert "" == x; }
1733+
1 { assert "Mary had a little lamb" == x; }
1734+
2 { assert "Little lamb" == x; }
1735+
3 { assert "" == x; }
1736+
_ { () }
1737+
}
1738+
ii += 1;
1739+
}
17161740
}
17171741

17181742
#[test]

0 commit comments

Comments
 (0)