55
66namespace spellchecker {
77
8- HunspellSpellchecker::HunspellSpellchecker () {
9- this ->hunspell = NULL ;
10- }
11-
8+ HunspellSpellchecker::HunspellSpellchecker () : hunspell(nullptr ) { }
129HunspellSpellchecker::~HunspellSpellchecker () {
13- if (! this -> hunspell ) return ;
14-
15- delete this -> hunspell ;
10+ if (hunspell) {
11+ delete hunspell;
12+ }
1613}
1714
1815bool HunspellSpellchecker::SetDictionary (const std::string& language, const std::string& dirname) {
19- if (hunspell != NULL ) {
20- delete this -> hunspell ;
21- hunspell = NULL ;
16+ if (hunspell) {
17+ delete hunspell;
18+ hunspell = nullptr ;
2219 }
2320
2421 // NB: Hunspell uses underscore to separate language and locale, and Win8 uses
@@ -34,9 +31,9 @@ bool HunspellSpellchecker::SetDictionary(const std::string& language, const std:
3431 if (!handle) {
3532 return false ;
3633 }
37-
3834 fclose (handle);
39- this ->hunspell = new Hunspell (affixpath.c_str (), dpath.c_str ());
35+
36+ hunspell = new Hunspell (affixpath.c_str (), dpath.c_str ());
4037 return true ;
4138}
4239
@@ -45,24 +42,32 @@ std::vector<std::string> HunspellSpellchecker::GetAvailableDictionaries(const st
4542}
4643
4744bool HunspellSpellchecker::IsMisspelled (const std::string& word) {
48- return this ->hunspell ->spell (word.c_str ()) == 0 ;
45+ if (!hunspell) {
46+ return false ;
47+ }
48+ return hunspell->spell (word.c_str ()) == 0 ;
4949}
5050
5151void HunspellSpellchecker::Add (const std::string& word) {
52- this ->hunspell ->add (word.c_str ());
52+ if (hunspell) {
53+ hunspell->add (word.c_str ());
54+ }
5355}
5456
5557std::vector<std::string> HunspellSpellchecker::GetCorrectionsForMisspelling (const std::string& word) {
5658 std::vector<std::string> corrections;
57- char ** slist;
58- int size = hunspell->suggest (&slist, word.c_str ());
5959
60- corrections.reserve (size);
61- for (int i = 0 ; i < size; ++i) {
62- corrections.push_back (slist[i]);
63- }
60+ if (hunspell) {
61+ char ** slist;
62+ int size = hunspell->suggest (&slist, word.c_str ());
63+
64+ corrections.reserve (size);
65+ for (int i = 0 ; i < size; ++i) {
66+ corrections.push_back (slist[i]);
67+ }
6468
65- this ->hunspell ->free_list (&slist, size);
69+ hunspell->free_list (&slist, size);
70+ }
6671 return corrections;
6772}
6873
0 commit comments