diff options
| author | QuarticCat <QuarticCat@pm.me> | 2024-04-30 20:22:38 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-30 12:22:38 +0000 |
| commit | 9f8cb27aef3045a6f86de8bca1b0ec5a54a74901 (patch) | |
| tree | 2c9c7f11a176d95db0e06d5e31ee3e7675d0d6bb /crates/typst-syntax | |
| parent | c8cc252a4564369abcfaa81ee2d5e0bf453a7cbc (diff) | |
Fix suffix computation for Source::replace (#3989)
Diffstat (limited to 'crates/typst-syntax')
| -rw-r--r-- | crates/typst-syntax/src/source.rs | 15 |
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(); |
