Skip to content

Commit

Permalink
suggester: Refactor out unnecessary unsafe string modification
Browse files Browse the repository at this point in the history
At the end of the day the old and new code are equivalent.
`String::insert` does slightly more work (calculating the UTF-8 length
and such) which is unnecessary since we know that `' '.len_utf8() == 1`,
but this string modification is done at most once per suggestions so the
savings are not perceptible.
  • Loading branch information
the-mikedavis committed Nov 14, 2024
1 parent 10a5f9e commit dfb9e07
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions src/suggester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ impl<'a, S: BuildHasher> Suggester<'a, S> {
let casing_after_dot = classify_casing(after_dot);
if matches!(casing_after_dot, Casing::Init) {
let mut buffer = String::from(word.as_ref());
unsafe {
// SAFETY: b' ' is ASCII and therefore a valid UTF-8 character, so
// we can safely assume it after the '.' character's right boundary
// without invalidating the UTF-8.
buffer.as_mut_vec().insert(dot_idx + 1, b' ');
}
buffer.insert(dot_idx + 1, ' ');
// Nuspell inserts suggestions at the beginning of the list in this block.
// `insert_sug_first(word, out)`
out.insert(0, buffer);
Expand Down

0 comments on commit dfb9e07

Please sign in to comment.