@@ -35,6 +35,7 @@ use raw::{Repr, Slice};
35
35
use result:: Result :: { self , Ok , Err } ;
36
36
use slice:: { self , SliceExt } ;
37
37
use usize;
38
+ use clone:: Clone ;
38
39
39
40
pub use self :: pattern:: { Pattern , Matcher , ReverseMatcher , DoubleEndedMatcher } ;
40
41
@@ -933,41 +934,48 @@ impl Searcher {
933
934
934
935
#[ derive( Clone ) ]
935
936
#[ unstable( feature = "core" , reason = "type may be removed" ) ]
936
- struct OldMatchIndices < ' a > {
937
+ struct OldMatchIndices < ' a , ' b > {
937
938
// constants
938
939
haystack : & ' a str ,
939
- needle : & ' a str ,
940
+ needle : & ' b str ,
940
941
searcher : Searcher
941
942
}
942
943
943
944
/// An iterator over the start and end indices of the matches of a
944
945
/// substring within a larger string
945
- #[ derive( Clone ) ]
946
946
#[ unstable( feature = "core" , reason = "type may be removed" ) ]
947
- pub struct MatchIndices < ' a > ( OldMatchIndices < ' a > ) ;
947
+ pub struct MatchIndices < ' a , P : Pattern < ' a > > ( P :: Matcher ) ;
948
948
949
949
#[ stable]
950
- impl < ' a > Iterator for MatchIndices < ' a > {
950
+ impl < ' a , P : Pattern < ' a > > Iterator for MatchIndices < ' a , P > {
951
951
type Item = ( uint , uint ) ;
952
952
953
953
#[ inline]
954
954
fn next ( & mut self ) -> Option < ( uint , uint ) > {
955
- self . 0 . next ( )
955
+ Matcher :: next ( & mut self . 0 )
956
956
}
957
957
}
958
958
959
959
/// An iterator over the substrings of a string separated by a given
960
960
/// search string
961
- #[ derive( Clone ) ]
962
961
#[ unstable( feature = "core" , reason = "type may be removed" ) ]
963
- pub struct SplitStr < ' a > {
964
- it : OldMatchIndices < ' a > ,
962
+ pub struct SplitStr < ' a , ' b > {
963
+ it : pattern :: StrMatcher < ' a , ' b > ,
965
964
last_end : uint ,
966
965
finished : bool
967
966
}
968
967
968
+ impl < ' a , ' b > Clone for SplitStr < ' a , ' b > {
969
+ fn clone ( & self ) -> Self {
970
+ SplitStr {
971
+ it : Clone :: clone ( & self . it ) ,
972
+ ..* self
973
+ }
974
+ }
975
+ }
976
+
969
977
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
970
- impl < ' a > Iterator for OldMatchIndices < ' a > {
978
+ impl < ' a , ' b > Iterator for OldMatchIndices < ' a , ' b > {
971
979
type Item = ( uint , uint ) ;
972
980
973
981
#[ inline]
@@ -998,22 +1006,22 @@ impl<'a> Iterator for OldMatchIndices<'a> {
998
1006
}
999
1007
1000
1008
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1001
- impl < ' a > Iterator for SplitStr < ' a > {
1009
+ impl < ' a , ' b > Iterator for SplitStr < ' a , ' b > {
1002
1010
type Item = & ' a str ;
1003
1011
1004
1012
#[ inline]
1005
1013
fn next ( & mut self ) -> Option < & ' a str > {
1006
1014
if self . finished { return None ; }
1007
-
1008
- match self . it . next ( ) {
1015
+ let haystack = Matcher :: haystack ( & self . it ) ;
1016
+ match Matcher :: next ( & mut self . it ) {
1009
1017
Some ( ( from, to) ) => {
1010
- let ret = Some ( & self . it . haystack [ self . last_end .. from] ) ;
1018
+ let ret = Some ( & haystack[ self . last_end .. from] ) ;
1011
1019
self . last_end = to;
1012
1020
ret
1013
1021
}
1014
1022
None => {
1015
1023
self . finished = true ;
1016
- Some ( & self . it . haystack [ self . last_end .. self . it . haystack . len ( ) ] )
1024
+ Some ( & haystack[ self . last_end .. ] )
1017
1025
}
1018
1026
}
1019
1027
}
@@ -1375,8 +1383,8 @@ pub trait StrExt {
1375
1383
fn splitn < ' a , P : CharEq > ( & ' a self , count : uint , pat : P ) -> SplitN < ' a , P > ;
1376
1384
fn split_terminator < ' a , P : CharEq > ( & ' a self , pat : P ) -> SplitTerminator < ' a , P > ;
1377
1385
fn rsplitn < ' a , P : CharEq > ( & ' a self , count : uint , pat : P ) -> RSplitN < ' a , P > ;
1378
- fn match_indices < ' a > ( & ' a self , sep : & ' a str ) -> MatchIndices < ' a > ;
1379
- fn split_str < ' a > ( & ' a self , pat : & ' a str ) -> SplitStr < ' a > ;
1386
+ fn match_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> MatchIndices < ' a , P > ;
1387
+ fn split_str < ' a , ' b > ( & ' a self , pat : & ' b str ) -> SplitStr < ' a , ' b > ;
1380
1388
fn lines < ' a > ( & ' a self ) -> Lines < ' a > ;
1381
1389
fn lines_any < ' a > ( & ' a self ) -> LinesAny < ' a > ;
1382
1390
fn char_len ( & self ) -> uint ;
@@ -1478,16 +1486,12 @@ impl StrExt for str {
1478
1486
}
1479
1487
1480
1488
#[ inline]
1481
- fn match_indices < ' a > ( & ' a self , sep : & ' a str ) -> MatchIndices < ' a > {
1482
- MatchIndices ( OldMatchIndices {
1483
- haystack : self ,
1484
- needle : sep,
1485
- searcher : Searcher :: new ( self . as_bytes ( ) , sep. as_bytes ( ) )
1486
- } )
1489
+ fn match_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> MatchIndices < ' a , P > {
1490
+ MatchIndices ( pat. into_matcher ( self ) )
1487
1491
}
1488
1492
1489
1493
#[ inline]
1490
- fn split_str < ' a > ( & ' a self , sep : & ' a str ) -> SplitStr < ' a > {
1494
+ fn split_str < ' a , ' b > ( & ' a self , sep : & ' b str ) -> SplitStr < ' a , ' b > {
1491
1495
SplitStr {
1492
1496
it : self . match_indices ( sep) . 0 ,
1493
1497
last_end : 0 ,
0 commit comments