9
9
// except according to those terms.
10
10
11
11
use std:: borrow:: Cow ;
12
+ use std:: collections:: HashMap ;
12
13
use std:: fmt;
13
14
use std:: ops:: Index ;
14
15
#[ cfg( feature = "pattern" ) ]
15
16
use std:: str:: pattern:: { Pattern , Searcher , SearchStep } ;
16
17
use std:: str:: FromStr ;
17
- use std:: collections:: HashMap ;
18
18
use std:: sync:: Arc ;
19
19
20
20
use exec:: { Exec , ExecBuilder } ;
@@ -837,16 +837,28 @@ impl NamedGroups {
837
837
}
838
838
}
839
839
840
- fn iter < ' n > ( & ' n self ) -> Box < Iterator < Item = ( & ' n str , usize ) > + ' n > {
840
+ fn iter < ' n > ( & ' n self ) -> NamedGroupsIter < ' n > {
841
841
match * self {
842
- NamedGroups :: Native ( groups) => {
843
- Box :: new ( groups. iter ( ) . map ( |& v| v) )
844
- as Box < Iterator < Item =( & ' n str , usize ) > + ' n >
845
- } ,
846
- NamedGroups :: Dynamic ( ref groups) => {
847
- Box :: new ( groups. iter ( ) . map ( |( s, i) | ( & s[ ..] , * i) ) )
848
- as Box < Iterator < Item =( & ' n str , usize ) > + ' n >
849
- } ,
842
+ NamedGroups :: Native ( g) => NamedGroupsIter :: Native ( g. iter ( ) ) ,
843
+ NamedGroups :: Dynamic ( ref g) => NamedGroupsIter :: Dynamic ( g. iter ( ) ) ,
844
+ }
845
+ }
846
+ }
847
+
848
+ enum NamedGroupsIter < ' n > {
849
+ Native ( :: std:: slice:: Iter < ' static , ( & ' static str , usize ) > ) ,
850
+ Dynamic ( :: std:: collections:: hash_map:: Iter < ' n , String , usize > ) ,
851
+ }
852
+
853
+ impl < ' n > Iterator for NamedGroupsIter < ' n > {
854
+ type Item = ( & ' n str , usize ) ;
855
+
856
+ fn next ( & mut self ) -> Option < Self :: Item > {
857
+ match * self {
858
+ NamedGroupsIter :: Native ( ref mut it) =>
859
+ it. next ( ) . map ( |& v| v) ,
860
+ NamedGroupsIter :: Dynamic ( ref mut it) =>
861
+ it. next ( ) . map ( |( s, i) | ( s. as_ref ( ) , * i) )
850
862
}
851
863
}
852
864
}
@@ -994,6 +1006,7 @@ impl<'t> Index<&'t str> for Captures<'t> {
994
1006
/// expression.
995
1007
///
996
1008
/// `'t` is the lifetime of the matched text.
1009
+ /// `'c` is the lifetime of the captures.
997
1010
pub struct SubCaptures < ' c , ' t : ' c > {
998
1011
idx : usize ,
999
1012
caps : & ' c Captures < ' t > ,
@@ -1017,7 +1030,7 @@ impl<'c, 't> Iterator for SubCaptures<'c, 't> {
1017
1030
///
1018
1031
/// Positions are byte indices in terms of the original string matched.
1019
1032
///
1020
- /// `'t ` is the lifetime of the matched text .
1033
+ /// `'c ` is the lifetime of the captures .
1021
1034
pub struct SubCapturesPos < ' c > {
1022
1035
idx : usize ,
1023
1036
locs : & ' c [ Option < usize > ]
@@ -1044,9 +1057,10 @@ impl<'c> Iterator for SubCapturesPos<'c> {
1044
1057
/// name and the value.
1045
1058
///
1046
1059
/// `'t` is the lifetime of the matched text.
1060
+ /// `'c` is the lifetime of the captures.
1047
1061
pub struct SubCapturesNamed < ' c , ' t : ' c > {
1048
1062
caps : & ' c Captures < ' t > ,
1049
- names : Box < Iterator < Item = ( & ' c str , usize ) > + ' c > ,
1063
+ names : NamedGroupsIter < ' c > ,
1050
1064
}
1051
1065
1052
1066
impl < ' c , ' t : ' c > Iterator for SubCapturesNamed < ' c , ' t > {
0 commit comments