File tree 3 files changed +30
-12
lines changed
3 files changed +30
-12
lines changed Original file line number Diff line number Diff line change @@ -496,12 +496,12 @@ impl Regex {
496
496
let mut new = Vec :: with_capacity ( text. len ( ) ) ;
497
497
let mut last_match = 0 ;
498
498
for ( i, m) in it {
499
- if limit > 0 && i >= limit {
500
- break ;
501
- }
502
499
new. extend_from_slice ( & text[ last_match..m. start ( ) ] ) ;
503
500
new. extend_from_slice ( & rep) ;
504
501
last_match = m. end ( ) ;
502
+ if limit > 0 && i >= limit - 1 {
503
+ break ;
504
+ }
505
505
}
506
506
new. extend_from_slice ( & text[ last_match..] ) ;
507
507
return Cow :: Owned ( new) ;
@@ -516,14 +516,14 @@ impl Regex {
516
516
let mut new = Vec :: with_capacity ( text. len ( ) ) ;
517
517
let mut last_match = 0 ;
518
518
for ( i, cap) in it {
519
- if limit > 0 && i >= limit {
520
- break ;
521
- }
522
519
// unwrap on 0 is OK because captures only reports matches
523
520
let m = cap. get ( 0 ) . unwrap ( ) ;
524
521
new. extend_from_slice ( & text[ last_match..m. start ( ) ] ) ;
525
522
rep. replace_append ( & cap, & mut new) ;
526
523
last_match = m. end ( ) ;
524
+ if limit > 0 && i >= limit - 1 {
525
+ break ;
526
+ }
527
527
}
528
528
new. extend_from_slice ( & text[ last_match..] ) ;
529
529
Cow :: Owned ( new)
Original file line number Diff line number Diff line change @@ -554,12 +554,12 @@ impl Regex {
554
554
let mut new = String :: with_capacity ( text. len ( ) ) ;
555
555
let mut last_match = 0 ;
556
556
for ( i, m) in it {
557
- if limit > 0 && i >= limit {
558
- break ;
559
- }
560
557
new. push_str ( & text[ last_match..m. start ( ) ] ) ;
561
558
new. push_str ( & rep) ;
562
559
last_match = m. end ( ) ;
560
+ if limit > 0 && i >= limit - 1 {
561
+ break ;
562
+ }
563
563
}
564
564
new. push_str ( & text[ last_match..] ) ;
565
565
return Cow :: Owned ( new) ;
@@ -574,14 +574,14 @@ impl Regex {
574
574
let mut new = String :: with_capacity ( text. len ( ) ) ;
575
575
let mut last_match = 0 ;
576
576
for ( i, cap) in it {
577
- if limit > 0 && i >= limit {
578
- break ;
579
- }
580
577
// unwrap on 0 is OK because captures only reports matches
581
578
let m = cap. get ( 0 ) . unwrap ( ) ;
582
579
new. push_str ( & text[ last_match..m. start ( ) ] ) ;
583
580
rep. replace_append ( & cap, & mut new) ;
584
581
last_match = m. end ( ) ;
582
+ if limit > 0 && i >= limit - 1 {
583
+ break ;
584
+ }
585
585
}
586
586
new. push_str ( & text[ last_match..] ) ;
587
587
Cow :: Owned ( new)
Original file line number Diff line number Diff line change @@ -228,3 +228,21 @@ replace!(
228
228
bytes!( & std:: borrow:: Cow :: <' _, [ u8 ] >:: Owned ( vec![ b'Z' ] ) ) ,
229
229
"age: Z6"
230
230
) ;
231
+
232
+ #[ test]
233
+ fn replacen_no_captures ( ) {
234
+ let re = regex ! ( r"[0-9]" ) ;
235
+ assert_eq ! (
236
+ re. replacen( text!( "age: 1234" ) , 2 , t!( "Z" ) ) ,
237
+ text!( "age: ZZ34" )
238
+ ) ;
239
+ }
240
+
241
+ #[ test]
242
+ fn replacen_with_captures ( ) {
243
+ let re = regex ! ( r"([0-9])" ) ;
244
+ assert_eq ! (
245
+ re. replacen( text!( "age: 1234" ) , 2 , t!( "${1}Z" ) ) ,
246
+ text!( "age: 1Z2Z34" )
247
+ ) ;
248
+ }
You can’t perform that action at this time.
0 commit comments