@@ -353,7 +353,7 @@ impl Regex {
353
353
/// # extern crate regex; use regex::bytes::Regex;
354
354
/// # fn main() {
355
355
/// let re = Regex::new("[^01]+").unwrap();
356
- /// assert_eq!(re.replace(b"1078910", &b""[..]), b"1010");
356
+ /// assert_eq!(re.replace(b"1078910", &b""[..]), & b"1010"[..] );
357
357
/// # }
358
358
/// ```
359
359
///
@@ -372,7 +372,7 @@ impl Regex {
372
372
/// replacement.extend(&caps[1]);
373
373
/// replacement
374
374
/// });
375
- /// assert_eq!(result, b"Bruce Springsteen");
375
+ /// assert_eq!(result, & b"Bruce Springsteen"[..] );
376
376
/// # }
377
377
/// ```
378
378
///
@@ -386,7 +386,7 @@ impl Regex {
386
386
/// # fn main() {
387
387
/// let re = Regex::new(r"(?P<last>[^,\s]+),\s+(?P<first>\S+)").unwrap();
388
388
/// let result = re.replace(b"Springsteen, Bruce", &b"$first $last"[..]);
389
- /// assert_eq!(result, b"Bruce Springsteen");
389
+ /// assert_eq!(result, & b"Bruce Springsteen"[..] );
390
390
/// # }
391
391
/// ```
392
392
///
@@ -411,10 +411,14 @@ impl Regex {
411
411
///
412
412
/// let re = Regex::new(r"(?P<last>[^,\s]+),\s+(\S+)").unwrap();
413
413
/// let result = re.replace(b"Springsteen, Bruce", NoExpand(b"$2 $last"));
414
- /// assert_eq!(result, b"$2 $last");
414
+ /// assert_eq!(result, & b"$2 $last"[..] );
415
415
/// # }
416
416
/// ```
417
- pub fn replace < R : Replacer > ( & self , text : & [ u8 ] , rep : R ) -> Vec < u8 > {
417
+ pub fn replace < ' t , R : Replacer > (
418
+ & self ,
419
+ text : & ' t [ u8 ] ,
420
+ rep : R ,
421
+ ) -> Cow < ' t , [ u8 ] > {
418
422
self . replacen ( text, 1 , rep)
419
423
}
420
424
@@ -424,7 +428,11 @@ impl Regex {
424
428
///
425
429
/// See the documentation for `replace` for details on how to access
426
430
/// submatches in the replacement text.
427
- pub fn replace_all < R : Replacer > ( & self , text : & [ u8 ] , rep : R ) -> Vec < u8 > {
431
+ pub fn replace_all < ' t , R : Replacer > (
432
+ & self ,
433
+ text : & ' t [ u8 ] ,
434
+ rep : R ,
435
+ ) -> Cow < ' t , [ u8 ] > {
428
436
self . replacen ( text, 0 , rep)
429
437
}
430
438
@@ -434,16 +442,20 @@ impl Regex {
434
442
///
435
443
/// See the documentation for `replace` for details on how to access
436
444
/// submatches in the replacement text.
437
- pub fn replacen < R : Replacer > (
445
+ pub fn replacen < ' t , R : Replacer > (
438
446
& self ,
439
- text : & [ u8 ] ,
447
+ text : & ' t [ u8 ] ,
440
448
limit : usize ,
441
449
mut rep : R ,
442
- ) -> Vec < u8 > {
450
+ ) -> Cow < ' t , [ u8 ] > {
443
451
if let Some ( rep) = rep. no_expansion ( ) {
452
+ let mut it = self . find_iter ( text) . enumerate ( ) . peekable ( ) ;
453
+ if it. peek ( ) . is_none ( ) {
454
+ return Cow :: Borrowed ( text) ;
455
+ }
444
456
let mut new = Vec :: with_capacity ( text. len ( ) ) ;
445
457
let mut last_match = 0 ;
446
- for ( i, ( s, e) ) in self . find_iter ( text ) . enumerate ( ) {
458
+ for ( i, ( s, e) ) in it {
447
459
if limit > 0 && i >= limit {
448
460
break
449
461
}
@@ -452,14 +464,18 @@ impl Regex {
452
464
last_match = e;
453
465
}
454
466
extend_from_slice ( & mut new, & text[ last_match..] ) ;
455
- return new;
467
+ return Cow :: Owned ( new) ;
456
468
}
457
469
458
470
// The slower path, which we use if the replacement needs access to
459
471
// capture groups.
472
+ let mut it = self . captures_iter ( text) . enumerate ( ) . peekable ( ) ;
473
+ if it. peek ( ) . is_none ( ) {
474
+ return Cow :: Borrowed ( text) ;
475
+ }
460
476
let mut new = Vec :: with_capacity ( text. len ( ) ) ;
461
477
let mut last_match = 0 ;
462
- for ( i, cap) in self . captures_iter ( text ) . enumerate ( ) {
478
+ for ( i, cap) in it {
463
479
if limit > 0 && i >= limit {
464
480
break
465
481
}
@@ -470,7 +486,7 @@ impl Regex {
470
486
last_match = e;
471
487
}
472
488
extend_from_slice ( & mut new, & text[ last_match..] ) ;
473
- new
489
+ Cow :: Owned ( new)
474
490
}
475
491
}
476
492
0 commit comments