Skip to content

Commit

Permalink
Add str::trim{_,_left_,_right_}chars.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Sep 5, 2012
1 parent 15b4734 commit 0ddae5e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Brendan Eich <brendan@mozilla.org>
Brian Anderson <banderson@mozilla.com>
Chris Double <chris.double@double.co.nz>
Chris Peterson <cpeterson@mozilla.com>
Coppola Ivano <rgbfirefox@gmail.com>
Damian Gryski <damian@gryski.com>
Damien Grassart <damien@grassart.com>
Daniel Brooks <db48x@db48x.net>
Expand Down
79 changes: 79 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export
trim_left,
trim_right,
trim,
trim_left_chars,
trim_right_chars,
trim_chars,

// Transforming strings
to_bytes,
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -2731,6 +2786,30 @@ mod tests {
slice(~"中华Việt Nam", 0u, 2u);
}

#[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(~"") == ~"");
Expand Down

0 comments on commit 0ddae5e

Please sign in to comment.