summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuarticCat <QuarticCat@pm.me>2024-04-30 20:22:38 +0800
committerLaurenz <laurmaedje@gmail.com>2024-05-17 14:27:59 +0200
commitac888e085af40ca24878b2ec3678a18a11f14ce3 (patch)
tree6bda0a890fb7c99c018c7cc49c0958f6c18c8c2c
parentdd60b3b7ca0bd84950f530270106792554155595 (diff)
Fix suffix computation for Source::replace (#3989)
-rw-r--r--crates/typst-syntax/src/source.rs15
1 files changed, 4 insertions, 11 deletions
diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs
index b4a80d31..a68a53da 100644
--- a/crates/typst-syntax/src/source.rs
+++ b/crates/typst-syntax/src/source.rs
@@ -2,6 +2,7 @@
use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
+use std::iter::zip;
use std::ops::Range;
use std::sync::Arc;
@@ -76,12 +77,8 @@ impl Source {
pub fn replace(&mut self, new: &str) -> Range<usize> {
let old = self.text();
- let mut prefix = old
- .as_bytes()
- .iter()
- .zip(new.as_bytes())
- .take_while(|(x, y)| x == y)
- .count();
+ let mut prefix =
+ zip(old.bytes(), new.bytes()).take_while(|(x, y)| x == y).count();
if prefix == old.len() && prefix == new.len() {
return 0..0;
@@ -91,11 +88,7 @@ impl Source {
prefix -= 1;
}
- let mut suffix = old[prefix..]
- .as_bytes()
- .iter()
- .zip(new[prefix..].as_bytes())
- .rev()
+ let mut suffix = zip(old[prefix..].bytes().rev(), new[prefix..].bytes().rev())
.take_while(|(x, y)| x == y)
.count();