input: Fix RopeExt replace method may not on a char boundary. (#1295)

This commit is contained in:
Jason Lee 2025-09-26 22:27:44 +08:00 committed by GitHub
parent 13e2b08873
commit 328da4da21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -367,6 +367,8 @@ impl RopeExt for Rope {
}
fn replace(&mut self, range: Range<usize>, new_text: &str) {
let range =
self.clip_offset(range.start, Bias::Left)..self.clip_offset(range.end, Bias::Right);
self.remove(range.clone());
self.insert(range.start, new_text);
}
@ -586,6 +588,20 @@ mod tests {
rope.to_string(),
"Hi\nUniverse\r\nThis is a test 中文\nString"
);
// Test for not on a char boundary
let mut rope = Rope::from("中文");
rope.replace(0..1, "New");
assert_eq!(rope.to_string(), "New文");
let mut rope = Rope::from("中文");
rope.replace(0..2, "New");
assert_eq!(rope.to_string(), "New文");
let mut rope = Rope::from("中文");
rope.replace(0..3, "New");
assert_eq!(rope.to_string(), "New文");
let mut rope = Rope::from("中文");
rope.replace(1..4, "New");
assert_eq!(rope.to_string(), "New");
}
#[test]