mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Fix to implements in-place stdx::replace
This commit is contained in:
parent
259a01d73d
commit
48dc150f31
@ -187,11 +187,19 @@ pub fn is_upper_snake_case(s: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace(buf: &mut String, from: char, to: &str) {
|
pub fn replace(buf: &mut String, from: char, to: &str) {
|
||||||
if !buf.contains(from) {
|
let replace_count = buf.chars().filter(|&ch| ch == from).count();
|
||||||
|
if replace_count == 0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// FIXME: do this in place.
|
let from_len = from.len_utf8();
|
||||||
*buf = buf.replace(from, to);
|
let additional = to.len().saturating_sub(from_len);
|
||||||
|
buf.reserve(additional * replace_count);
|
||||||
|
|
||||||
|
let mut end = buf.len();
|
||||||
|
while let Some(i) = buf[..end].rfind(from) {
|
||||||
|
buf.replace_range(i..i + from_len, to);
|
||||||
|
end = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user