about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-08-01 18:15:18 -0700
committerBrian Anderson <banderson@mozilla.com>2011-08-01 18:55:51 -0700
commit598e25e09194f3ff89d413d9bb2a4246ce5cfeee (patch)
tree9844d6932a9f647ce4a1955a8ede6d03dcbf5ef5 /src
parent0c7a95fde5c1ea6d686c5194f2d46ff0f7305aca (diff)
downloadrust-598e25e09194f3ff89d413d9bb2a4246ce5cfeee.tar.gz
rust-598e25e09194f3ff89d413d9bb2a4246ce5cfeee.zip
Add std::char_slice
Diffstat (limited to 'src')
-rw-r--r--src/lib/str.rs6
-rw-r--r--src/test/stdtest/str.rs7
2 files changed, 13 insertions, 0 deletions
diff --git a/src/lib/str.rs b/src/lib/str.rs
index 0adf3561165..b3a94cd6e12 100644
--- a/src/lib/str.rs
+++ b/src/lib/str.rs
@@ -53,6 +53,7 @@ export unsafe_from_bytes_ivec;
 export is_empty;
 export is_not_empty;
 export replace;
+export char_slice;
 
 native "rust" mod rustrt {
     type sbuf;
@@ -518,6 +519,11 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
     }
 }
 
+// FIXME: Also not efficient
+fn char_slice(s: &str, begin: uint, end: uint) -> str {
+    from_chars(vec::slice(to_chars(s), begin, end))
+}
+
 // Local Variables:
 // mode: rust;
 // fill-column: 78;
diff --git a/src/test/stdtest/str.rs b/src/test/stdtest/str.rs
index f2b6a6b46ec..00652996e77 100644
--- a/src/test/stdtest/str.rs
+++ b/src/test/stdtest/str.rs
@@ -154,6 +154,13 @@ fn test_replace() {
     assert (str::replace(" test test ", test, "") == "   ");
 }
 
+#[test]
+fn test_char_slice() {
+    assert (str::eq("ab", str::char_slice("abc", 0u, 2u)));
+    assert (str::eq("bc", str::char_slice("abc", 1u, 3u)));
+    assert (str::eq("", str::char_slice("abc", 1u, 1u)));
+    assert (str::eq("\u65e5", str::char_slice("\u65e5\u672c", 0u, 1u)));
+}
 
 // Local Variables:
 // mode: rust;