@@ -445,11 +445,11 @@ impl Regex {
445
445
if limit > 0 && i >= limit {
446
446
break
447
447
}
448
- new . extend ( & text[ last_match..s] ) ;
449
- new . extend ( & * rep) ;
448
+ extend_from_slice ( & mut new , & text[ last_match..s] ) ;
449
+ extend_from_slice ( & mut new , & * rep) ;
450
450
last_match = e;
451
451
}
452
- new . extend ( & text[ last_match..] ) ;
452
+ extend_from_slice ( & mut new , & text[ last_match..] ) ;
453
453
return new;
454
454
}
455
455
@@ -463,11 +463,11 @@ impl Regex {
463
463
}
464
464
// unwrap on 0 is OK because captures only reports matches
465
465
let ( s, e) = cap. pos ( 0 ) . unwrap ( ) ;
466
- new . extend ( & text[ last_match..s] ) ;
466
+ extend_from_slice ( & mut new , & text[ last_match..s] ) ;
467
467
rep. replace_append ( & cap, & mut new) ;
468
468
last_match = e;
469
469
}
470
- new . extend ( & text[ last_match..] ) ;
470
+ extend_from_slice ( & mut new , & text[ last_match..] ) ;
471
471
new
472
472
}
473
473
@@ -928,7 +928,7 @@ impl<'a> Replacer for &'a [u8] {
928
928
929
929
impl < F > Replacer for F where F : FnMut ( & Captures ) -> Vec < u8 > {
930
930
fn replace_append ( & mut self , caps : & Captures , dst : & mut Vec < u8 > ) {
931
- dst. extend ( ( * self ) ( caps) )
931
+ extend_from_slice ( dst, & ( * self ) ( caps) ) ;
932
932
}
933
933
}
934
934
@@ -944,10 +944,26 @@ pub struct NoExpand<'r>(pub &'r [u8]);
944
944
945
945
impl < ' a > Replacer for NoExpand < ' a > {
946
946
fn replace_append ( & mut self , _: & Captures , dst : & mut Vec < u8 > ) {
947
- dst . extend ( self . 0 )
947
+ extend_from_slice ( dst , self . 0 ) ;
948
948
}
949
949
950
950
fn no_expansion < ' r > ( & ' r mut self ) -> Option < Cow < ' r , [ u8 ] > > {
951
951
Some ( Cow :: Borrowed ( self . 0 ) )
952
952
}
953
953
}
954
+
955
+ /// This hopefully has the same performance characteristics as
956
+ /// Vec::extend_from_slice (which was introduced in Rust 1.6), but works on
957
+ /// Rust 1.3.
958
+ ///
959
+ /// N.B. Remove this once we do a semver bump. At that point, we'll bump
960
+ /// required Rust version to at least 1.6.
961
+ fn extend_from_slice ( dst : & mut Vec < u8 > , src : & [ u8 ] ) {
962
+ dst. reserve ( src. len ( ) ) ;
963
+ let dst_len = dst. len ( ) ;
964
+ unsafe { dst. set_len ( dst_len + src. len ( ) ) ; }
965
+ let mut dst = & mut dst[ dst_len..dst_len + src. len ( ) ] ;
966
+ for i in 0 ..src. len ( ) {
967
+ dst[ i] = src[ i] ;
968
+ }
969
+ }
0 commit comments