File tree Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Original file line number Diff line number Diff line change @@ -49,14 +49,14 @@ impl<'t> Match<'t> {
4949 /// Returns the matched text.
5050 #[ inline]
5151 pub fn as_bytes ( & self ) -> & ' t [ u8 ] {
52- self . text
52+ & self . text [ self . start .. self . end ]
5353 }
5454
5555 /// Creates a new match from the given haystack and byte offsets.
5656 #[ inline]
5757 fn new ( haystack : & ' t [ u8 ] , start : usize , end : usize ) -> Match < ' t > {
5858 Match {
59- text : & haystack[ start..end ] ,
59+ text : haystack,
6060 start : start,
6161 end : end,
6262 }
@@ -483,20 +483,19 @@ impl Regex {
483483 mut rep : R ,
484484 ) -> Cow < ' t , [ u8 ] > {
485485 if let Some ( rep) = rep. no_expansion ( ) {
486- let mut it = self . find_iter ( text) . enumerate ( ) . peekable ( ) ;
487- if it. peek ( ) . is_none ( ) {
488- return Cow :: Borrowed ( text) ;
489- }
490486 let mut new = Vec :: with_capacity ( text. len ( ) ) ;
491487 let mut last_match = 0 ;
492- for ( i, m) in it {
488+ for ( i, m) in self . find_iter ( text ) . enumerate ( ) {
493489 if limit > 0 && i >= limit {
494490 break
495491 }
496492 new. extend_from_slice ( & text[ last_match..m. start ( ) ] ) ;
497493 new. extend_from_slice ( & rep) ;
498494 last_match = m. end ( ) ;
499495 }
496+ if new. is_empty ( ) {
497+ return Cow :: Borrowed ( text) ;
498+ }
500499 new. extend_from_slice ( & text[ last_match..] ) ;
501500 return Cow :: Owned ( new) ;
502501 }
Original file line number Diff line number Diff line change @@ -59,14 +59,14 @@ impl<'t> Match<'t> {
5959 /// Returns the matched text.
6060 #[ inline]
6161 pub fn as_str ( & self ) -> & ' t str {
62- self . text
62+ & self . text [ self . start .. self . end ]
6363 }
6464
6565 /// Creates a new match from the given haystack and byte offsets.
6666 #[ inline]
6767 fn new ( haystack : & ' t str , start : usize , end : usize ) -> Match < ' t > {
6868 Match {
69- text : & haystack[ start..end ] ,
69+ text : haystack,
7070 start : start,
7171 end : end,
7272 }
@@ -559,20 +559,19 @@ impl Regex {
559559 // replacements inside the replacement string. We just push it
560560 // at each match and be done with it.
561561 if let Some ( rep) = rep. no_expansion ( ) {
562- let mut it = self . find_iter ( text) . enumerate ( ) . peekable ( ) ;
563- if it. peek ( ) . is_none ( ) {
564- return Cow :: Borrowed ( text) ;
565- }
566562 let mut new = String :: with_capacity ( text. len ( ) ) ;
567563 let mut last_match = 0 ;
568- for ( i, m) in it {
564+ for ( i, m) in self . find_iter ( text ) . enumerate ( ) {
569565 if limit > 0 && i >= limit {
570566 break
571567 }
572568 new. push_str ( & text[ last_match..m. start ( ) ] ) ;
573569 new. push_str ( & rep) ;
574570 last_match = m. end ( ) ;
575571 }
572+ if new. is_empty ( ) {
573+ return Cow :: Borrowed ( text) ;
574+ }
576575 new. push_str ( & text[ last_match..] ) ;
577576 return Cow :: Owned ( new) ;
578577 }
You can’t perform that action at this time.
0 commit comments