Skip to content

Commit

Permalink
aff: Prefer Box<str> to String
Browse files Browse the repository at this point in the history
As mentioned in the internals guide. There were a few fields of structs
left that were `String`s only for simplicity.
  • Loading branch information
the-mikedavis committed Nov 11, 2024
1 parent 808ae1f commit 5e4dfba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
29 changes: 14 additions & 15 deletions src/aff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub(crate) struct Condition {
/// over it directly to attempt to match the pattern.
///
/// This string is non-empty.
// TODO: Box<str>?
pattern: String,
pattern: Box<str>,
/// 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
Expand Down Expand Up @@ -124,9 +123,9 @@ pub(crate) struct Affix<K> {
/// 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<String>,
pub strip: Option<Box<str>>,
/// What should be added when the affix is applied.
pub add: String,
pub add: Box<str>,
/// 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
Expand Down Expand Up @@ -155,8 +154,8 @@ impl<K: AffixKind> Affix<K> {
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
}
Expand Down Expand Up @@ -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<str>,
pub compound_patterns: Box<[CompoundPattern]>,
pub input_conversions: ConversionTable,
pub output_conversions: ConversionTable,
Expand All @@ -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<str>,
pub try_chars: Box<str>,
pub options: AffOptions,
// Parsing options. These are preserved so that we can re-use them in `Dictionary::add`.
Expand Down Expand Up @@ -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());
Expand All @@ -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!(
Expand Down
18 changes: 9 additions & 9 deletions src/aff/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -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,
Expand Down Expand Up @@ -1791,7 +1791,7 @@ impl FromStr for Condition {
}

Ok(Self {
pattern: String::from(s),
pattern: s.into(),
chars,
})
}
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions src/suggester/ngram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 5e4dfba

Please sign in to comment.