Skip to content

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

refactor: get rid of some allocations from `WordSet` (#4087)

Andrew Dunbar2fb11de577a2
diff --git a/harper-core/src/char_string.rs b/harper-core/src/char_string.rs
index 3d0cadb38..b5b101b8f 100644
--- a/harper-core/src/char_string.rs
+++ b/harper-core/src/char_string.rs
@@ -32,10 +32,18 @@ pub trait CharStringExt: private::Sealed {
/// Only normalizes the left side to lowercase and avoids allocations.
fn eq_ch(&self, other: &[char]) -> bool;
+ /// Case-insensitive comparison with a character slice, not requiring the right-hand side to be lowercase.
+ /// Normalizes both sides to lowercase and avoids allocations.
+ fn eq_ch_lenient(&self, other: &[char]) -> bool;
+
/// Case-insensitive comparison with a string slice, assuming the right-hand side is lowercase ASCII.
/// Only normalizes the left side to lowercase and avoids allocations.
fn eq_str(&self, other: &str) -> bool;
+ /// Case-insensitive comparison with a string slice, not requiring the right-hand side to be lowercase.
+ /// Normalizes both sides to lowercase and avoids allocations.
+ fn eq_str_lenient(&self, other: &str) -> bool;
+
/// Case-insensitive comparison with any of a list of string slices, assuming the right-hand side is lowercase ASCII.
/// Only normalizes the left side to lowercase and avoids allocations.
fn eq_any_ignore_ascii_case_str(&self, others: &[&str]) -> bool;
@@ -131,6 +139,24 @@ impl CharStringExt for [char] {
}
}
+ fn eq_str_lenient(&self, other: &str) -> bool {
+ let mut chit = self.iter();
+ let mut strit = other.chars();
+
+ loop {
+ let (c, s) = (chit.next(), strit.next());
+ match (c, s) {
+ (Some(c), Some(s)) => {
+ if !c.eq_ignore_ascii_case(&s) {
+ return false;
+ }
+ }
+ (None, None) => return true,
+ _ => return false,
+ }
+ }
+ }
+
fn eq_ch(&self, other: &[char]) -> bool {
// Assert that the right-hand side is all-lowercase as required
debug_assert!(
@@ -148,6 +174,14 @@ impl CharStringExt for [char] {
.all(|(a, b)| a.to_ascii_lowercase() == *b)
}
+ fn eq_ch_lenient(&self, other: &[char]) -> bool {
+ self.len() == other.len()
+ && self
+ .iter()
+ .zip(other.iter())
+ .all(|(a, b)| a.eq_ignore_ascii_case(b))
+ }
+
fn eq_any_ignore_ascii_case_str(&self, others: &[&str]) -> bool {
others.iter().any(|str| self.eq_str(str))
}
@@ -297,4 +331,24 @@ mod tests {
fn right_side_must_be_all_lowercase_ch() {
assert!(['c'].eq_ch(&['C']))
}
+
+ #[test]
+ fn lenient_right_side_can_be_all_lowercase_str() {
+ assert!(['H', 'e', 'l', 'l', 'o'].eq_str_lenient("hello"));
+ }
+
+ #[test]
+ fn lenient_right_side_can_be_mixed_case_str() {
+ assert!(['H', 'e', 'l', 'l', 'o'].eq_str_lenient("HeLlO"));
+ }
+
+ #[test]
+ fn lenient_right_side_can_be_all_lowercase_ch() {
+ assert!(['H', 'e', 'l', 'l', 'o'].eq_ch_lenient(&['h', 'e', 'l', 'l', 'o']));
+ }
+
+ #[test]
+ fn lenient_right_side_can_be_mixed_case_ch() {
+ assert!(['H', 'e', 'l', 'l', 'o'].eq_ch_lenient(&['H', 'E', 'L', 'L', 'O']));
+ }
}
diff --git a/harper-core/src/patterns/word_set.rs b/harper-core/src/patterns/word_set.rs
index 01f9bba7c..b3a7d6441 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, char_ext::CharExt};
+use crate::{CharString, CharStringExt, 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.
@@ -14,21 +14,32 @@ pub struct WordSet {
impl WordSet {
pub fn add(&mut self, word: &str) {
- let chars = word.chars().collect();
-
- if !self.words.contains(&chars) {
- self.words.push(chars);
+ if !self.contains(word) {
+ self.words.push(
+ word.chars()
+ .map(|c| c.normalized().to_ascii_lowercase())
+ .collect(),
+ );
}
}
pub fn add_chars(&mut self, chars: &[char]) {
- if !self.words.iter().any(|i| i.as_ref() == chars) {
- self.words.push(chars.into());
+ if !self.contains_chars(chars) {
+ self.words.push(
+ chars
+ .iter()
+ .map(|c| c.normalized().to_ascii_lowercase())
+ .collect(),
+ );
}
}
pub fn contains(&self, word: &str) -> bool {
- self.words.contains(&word.chars().collect())
+ self.words.iter().any(|w| w.as_ref().eq_str_lenient(word))
+ }
+
+ pub fn contains_chars(&self, chars: &[char]) -> bool {
+ self.words.iter().any(|w| w.as_ref().eq_ch_lenient(chars))
}
/// Create a new word set that matches against any word in the provided list.
@@ -75,9 +86,8 @@ impl SingleTokenPattern for WordSet {
let partial_match = tok_chars
.iter()
- .map(CharExt::normalized)
- .zip(word.iter().map(CharExt::normalized))
- .all(|(a, b)| a.eq_ignore_ascii_case(&b));
+ .zip(word.iter())
+ .all(|(a, b)| a.normalized().eq_ignore_ascii_case(&b.normalized()));
if partial_match {
return true;