From 6216fa08374bf866290ddf3cab3077f88100b77e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 30 Apr 2021 21:16:04 -0500 Subject: [PATCH] fix(dict)!: Clarify word sizes with Ranges The code was generated with separate min / max, rather than using a Range and ensuring the API is used correctly. --- crates/typos-dict/codegen/src/main.rs | 8 ++++++-- crates/typos-dict/src/dict_codegen.rs | 3 +-- crates/typos-vars/codegen/src/main.rs | 8 ++++++-- crates/typos-vars/src/vars_codegen.rs | 3 +-- src/dict.rs | 8 ++------ 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/typos-dict/codegen/src/main.rs b/crates/typos-dict/codegen/src/main.rs index af0b5b2..90f5822 100644 --- a/crates/typos-dict/codegen/src/main.rs +++ b/crates/typos-dict/codegen/src/main.rs @@ -38,8 +38,12 @@ fn generate(file: &mut W) { 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(); + writeln!( + file, + "pub const WORD_RANGE: std::ops::RangeInclusive = {}..={};", + smallest, largest + ) + .unwrap(); } #[derive(Debug, StructOpt)] diff --git a/crates/typos-dict/src/dict_codegen.rs b/crates/typos-dict/src/dict_codegen.rs index 13008b5..17f194a 100644 --- a/crates/typos-dict/src/dict_codegen.rs +++ b/crates/typos-dict/src/dict_codegen.rs @@ -33649,5 +33649,4 @@ pub static WORD_DICTIONARY: phf::Map, &'static st ]), }; -pub const WORD_MIN: usize = 3; -pub const WORD_MAX: usize = 19; +pub const WORD_RANGE: std::ops::RangeInclusive = 3..=19; diff --git a/crates/typos-vars/codegen/src/main.rs b/crates/typos-vars/codegen/src/main.rs index 981a85c..d861dd9 100644 --- a/crates/typos-vars/codegen/src/main.rs +++ b/crates/typos-vars/codegen/src/main.rs @@ -103,8 +103,12 @@ fn generate_variations(file: &mut W) { writeln!(file, ";").unwrap(); writeln!(file).unwrap(); - writeln!(file, "pub const WORD_MIN: usize = {};", smallest).unwrap(); - writeln!(file, "pub const WORD_MAX: usize = {};", largest).unwrap(); + writeln!( + file, + "pub const WORD_RANGE: std::ops::RangeInclusive = {}..={};", + smallest, largest + ) + .unwrap(); for (symbol, entry) in entries.iter() { if !referenced_symbols.contains(symbol.as_str()) { diff --git a/crates/typos-vars/src/vars_codegen.rs b/crates/typos-vars/src/vars_codegen.rs index e511b01..1381064 100644 --- a/crates/typos-vars/src/vars_codegen.rs +++ b/crates/typos-vars/src/vars_codegen.rs @@ -113082,8 +113082,7 @@ pub static VARS_DICTIONARY: phf::Map< ]), }; -pub const WORD_MIN: usize = 2; -pub const WORD_MAX: usize = 24; +pub const WORD_RANGE: std::ops::RangeInclusive = 2..=24; pub(crate) static ENTRY_ABETTORS_7043394254318611656: VariantsMap = [&["abettors"], &["abetters"], &["abettors"], &["abetters"]]; diff --git a/src/dict.rs b/src/dict.rs index 681716c..016d1fb 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -48,9 +48,7 @@ impl BuiltIn { // Not using `Status` to avoid the allocations fn correct_with_dict(&self, word: &str) -> Option<&'static str> { - const WORD_RANGE: std::ops::RangeInclusive = - typos_dict::WORD_MIN..=typos_dict::WORD_MAX; - if WORD_RANGE.contains(&word.len()) { + if typos_dict::WORD_RANGE.contains(&word.len()) { map_lookup(&typos_dict::WORD_DICTIONARY, word) } else { None @@ -58,9 +56,7 @@ impl BuiltIn { } fn correct_with_vars(&self, word: &str) -> Option> { - const WORD_RANGE: std::ops::RangeInclusive = - typos_vars::WORD_MIN..=typos_vars::WORD_MAX; - if WORD_RANGE.contains(&word.len()) { + if typos_vars::WORD_RANGE.contains(&word.len()) { map_lookup(&typos_vars::VARS_DICTIONARY, word) .map(|variants| self.select_variant(variants)) } else {