Skip to content

Commit 69cdc58

Browse files
committed
---
yaml --- r: 30350 b: refs/heads/incoming c: 0ddae5e h: refs/heads/master v: v3
1 parent adcf032 commit 69cdc58

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 15b4734d0c1b6e8689d6715ef8b808f5ce6f546c
9+
refs/heads/incoming: 0ddae5ec7dc2c1a4237bb091e52fff5940f18b48
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/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]>

branches/incoming/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)