@@ -413,80 +413,6 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
413
413
* s = new_str;
414
414
}
415
415
416
- /**
417
- * Returns a string with leading `chars_to_trim` removed.
418
- *
419
- * # Arguments
420
- *
421
- * * s - A string
422
- * * chars_to_trim - A vector of chars
423
- *
424
- */
425
- pub fn trim_left_chars < ' a > ( s : & ' a str , chars_to_trim : & [ char ] ) -> & ' a str {
426
- if chars_to_trim. is_empty ( ) { return s; }
427
-
428
- match s. find ( |c| !chars_to_trim. contains ( & c) ) {
429
- None => "" ,
430
- Some ( first) => unsafe { raw:: slice_bytes ( s, first, s. len ( ) ) }
431
- }
432
- }
433
-
434
- /**
435
- * Returns a string with trailing `chars_to_trim` removed.
436
- *
437
- * # Arguments
438
- *
439
- * * s - A string
440
- * * chars_to_trim - A vector of chars
441
- *
442
- */
443
- pub fn trim_right_chars < ' a > ( s : & ' a str , chars_to_trim : & [ char ] ) -> & ' a str {
444
- if chars_to_trim. is_empty ( ) { return s; }
445
-
446
- match s. rfind ( |c| !chars_to_trim. contains ( & c) ) {
447
- None => "" ,
448
- Some ( last) => {
449
- let next = char_range_at ( s, last) . next ;
450
- unsafe { raw:: slice_bytes ( s, 0 u, next) }
451
- }
452
- }
453
- }
454
-
455
- /**
456
- * Returns a string with leading and trailing `chars_to_trim` removed.
457
- *
458
- * # Arguments
459
- *
460
- * * s - A string
461
- * * chars_to_trim - A vector of chars
462
- *
463
- */
464
- pub fn trim_chars < ' a > ( s : & ' a str , chars_to_trim : & [ char ] ) -> & ' a str {
465
- trim_left_chars ( trim_right_chars ( s, chars_to_trim) , chars_to_trim)
466
- }
467
-
468
- /// Returns a string with leading whitespace removed
469
- pub fn trim_left < ' a > ( s : & ' a str ) -> & ' a str {
470
- match s. find ( |c| !char:: is_whitespace ( c) ) {
471
- None => "" ,
472
- Some ( first) => unsafe { raw:: slice_bytes ( s, first, s. len ( ) ) }
473
- }
474
- }
475
-
476
- /// Returns a string with trailing whitespace removed
477
- pub fn trim_right < ' a > ( s : & ' a str ) -> & ' a str {
478
- match s. rfind ( |c| !char:: is_whitespace ( c) ) {
479
- None => "" ,
480
- Some ( last) => {
481
- let next = char_range_at ( s, last) . next ;
482
- unsafe { raw:: slice_bytes ( s, 0 u, next) }
483
- }
484
- }
485
- }
486
-
487
- /// Returns a string with leading and trailing whitespace removed
488
- pub fn trim < ' a > ( s : & ' a str ) -> & ' a str { trim_left ( trim_right ( s) ) }
489
-
490
416
/*
491
417
Section: Transforming strings
492
418
*/
@@ -2024,25 +1950,79 @@ impl<'self> StrSlice<'self> for &'self str {
2024
1950
2025
1951
/// Returns a string with leading and trailing whitespace removed
2026
1952
#[inline]
2027
- fn trim(&self) -> &'self str { trim(*self) }
1953
+ fn trim(&self) -> &'self str {
1954
+ self.trim_left().trim_right()
1955
+ }
2028
1956
/// Returns a string with leading whitespace removed
2029
1957
#[inline]
2030
- fn trim_left(&self) -> &'self str { trim_left(*self) }
1958
+ fn trim_left(&self) -> &'self str {
1959
+ match self.find(|c| !char::is_whitespace(c)) {
1960
+ None => " ",
1961
+ Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
1962
+ }
1963
+ }
2031
1964
/// Returns a string with trailing whitespace removed
2032
1965
#[inline]
2033
- fn trim_right(&self) -> &'self str { trim_right(*self) }
1966
+ fn trim_right(&self) -> &'self str {
1967
+ match self.rfind(|c| !char::is_whitespace(c)) {
1968
+ None => " ",
1969
+ Some(last) => {
1970
+ let next = char_range_at(*self, last).next;
1971
+ unsafe { raw::slice_bytes(*self, 0u, next) }
1972
+ }
1973
+ }
1974
+ }
2034
1975
1976
+ /**
1977
+ * Returns a string with leading and trailing `chars_to_trim` removed.
1978
+ *
1979
+ * # Arguments
1980
+ *
1981
+ * * chars_to_trim - A vector of chars
1982
+ *
1983
+ */
2035
1984
#[inline]
2036
1985
fn trim_chars(&self, chars_to_trim: &[char]) -> &'self str {
2037
- trim_chars(* self, chars_to_trim)
1986
+ self.trim_left_chars(chars_to_trim).trim_right_chars( chars_to_trim)
2038
1987
}
1988
+ /**
1989
+ * Returns a string with leading `chars_to_trim` removed.
1990
+ *
1991
+ * # Arguments
1992
+ *
1993
+ * * s - A string
1994
+ * * chars_to_trim - A vector of chars
1995
+ *
1996
+ */
2039
1997
#[inline]
2040
1998
fn trim_left_chars(&self, chars_to_trim: &[char]) -> &'self str {
2041
- trim_left_chars(*self, chars_to_trim)
1999
+ if chars_to_trim.is_empty() { return *self; }
2000
+
2001
+ match self.find(|c| !chars_to_trim.contains(&c)) {
2002
+ None => " ",
2003
+ Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
2004
+ }
2042
2005
}
2006
+ /**
2007
+ * Returns a string with trailing `chars_to_trim` removed.
2008
+ *
2009
+ * # Arguments
2010
+ *
2011
+ * * s - A string
2012
+ * * chars_to_trim - A vector of chars
2013
+ *
2014
+ */
2043
2015
#[inline]
2044
2016
fn trim_right_chars(&self, chars_to_trim: &[char]) -> &'self str {
2045
- trim_right_chars(*self, chars_to_trim)
2017
+ if chars_to_trim.is_empty() { return *self; }
2018
+
2019
+ match self.rfind(|c| !chars_to_trim.contains(&c)) {
2020
+ None => " ",
2021
+ Some(last) => {
2022
+ let next = char_range_at(self, last).next;
2023
+ unsafe { raw::slice_bytes(self, 0u, next) }
2024
+ }
2025
+ }
2046
2026
}
2047
2027
2048
2028
@@ -2754,56 +2734,56 @@ mod tests {
2754
2734
2755
2735
#[ test]
2756
2736
fn test_trim_left_chars( ) {
2757
- assert! ( trim_left_chars ( " *** foo *** " , [ ] ) == " *** foo *** " ) ;
2758
- assert! ( trim_left_chars ( " *** foo *** " , [ '*' , ' ' ] ) == "foo *** " ) ;
2759
- assert_eq!( trim_left_chars ( " *** *** " , [ '*' , ' ' ] ) , "" ) ;
2760
- assert! ( trim_left_chars ( "foo *** " , [ '*' , ' ' ] ) == "foo *** " ) ;
2737
+ assert_eq! ( " *** foo *** " . trim_left_chars ( [ ] ) , " *** foo *** " ) ;
2738
+ assert_eq! ( " *** foo *** " . trim_left_chars ( [ '*' , ' ' ] ) , "foo *** " ) ;
2739
+ assert_eq!( " *** *** " . trim_left_chars ( [ '*' , ' ' ] ) , "" ) ;
2740
+ assert_eq! ( "foo *** " . trim_left_chars ( [ '*' , ' ' ] ) , "foo *** " ) ;
2761
2741
}
2762
2742
2763
2743
#[ test]
2764
2744
fn test_trim_right_chars( ) {
2765
- assert! ( trim_right_chars ( " *** foo *** " , [ ] ) == " *** foo *** " ) ;
2766
- assert! ( trim_right_chars ( " *** foo *** " , [ '*' , ' ' ] ) == " *** foo" ) ;
2767
- assert_eq!( trim_right_chars ( " *** *** " , [ '*' , ' ' ] ) , "" ) ;
2768
- assert! ( trim_right_chars ( " *** foo" , [ '*' , ' ' ] ) == " *** foo" ) ;
2745
+ assert_eq! ( " *** foo *** " . trim_right_chars ( [ ] ) , " *** foo *** " ) ;
2746
+ assert_eq! ( " *** foo *** " . trim_right_chars ( [ '*' , ' ' ] ) , " *** foo" ) ;
2747
+ assert_eq!( " *** *** " . trim_right_chars ( [ '*' , ' ' ] ) , "" ) ;
2748
+ assert_eq! ( " *** foo" . trim_right_chars ( [ '*' , ' ' ] ) , " *** foo" ) ;
2769
2749
}
2770
2750
2771
2751
#[ test]
2772
2752
fn test_trim_chars( ) {
2773
- assert_eq!( trim_chars ( " *** foo *** " , [ ] ) , " *** foo *** " ) ;
2774
- assert_eq!( trim_chars ( " *** foo *** " , [ '*' , ' ' ] ) , "foo" ) ;
2775
- assert_eq!( trim_chars ( " *** *** " , [ '*' , ' ' ] ) , "" ) ;
2776
- assert_eq!( trim_chars ( "foo" , [ '*' , ' ' ] ) , "foo" ) ;
2753
+ assert_eq!( " *** foo *** " . trim_chars ( [ ] ) , " *** foo *** " ) ;
2754
+ assert_eq!( " *** foo *** " . trim_chars ( [ '*' , ' ' ] ) , "foo" ) ;
2755
+ assert_eq!( " *** *** " . trim_chars ( [ '*' , ' ' ] ) , "" ) ;
2756
+ assert_eq!( "foo" . trim_chars ( [ '*' , ' ' ] ) , "foo" ) ;
2777
2757
}
2778
2758
2779
2759
#[ test]
2780
2760
fn test_trim_left( ) {
2781
- assert_eq!( trim_left ( "" ) , "" ) ;
2782
- assert_eq!( trim_left ( "a" ) , "a" ) ;
2783
- assert_eq!( trim_left ( " " ) , "" ) ;
2784
- assert_eq!( trim_left ( " blah" ) , "blah" ) ;
2785
- assert_eq!( trim_left ( " \u3000 wut" ) , "wut" ) ;
2786
- assert_eq!( trim_left ( "hey " ) , "hey " ) ;
2761
+ assert_eq!( "" . trim_left ( ) , "" ) ;
2762
+ assert_eq!( "a" . trim_left ( ) , "a" ) ;
2763
+ assert_eq!( " " . trim_left ( ) , "" ) ;
2764
+ assert_eq!( " blah" . trim_left ( ) , "blah" ) ;
2765
+ assert_eq!( " \u3000 wut" . trim_left ( ) , "wut" ) ;
2766
+ assert_eq!( "hey " . trim_left ( ) , "hey " ) ;
2787
2767
}
2788
2768
2789
2769
#[ test]
2790
2770
fn test_trim_right( ) {
2791
- assert_eq!( trim_right ( "" ) , "" ) ;
2792
- assert_eq!( trim_right ( "a" ) , "a" ) ;
2793
- assert_eq!( trim_right ( " " ) , "" ) ;
2794
- assert_eq!( trim_right ( "blah " ) , "blah" ) ;
2795
- assert_eq!( trim_right ( "wut \u3000 " ) , "wut" ) ;
2796
- assert_eq!( trim_right ( " hey" ) , " hey" ) ;
2771
+ assert_eq!( "" . trim_right ( ) , "" ) ;
2772
+ assert_eq!( "a" . trim_right ( ) , "a" ) ;
2773
+ assert_eq!( " " . trim_right ( ) , "" ) ;
2774
+ assert_eq!( "blah " . trim_right ( ) , "blah" ) ;
2775
+ assert_eq!( "wut \u3000 " . trim_right ( ) , "wut" ) ;
2776
+ assert_eq!( " hey" . trim_right ( ) , " hey" ) ;
2797
2777
}
2798
2778
2799
2779
#[ test]
2800
2780
fn test_trim( ) {
2801
- assert_eq!( trim ( "" ) , "" ) ;
2802
- assert_eq!( trim ( "a" ) , "a" ) ;
2803
- assert_eq!( trim ( " " ) , "" ) ;
2804
- assert_eq!( trim ( " blah " ) , "blah" ) ;
2805
- assert_eq!( trim ( "\n wut \u3000 " ) , "wut" ) ;
2806
- assert_eq!( trim ( " hey dude " ) , "hey dude" ) ;
2781
+ assert_eq!( "" . trim ( ) , "" ) ;
2782
+ assert_eq!( "a" . trim ( ) , "a" ) ;
2783
+ assert_eq!( " " . trim ( ) , "" ) ;
2784
+ assert_eq!( " blah " . trim ( ) , "blah" ) ;
2785
+ assert_eq!( "\n wut \u3000 " . trim ( ) , "wut" ) ;
2786
+ assert_eq!( " hey dude " . trim ( ) , "hey dude" ) ;
2807
2787
}
2808
2788
2809
2789
#[ test]
0 commit comments