From 44a8248b1b19375130144a783d1758e1f83d3c88 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 18 Jan 2018 23:42:33 +0300 Subject: SpellChecker: - fix for a memory allocation zoo; - code cleaning; - version bump --- plugins/SpellChecker/src/RichEdit.cpp | 6 +++--- plugins/SpellChecker/src/autoreplace.cpp | 24 +++++++++++++----------- plugins/SpellChecker/src/autoreplace.h | 2 +- plugins/SpellChecker/src/dictionary.cpp | 15 +++++++-------- plugins/SpellChecker/src/utils.cpp | 31 ++++++++++++++----------------- plugins/SpellChecker/src/version.h | 2 +- 6 files changed, 39 insertions(+), 41 deletions(-) (limited to 'plugins/SpellChecker/src') diff --git a/plugins/SpellChecker/src/RichEdit.cpp b/plugins/SpellChecker/src/RichEdit.cpp index 8cc6fd0e20..fa001f1847 100644 --- a/plugins/SpellChecker/src/RichEdit.cpp +++ b/plugins/SpellChecker/src/RichEdit.cpp @@ -241,7 +241,7 @@ void RichEdit::ReplaceSel(const wchar_t *new_text) SuspendUndo(); - FixSel(&m_old_sel, sel, mir_wstrlen(new_text)); + FixSel(&m_old_sel, sel, (int)mir_wstrlen(new_text)); SendMessage(WM_SETREDRAW, FALSE, 0); SendMessage(EM_SETEVENTMASK, 0, m_old_mask & ~ENM_CHANGE); @@ -257,7 +257,7 @@ int RichEdit::Replace(int start, int end, const wchar_t *new_text) ReplaceSel(new_text); - int dif = FixSel(&sel, replace_sel, mir_wstrlen(new_text)); + int dif = FixSel(&sel, replace_sel, (int)mir_wstrlen(new_text)); SetSel(sel); return dif; } @@ -270,7 +270,7 @@ int RichEdit::Insert(int pos, const wchar_t *text) ReplaceSel(text); - int dif = FixSel(&sel, replace_sel, mir_wstrlen(text)); + int dif = FixSel(&sel, replace_sel, (int)mir_wstrlen(text)); SetSel(sel); return dif; } diff --git a/plugins/SpellChecker/src/autoreplace.cpp b/plugins/SpellChecker/src/autoreplace.cpp index b111862c3b..e2db8efbae 100644 --- a/plugins/SpellChecker/src/autoreplace.cpp +++ b/plugins/SpellChecker/src/autoreplace.cpp @@ -127,20 +127,20 @@ BOOL AutoReplaceMap::isWordChar(wchar_t c) } -wchar_t* AutoReplaceMap::autoReplace(const wchar_t * word) +CMStringW AutoReplaceMap::autoReplace(const wchar_t * word) { ptrW from(wcslwr(mir_wstrdup(word))); if (m_replacements.find(from.get()) == m_replacements.end()) - return nullptr; + return CMStringW(); AutoReplacement &ar = m_replacements[from.get()]; - wchar_t *to; + CMStringW ret; if (ar.useVariables) - to = variables_parsedup((wchar_t *)ar.replace.c_str(), (wchar_t *)word, NULL); + ret = ptrW(variables_parsedup((wchar_t *)ar.replace.c_str(), (wchar_t *)word, NULL)); else - to = mir_wstrdup(ar.replace.c_str()); + ret = ar.replace.c_str(); // Wich case to use? size_t len = mir_wstrlen(word); @@ -150,18 +150,20 @@ wchar_t* AutoReplaceMap::autoReplace(const wchar_t * word) break; if (i <= 0) // All lower - return to; + return ret; - if (i >= len) // All upper - return CharUpper(to); + if (i >= len) { // All upper + ret.MakeUpper(); + return ret; + } // First upper wchar_t tmp[2]; - tmp[0] = to[0]; + tmp[0] = ret[0]; tmp[1] = '\0'; CharUpper(tmp); - to[0] = tmp[0]; - return to; + ret.SetAt(0, tmp[0]); + return ret; } wchar_t* AutoReplaceMap::filterText(const wchar_t *find) diff --git a/plugins/SpellChecker/src/autoreplace.h b/plugins/SpellChecker/src/autoreplace.h index 316f015e0d..f3ce520e80 100644 --- a/plugins/SpellChecker/src/autoreplace.h +++ b/plugins/SpellChecker/src/autoreplace.h @@ -53,7 +53,7 @@ public: /// Return an auto replacement to a word or NULL if none exists. /// You have to free the item. - wchar_t* autoReplace(const wchar_t *word); + CMStringW autoReplace(const wchar_t *word); /// Add a word to the list of auto-replaced words void add(const wchar_t *from, const wchar_t *to, BOOL useVariables = FALSE); diff --git a/plugins/SpellChecker/src/dictionary.cpp b/plugins/SpellChecker/src/dictionary.cpp index 92f74af3b2..4fd5123aa5 100644 --- a/plugins/SpellChecker/src/dictionary.cpp +++ b/plugins/SpellChecker/src/dictionary.cpp @@ -465,10 +465,10 @@ public: free(wordChars); } - wchar_t * merge(wchar_t * s1, wchar_t *s2) + wchar_t* merge(wchar_t * s1, wchar_t *s2) { - int len1 = mir_wstrlen(s1); - int len2 = mir_wstrlen(s2); + size_t len1 = mir_wstrlen(s1); + size_t len2 = mir_wstrlen(s2); wchar_t *ret; if (len1 > 0 && len2 > 0) { @@ -496,10 +496,10 @@ public: } // Remove duplicated chars - int last = mir_wstrlen(ret) - 1; - for (int i = 0; i <= last; i++) { + size_t last = mir_wstrlen(ret) - 1; + for (size_t i = 0; i <= last; i++) { wchar_t c = ret[i]; - for (int j = last; j > i; j--) { + for (size_t j = last; j > i; j--) { if (c != ret[j]) continue; if (j != last) @@ -651,9 +651,8 @@ public: char hunspell_word[1024]; toHunspell(hunspell_word, word, _countof(hunspell_word)); - char ** words; + char **words; int count = hunspell->suggest(&words, hunspell_word); - if (count <= 0) return nullptr; diff --git a/plugins/SpellChecker/src/utils.cpp b/plugins/SpellChecker/src/utils.cpp index dcb004a245..1b88e94621 100644 --- a/plugins/SpellChecker/src/utils.cpp +++ b/plugins/SpellChecker/src/utils.cpp @@ -164,7 +164,7 @@ public: virtual bool feed(int pos, wchar_t c) = 0; virtual int getFirstCharPos() = 0; virtual void reset() = 0; - virtual void deal(const wchar_t *text, bool *mark, bool *replace, wchar_t **replacement) = 0; + virtual bool deal(const wchar_t *text, bool*, CMStringW &replacement) = 0; }; class SpellParser : public TextParser @@ -209,22 +209,24 @@ public: return (!found_real_char) ? -1 : last_pos; } - virtual void deal(const wchar_t *text, bool *mark, bool *replace, wchar_t **replacement) + virtual bool deal(const wchar_t *text, bool *mark, CMStringW &replacement) override { // Is it correct? if (dict->spell(text)) - return; + return false; // Has to auto-correct? if (opts.auto_replace_dict) { - *replacement = dict->autoSuggestOne(text); - if (*replacement != nullptr) { - *replace = true; - return; + wchar_t *pwszWord = dict->autoSuggestOne(text); + if (pwszWord != nullptr) { + replacement = pwszWord; + free(pwszWord); + return true; } } *mark = true; + return false; } }; @@ -261,11 +263,10 @@ public: return last_pos; } - virtual void deal(const wchar_t *text, bool*, bool *replace, wchar_t **replacement) + virtual bool deal(const wchar_t *text, bool*, CMStringW &replacement) override { - *replacement = ar->autoReplace(text); - if (*replacement != nullptr) - *replace = true; + replacement = ar->autoReplace(text); + return !replacement.IsEmpty(); } }; @@ -355,10 +356,8 @@ int CheckTextLine(Dialog *dlg, int line, TextParser *parser, text[pos] = 0; bool mark = false; - bool replace = false; - wchar_t *replacement = nullptr; - parser->deal(&text[last_pos], &mark, &replace, &replacement); - + CMStringW replacement; + bool replace = parser->deal(&text[last_pos], &mark, replacement); if (replace) { // Replace in rich edit int dif = dlg->re->Replace(sel.cpMin, sel.cpMax, replacement); @@ -372,8 +371,6 @@ int CheckTextLine(Dialog *dlg, int line, TextParser *parser, pos = max(-1, pos + dif + old_first_char - first_char); } - - free(replacement); } else if (mark) { SetUnderline(dlg, sel.cpMin, sel.cpMax); diff --git a/plugins/SpellChecker/src/version.h b/plugins/SpellChecker/src/version.h index dd807a953e..ba974c06fe 100644 --- a/plugins/SpellChecker/src/version.h +++ b/plugins/SpellChecker/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 2 #define __RELEASE_NUM 6 -#define __BUILD_NUM 5 +#define __BUILD_NUM 6 #include -- cgit v1.2.3