From 0ddae5ec7dc2c1a4237bb091e52fff5940f18b48 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 5 Sep 2012 16:39:06 -0700 Subject: [PATCH] Add str::trim{_,_left_,_right_}chars. --- AUTHORS.txt | 1 + src/libcore/str.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index ff682b93a02..1d77347b03e 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -21,6 +21,7 @@ Brendan Eich Brian Anderson Chris Double Chris Peterson +Coppola Ivano Damian Gryski Damien Grassart Daniel Brooks 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)) { @@ -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(~"") == ~"");