]> git.lizzy.rs Git - rust.git/commitdiff
libcore: add splitn to split a string N times.
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 19 Dec 2011 18:08:36 +0000 (10:08 -0800)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 19 Dec 2011 18:22:07 +0000 (10:22 -0800)
src/libcore/str.rs
src/test/stdtest/str.rs

index 802297223e9bce490dda0f670d91c9e45bff7119..080c7ffc1cf53e772ac068d77c32c91756975575 100644 (file)
@@ -6,11 +6,11 @@
 
 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
 
index b519904836480eca234ee2159b08e5d17d15646d..b70cafe739281875804172437e0368a389e50411 100644 (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]