@@ -82,6 +82,8 @@ pub use core::str::{SplitN, RSplitN};
82
82
pub use core:: str:: { from_utf8, CharEq , Chars , CharIndices , Bytes } ;
83
83
pub use core:: str:: { from_utf8_unchecked, from_c_str, ParseBoolError } ;
84
84
pub use unicode:: str:: { Words , Graphemes , GraphemeIndices } ;
85
+ pub use core:: str:: Pattern ;
86
+ pub use core:: str:: { Searcher , ReverseSearcher , DoubleEndedSearcher , SearchStep } ;
85
87
86
88
/*
87
89
Section: Creating a string
@@ -530,7 +532,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
530
532
/// assert!("bananas".contains("nana"));
531
533
/// ```
532
534
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
533
- fn contains ( & self , pat : & str ) -> bool {
535
+ fn contains < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool {
534
536
core_str:: StrExt :: contains ( & self [ ..] , pat)
535
537
}
536
538
@@ -545,9 +547,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
545
547
/// ```rust
546
548
/// assert!("hello".contains_char('e'));
547
549
/// ```
548
- #[ unstable( feature = "collections" ,
549
- reason = "might get removed in favour of a more generic contains() " ) ]
550
- fn contains_char < P : CharEq > ( & self , pat : P ) -> bool {
550
+ #[ unstable( feature = "collections" ) ]
551
+ # [ deprecated ( since = "1.0.0" , reason = "use `contains()` with a char " ) ]
552
+ fn contains_char < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool {
551
553
core_str:: StrExt :: contains_char ( & self [ ..] , pat)
552
554
}
553
555
@@ -603,7 +605,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
603
605
/// assert_eq!(v, vec![""]);
604
606
/// ```
605
607
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
606
- fn split < P : CharEq > ( & self , pat : P ) -> Split < P > {
608
+ fn split < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Split < ' a , P > {
607
609
core_str:: StrExt :: split ( & self [ ..] , pat)
608
610
}
609
611
@@ -630,7 +632,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
630
632
/// assert_eq!(v, vec![""]);
631
633
/// ```
632
634
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
633
- fn splitn < P : CharEq > ( & self , count : usize , pat : P ) -> SplitN < P > {
635
+ fn splitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> SplitN < ' a , P > {
634
636
core_str:: StrExt :: splitn ( & self [ ..] , count, pat)
635
637
}
636
638
@@ -658,8 +660,8 @@ pub trait StrExt: Index<RangeFull, Output = str> {
658
660
/// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect();
659
661
/// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]);
660
662
/// ```
661
- #[ unstable ( feature = "collections " , reason = "might get removed " ) ]
662
- fn split_terminator < P : CharEq > ( & self , pat : P ) -> SplitTerminator < P > {
663
+ #[ stable ( feature = "rust1 " , since = "1.0.0 " ) ]
664
+ fn split_terminator < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> SplitTerminator < ' a , P > {
663
665
core_str:: StrExt :: split_terminator ( & self [ ..] , pat)
664
666
}
665
667
@@ -680,7 +682,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
680
682
/// assert_eq!(v, vec!["leopard", "tiger", "lionX"]);
681
683
/// ```
682
684
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
683
- fn rsplitn < P : CharEq > ( & self , count : usize , pat : P ) -> RSplitN < P > {
685
+ fn rsplitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> RSplitN < ' a , P > {
684
686
core_str:: StrExt :: rsplitn ( & self [ ..] , count, pat)
685
687
}
686
688
@@ -706,7 +708,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
706
708
/// ```
707
709
#[ unstable( feature = "collections" ,
708
710
reason = "might have its iterator type changed" ) ]
709
- fn match_indices < ' a > ( & ' a self , pat : & ' a str ) -> MatchIndices < ' a > {
711
+ // NB: Right now MatchIndices yields `(usize, usize)`,
712
+ // but it would be more consistent and useful to return `(usize, &str)`
713
+ fn match_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> MatchIndices < ' a , P > {
710
714
core_str:: StrExt :: match_indices ( & self [ ..] , pat)
711
715
}
712
716
@@ -721,9 +725,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
721
725
/// let v: Vec<&str> = "1abcabc2".split_str("abc").collect();
722
726
/// assert_eq!(v, vec!["1", "", "2"]);
723
727
/// ```
724
- #[ unstable( feature = "collections" ,
725
- reason = "might get removed in the future in favor of a more generic split() " ) ]
726
- fn split_str < ' a > ( & ' a self , pat : & ' a str ) -> SplitStr < ' a > {
728
+ #[ unstable( feature = "collections" ) ]
729
+ # [ deprecated ( since = "1.0.0" , reason = "use `split()` with a `&str` " ) ]
730
+ fn split_str < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> SplitStr < ' a , P > {
727
731
core_str:: StrExt :: split_str ( & self [ ..] , pat)
728
732
}
729
733
@@ -825,7 +829,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
825
829
/// assert!("banana".starts_with("ba"));
826
830
/// ```
827
831
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
828
- fn starts_with ( & self , pat : & str ) -> bool {
832
+ fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool {
829
833
core_str:: StrExt :: starts_with ( & self [ ..] , pat)
830
834
}
831
835
@@ -837,7 +841,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
837
841
/// assert!("banana".ends_with("nana"));
838
842
/// ```
839
843
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
840
- fn ends_with ( & self , pat : & str ) -> bool {
844
+ fn ends_with < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> bool
845
+ where P :: Searcher : ReverseSearcher < ' a >
846
+ {
841
847
core_str:: StrExt :: ends_with ( & self [ ..] , pat)
842
848
}
843
849
@@ -857,7 +863,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
857
863
/// assert_eq!("123foo1bar123".trim_matches(|c: char| c.is_numeric()), "foo1bar");
858
864
/// ```
859
865
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
860
- fn trim_matches < P : CharEq > ( & self , pat : P ) -> & str {
866
+ fn trim_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str
867
+ where P :: Searcher : DoubleEndedSearcher < ' a >
868
+ {
861
869
core_str:: StrExt :: trim_matches ( & self [ ..] , pat)
862
870
}
863
871
@@ -877,7 +885,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
877
885
/// assert_eq!("123foo1bar123".trim_left_matches(|c: char| c.is_numeric()), "foo1bar123");
878
886
/// ```
879
887
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
880
- fn trim_left_matches < P : CharEq > ( & self , pat : P ) -> & str {
888
+ fn trim_left_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str {
881
889
core_str:: StrExt :: trim_left_matches ( & self [ ..] , pat)
882
890
}
883
891
@@ -897,7 +905,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
897
905
/// assert_eq!("123foo1bar123".trim_right_matches(|c: char| c.is_numeric()), "123foo1bar");
898
906
/// ```
899
907
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
900
- fn trim_right_matches < P : CharEq > ( & self , pat : P ) -> & str {
908
+ fn trim_right_matches < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> & ' a str
909
+ where P :: Searcher : ReverseSearcher < ' a >
910
+ {
901
911
core_str:: StrExt :: trim_right_matches ( & self [ ..] , pat)
902
912
}
903
913
@@ -1074,7 +1084,7 @@ pub trait StrExt: Index<RangeFull, Output = str> {
1074
1084
/// assert_eq!(s.find(x), None);
1075
1085
/// ```
1076
1086
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1077
- fn find < P : CharEq > ( & self , pat : P ) -> Option < usize > {
1087
+ fn find < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Option < usize > {
1078
1088
core_str:: StrExt :: find ( & self [ ..] , pat)
1079
1089
}
1080
1090
@@ -1102,7 +1112,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
1102
1112
/// assert_eq!(s.rfind(x), None);
1103
1113
/// ```
1104
1114
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1105
- fn rfind < P : CharEq > ( & self , pat : P ) -> Option < usize > {
1115
+ fn rfind < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Option < usize >
1116
+ where P :: Searcher : ReverseSearcher < ' a >
1117
+ {
1106
1118
core_str:: StrExt :: rfind ( & self [ ..] , pat)
1107
1119
}
1108
1120
@@ -1125,9 +1137,9 @@ pub trait StrExt: Index<RangeFull, Output = str> {
1125
1137
/// assert_eq!(s.find_str("老虎 L"), Some(6));
1126
1138
/// assert_eq!(s.find_str("muffin man"), None);
1127
1139
/// ```
1128
- #[ unstable( feature = "collections" ,
1129
- reason = "might get removed in favor of a more generic find in the future " ) ]
1130
- fn find_str ( & self , needle : & str ) -> Option < usize > {
1140
+ #[ unstable( feature = "collections" ) ]
1141
+ # [ deprecated ( since = "1.0.0" , reason = "use `find()` with a `&str` " ) ]
1142
+ fn find_str < ' a , P : Pattern < ' a > > ( & ' a self , needle : P ) -> Option < usize > {
1131
1143
core_str:: StrExt :: find_str ( & self [ ..] , needle)
1132
1144
}
1133
1145
@@ -2887,22 +2899,6 @@ mod bench {
2887
2899
b. iter ( || assert_eq ! ( s. split( 'V' ) . count( ) , 3 ) ) ;
2888
2900
}
2889
2901
2890
- #[ bench]
2891
- fn split_unicode_not_ascii ( b : & mut Bencher ) {
2892
- struct NotAscii ( char ) ;
2893
- impl CharEq for NotAscii {
2894
- fn matches ( & mut self , c : char ) -> bool {
2895
- let NotAscii ( cc) = * self ;
2896
- cc == c
2897
- }
2898
- fn only_ascii ( & self ) -> bool { false }
2899
- }
2900
- let s = "ประเทศไทย中华Việt Namประเทศไทย中华Việt Nam" ;
2901
-
2902
- b. iter ( || assert_eq ! ( s. split( NotAscii ( 'V' ) ) . count( ) , 3 ) ) ;
2903
- }
2904
-
2905
-
2906
2902
#[ bench]
2907
2903
fn split_ascii ( b : & mut Bencher ) {
2908
2904
let s = "Mary had a little lamb, Little lamb, little-lamb." ;
@@ -2911,23 +2907,6 @@ mod bench {
2911
2907
b. iter ( || assert_eq ! ( s. split( ' ' ) . count( ) , len) ) ;
2912
2908
}
2913
2909
2914
- #[ bench]
2915
- fn split_not_ascii ( b : & mut Bencher ) {
2916
- struct NotAscii ( char ) ;
2917
- impl CharEq for NotAscii {
2918
- #[ inline]
2919
- fn matches ( & mut self , c : char ) -> bool {
2920
- let NotAscii ( cc) = * self ;
2921
- cc == c
2922
- }
2923
- fn only_ascii ( & self ) -> bool { false }
2924
- }
2925
- let s = "Mary had a little lamb, Little lamb, little-lamb." ;
2926
- let len = s. split ( ' ' ) . count ( ) ;
2927
-
2928
- b. iter ( || assert_eq ! ( s. split( NotAscii ( ' ' ) ) . count( ) , len) ) ;
2929
- }
2930
-
2931
2910
#[ bench]
2932
2911
fn split_extern_fn ( b : & mut Bencher ) {
2933
2912
let s = "Mary had a little lamb, Little lamb, little-lamb." ;
0 commit comments