diff --git a/src/aff.rs b/src/aff.rs index 2850cae..96bfcf7 100644 --- a/src/aff.rs +++ b/src/aff.rs @@ -57,8 +57,7 @@ pub(crate) struct Condition { /// over it directly to attempt to match the pattern. /// /// This string is non-empty. - // TODO: Box? - pattern: String, + pattern: Box, /// The number of `char`s that the pattern describes. /// /// `Condition` is such a small subset of regex that we can tell only from a linear scan of @@ -124,9 +123,9 @@ pub(crate) struct Affix { /// a prefix and a suffix, both the prefix and suffix should have `crossproduct: true`. pub crossproduct: bool, /// What is stripped from the stem when the affix is applied. - pub strip: Option, + pub strip: Option>, /// What should be added when the affix is applied. - pub add: String, + pub add: Box, /// Condition that the stem should be checked against to query if the affix is relevant. /// /// This is optional in Spellbook. Hunspell and Nuspell represent what we say is `None` as @@ -155,8 +154,8 @@ impl Affix { Ok(Self { flag, crossproduct, - strip: strip.map(|str| str.to_string()), - add: add.to_string(), + strip: strip.map(|str| str.into()), + add: add.into(), flags, condition, phantom_data: PhantomData, @@ -263,7 +262,7 @@ impl Prefix { /// This function `expect`s that the `Prefix`'s `add` is a prefix of the input `word`. pub fn to_stem<'a>(&self, word: &'a str) -> Cow<'a, str> { let stripped = word - .strip_prefix(&self.add) + .strip_prefix(&*self.add) .expect("to_stem should only be called when the `add` is a prefix of the word"); match &self.strip { @@ -290,11 +289,11 @@ impl Prefix { pub fn to_derived(&self, word: &str) -> String { let stripped = match &self.strip { Some(strip) => word - .strip_prefix(strip) + .strip_prefix(&**strip) .expect("to_derived should only be called when `strip` is a prefix of the word"), None => word, }; - let mut stem = self.add.clone(); + let mut stem = self.add.to_string(); stem.push_str(stripped); stem } @@ -326,7 +325,7 @@ impl Suffix { /// This function `expect`s that the `Suffix`'s `add` is a suffix of the input `word`. pub fn to_stem<'a>(&self, word: &'a str) -> Cow<'a, str> { let stripped = word - .strip_suffix(&self.add) + .strip_suffix(&*self.add) .expect("to_stem should only be called when the `add` is a suffix of the word"); match self.strip.as_deref() { @@ -353,7 +352,7 @@ impl Suffix { pub fn to_derived(&self, word: &str) -> String { let mut stem = match &self.strip { Some(strip) => word - .strip_suffix(strip) + .strip_suffix(&**strip) .expect("to_derived should only be called when `strip` is a prefix of the word"), None => word, } @@ -1218,7 +1217,7 @@ pub(crate) struct AffData { pub suffixes: SuffixIndex, pub break_table: BreakTable, pub compound_rules: CompoundRuleTable, - pub compound_syllable_vowels: String, + pub compound_syllable_vowels: Box, pub compound_patterns: Box<[CompoundPattern]>, pub input_conversions: ConversionTable, pub output_conversions: ConversionTable, @@ -1228,7 +1227,7 @@ pub(crate) struct AffData { pub similarities: Box<[SimilarityGroup]>, // phonetic_table: PhoneticTable, pub ignore_chars: Box<[char]>, - pub keyboard_closeness: String, + pub keyboard_closeness: Box, pub try_chars: Box, pub options: AffOptions, // Parsing options. These are preserved so that we can re-use them in `Dictionary::add`. @@ -1511,7 +1510,7 @@ mod test { let prefixes: Vec<_> = index .affixes_of("asdfg") - .map(|prefix| prefix.add.as_str()) + .map(|prefix| prefix.add.as_ref()) .collect(); assert_eq!(&["", "", "a", "as", "as", "asdf"], prefixes.as_slice()); @@ -1533,7 +1532,7 @@ mod test { let suffixes: Vec<_> = index .affixes_of("ahahuub") - .map(|suffix| suffix.add.as_str()) + .map(|suffix| suffix.add.as_ref()) .collect(); assert_eq!( diff --git a/src/aff/parser.rs b/src/aff/parser.rs index 44e1223..9d65962 100644 --- a/src/aff/parser.rs +++ b/src/aff/parser.rs @@ -212,11 +212,11 @@ pub(crate) fn parse<'aff, 'dic, S: BuildHasher + Clone>( if !cx.ignore_chars.is_empty() { for prefix in cx.prefixes.iter_mut() { let add = erase_chars(&prefix.add, &cx.ignore_chars); - prefix.add = add.into_owned(); + prefix.add = add.into(); } for suffix in cx.suffixes.iter_mut() { let add = erase_chars(&suffix.add, &cx.ignore_chars); - suffix.add = add.into_owned(); + suffix.add = add.into(); } } @@ -230,10 +230,10 @@ pub(crate) fn parse<'aff, 'dic, S: BuildHasher + Clone>( input_conversions: cx.input_conversions.into(), output_conversions: cx.output_conversions.into(), compound_rules: cx.compound_rules.into(), - compound_syllable_vowels: cx.compound_syllable_vowels.to_string(), + compound_syllable_vowels: cx.compound_syllable_vowels.into(), compound_patterns: cx.compound_patterns.into(), ignore_chars: cx.ignore_chars.into(), - keyboard_closeness: cx.keyboard_closeness.to_string(), + keyboard_closeness: cx.keyboard_closeness.into(), try_chars: cx.try_chars.into(), options: cx.options, flag_type: cx.flag_type, @@ -1791,7 +1791,7 @@ impl FromStr for Condition { } Ok(Self { - pattern: String::from(s), + pattern: s.into(), chars, }) } @@ -1897,28 +1897,28 @@ mod test { ); assert_eq!( Ok(Condition { - pattern: "foo".to_string(), + pattern: "foo".into(), chars: 3 }), "foo".parse() ); assert_eq!( Ok(Condition { - pattern: "foo[bar]".to_string(), + pattern: "foo[bar]".into(), chars: 4 }), "foo[bar]".parse() ); assert_eq!( Ok(Condition { - pattern: "[foo]bar".to_string(), + pattern: "[foo]bar".into(), chars: 4 }), "[foo]bar".parse() ); assert_eq!( Ok(Condition { - pattern: "foo[bar]baz".to_string(), + pattern: "foo[bar]baz".into(), chars: 7 }), "foo[bar]baz".parse() diff --git a/src/suggester/ngram.rs b/src/suggester/ngram.rs index f2c417e..add7271 100644 --- a/src/suggester/ngram.rs +++ b/src/suggester/ngram.rs @@ -359,14 +359,14 @@ impl<'a, S: BuildHasher> Suggester<'a, S> { if suffix .strip .as_ref() - .is_some_and(|suf| !stem.as_str().ends_with(suf)) + .is_some_and(|suf| !stem.as_str().ends_with(&**suf)) { continue; } if !suffix.condition_matches(stem.as_str()) { continue; } - if !suffix.add.is_empty() && !word.ends_with(&suffix.add) { + if !suffix.add.is_empty() && !word.ends_with(&*suffix.add) { continue; } @@ -398,14 +398,14 @@ impl<'a, S: BuildHasher> Suggester<'a, S> { if prefix .strip .as_ref() - .is_some_and(|pre| !suffixed_stem.starts_with(pre)) + .is_some_and(|pre| !suffixed_stem.starts_with(&**pre)) { continue; } if !prefix.condition_matches(suffixed_stem) { continue; } - if !prefix.add.is_empty() && !word.starts_with(&prefix.add) { + if !prefix.add.is_empty() && !word.starts_with(&*prefix.add) { continue; } @@ -430,14 +430,14 @@ impl<'a, S: BuildHasher> Suggester<'a, S> { if prefix .strip .as_ref() - .is_some_and(|pre| !stem.as_str().starts_with(pre)) + .is_some_and(|pre| !stem.as_str().starts_with(&**pre)) { continue; } if !prefix.condition_matches(stem.as_str()) { continue; } - if !prefix.add.is_empty() && !word.starts_with(&prefix.add) { + if !prefix.add.is_empty() && !word.starts_with(&*prefix.add) { continue; }