aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraydon Hoare <[email protected]>2010-09-22 15:20:19 -0700
committerGraydon Hoare <[email protected]>2010-09-22 15:27:40 -0700
commite0f682e5fb3a4d860afe38b74673c61ab0193c8e (patch)
treed9175a81b6d24c9306c20bd23255e67306cf0a06
parentFix linear for loops on strings to not hit trailing null. (diff)
downloadrust-e0f682e5fb3a4d860afe38b74673c61ab0193c8e.tar.xz
rust-e0f682e5fb3a4d860afe38b74673c61ab0193c8e.zip
Add some basic string functions: index, rindes, find, substr, split, concat, connect.
-rw-r--r--src/lib/_str.rs119
-rw-r--r--src/test/run-pass/lib-str.rs84
2 files changed, 203 insertions, 0 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs
index 751d79cf..81bbd91f 100644
--- a/src/lib/_str.rs
+++ b/src/lib/_str.rs
@@ -100,3 +100,122 @@ fn refcount(str s) -> uint {
// -1 because calling this function incremented the refcount.
ret rustrt.refcount[u8](s) - 1u;
}
+
+
+// Standard bits from the world of string libraries.
+
+fn index(str s, u8 c) -> int {
+ let int i = 0;
+ for (u8 k in s) {
+ if (k == c) {
+ ret i;
+ }
+ i += 1;
+ }
+ ret -1;
+}
+
+fn rindex(str s, u8 c) -> int {
+ let int n = _str.byte_len(s) as int;
+ while (n >= 0) {
+ if (s.(n) == c) {
+ ret n;
+ }
+ n -= 1;
+ }
+ ret n;
+}
+
+fn find(str haystack, str needle) -> int {
+
+ let int haystack_len = byte_len(haystack) as int;
+ let int needle_len = byte_len(needle) as int;
+
+ if (needle_len == 0) {
+ ret 0;
+ }
+
+ fn match_at(&str haystack,
+ &str needle,
+ int i) -> bool {
+ let int j = i;
+ for (u8 c in needle) {
+ if (haystack.(j) != c) {
+ ret false;
+ }
+ j += 1;
+ }
+ ret true;
+ }
+
+ let int i = 0;
+ while (i <= haystack_len - needle_len) {
+ if (match_at(haystack, needle, i)) {
+ ret i;
+ }
+ i += 1;
+ }
+ ret -1;
+}
+
+fn substr(str s, uint begin, uint len) -> str {
+ let str accum = "";
+ let uint i = begin;
+ while (i < begin+len) {
+ accum += s.(i);
+ i += 1u;
+ }
+ ret accum;
+}
+
+fn split(str s, u8 sep) -> vec[str] {
+ let vec[str] v = vec();
+ let str accum = "";
+ let bool ends_with_sep = false;
+ for (u8 c in s) {
+ if (c == sep) {
+ v += accum;
+ accum = "";
+ ends_with_sep = true;
+ } else {
+ accum += c;
+ ends_with_sep = false;
+ }
+ }
+ if (_str.byte_len(accum) != 0u ||
+ ends_with_sep) {
+ v += accum;
+ }
+ ret v;
+}
+
+fn concat(vec[str] v) -> str {
+ let str s = "";
+ for (str ss in v) {
+ s += ss;
+ }
+ ret s;
+}
+
+fn connect(vec[str] v, str sep) -> str {
+ let str s = "";
+ let bool first = true;
+ for (str ss in v) {
+ if (first) {
+ first = false;
+ } else {
+ s += sep;
+ }
+ s += ss;
+ }
+ ret s;
+}
+
+
+// Local Variables:
+// mode: rust;
+// fill-column: 78;
+// indent-tabs-mode: nil
+// buffer-file-coding-system: utf-8-unix
+// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
+// End:
diff --git a/src/test/run-pass/lib-str.rs b/src/test/run-pass/lib-str.rs
index 585f9b8d..f3552aad 100644
--- a/src/test/run-pass/lib-str.rs
+++ b/src/test/run-pass/lib-str.rs
@@ -11,6 +11,90 @@ fn test_bytes_len() {
check (_str.byte_len("\U0001d11e") == 4u);
}
+fn test_index_and_rindex() {
+ check(_str.index("hello", 'e' as u8) == 1);
+ check(_str.index("hello", 'o' as u8) == 4);
+ check(_str.index("hello", 'z' as u8) == -1);
+ check(_str.rindex("hello", 'l' as u8) == 3);
+ check(_str.rindex("hello", 'h' as u8) == 0);
+ check(_str.rindex("hello", 'z' as u8) == -1);
+}
+
+fn test_split() {
+ fn t(&str s, char c, int i, &str k) {
+ log "splitting: " + s;
+ log i;
+ auto v = _str.split(s, c as u8);
+ log "split to: ";
+ for (str z in v) {
+ log z;
+ }
+ log "comparing: " + v.(i) + " vs. " + k;
+ check(_str.eq(v.(i), k));
+ }
+ 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, "");
+}
+
+fn test_find() {
+ fn t(&str haystack, &str needle, int i) {
+ let int j = _str.find(haystack,needle);
+ log "searched for " + needle;
+ log j;
+ check (i == j);
+ }
+ t("this is a simple", "is a", 5);
+ t("this is a simple", "is z", -1);
+ t("this is a simple", "", 0);
+ t("this is a simple", "simple", 10);
+ t("this", "simple", -1);
+}
+
+fn test_substr() {
+ fn t(&str a, &str b, int start) {
+ check(_str.eq(_str.substr(a, start as uint,
+ _str.byte_len(b)), b));
+ }
+
+ t("hello", "llo", 2);
+ t("hello", "el", 1);
+ t("substr should not be a challenge", "not", 14);
+}
+
+fn test_concat() {
+ fn t(&vec[str] v, &str s) {
+ check(_str.eq(_str.concat(v), s));
+ }
+
+ t(vec("you", "know", "I'm", "no", "good"), "youknowI'mnogood");
+ let vec[str] v = vec();
+ t(v, "");
+ t(vec("hi"), "hi");
+}
+
+fn test_connect() {
+ fn t(&vec[str] v, &str sep, &str s) {
+ check(_str.eq(_str.connect(v, sep), s));
+ }
+
+ t(vec("you", "know", "I'm", "no", "good"), " ", "you know I'm no good");
+ let vec[str] v = vec();
+ t(v, " ", "");
+ t(vec("hi"), " ", "hi");
+}
+
+
fn main() {
test_bytes_len();
+ test_index_and_rindex();
+ test_split();
+ test_find();
+ test_substr();
+ test_concat();
+ test_connect();
}