about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-06-10 21:03:16 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-06-10 23:02:55 +1000
commitf632f466148c7e5571393bb2a24cd628703a60fa (patch)
tree6a5fa9687b4df16bf4fe0e6ff564c15f61c4a452
parent1553874149c3c37b94d75e9122092b131ba74c77 (diff)
downloadrust-f632f466148c7e5571393bb2a24cd628703a60fa.tar.gz
rust-f632f466148c7e5571393bb2a24cd628703a60fa.zip
std: convert str::trim* to methods.
-rw-r--r--src/libextra/num/bigint.rs2
-rw-r--r--src/librustdoc/markdown_index_pass.rs2
-rw-r--r--src/librustdoc/text_pass.rs2
-rw-r--r--src/librusti/rusti.rc2
-rw-r--r--src/libstd/str.rs200
5 files changed, 94 insertions, 114 deletions
diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs
index 1411079d52f..0294b595cfd 100644
--- a/src/libextra/num/bigint.rs
+++ b/src/libextra/num/bigint.rs
@@ -524,7 +524,7 @@ impl ToStrRadix for BigUint {
                 let s = uint::to_str_radix(*n as uint, radix);
                 str::from_chars(vec::from_elem(l - s.len(), '0')) + s
             }));
-            str::trim_left_chars(s, ['0']).to_owned()
+            s.trim_left_chars(['0']).to_owned()
         }
     }
 }
diff --git a/src/librustdoc/markdown_index_pass.rs b/src/librustdoc/markdown_index_pass.rs
index 87a73f5c9d3..36eb5e77ad6 100644
--- a/src/librustdoc/markdown_index_pass.rs
+++ b/src/librustdoc/markdown_index_pass.rs
@@ -152,7 +152,7 @@ pub fn pandoc_header_id(header: &str) -> ~str {
         // Collapse sequences of whitespace to a single dash
         // XXX: Hacky implementation here that only covers
         // one or two spaces.
-        let s = str::trim(s);
+        let s = s.trim();
         let s = str::replace(s, "  ", "-");
         let s = str::replace(s, " ", "-");
         return s;
diff --git a/src/librustdoc/text_pass.rs b/src/librustdoc/text_pass.rs
index 96a67ea3243..1f7a71e0fd8 100644
--- a/src/librustdoc/text_pass.rs
+++ b/src/librustdoc/text_pass.rs
@@ -157,7 +157,7 @@ mod test {
             let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
             let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
             let doc = (sectionalize_pass::mk_pass().f)(srv.clone(), doc);
-            (mk_pass(~"", |s| str::trim(s).to_owned() ).f)(srv.clone(), doc)
+            (mk_pass(~"", |s| s.trim().to_owned() ).f)(srv.clone(), doc)
         }
     }
 
diff --git a/src/librusti/rusti.rc b/src/librusti/rusti.rc
index 6d4043bfc89..17caaf6bf7d 100644
--- a/src/librusti/rusti.rc
+++ b/src/librusti/rusti.rc
@@ -318,7 +318,7 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
                 match get_line(use_rl, "rusti| ") {
                     None => fail!("unterminated multiline command :{ .. :}"),
                     Some(line) => {
-                        if str::trim(line) == ":}" {
+                        if line.trim() == ":}" {
                             end_multiline = true;
                         } else {
                             multiline_cmd += line + "\n";
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 5455edc2586..605a11032a1 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -413,80 +413,6 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
     *s = new_str;
 }
 
-/**
- * Returns a string with leading `chars_to_trim` removed.
- *
- * # Arguments
- *
- * * s - A string
- * * chars_to_trim - A vector of chars
- *
- */
-pub fn trim_left_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
-    if chars_to_trim.is_empty() { return s; }
-
-    match s.find(|c| !chars_to_trim.contains(&c)) {
-      None => "",
-      Some(first) => unsafe { raw::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
- *
- */
-pub fn trim_right_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
-    if chars_to_trim.is_empty() { return s; }
-
-    match s.rfind(|c| !chars_to_trim.contains(&c)) {
-      None => "",
-      Some(last) => {
-        let next = char_range_at(s, last).next;
-        unsafe { raw::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
- *
- */
-pub fn trim_chars<'a>(s: &'a str, chars_to_trim: &[char]) -> &'a str {
-    trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
-}
-
-/// Returns a string with leading whitespace removed
-pub fn trim_left<'a>(s: &'a str) -> &'a str {
-    match s.find(|c| !char::is_whitespace(c)) {
-      None => "",
-      Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
-    }
-}
-
-/// Returns a string with trailing whitespace removed
-pub fn trim_right<'a>(s: &'a str) -> &'a str {
-    match s.rfind(|c| !char::is_whitespace(c)) {
-      None => "",
-      Some(last) => {
-        let next = char_range_at(s, last).next;
-        unsafe { raw::slice_bytes(s, 0u, next) }
-      }
-    }
-}
-
-/// Returns a string with leading and trailing whitespace removed
-pub fn trim<'a>(s: &'a str) -> &'a str { trim_left(trim_right(s)) }
-
 /*
 Section: Transforming strings
 */
@@ -2024,25 +1950,79 @@ impl<'self> StrSlice<'self> for &'self str {
 
     /// Returns a string with leading and trailing whitespace removed
     #[inline]
-    fn trim(&self) -> &'self str { trim(*self) }
+    fn trim(&self) -> &'self str {
+        self.trim_left().trim_right()
+    }
     /// Returns a string with leading whitespace removed
     #[inline]
-    fn trim_left(&self) -> &'self str { trim_left(*self) }
+    fn trim_left(&self) -> &'self str {
+        match self.find(|c| !char::is_whitespace(c)) {
+            None => "",
+            Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
+        }
+    }
     /// Returns a string with trailing whitespace removed
     #[inline]
-    fn trim_right(&self) -> &'self str { trim_right(*self) }
+    fn trim_right(&self) -> &'self str {
+        match self.rfind(|c| !char::is_whitespace(c)) {
+            None => "",
+            Some(last) => {
+                let next = char_range_at(*self, last).next;
+                unsafe { raw::slice_bytes(*self, 0u, next) }
+            }
+        }
+    }
 
+    /**
+     * Returns a string with leading and trailing `chars_to_trim` removed.
+     *
+     * # Arguments
+     *
+     * * chars_to_trim - A vector of chars
+     *
+     */
     #[inline]
     fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str {
-        trim_chars(*self, chars_to_trim)
+        self.trim_left_chars(chars_to_trim).trim_right_chars(chars_to_trim)
     }
+    /**
+     * Returns a string with leading `chars_to_trim` removed.
+     *
+     * # Arguments
+     *
+     * * s - A string
+     * * chars_to_trim - A vector of chars
+     *
+     */
     #[inline]
     fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str {
-        trim_left_chars(*self, chars_to_trim)
+        if chars_to_trim.is_empty() { return *self; }
+
+        match self.find(|c| !chars_to_trim.contains(&c)) {
+            None => "",
+            Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
+        }
     }
+    /**
+     * Returns a string with trailing `chars_to_trim` removed.
+     *
+     * # Arguments
+     *
+     * * s - A string
+     * * chars_to_trim - A vector of chars
+     *
+     */
     #[inline]
     fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str {
-        trim_right_chars(*self, chars_to_trim)
+        if chars_to_trim.is_empty() { return *self; }
+
+        match self.rfind(|c| !chars_to_trim.contains(&c)) {
+            None => "",
+            Some(last) => {
+                let next = char_range_at(self, last).next;
+                unsafe { raw::slice_bytes(self, 0u, next) }
+            }
+        }
     }
 
 
@@ -2754,56 +2734,56 @@ mod tests {
 
     #[test]
     fn test_trim_left_chars() {
-        assert!(trim_left_chars(" *** foo *** ", []) == " *** foo *** ");
-        assert!(trim_left_chars(" *** foo *** ", ['*', ' ']) == "foo *** ");
-        assert_eq!(trim_left_chars(" ***  *** ", ['*', ' ']), "");
-        assert!(trim_left_chars("foo *** ", ['*', ' ']) == "foo *** ");
+        assert_eq!(" *** foo *** ".trim_left_chars([]), " *** foo *** ");
+        assert_eq!(" *** foo *** ".trim_left_chars(['*', ' ']), "foo *** ");
+        assert_eq!(" ***  *** ".trim_left_chars(['*', ' ']), "");
+        assert_eq!("foo *** ".trim_left_chars(['*', ' ']), "foo *** ");
     }
 
     #[test]
     fn test_trim_right_chars() {
-        assert!(trim_right_chars(" *** foo *** ", []) == " *** foo *** ");
-        assert!(trim_right_chars(" *** foo *** ", ['*', ' ']) == " *** foo");
-        assert_eq!(trim_right_chars(" ***  *** ", ['*', ' ']), "");
-        assert!(trim_right_chars(" *** foo", ['*', ' ']) == " *** foo");
+        assert_eq!(" *** foo *** ".trim_right_chars([]), " *** foo *** ");
+        assert_eq!(" *** foo *** ".trim_right_chars(['*', ' ']), " *** foo");
+        assert_eq!(" ***  *** ".trim_right_chars(['*', ' ']), "");
+        assert_eq!(" *** foo".trim_right_chars(['*', ' ']), " *** foo");
     }
 
     #[test]
     fn test_trim_chars() {
-        assert_eq!(trim_chars(" *** foo *** ", []), " *** foo *** ");
-        assert_eq!(trim_chars(" *** foo *** ", ['*', ' ']), "foo");
-        assert_eq!(trim_chars(" ***  *** ", ['*', ' ']), "");
-        assert_eq!(trim_chars("foo", ['*', ' ']), "foo");
+        assert_eq!(" *** foo *** ".trim_chars([]), " *** foo *** ");
+        assert_eq!(" *** foo *** ".trim_chars(['*', ' ']), "foo");
+        assert_eq!(" ***  *** ".trim_chars(['*', ' ']), "");
+        assert_eq!("foo".trim_chars(['*', ' ']), "foo");
     }
 
     #[test]
     fn test_trim_left() {
-        assert_eq!(trim_left(""), "");
-        assert_eq!(trim_left("a"), "a");
-        assert_eq!(trim_left("    "), "");
-        assert_eq!(trim_left("     blah"), "blah");
-        assert_eq!(trim_left("   \u3000  wut"), "wut");
-        assert_eq!(trim_left("hey "), "hey ");
+        assert_eq!("".trim_left(), "");
+        assert_eq!("a".trim_left(), "a");
+        assert_eq!("    ".trim_left(), "");
+        assert_eq!("     blah".trim_left(), "blah");
+        assert_eq!("   \u3000  wut".trim_left(), "wut");
+        assert_eq!("hey ".trim_left(), "hey ");
     }
 
     #[test]
     fn test_trim_right() {
-        assert_eq!(trim_right(""), "");
-        assert_eq!(trim_right("a"), "a");
-        assert_eq!(trim_right("    "), "");
-        assert_eq!(trim_right("blah     "), "blah");
-        assert_eq!(trim_right("wut   \u3000  "), "wut");
-        assert_eq!(trim_right(" hey"), " hey");
+        assert_eq!("".trim_right(), "");
+        assert_eq!("a".trim_right(), "a");
+        assert_eq!("    ".trim_right(), "");
+        assert_eq!("blah     ".trim_right(), "blah");
+        assert_eq!("wut   \u3000  ".trim_right(), "wut");
+        assert_eq!(" hey".trim_right(), " hey");
     }
 
     #[test]
     fn test_trim() {
-        assert_eq!(trim(""), "");
-        assert_eq!(trim("a"), "a");
-        assert_eq!(trim("    "), "");
-        assert_eq!(trim("    blah     "), "blah");
-        assert_eq!(trim("\nwut   \u3000  "), "wut");
-        assert_eq!(trim(" hey dude "), "hey dude");
+        assert_eq!("".trim(), "");
+        assert_eq!("a".trim(), "a");
+        assert_eq!("    ".trim(), "");
+        assert_eq!("    blah     ".trim(), "blah");
+        assert_eq!("\nwut   \u3000  ".trim(), "wut");
+        assert_eq!(" hey dude ".trim(), "hey dude");
     }
 
     #[test]