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

Commit 7bfd86d

Browse files
committed
Pass UTF16-encoded string to CheckSpelling
1 parent 3f0bc25 commit 7bfd86d

File tree

8 files changed

+18
-12
lines changed

8 files changed

+18
-12
lines changed

src/main.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ class Spellchecker : public Nan::ObjectWrap {
5757
}
5858

5959
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
60-
String::Utf8Value text(info[0]);
60+
Handle<String> string = Handle<String>::Cast(info[0]);
61+
std::vector<uint16_t> text(string->Length());
62+
string->Write(reinterpret_cast<uint16_t *>(text.data()));
6163

62-
std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(*text, text.length());
64+
std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(text.data(), text.size());
6365

6466
Local<Array> result = Nan::New<Array>();
6567
std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin();

src/spellchecker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <string>
55
#include <vector>
6+
#include <stdint.h>
67

78
namespace spellchecker {
89

@@ -22,7 +23,7 @@ class SpellcheckerImplementation {
2223
// Returns true if the word is misspelled.
2324
virtual bool IsMisspelled(const std::string& word) = 0;
2425

25-
virtual std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length) = 0;
26+
virtual std::vector<MisspelledRange> CheckSpelling(const uint16_t *text, size_t length) = 0;
2627

2728
// Adds a new word to the dictionary.
2829
// NB: When using Hunspell, this will not modify the .dic file; custom words must be added each

src/spellchecker_hunspell.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <cstdio>
2+
#include <cwctype>
23
#include <algorithm>
34
#include "../vendor/hunspell/src/hunspell/hunspell.hxx"
5+
#include "../vendor/hunspell/src/hunspell/csutil.hxx"
46
#include "spellchecker_hunspell.h"
57

68
namespace spellchecker {
@@ -48,7 +50,7 @@ bool HunspellSpellchecker::IsMisspelled(const std::string& word) {
4850
return hunspell->spell(word.c_str()) == 0;
4951
}
5052

51-
std::vector<MisspelledRange> HunspellSpellchecker::CheckSpelling(const char *text, size_t length) {
53+
std::vector<MisspelledRange> HunspellSpellchecker::CheckSpelling(const uint16_t *utf16_text, size_t utf16_length) {
5254
std::vector<MisspelledRange> result;
5355
return result;
5456
}

src/spellchecker_hunspell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HunspellSpellchecker : public SpellcheckerImplementation {
1616
std::vector<std::string> GetAvailableDictionaries(const std::string& path);
1717
std::vector<std::string> GetCorrectionsForMisspelling(const std::string& word);
1818
bool IsMisspelled(const std::string& word);
19-
std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length);
19+
std::vector<MisspelledRange> CheckSpelling(const uint16_t *text, size_t length);
2020
void Add(const std::string& word);
2121

2222
private:

src/spellchecker_mac.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MacSpellchecker : public SpellcheckerImplementation {
1717
std::vector<std::string> GetAvailableDictionaries(const std::string& path);
1818
std::vector<std::string> GetCorrectionsForMisspelling(const std::string& word);
1919
bool IsMisspelled(const std::string& word);
20-
std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length);
20+
std::vector<MisspelledRange> CheckSpelling(const uint16_t *text, size_t length);
2121
void Add(const std::string& word);
2222

2323
private:

src/spellchecker_mac.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
return result;
5353
}
5454

55-
std::vector<MisspelledRange> MacSpellchecker::CheckSpelling(const char *text, size_t length) {
55+
std::vector<MisspelledRange> MacSpellchecker::CheckSpelling(const uint16_t *text, size_t length) {
5656
std::vector<MisspelledRange> result;
5757

5858
@autoreleasepool {
59-
NSString* string = [NSString stringWithUTF8String:text];
59+
NSData *data = [[NSData alloc] initWithBytesNoCopy:(void *)(text) length:(length * 2) freeWhenDone:NO];
60+
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding];
6061
NSArray *misspellings = [this->spellChecker checkString:string
61-
range:NSMakeRange(0, length)
62+
range:NSMakeRange(0, string.length)
6263
types:NSTextCheckingTypeSpelling
6364
options:nil
6465
inSpellDocumentWithTag:0

src/spellchecker_win.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ bool WindowsSpellchecker::IsMisspelled(const std::string& word) {
187187
return ret;
188188
}
189189

190-
std::vector<MisspelledRange> WindowsSpellchecker::CheckSpelling(const char *text, size_t length) {
190+
std::vector<MisspelledRange> WindowsSpellchecker::CheckSpelling(const uint16_t *text, size_t length) {
191191
std::vector<MisspelledRange> result;
192192

193193
if (this->currentSpellchecker == NULL) {
194194
return result;
195195
}
196196

197197
IEnumSpellingError* errors = NULL;
198-
std::wstring wtext = ToWString(text);
198+
std::wstring wtext(reinterpret_cast<const wchar_t *>(text), length);
199199
if (FAILED(this->currentSpellchecker->Check(wtext.c_str(), &errors))) {
200200
return result;
201201
}

src/spellchecker_win.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WindowsSpellchecker : public SpellcheckerImplementation {
1818

1919
std::vector<std::string> GetCorrectionsForMisspelling(const std::string& word);
2020
bool IsMisspelled(const std::string& word);
21-
std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length);
21+
std::vector<MisspelledRange> CheckSpelling(const uint16_t *text, size_t length);
2222
void Add(const std::string& word);
2323

2424
private:

0 commit comments

Comments
 (0)