@@ -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]
@@ -343,4 +351,34 @@ mod tests {
343
351
"fn main() {\n return 92;\n }\n "
344
352
) ;
345
353
}
354
+
355
+ #[ test]
356
+ fn test_replace ( ) {
357
+ #[ track_caller]
358
+ fn test_replace ( src : & str , from : char , to : & str , expected : & str ) {
359
+ let mut s = src. to_owned ( ) ;
360
+ replace ( & mut s, from, to) ;
361
+ assert_eq ! ( s, expected, "from: {from:?}, to: {to:?}" ) ;
362
+ }
363
+
364
+ test_replace ( "" , 'a' , "b" , "" ) ;
365
+ test_replace ( "" , 'a' , "π" , "" ) ;
366
+ test_replace ( "" , 'π' , "a" , "" ) ;
367
+ test_replace ( "a" , 'a' , "b" , "b" ) ;
368
+ test_replace ( "aa" , 'a' , "b" , "bb" ) ;
369
+ test_replace ( "ada" , 'a' , "b" , "bdb" ) ;
370
+ test_replace ( "a" , 'a' , "π" , "π" ) ;
371
+ test_replace ( "π" , 'π' , "a" , "a" ) ;
372
+ test_replace ( "πx" , 'π' , "a" , "ax" ) ;
373
+ test_replace ( "yπx" , 'π' , "a" , "yax" ) ;
374
+ test_replace ( "a,b,c" , ',' , "." , "a.b.c" ) ;
375
+ test_replace ( "a,b,c" , ',' , ".." , "a..b..c" ) ;
376
+ test_replace ( "a.b.c" , '.' , ".." , "a..b..c" ) ;
377
+ test_replace ( "a.b.c" , '.' , ".." , "a..b..c" ) ;
378
+ test_replace ( "aπbπc" , 'π' , "." , "a.b.c" ) ;
379
+ test_replace ( "a.b.c" , '.' , "π" , "aπbπc" ) ;
380
+ test_replace ( "a.b.c" , '.' , "ππ" , "aππbππc" ) ;
381
+ test_replace ( ".a.b.c." , '.' , "()" , "()a()b()c()" ) ;
382
+ test_replace ( ".a.b.c." , '.' , "" , "abc" ) ;
383
+ }
346
384
}
0 commit comments