@@ -928,6 +928,8 @@ struct EmptyNeedle {
928
928
end : usize ,
929
929
is_match_fw : bool ,
930
930
is_match_bw : bool ,
931
+ // Needed in case of an empty haystack, see #85462
932
+ is_finished : bool ,
931
933
}
932
934
933
935
impl < ' a , ' b > StrSearcher < ' a , ' b > {
@@ -941,6 +943,7 @@ impl<'a, 'b> StrSearcher<'a, 'b> {
941
943
end : haystack. len ( ) ,
942
944
is_match_fw : true ,
943
945
is_match_bw : true ,
946
+ is_finished : false ,
944
947
} ) ,
945
948
}
946
949
} else {
@@ -966,13 +969,19 @@ unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> {
966
969
fn next ( & mut self ) -> SearchStep {
967
970
match self . searcher {
968
971
StrSearcherImpl :: Empty ( ref mut searcher) => {
972
+ if searcher. is_finished {
973
+ return SearchStep :: Done ;
974
+ }
969
975
// empty needle rejects every char and matches every empty string between them
970
976
let is_match = searcher. is_match_fw ;
971
977
searcher. is_match_fw = !searcher. is_match_fw ;
972
978
let pos = searcher. position ;
973
979
match self . haystack [ pos..] . chars ( ) . next ( ) {
974
980
_ if is_match => SearchStep :: Match ( pos, pos) ,
975
- None => SearchStep :: Done ,
981
+ None => {
982
+ searcher. is_finished = true ;
983
+ SearchStep :: Done
984
+ }
976
985
Some ( ch) => {
977
986
searcher. position += ch. len_utf8 ( ) ;
978
987
SearchStep :: Reject ( pos, searcher. position )
@@ -1045,12 +1054,18 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for StrSearcher<'a, 'b> {
1045
1054
fn next_back ( & mut self ) -> SearchStep {
1046
1055
match self . searcher {
1047
1056
StrSearcherImpl :: Empty ( ref mut searcher) => {
1057
+ if searcher. is_finished {
1058
+ return SearchStep :: Done ;
1059
+ }
1048
1060
let is_match = searcher. is_match_bw ;
1049
1061
searcher. is_match_bw = !searcher. is_match_bw ;
1050
1062
let end = searcher. end ;
1051
1063
match self . haystack [ ..end] . chars ( ) . next_back ( ) {
1052
1064
_ if is_match => SearchStep :: Match ( end, end) ,
1053
- None => SearchStep :: Done ,
1065
+ None => {
1066
+ searcher. is_finished = true ;
1067
+ SearchStep :: Done
1068
+ }
1054
1069
Some ( ch) => {
1055
1070
searcher. end -= ch. len_utf8 ( ) ;
1056
1071
SearchStep :: Reject ( searcher. end , end)
0 commit comments