@@ -3587,6 +3587,78 @@ impl str {
3587
3587
self . trim_matches ( |c : char | c. is_whitespace ( ) )
3588
3588
}
3589
3589
3590
+ /// Returns a string slice with leading whitespace removed.
3591
+ ///
3592
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
3593
+ /// Core Property `White_Space`.
3594
+ ///
3595
+ /// # Text directionality
3596
+ ///
3597
+ /// A string is a sequence of bytes. `start` in this context means the first
3598
+ /// position of that byte string; for a left-to-right language like English or
3599
+ /// Russian, this will be left side; and for right-to-left languages like
3600
+ /// like Arabic or Hebrew, this will be the right side.
3601
+ ///
3602
+ /// # Examples
3603
+ ///
3604
+ /// Basic usage:
3605
+ ///
3606
+ /// ```
3607
+ /// let s = " Hello\tworld\t";
3608
+ ///
3609
+ /// assert_eq!("Hello\tworld\t", s.trim_start());
3610
+ /// ```
3611
+ ///
3612
+ /// Directionality:
3613
+ ///
3614
+ /// ```
3615
+ /// let s = " English";
3616
+ /// assert!(Some('E') == s.trim_start().chars().next());
3617
+ ///
3618
+ /// let s = " עברית";
3619
+ /// assert!(Some('ע') == s.trim_start().chars().next());
3620
+ /// ```
3621
+ #[ unstable( feature = "trim_direction" , issue = "30459" ) ]
3622
+ pub fn trim_start ( & self ) -> & str {
3623
+ self . trim_start_matches ( |c : char | c. is_whitespace ( ) )
3624
+ }
3625
+
3626
+ /// Returns a string slice with trailing whitespace removed.
3627
+ ///
3628
+ /// 'Whitespace' is defined according to the terms of the Unicode Derived
3629
+ /// Core Property `White_Space`.
3630
+ ///
3631
+ /// # Text directionality
3632
+ ///
3633
+ /// A string is a sequence of bytes. `end` in this context means the last
3634
+ /// position of that byte string; for a left-to-right language like English or
3635
+ /// Russian, this will be right side; and for right-to-left languages like
3636
+ /// like Arabic or Hebrew, this will be the left side.
3637
+ ///
3638
+ /// # Examples
3639
+ ///
3640
+ /// Basic usage:
3641
+ ///
3642
+ /// ```
3643
+ /// let s = " Hello\tworld\t";
3644
+ ///
3645
+ /// assert_eq!(" Hello\tworld", s.trim_end());
3646
+ /// ```
3647
+ ///
3648
+ /// Directionality:
3649
+ ///
3650
+ /// ```
3651
+ /// let s = "English ";
3652
+ /// assert!(Some('h') == s.trim_end().chars().rev().next());
3653
+ ///
3654
+ /// let s = "עברית ";
3655
+ /// assert!(Some('ת') == s.trim_end().chars().rev().next());
3656
+ /// ```
3657
+ #[ unstable( feature = "trim_direction" , issue = "30459" ) ]
3658
+ pub fn trim_end ( & self ) -> & str {
3659
+ self . trim_end_matches ( |c : char | c. is_whitespace ( ) )
3660
+ }
3661
+
3590
3662
/// Returns a string slice with leading whitespace removed.
3591
3663
///
3592
3664
/// 'Whitespace' is defined according to the terms of the Unicode Derived
@@ -3619,8 +3691,9 @@ impl str {
3619
3691
/// assert!(Some('ע') == s.trim_left().chars().next());
3620
3692
/// ```
3621
3693
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3694
+ #[ rustc_deprecated( reason = "superseded by `trim_start`" , since = "1.33.0" ) ]
3622
3695
pub fn trim_left ( & self ) -> & str {
3623
- self . trim_left_matches ( | c : char | c . is_whitespace ( ) )
3696
+ self . trim_start ( )
3624
3697
}
3625
3698
3626
3699
/// Returns a string slice with trailing whitespace removed.
@@ -3655,8 +3728,9 @@ impl str {
3655
3728
/// assert!(Some('ת') == s.trim_right().chars().rev().next());
3656
3729
/// ```
3657
3730
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3731
+ #[ rustc_deprecated( reason = "superseded by `trim_end`" , since = "1.33.0" ) ]
3658
3732
pub fn trim_right ( & self ) -> & str {
3659
- self . trim_right_matches ( | c : char | c . is_whitespace ( ) )
3733
+ self . trim_end ( )
3660
3734
}
3661
3735
3662
3736
/// Returns a string slice with all prefixes and suffixes that match a
@@ -3725,14 +3799,14 @@ impl str {
3725
3799
/// Basic usage:
3726
3800
///
3727
3801
/// ```
3728
- /// assert_eq!("11foo1bar11".trim_left_matches ('1'), "foo1bar11");
3729
- /// assert_eq!("123foo1bar123".trim_left_matches (char::is_numeric), "foo1bar123");
3802
+ /// assert_eq!("11foo1bar11".trim_start_matches ('1'), "foo1bar11");
3803
+ /// assert_eq!("123foo1bar123".trim_start_matches (char::is_numeric), "foo1bar123");
3730
3804
///
3731
3805
/// let x: &[_] = &['1', '2'];
3732
- /// assert_eq!("12foo1bar12".trim_left_matches (x), "foo1bar12");
3806
+ /// assert_eq!("12foo1bar12".trim_start_matches (x), "foo1bar12");
3733
3807
/// ```
3734
- #[ stable ( feature = "rust1 " , since = "1.0.0 " ) ]
3735
- pub fn trim_left_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str {
3808
+ #[ unstable ( feature = "trim_direction " , issue = "30459 " ) ]
3809
+ pub fn trim_start_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str {
3736
3810
let mut i = self . len ( ) ;
3737
3811
let mut matcher = pat. into_searcher ( self ) ;
3738
3812
if let Some ( ( a, _) ) = matcher. next_reject ( ) {
@@ -3764,20 +3838,20 @@ impl str {
3764
3838
/// Simple patterns:
3765
3839
///
3766
3840
/// ```
3767
- /// assert_eq!("11foo1bar11".trim_right_matches ('1'), "11foo1bar");
3768
- /// assert_eq!("123foo1bar123".trim_right_matches (char::is_numeric), "123foo1bar");
3841
+ /// assert_eq!("11foo1bar11".trim_end_matches ('1'), "11foo1bar");
3842
+ /// assert_eq!("123foo1bar123".trim_end_matches (char::is_numeric), "123foo1bar");
3769
3843
///
3770
3844
/// let x: &[_] = &['1', '2'];
3771
- /// assert_eq!("12foo1bar12".trim_right_matches (x), "12foo1bar");
3845
+ /// assert_eq!("12foo1bar12".trim_end_matches (x), "12foo1bar");
3772
3846
/// ```
3773
3847
///
3774
3848
/// A more complex pattern, using a closure:
3775
3849
///
3776
3850
/// ```
3777
- /// assert_eq!("1fooX".trim_right_matches (|c| c == '1' || c == 'X'), "1foo");
3851
+ /// assert_eq!("1fooX".trim_end_matches (|c| c == '1' || c == 'X'), "1foo");
3778
3852
/// ```
3779
- #[ stable ( feature = "rust1 " , since = "1.0.0 " ) ]
3780
- pub fn trim_right_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str
3853
+ #[ unstable ( feature = "trim_direction " , issue = "30459 " ) ]
3854
+ pub fn trim_end_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str
3781
3855
where P :: Searcher : ReverseSearcher < ' a >
3782
3856
{
3783
3857
let mut j = 0 ;
@@ -3791,6 +3865,78 @@ impl str {
3791
3865
}
3792
3866
}
3793
3867
3868
+ /// Returns a string slice with all prefixes that match a pattern
3869
+ /// repeatedly removed.
3870
+ ///
3871
+ /// The pattern can be a `&str`, [`char`], or a closure that determines if
3872
+ /// a character matches.
3873
+ ///
3874
+ /// [`char`]: primitive.char.html
3875
+ ///
3876
+ /// # Text directionality
3877
+ ///
3878
+ /// A string is a sequence of bytes. 'Left' in this context means the first
3879
+ /// position of that byte string; for a language like Arabic or Hebrew
3880
+ /// which are 'right to left' rather than 'left to right', this will be
3881
+ /// the _right_ side, not the left.
3882
+ ///
3883
+ /// # Examples
3884
+ ///
3885
+ /// Basic usage:
3886
+ ///
3887
+ /// ```
3888
+ /// assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
3889
+ /// assert_eq!("123foo1bar123".trim_left_matches(char::is_numeric), "foo1bar123");
3890
+ ///
3891
+ /// let x: &[_] = &['1', '2'];
3892
+ /// assert_eq!("12foo1bar12".trim_left_matches(x), "foo1bar12");
3893
+ /// ```
3894
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
3895
+ #[ rustc_deprecated( reason = "superseded by `trim_start_matches`" , since = "1.33.0" ) ]
3896
+ pub fn trim_left_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str {
3897
+ self . trim_start_matches ( pat)
3898
+ }
3899
+
3900
+ /// Returns a string slice with all suffixes that match a pattern
3901
+ /// repeatedly removed.
3902
+ ///
3903
+ /// The pattern can be a `&str`, [`char`], or a closure that
3904
+ /// determines if a character matches.
3905
+ ///
3906
+ /// [`char`]: primitive.char.html
3907
+ ///
3908
+ /// # Text directionality
3909
+ ///
3910
+ /// A string is a sequence of bytes. 'Right' in this context means the last
3911
+ /// position of that byte string; for a language like Arabic or Hebrew
3912
+ /// which are 'right to left' rather than 'left to right', this will be
3913
+ /// the _left_ side, not the right.
3914
+ ///
3915
+ /// # Examples
3916
+ ///
3917
+ /// Simple patterns:
3918
+ ///
3919
+ /// ```
3920
+ /// assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
3921
+ /// assert_eq!("123foo1bar123".trim_right_matches(char::is_numeric), "123foo1bar");
3922
+ ///
3923
+ /// let x: &[_] = &['1', '2'];
3924
+ /// assert_eq!("12foo1bar12".trim_right_matches(x), "12foo1bar");
3925
+ /// ```
3926
+ ///
3927
+ /// A more complex pattern, using a closure:
3928
+ ///
3929
+ /// ```
3930
+ /// assert_eq!("1fooX".trim_right_matches(|c| c == '1' || c == 'X'), "1foo");
3931
+ /// ```
3932
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
3933
+ #[ rustc_deprecated( reason = "superseded by `trim_end_matches`" , since = "1.33.0" ) ]
3934
+ pub fn trim_right_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str
3935
+ where P :: Searcher : ReverseSearcher < ' a >
3936
+ {
3937
+ self . trim_end_matches ( pat)
3938
+ }
3939
+
3794
3940
/// Parses this string slice into another type.
3795
3941
///
3796
3942
/// Because `parse` is so general, it can cause problems with type
0 commit comments