about summary refs log tree commit diff
path: root/src/tools/rust-analyzer
diff options
context:
space:
mode:
authorA4-Tacks <wdsjxhno1001@163.com>2025-09-20 12:04:59 +0800
committerA4-Tacks <wdsjxhno1001@163.com>2025-09-20 20:58:44 +0800
commit66b1066028ecdf7d4814926600a80222a21a846b (patch)
treeed27cddafce0279793a181a96ed900ead2d2f17c /src/tools/rust-analyzer
parente976c9c762e1d19e177e41a9fe2b2a397229f8c4 (diff)
Fix to implements in-place stdx::replace
Diffstat (limited to 'src/tools/rust-analyzer')
-rw-r--r--src/tools/rust-analyzer/crates/stdx/src/lib.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/stdx/src/lib.rs b/src/tools/rust-analyzer/crates/stdx/src/lib.rs
index 978c50d807b..82f17fb9f78 100644
--- a/src/tools/rust-analyzer/crates/stdx/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/stdx/src/lib.rs
@@ -187,11 +187,19 @@ pub fn is_upper_snake_case(s: &str) -> bool {
 }
 
 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;
     }
-    // FIXME: do this in place.
-    *buf = buf.replace(from, to);
+    let from_len = from.len_utf8();
+    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]