|
2 | 2 | #include <cwctype> |
3 | 3 | #include <algorithm> |
4 | 4 | #include "../vendor/hunspell/src/hunspell/hunspell.hxx" |
5 | | -#include "../vendor/hunspell/src/hunspell/csutil.hxx" |
6 | 5 | #include "spellchecker_hunspell.h" |
7 | 6 |
|
8 | 7 | namespace spellchecker { |
9 | 8 |
|
10 | | -HunspellSpellchecker::HunspellSpellchecker() : hunspell(NULL) { } |
| 9 | +HunspellSpellchecker::HunspellSpellchecker() : hunspell(NULL), transcoder(NewTranscoder()) { } |
| 10 | + |
11 | 11 | HunspellSpellchecker::~HunspellSpellchecker() { |
12 | 12 | if (hunspell) { |
13 | 13 | delete hunspell; |
14 | 14 | } |
| 15 | + |
| 16 | + if (transcoder) { |
| 17 | + FreeTranscoder(transcoder); |
| 18 | + } |
15 | 19 | } |
16 | 20 |
|
17 | 21 | bool HunspellSpellchecker::SetDictionary(const std::string& language, const std::string& dirname) { |
@@ -53,27 +57,29 @@ bool HunspellSpellchecker::IsMisspelled(const std::string& word) { |
53 | 57 | std::vector<MisspelledRange> HunspellSpellchecker::CheckSpelling(const uint16_t *utf16_text, size_t utf16_length) { |
54 | 58 | std::vector<MisspelledRange> result; |
55 | 59 |
|
56 | | - if (!hunspell) { |
| 60 | + if (!hunspell || !transcoder) { |
57 | 61 | return result; |
58 | 62 | } |
59 | 63 |
|
60 | 64 | std::vector<char> utf8_buffer(256); |
61 | | - char *utf8_word = utf8_buffer.data(); |
62 | 65 |
|
63 | 66 | size_t word_start = 0; |
64 | 67 | bool within_word = false; |
65 | 68 | for (size_t i = 0; i < utf16_length; i++) { |
66 | | - bool is_word_character = iswalpha(utf16_text[i]); |
| 69 | + uint16_t c = utf16_text[i]; |
| 70 | + bool is_word_character = iswalpha(c); |
67 | 71 | if (within_word) { |
68 | 72 | if (!is_word_character) { |
69 | 73 | within_word = false; |
70 | | - const w_char *utf16_word = reinterpret_cast<const w_char *>(utf16_text + word_start); |
71 | | - u16_u8(utf8_word, utf8_buffer.size(), utf16_word, i - word_start); |
72 | | - if (hunspell->spell(utf8_word) == 0) { |
73 | | - MisspelledRange range; |
74 | | - range.start = word_start; |
75 | | - range.end = i; |
76 | | - result.push_back(range); |
| 74 | + |
| 75 | + bool converted = TranscodeUTF16ToUTF8(transcoder, (char *)utf8_buffer.data(), utf8_buffer.size(), utf16_text + word_start, i - word_start); |
| 76 | + if (converted) { |
| 77 | + if (hunspell->spell(utf8_buffer.data()) == 0) { |
| 78 | + MisspelledRange range; |
| 79 | + range.start = word_start; |
| 80 | + range.end = i; |
| 81 | + result.push_back(range); |
| 82 | + } |
77 | 83 | } |
78 | 84 | } |
79 | 85 | } else if (is_word_character) { |
|
0 commit comments