1
Fork 0

libcore: add splitn to split a string N times.

This commit is contained in:
Erick Tryzelaar 2011-12-19 10:08:36 -08:00
parent 8e54e74be4
commit 02d84d89e0
2 changed files with 59 additions and 18 deletions

View file

@ -6,11 +6,11 @@ String manipulation.
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
byte_len_range, index,
rindex, find, starts_with, ends_with, substr, slice, split, split_str,
concat, connect, to_upper, replace, char_slice, trim_left, trim_right,
trim, unshift_char, shift_char, pop_char, push_char, is_utf8,
from_chars, to_chars, char_len, char_len_range, char_at, bytes,
is_ascii, shift_byte, pop_byte,
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
is_utf8, from_chars, to_chars, char_len, char_len_range, 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, iter_chars, loop_chars, loop_chars_sub,
@ -744,6 +744,32 @@ fn split(s: str, sep: u8) -> [str] {
ret v;
}
/*
Function: splitn
Split a string at each occurance of a given separator up to count times.
Returns:
A vector containing all the strings between each occurance of the separator
*/
fn splitn(s: str, sep: u8, count: uint) -> [str] {
let v = [];
let accum = "";
let n = count;
let ends_with_sep: bool = false;
for c in s {
if n > 0u && c == sep {
n -= 1u;
v += [accum];
accum = "";
ends_with_sep = true;
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
}
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
ret v;
}
/*
Function: split_str

View file

@ -41,22 +41,37 @@ fn test_index_and_rindex() {
#[test]
fn test_split() {
fn t(s: str, c: char, i: int, k: str) {
log "splitting: " + s;
log i;
fn t(s: str, c: char, u: [str]) {
log "split: " + s;
let v = str::split(s, c as u8);
log "split to: ";
for z: str in v { log z; }
log "comparing: " + v[i] + " vs. " + k;
assert (str::eq(v[i], k));
log v;
assert (vec::all2(v, u, { |a,b| a == b }));
}
t("abc.hello.there", '.', 0, "abc");
t("abc.hello.there", '.', 1, "hello");
t("abc.hello.there", '.', 2, "there");
t(".hello.there", '.', 0, "");
t(".hello.there", '.', 1, "hello");
t("...hello.there.", '.', 3, "hello");
t("...hello.there.", '.', 5, "");
t("abc.hello.there", '.', ["abc", "hello", "there"]);
t(".hello.there", '.', ["", "hello", "there"]);
t("...hello.there.", '.', ["", "", "", "hello", "there", ""]);
}
#[test]
fn test_splitn() {
fn t(s: str, c: char, n: uint, u: [str]) {
log "splitn: " + s;
let v = str::splitn(s, c as u8, n);
log "split to: ";
log v;
log "comparing vs. ";
log u;
assert (vec::all2(v, u, { |a,b| a == b }));
}
t("abc.hello.there", '.', 0u, ["abc.hello.there"]);
t("abc.hello.there", '.', 1u, ["abc", "hello.there"]);
t("abc.hello.there", '.', 2u, ["abc", "hello", "there"]);
t("abc.hello.there", '.', 3u, ["abc", "hello", "there"]);
t(".hello.there", '.', 0u, [".hello.there"]);
t(".hello.there", '.', 1u, ["", "hello.there"]);
t("...hello.there.", '.', 3u, ["", "", "", "hello.there."]);
t("...hello.there.", '.', 5u, ["", "", "", "hello", "there", ""]);
}
#[test]