PhotoCraft: An open-source, photo editor written by scratch in pure Rust
security(engine): convert spelling offsets in one pass
Dylan M. Taylorac7eca536aa8
diff --git a/crates/engine/src/type_spell_cmds.rs b/crates/engine/src/type_spell_cmds.rsindex 12a63a364..c544634e5 100644--- a/crates/engine/src/type_spell_cmds.rs+++ b/crates/engine/src/type_spell_cmds.rs@@ -27,8 +27,30 @@ fn byte_at(text: &str, ci: usize) -> usize {text.char_indices().nth(ci).map_or(text.len(), |(b, _)| b)}-fn char_at(text: &str, bi: usize) -> usize {- text[..bi.min(text.len())].chars().count()+/// Maps ascending byte offsets to character offsets in a single forward pass. Calling+/// `char_at` per offset rescans the text prefix each time, which is quadratic when a layer+/// has many misspellings; this walks the text once for the whole batch. `offsets` must be+/// sorted ascending (misspelling start/end pairs are, since words are found left to right).+fn char_offsets(text: &str, offsets: &[usize]) -> Vec<usize> {+ // (byte offset, char index) for each char boundary, plus the end-of-text boundary.+ let mut boundaries = text.char_indices().map(|(b, _)| b);+ let mut char_index = 0usize;+ let mut current = boundaries.next();+ let mut result = Vec::with_capacity(offsets.len());+ for &target in offsets {+ let clamped = target.min(text.len());+ // Advance to the first boundary at or past `clamped`; byte offsets from char+ // boundaries land exactly on one, so the char index there is the answer.+ while let Some(b) = current {+ if b >= clamped {+ break;+ }+ char_index += 1;+ current = boundaries.next();+ }+ result.push(char_index);+ }+ result}/// Type layers top to bottom (Layers panel order), skipping fully locked ones.@@ -76,10 +98,17 @@ fn list(s: &Session, p: &Value) -> Result<Value> {let mut out = Vec::new();for id in layers_param(s, p) {let Some(t) = text(&st.doc, id) else { continue };- for m in dict.misspellings(t, &ok) {- out.push(- json!({ "layer": id.0, "start": char_at(t, m.start), "end": char_at(t, m.end), "word": m.word, "suggestions": dict.suggest(&m.word, max) }),- );+ let found = dict.misspellings(t, &ok);+ // Convert every start/end byte offset to a character offset in one pass over the+ // text, instead of rescanning the prefix with char_at twice per misspelling.+ let mut offsets = Vec::with_capacity(found.len() * 2);+ for m in &found {+ offsets.push(m.start);+ offsets.push(m.end);+ }+ let chars = char_offsets(t, &offsets);+ for (i, m) in found.iter().enumerate() {+ out.push(json!({ "layer": id.0, "start": chars[i * 2], "end": chars[i * 2 + 1], "word": m.word, "suggestions": dict.suggest(&m.word, max) }));}}Ok(json!({ "misspellings": out, "count": out.len() }))@@ -263,6 +292,17 @@ pub fn specs() -> Vec<CommandSpec> {mod tests {use super::*;+ #[test]+ fn char_offsets_matches_per_offset_counting() {+ // Multibyte text so byte offsets and char offsets differ. The batch result must+ // equal counting chars in each prefix individually, including the end-of-text offset.+ let text = "café naïve ☃ résumé";+ let targets: Vec<usize> = text.char_indices().map(|(b, _)| b).chain(std::iter::once(text.len())).collect();+ let got = char_offsets(text, &targets);+ let want: Vec<usize> = targets.iter().map(|&b| text[..b.min(text.len())].chars().count()).collect();+ assert_eq!(got, want);+ }+fn session(text: &str) -> (Session, u64) {let mut s = Session::new();s.execute("file.new", json!({"width": 300, "height": 100, "background": "white"})).unwrap();