2828// It's cleaner to just turn off the unused_imports warning than to fix them.
2929#![ allow( unused_imports) ]
3030
31- use core:: borrow :: Borrow ;
32- use core:: str :: pattern :: { Pattern , Searcher , ReverseSearcher , DoubleEndedSearcher } ;
31+ use core:: fmt ;
32+ use core:: needle :: { ext , Needle , Searcher , Consumer } ;
3333use core:: mem;
3434use core:: ptr;
3535use core:: iter:: FusedIterator ;
3636use core:: unicode:: conversions;
3737
38- use crate :: borrow:: ToOwned ;
38+ use crate :: borrow:: { Borrow , ToOwned } ;
3939use crate :: boxed:: Box ;
4040use crate :: slice:: { SliceConcatExt , SliceIndex } ;
4141use crate :: string:: String ;
@@ -62,8 +62,6 @@ pub use core::str::{from_utf8, from_utf8_mut, Chars, CharIndices, Bytes};
6262pub use core:: str:: { from_utf8_unchecked, from_utf8_unchecked_mut, ParseBoolError } ;
6363#[ stable( feature = "rust1" , since = "1.0.0" ) ]
6464pub use core:: str:: SplitWhitespace ;
65- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
66- pub use core:: str:: pattern;
6765#[ stable( feature = "encode_utf16" , since = "1.8.0" ) ]
6866pub use core:: str:: EncodeUtf16 ;
6967#[ stable( feature = "split_ascii_whitespace" , since = "1.34.0" ) ]
@@ -255,15 +253,14 @@ impl str {
255253 without modifying the original"]
256254 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
257255 #[ inline]
258- pub fn replace < ' a , P : Pattern < ' a > > ( & ' a self , from : P , to : & str ) -> String {
259- let mut result = String :: new ( ) ;
260- let mut last_end = 0 ;
261- for ( start, part) in self . match_indices ( from) {
262- result. push_str ( unsafe { self . get_unchecked ( last_end..start) } ) ;
263- result. push_str ( to) ;
264- last_end = start + part. len ( ) ;
265- }
266- result. push_str ( unsafe { self . get_unchecked ( last_end..self . len ( ) ) } ) ;
256+ pub fn replace < ' s : ' a , ' a , P > ( & ' s self , from : P , to : & ' a str ) -> String
257+ where
258+ P : Needle < & ' a str > ,
259+ P :: Searcher : Searcher < str > , // FIXME: RFC 2089
260+ P :: Consumer : Consumer < str > , // FIXME: RFC 2089
261+ {
262+ let mut result = String :: with_capacity ( self . len ( ) ) ;
263+ ext:: replace_with ( self , from, |_| to, |s| result. push_str ( s) ) ;
267264 result
268265 }
269266
@@ -295,16 +292,15 @@ impl str {
295292 #[ must_use = "this returns the replaced string as a new allocation, \
296293 without modifying the original"]
297294 #[ stable( feature = "str_replacen" , since = "1.16.0" ) ]
298- pub fn replacen < ' a , P : Pattern < ' a > > ( & ' a self , pat : P , to : & str , count : usize ) -> String {
295+ pub fn replacen < ' s : ' a , ' a , P > ( & ' s self , pat : P , to : & ' a str , count : usize ) -> String
296+ where
297+ P : Needle < & ' a str > ,
298+ P :: Searcher : Searcher < str > , // FIXME: RFC 2089
299+ P :: Consumer : Consumer < str > , // FIXME: RFC 2089
300+ {
299301 // Hope to reduce the times of re-allocation
300- let mut result = String :: with_capacity ( 32 ) ;
301- let mut last_end = 0 ;
302- for ( start, part) in self . match_indices ( pat) . take ( count) {
303- result. push_str ( unsafe { self . get_unchecked ( last_end..start) } ) ;
304- result. push_str ( to) ;
305- last_end = start + part. len ( ) ;
306- }
307- result. push_str ( unsafe { self . get_unchecked ( last_end..self . len ( ) ) } ) ;
302+ let mut result = String :: with_capacity ( self . len ( ) ) ;
303+ ext:: replacen_with ( self , pat, |_| to, count, |s| result. push_str ( s) ) ;
308304 result
309305 }
310306
0 commit comments