Fix suffix computation for Source::replace (#3989)

This commit is contained in:
QuarticCat 2024-04-30 20:22:38 +08:00 committed by GitHub
parent c8cc252a45
commit 9f8cb27aef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
use std::fmt::{self, Debug, Formatter}; use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::iter::zip;
use std::ops::Range; use std::ops::Range;
use std::sync::Arc; use std::sync::Arc;
@ -76,12 +77,8 @@ impl Source {
pub fn replace(&mut self, new: &str) -> Range<usize> { pub fn replace(&mut self, new: &str) -> Range<usize> {
let old = self.text(); let old = self.text();
let mut prefix = old let mut prefix =
.as_bytes() zip(old.bytes(), new.bytes()).take_while(|(x, y)| x == y).count();
.iter()
.zip(new.as_bytes())
.take_while(|(x, y)| x == y)
.count();
if prefix == old.len() && prefix == new.len() { if prefix == old.len() && prefix == new.len() {
return 0..0; return 0..0;
@ -91,11 +88,7 @@ impl Source {
prefix -= 1; prefix -= 1;
} }
let mut suffix = old[prefix..] let mut suffix = zip(old[prefix..].bytes().rev(), new[prefix..].bytes().rev())
.as_bytes()
.iter()
.zip(new[prefix..].as_bytes())
.rev()
.take_while(|(x, y)| x == y) .take_while(|(x, y)| x == y)
.count(); .count();