Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit f51154c

Browse files
committed
Add real hunspell impl for bulk spell-checking function
1 parent 8609f76 commit f51154c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/spellchecker_hunspell.cc

+32
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ bool HunspellSpellchecker::IsMisspelled(const std::string& word) {
5252

5353
std::vector<MisspelledRange> HunspellSpellchecker::CheckSpelling(const uint16_t *utf16_text, size_t utf16_length) {
5454
std::vector<MisspelledRange> result;
55+
56+
if (!hunspell) {
57+
return result;
58+
}
59+
60+
std::vector<char> utf8_buffer(256);
61+
char *utf8_word = utf8_buffer.data();
62+
63+
bool within_word = false;
64+
size_t word_start = 0;
65+
for (size_t i = 0; i <= utf16_length; i++) {
66+
bool is_alpha = i < utf16_length && std::iswalpha(utf16_text[i]);
67+
68+
if (within_word) {
69+
if (!is_alpha) {
70+
within_word = false;
71+
const w_char *utf16_word = reinterpret_cast<const w_char *>(utf16_text + word_start);
72+
u16_u8(utf8_word, utf8_buffer.size(), utf16_word, i - word_start);
73+
74+
if (hunspell->spell(utf8_word) == 0) {
75+
MisspelledRange range;
76+
range.start = word_start;
77+
range.end = i;
78+
result.push_back(range);
79+
}
80+
}
81+
} else if (is_alpha) {
82+
word_start = i;
83+
within_word = true;
84+
}
85+
}
86+
5587
return result;
5688
}
5789

0 commit comments

Comments
 (0)