@@ -263,13 +263,22 @@ fn apply_regex_to_chunks<'a>(
263
263
// on the chunks, but the `regex` crate does not support that.
264
264
265
265
let mut search_string = String :: new ( ) ;
266
+ let mut has_text = false ;
266
267
for chunk in & chunks {
267
268
match chunk {
268
- Chunk :: Text { text } => search_string. push_str ( & text. replace ( '\x00' , "" ) ) ,
269
+ Chunk :: Text { text } => {
270
+ has_text = true ;
271
+ search_string. push_str ( & text. replace ( '\x00' , "" ) ) ;
272
+ }
269
273
Chunk :: Redaction { .. } => search_string. push ( '\x00' ) ,
270
274
}
271
275
}
272
276
277
+ if !has_text {
278
+ // Nothing to replace.
279
+ return chunks;
280
+ }
281
+
273
282
// Early exit if this regex does not match and return the original chunks.
274
283
let mut captures_iter = regex. captures_iter ( & search_string) . peekable ( ) ;
275
284
if captures_iter. peek ( ) . is_none ( ) {
@@ -1032,6 +1041,30 @@ mod tests {
1032
1041
assert_eq ! ( chunks, res) ;
1033
1042
}
1034
1043
1044
+ #[ test]
1045
+ fn test_replace_replaced_text_anything ( ) {
1046
+ let chunks = vec ! [ Chunk :: Redaction {
1047
+ text: "[Filtered]" . into( ) ,
1048
+ rule_id: "@password:filter" . into( ) ,
1049
+ ty: RemarkType :: Substituted ,
1050
+ } ] ;
1051
+ let rule = RuleRef {
1052
+ id : "@anything:filter" . into ( ) ,
1053
+ origin : "@anything:filter" . into ( ) ,
1054
+ ty : RuleType :: Anything ,
1055
+ redaction : Redaction :: Replace ( ReplaceRedaction {
1056
+ text : "[Filtered]" . into ( ) ,
1057
+ } ) ,
1058
+ } ;
1059
+ let res = apply_regex_to_chunks (
1060
+ chunks. clone ( ) ,
1061
+ & rule,
1062
+ & Regex :: new ( r#".*"# ) . unwrap ( ) ,
1063
+ ReplaceBehavior :: Groups ( smallvec:: smallvec![ 0 ] ) ,
1064
+ ) ;
1065
+ assert_eq ! ( chunks, res) ;
1066
+ }
1067
+
1035
1068
#[ test]
1036
1069
fn test_scrub_span_data_not_scrubbed ( ) {
1037
1070
let mut span = Annotated :: new ( Span {
0 commit comments