Spelling: skip suggestions for words longer than 64 characters (#532)

This commit is contained in:
MarekSpodBiedry
2026-10-12 08:30:19 +02:00
parent 3926d21aa9
commit b5827f4198
+20
View File
@@ -64,7 +64,14 @@ fn known(s: &Session, w: &str) -> bool {
false
}
/// Longest word we generate suggestions for. Candidate text grows with the square of the word length, so longer
/// tokens (no real word is this long) get no suggestions instead of an unbounded allocation.
const MAX_SUGGEST_LEN: usize = 64;
fn edits1(w: &str) -> Vec<String> {
if w.chars().count() > MAX_SUGGEST_LEN {
return Vec::new();
}
let chars: Vec<char> = w.chars().collect();
let mut out = Vec::new();
let letters = "abcdefghijklmnopqrstuvwxyz";
@@ -177,3 +184,16 @@ fn thesaurus(_: &mut Session, p: &Json) -> Result<Json> {
related.truncate(20);
Ok(json!(related))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn edits1_is_capped_for_very_long_words() {
assert_eq!(edits1("helo").len(), 54 * 4 + 25);
assert_eq!(edits1(&"a".repeat(MAX_SUGGEST_LEN)).len(), 54 * MAX_SUGGEST_LEN + 25);
assert!(edits1(&"a".repeat(MAX_SUGGEST_LEN + 1)).is_empty());
assert!(edits1(&"a".repeat(1_000_000)).is_empty());
}
}