diff --git a/crates/typos-dict/codegen/src/main.rs b/crates/typos-dict/codegen/src/main.rs index f40e7c4..af0b5b2 100644 --- a/crates/typos-dict/codegen/src/main.rs +++ b/crates/typos-dict/codegen/src/main.rs @@ -13,6 +13,9 @@ fn generate(file: &mut W) { writeln!(file).unwrap(); writeln!(file, "use unicase::UniCase;").unwrap(); + let mut smallest = usize::MAX; + let mut largest = usize::MIN; + writeln!( file, "pub static WORD_DICTIONARY: phf::Map, &'static str> = " @@ -26,12 +29,17 @@ fn generate(file: &mut W) { .map(|r| r.unwrap()) .collect(); for record in &records { + smallest = std::cmp::min(smallest, record[0].len()); + largest = std::cmp::max(largest, record[0].len()); let value = format!(r#""{}""#, &record[1]); builder.entry(unicase::UniCase::new(&record[0]), &value); } let codegenned = builder.build(); writeln!(file, "{}", codegenned).unwrap(); writeln!(file, ";").unwrap(); + writeln!(file).unwrap(); + writeln!(file, "pub const WORD_MIN: usize = {};", smallest).unwrap(); + writeln!(file, "pub const WORD_MAX: usize = {};", largest).unwrap(); } #[derive(Debug, StructOpt)] diff --git a/crates/typos-dict/src/dict_codegen.rs b/crates/typos-dict/src/dict_codegen.rs index 0fee245..13008b5 100644 --- a/crates/typos-dict/src/dict_codegen.rs +++ b/crates/typos-dict/src/dict_codegen.rs @@ -33648,3 +33648,6 @@ pub static WORD_DICTIONARY: phf::Map, &'static st (UniCase::ascii("presumpton"), "presumption"), ]), }; + +pub const WORD_MIN: usize = 3; +pub const WORD_MAX: usize = 19; diff --git a/src/dict.rs b/src/dict.rs index 443a616..504bec1 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -44,7 +44,13 @@ impl BuiltIn { // Not using `Status` to avoid the allocations fn correct_with_dict(&self, word: &str) -> Option<&'static str> { - map_lookup(&typos_dict::WORD_DICTIONARY, word) + const WORD_RANGE: std::ops::RangeInclusive = + typos_dict::WORD_MIN..=typos_dict::WORD_MAX; + if WORD_RANGE.contains(&word.len()) { + map_lookup(&typos_dict::WORD_DICTIONARY, word) + } else { + None + } } fn correct_with_vars(&self, word: &str) -> Option> {