std: convert str::trim* to methods.

This commit is contained in:
Huon Wilson 2013-06-10 21:03:16 +10:00
parent 1553874149
commit f632f46614
5 changed files with 94 additions and 114 deletions

View File

@ -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()
}
}
}

View File

@ -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;

View File

@ -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)
}
}

View File

@ -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";

View File

@ -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]