From ac888e085af40ca24878b2ec3678a18a11f14ce3 Mon Sep 17 00:00:00 2001 From: QuarticCat Date: Tue, 30 Apr 2024 20:22:38 +0800 Subject: [PATCH] Fix suffix computation for Source::replace (#3989) --- crates/typst-syntax/src/source.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs index b4a80d318..a68a53da3 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 { 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();