Skip to content

Commit 0ddae5e

Browse files
committed
Add str::trim{_,_left_,_right_}chars.
1 parent 15b4734 commit 0ddae5e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Brendan Eich <[email protected]>
2121
Brian Anderson <[email protected]>
2222
Chris Double <[email protected]>
2323
Chris Peterson <[email protected]>
24+
Coppola Ivano <[email protected]>
2425
Damian Gryski <[email protected]>
2526
Damien Grassart <[email protected]>
2627
Daniel Brooks <[email protected]>

src/libcore/str.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export
3939
trim_left,
4040
trim_right,
4141
trim,
42+
trim_left_chars,
43+
trim_right_chars,
44+
trim_chars,
4245

4346
// Transforming strings
4447
to_bytes,
@@ -350,6 +353,58 @@ fn view_shift_char(s: &a/str) -> (char, &a/str) {
350353
/// Prepend a char to a string
351354
fn unshift_char(&s: ~str, ch: char) { s = from_char(ch) + s; }
352355

356+
/**
357+
* Returns a string with leading `chars_to_trim` removed.
358+
*
359+
* # Arguments
360+
*
361+
* * s - A string
362+
* * chars_to_trim - A vector of chars
363+
*
364+
*/
365+
pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
366+
if chars_to_trim.is_empty() { return from_slice(s); }
367+
368+
match find(s, |c| !chars_to_trim.contains(c)) {
369+
None => ~"",
370+
Some(first) => unsafe { unsafe::slice_bytes(s, first, s.len()) }
371+
}
372+
}
373+
374+
/**
375+
* Returns a string with trailing `chars_to_trim` removed.
376+
*
377+
* # Arguments
378+
*
379+
* * s - A string
380+
* * chars_to_trim - A vector of chars
381+
*
382+
*/
383+
pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
384+
if chars_to_trim.is_empty() { return str::from_slice(s); }
385+
386+
match rfind(s, |c| !chars_to_trim.contains(c)) {
387+
None => ~"",
388+
Some(last) => {
389+
let {next, _} = char_range_at(s, last);
390+
unsafe { unsafe::slice_bytes(s, 0u, next) }
391+
}
392+
}
393+
}
394+
395+
/**
396+
* Returns a string with leading and trailing `chars_to_trim` removed.
397+
*
398+
* # Arguments
399+
*
400+
* * s - A string
401+
* * chars_to_trim - A vector of chars
402+
*
403+
*/
404+
pure fn trim_chars(s: &str, chars_to_trim: &[char]) -> ~str {
405+
trim_left_chars(trim_right_chars(s, chars_to_trim), chars_to_trim)
406+
}
407+
353408
/// Returns a string with leading whitespace removed
354409
pure fn trim_left(s: &str) -> ~str {
355410
match find(s, |c| !char::is_whitespace(c)) {
@@ -2731,6 +2786,30 @@ mod tests {
27312786
slice(~"中华Việt Nam", 0u, 2u);
27322787
}
27332788

2789+
#[test]
2790+
fn test_trim_left_chars() {
2791+
assert trim_left_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
2792+
assert trim_left_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo *** ";
2793+
assert trim_left_chars(~" *** *** ", ~['*', ' ']) == ~"";
2794+
assert trim_left_chars(~"foo *** ", ~['*', ' ']) == ~"foo *** ";
2795+
}
2796+
2797+
#[test]
2798+
fn test_trim_right_chars() {
2799+
assert trim_right_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
2800+
assert trim_right_chars(~" *** foo *** ", ~['*', ' ']) == ~" *** foo";
2801+
assert trim_right_chars(~" *** *** ", ~['*', ' ']) == ~"";
2802+
assert trim_right_chars(~" *** foo", ~['*', ' ']) == ~" *** foo";
2803+
}
2804+
2805+
#[test]
2806+
fn test_trim_chars() {
2807+
assert trim_chars(~" *** foo *** ", ~[]) == ~" *** foo *** ";
2808+
assert trim_chars(~" *** foo *** ", ~['*', ' ']) == ~"foo";
2809+
assert trim_chars(~" *** *** ", ~['*', ' ']) == ~"";
2810+
assert trim_chars(~"foo", ~['*', ' ']) == ~"foo";
2811+
}
2812+
27342813
#[test]
27352814
fn test_trim_left() {
27362815
assert (trim_left(~"") == ~"");

0 commit comments

Comments
 (0)