libcore: add splitn to split a string N times.
This commit is contained in:
@@ -6,11 +6,11 @@ String manipulation.
|
|||||||
|
|
||||||
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
|
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
|
||||||
byte_len_range, index,
|
byte_len_range, index,
|
||||||
rindex, find, starts_with, ends_with, substr, slice, split, split_str,
|
rindex, find, starts_with, ends_with, substr, slice, split, splitn,
|
||||||
concat, connect, to_upper, replace, char_slice, trim_left, trim_right,
|
split_str, concat, connect, to_upper, replace, char_slice, trim_left,
|
||||||
trim, unshift_char, shift_char, pop_char, push_char, is_utf8,
|
trim_right, trim, unshift_char, shift_char, pop_char, push_char,
|
||||||
from_chars, to_chars, char_len, char_len_range, char_at, bytes,
|
is_utf8, from_chars, to_chars, char_len, char_len_range, char_at,
|
||||||
is_ascii, shift_byte, pop_byte,
|
bytes, is_ascii, shift_byte, pop_byte,
|
||||||
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
|
||||||
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
|
||||||
contains, iter_chars, loop_chars, loop_chars_sub,
|
contains, iter_chars, loop_chars, loop_chars_sub,
|
||||||
@@ -744,6 +744,32 @@ fn split(s: str, sep: u8) -> [str] {
|
|||||||
ret v;
|
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
|
Function: split_str
|
||||||
|
|
||||||
|
|||||||
@@ -41,22 +41,37 @@ fn test_index_and_rindex() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_split() {
|
fn test_split() {
|
||||||
fn t(s: str, c: char, i: int, k: str) {
|
fn t(s: str, c: char, u: [str]) {
|
||||||
log "splitting: " + s;
|
log "split: " + s;
|
||||||
log i;
|
|
||||||
let v = str::split(s, c as u8);
|
let v = str::split(s, c as u8);
|
||||||
log "split to: ";
|
log "split to: ";
|
||||||
for z: str in v { log z; }
|
log v;
|
||||||
log "comparing: " + v[i] + " vs. " + k;
|
assert (vec::all2(v, u, { |a,b| a == b }));
|
||||||
assert (str::eq(v[i], k));
|
|
||||||
}
|
}
|
||||||
t("abc.hello.there", '.', 0, "abc");
|
t("abc.hello.there", '.', ["abc", "hello", "there"]);
|
||||||
t("abc.hello.there", '.', 1, "hello");
|
t(".hello.there", '.', ["", "hello", "there"]);
|
||||||
t("abc.hello.there", '.', 2, "there");
|
t("...hello.there.", '.', ["", "", "", "hello", "there", ""]);
|
||||||
t(".hello.there", '.', 0, "");
|
}
|
||||||
t(".hello.there", '.', 1, "hello");
|
|
||||||
t("...hello.there.", '.', 3, "hello");
|
#[test]
|
||||||
t("...hello.there.", '.', 5, "");
|
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]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user