|
39 | 39 | trim_left,
|
40 | 40 | trim_right,
|
41 | 41 | trim,
|
| 42 | + trim_left_chars, |
| 43 | + trim_right_chars, |
| 44 | + trim_chars, |
42 | 45 |
|
43 | 46 | // Transforming strings
|
44 | 47 | to_bytes,
|
@@ -350,6 +353,58 @@ fn view_shift_char(s: &a/str) -> (char, &a/str) {
|
350 | 353 | /// Prepend a char to a string
|
351 | 354 | fn unshift_char(&s: ~str, ch: char) { s = from_char(ch) + s; }
|
352 | 355 |
|
| 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 | + |
353 | 408 | /// Returns a string with leading whitespace removed
|
354 | 409 | pure fn trim_left(s: &str) -> ~str {
|
355 | 410 | match find(s, |c| !char::is_whitespace(c)) {
|
@@ -2731,6 +2786,30 @@ mod tests {
|
2731 | 2786 | slice(~"中华Việt Nam", 0u, 2u);
|
2732 | 2787 | }
|
2733 | 2788 |
|
| 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 | + |
2734 | 2813 | #[test]
|
2735 | 2814 | fn test_trim_left() {
|
2736 | 2815 | assert (trim_left(~"") == ~"");
|
|
0 commit comments