Add str::trim{_,_left_,_right_}chars.

This commit is contained in:
Graydon Hoare 2012-09-05 16:39:06 -07:00
parent 15b4734d0c
commit 0ddae5ec7d
2 changed files with 80 additions and 0 deletions

View File

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

View File

@ -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)) {
@ -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(~"") == ~"");