We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b12a129 commit 314ab76Copy full SHA for 314ab76
crates/stdx/src/lib.rs
@@ -187,11 +187,19 @@ pub fn is_upper_snake_case(s: &str) -> bool {
187
}
188
189
pub fn replace(buf: &mut String, from: char, to: &str) {
190
- if !buf.contains(from) {
+ let replace_count = buf.chars().filter(|&ch| ch == from).count();
191
+ if replace_count == 0 {
192
return;
193
- // FIXME: do this in place.
194
- *buf = buf.replace(from, to);
+ 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
+ }
203
204
205
#[must_use]
0 commit comments