diff --git a/src/cpp/core/spelling/HunspellSpellChecker.cpp b/src/cpp/core/spelling/HunspellSpellChecker.cpp index 1f21f8797b2..1752513e8fa 100644 --- a/src/cpp/core/spelling/HunspellSpellChecker.cpp +++ b/src/cpp/core/spelling/HunspellSpellChecker.cpp @@ -68,41 +68,43 @@ class HunspellSpellChecker : public SpellChecker return Success(); } +private: + + // helpers + void copyToHunspellVector(std::vector* pVec, + char **wlst, + int len) + { + for (int i=0; i < len; i++) + { + pVec->push_back(wlst[i]); + } + pHunspell_->free_list(&wlst, len); + } + public: Error checkSpelling(const std::string& word, bool *pCorrect) { - std::string encoded; - - Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded); - if (error) - { - error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded); - if (error) - return error; - } - *pCorrect = pHunspell_->spell(encoded.c_str()); - return Success(); + std::string encoded; + + Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded); + if (error) + return error; + *pCorrect = pHunspell_->spell(encoded.c_str()); + return Success(); } Error suggestionList(const std::string& word, std::vector* pSug) { - char ** wlst; - std::string encoded; - - Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded); - if (error) - { - error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded); - if (error) - return error; - } - int ns = pHunspell_->suggest(&wlst,encoded.c_str()); - for (int i=0; i < ns; i++) - { - pSug->push_back(wlst[i]); - } - pHunspell_->free_list(&wlst, ns); - return Success(); + char ** wlst; + std::string encoded; + + Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded); + if (error) + return error; + int ns = pHunspell_->suggest(&wlst,encoded.c_str()); + copyToHunspellVector(pSug,wlst,ns); + return Success(); } Error analyzeWord(const std::string& word, std::vector* pResult) @@ -112,20 +114,13 @@ class HunspellSpellChecker : public SpellChecker Error error = iconvstrFunc_(word,"UTF-8",encoding_,false,&encoded); if (error) - { - error = iconvstrFunc_(word,"UTF-8",encoding_,true,&encoded); - if (error) - return error; - } + return error; int ns = pHunspell_->analyze(&wlst,encoded.c_str()); - for (int i=0; i < ns; i++) - { - pResult->push_back(wlst[i]); - } - pHunspell_->free_list(&wlst, ns); + copyToHunspellVector(pResult,wlst,ns); return Success(); } + private: boost::scoped_ptr pHunspell_; IconvstrFunction iconvstrFunc_; diff --git a/src/cpp/session/modules/SessionSpelling.cpp b/src/cpp/session/modules/SessionSpelling.cpp index fcce5469caf..e23a7103963 100644 --- a/src/cpp/session/modules/SessionSpelling.cpp +++ b/src/cpp/session/modules/SessionSpelling.cpp @@ -44,6 +44,13 @@ SEXP rs_checkSpelling(SEXP wordSEXP) Error error = s_pSpellChecker->checkSpelling(word,&isCorrect); + // We'll return true here so as not to tie up the front end. + if (error) + { + LOG_ERROR(error); + isCorrect = true; + } + r::sexp::Protect rProtect; return r::sexp::create(isCorrect, &rProtect); } @@ -53,7 +60,10 @@ SEXP rs_suggestionList(SEXP wordSEXP) std::string word = r::sexp::asString(wordSEXP); std::vector sugs; - s_pSpellChecker->suggestionList(word,&sugs); + Error error = s_pSpellChecker->suggestionList(word,&sugs); + + if (error) + LOG_ERROR(error); r::sexp::Protect rProtect; return r::sexp::create(sugs,&rProtect); @@ -77,22 +87,22 @@ SEXP rs_analyzeWord(SEXP wordSEXP) Error initialize() { // register rs_ensureFileHidden with R - R_CallMethodDef checkSpellingMethodDef; - - checkSpellingMethodDef.name = "rs_checkSpelling" ; - checkSpellingMethodDef.fun = (DL_FUNC) rs_checkSpelling ; - checkSpellingMethodDef.numArgs = 1; - r::routines::addCallMethod(checkSpellingMethodDef); - - checkSpellingMethodDef.name = "rs_suggestionList" ; - checkSpellingMethodDef.fun = (DL_FUNC) rs_suggestionList ; - checkSpellingMethodDef.numArgs = 1; - r::routines::addCallMethod(checkSpellingMethodDef); - - checkSpellingMethodDef.name = "rs_analyzeWord" ; - checkSpellingMethodDef.fun = (DL_FUNC) rs_analyzeWord ; - checkSpellingMethodDef.numArgs = 1; - r::routines::addCallMethod(checkSpellingMethodDef); + R_CallMethodDef methodDef; + + methodDef.name = "rs_checkSpelling" ; + methodDef.fun = (DL_FUNC) rs_checkSpelling ; + methodDef.numArgs = 1; + r::routines::addCallMethod(methodDef); + + methodDef.name = "rs_suggestionList" ; + methodDef.fun = (DL_FUNC) rs_suggestionList ; + methodDef.numArgs = 1; + r::routines::addCallMethod(methodDef); + + methodDef.name = "rs_analyzeWord" ; + methodDef.fun = (DL_FUNC) rs_analyzeWord ; + methodDef.numArgs = 1; + r::routines::addCallMethod(methodDef); // initialize the spell checker using namespace core::spelling;