about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2012-09-05 16:39:06 -0700
committerGraydon Hoare <graydon@mozilla.com>2012-09-05 16:53:55 -0700
commit0ddae5ec7dc2c1a4237bb091e52fff5940f18b48 (patch)
tree1d3299d3013cec7a7b0a62e682bd333a7cb438e7 /src/libcore
parent15b4734d0c1b6e8689d6715ef8b808f5ce6f546c (diff)
Add str::trim{_,_left_,_right_}chars.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/str.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 869981bdf67..6a30a431b83 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -39,6 +39,9 @@ export
    trim_left,
    trim_right,
    trim,
+   trim_left_chars,
+   trim_right_chars,
+   trim_chars,
 
    // Transforming strings
    to_bytes,
@@ -350,6 +353,58 @@ fn view_shift_char(s: &a/str) -> (char, &a/str) {
 /// Prepend a char to a string
 fn unshift_char(&s: ~str, ch: char) { s = from_char(ch) + s; }
 
+/**
+ * Returns a string with leading `chars_to_trim` removed.
+ *
+ * # Arguments
+ *
+ * * s - A string
+ * * chars_to_trim - A vector of chars
+ *
+ */
+pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
+    if chars_to_trim.is_empty() { return from_slice(s); }
+
+    match find(s, |c| !chars_to_trim.contains(c)) {
+      None => ~"",
+      Some(first) => unsafe { unsafe::slice_bytes(s, first, s.len()) }
+    }
+}
+
+/**
+ * Returns a string with trailing `chars_to_trim` removed.
+ *
+ * # Arguments
+ *
+ * * s - A string
+ * * chars_to_trim - A vector of chars
+ *
+ */
+pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
+    if chars_to_trim.is_empty() { return str::from_slice(s); }
+
+    match rfind(s, |c| !chars_to_trim.contains(c)) {
+      None => ~"",
+      Some(last) => {
+        let {next, _} = char_range_at(s, last);
+        unsafe { unsafe::slice_bytes(s, 0u, next) }
+      }
+    }
+}
+
+/**
+ * Returns a string with leading and trailing `chars_to_trim` removed.
+ *
+ * # Arguments
+ *
+ * * s - A string
+ * * chars_to_trim - A vector of chars
+ *
+ */
+pure fn trim_chars(s: &str, chars_to_trim: &[char]) -> ~str {
+    trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
+}
+
 /// Returns a string with leading whitespace removed
 pure fn trim_left(s: &str) -> ~str {
     match find(s, |c| !char::is_whitespace(c)) {
@@ -2732,6 +2787,30 @@ mod tests {
     }
 
     #[test]
+    fn test_trim_left_chars() {
+        assert trim_left_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
+        assert trim_left_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo *** ";
+        assert trim_left_chars(~" ***  *** ", ~['*', ' ']) == ~"";
+        assert trim_left_chars(~"foo *** ", ~['*', ' ']) == ~"foo *** ";
+    }
+
+    #[test]
+    fn test_trim_right_chars() {
+        assert trim_right_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
+        assert trim_right_chars(~" *** foo *** ", ~['*', ' ']) == ~" *** foo";
+        assert trim_right_chars(~" ***  *** ", ~['*', ' ']) == ~"";
+        assert trim_right_chars(~" *** foo", ~['*', ' ']) == ~" *** foo";
+    }
+
+    #[test]
+    fn test_trim_chars() {
+        assert trim_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
+        assert trim_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo";
+        assert trim_chars(~" ***  *** ", ~['*', ' ']) == ~"";
+        assert trim_chars(~"foo", ~['*', ' ']) == ~"foo";
+    }
+
+    #[test]
     fn test_trim_left() {
         assert (trim_left(~"") == ~"");
         assert (trim_left(~"a") == ~"a");