Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

refactor(core): merge semantically identical funcs (#2458)

Steve Kurchc8da8ea37f9f
diff --git a/harper-core/src/char_ext.rs b/harper-core/src/char_ext.rs
index ef218a988..eec3812f5 100644
--- a/harper-core/src/char_ext.rs
+++ b/harper-core/src/char_ext.rs
@@ -19,7 +19,7 @@ pub trait CharExt: private::Sealed {
///
/// Checks whether the character is in the set (A, E, I, O, U); case-insensitive.
fn is_vowel(&self) -> bool;
- fn normalized(self) -> Self;
+ fn normalized(&self) -> Self;
}
impl CharExt for char {
@@ -34,10 +34,12 @@ impl CharExt for char {
&& self.script() == Script::Latin
}
- fn normalized(self) -> Self {
+ fn normalized(&self) -> Self {
match self {
- '’' | '‘' | 'ʼ' | ''' => '\'',
- _ => self,
+ '\u{2018}' | '\u{2019}' | '\u{02BC}' | '\u{FF07}' => '\'',
+ '\u{201C}' | '\u{201D}' | '\u{FF02}' => '"',
+ '\u{2013}' | '\u{2014}' | '\u{2212}' | '\u{FF0D}' => '-',
+ _ => *self,
}
}
diff --git a/harper-core/src/patterns/word_set.rs b/harper-core/src/patterns/word_set.rs
index ec3395c4d..bf61ca0f4 100644
--- a/harper-core/src/patterns/word_set.rs
+++ b/harper-core/src/patterns/word_set.rs
@@ -1,7 +1,7 @@
use super::SingleTokenPattern;
use smallvec::SmallVec;
-use crate::{CharString, Token};
+use crate::{CharString, Token, char_ext::CharExt};
/// A [`super::Pattern`] that matches against any of a set of provided words.
/// For small sets of short words, it doesn't allocate.
@@ -56,19 +56,10 @@ impl SingleTokenPattern for WordSet {
continue;
}
- fn canonical(c: &char) -> char {
- match c {
- '\u{2018}' | '\u{2019}' | '\u{02BC}' | '\u{FF07}' => '\'',
- '\u{201C}' | '\u{201D}' | '\u{FF02}' => '"',
- '\u{2013}' | '\u{2014}' | '\u{2212}' | '\u{FF0D}' => '-',
- _ => *c,
- }
- }
-
let partial_match = tok_chars
.iter()
- .map(canonical)
- .zip(word.iter().map(canonical))
+ .map(CharExt::normalized)
+ .zip(word.iter().map(CharExt::normalized))
.all(|(a, b)| a.eq_ignore_ascii_case(&b));
if partial_match {