File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
src/tools/rust-analyzer/crates/stdx/src Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -187,11 +187,19 @@ pub fn is_upper_snake_case(s: &str) -> bool {
187
187
}
188
188
189
189
pub fn replace ( buf : & mut String , from : char , to : & str ) {
190
- if !buf. contains ( from) {
190
+ let replace_count = buf. chars ( ) . filter ( |& ch| ch == from) . count ( ) ;
191
+ if replace_count == 0 {
191
192
return ;
192
193
}
193
- // FIXME: do this in place.
194
- * buf = buf. replace ( from, to) ;
194
+ let from_len = from. len_utf8 ( ) ;
195
+ let additional = to. len ( ) . saturating_sub ( from_len) ;
196
+ buf. reserve ( additional * replace_count) ;
197
+
198
+ let mut end = buf. len ( ) ;
199
+ while let Some ( i) = buf[ ..end] . rfind ( from) {
200
+ buf. replace_range ( i..i + from_len, to) ;
201
+ end = i;
202
+ }
195
203
}
196
204
197
205
#[ must_use]
You can’t perform that action at this time.
0 commit comments