Index: chrome/renderer/spellchecker/hunspell_engine.cc |
diff --git a/chrome/renderer/spellchecker/hunspell_engine.cc b/chrome/renderer/spellchecker/hunspell_engine.cc |
index c1a03592652710cf2838d50c691406787f6f2a82..da1a1c8b591896685479e0ce9f5807965f5f3ac2 100644 |
--- a/chrome/renderer/spellchecker/hunspell_engine.cc |
+++ b/chrome/renderer/spellchecker/hunspell_engine.cc |
@@ -4,6 +4,9 @@ |
#include "hunspell_engine.h" |
+#include <algorithm> |
+#include <iterator> |
+ |
#include "base/metrics/histogram.h" |
#include "base/time.h" |
#include "chrome/common/spellcheck_common.h" |
@@ -69,9 +72,7 @@ void HunspellEngine::InitializeHunspell() { |
new Hunspell(bdict_file_->data(), bdict_file_->length())); |
// Add custom words to Hunspell. |
- chrome::spellcheck_common::WordList::iterator it; |
- for (it = custom_words_.begin(); it != custom_words_.end(); ++it) |
- AddWordToHunspell(*it); |
+ AddWordsToHunspell(custom_words_); |
DHISTOGRAM_TIMES("Spellcheck.InitTime", |
base::Histogram::DebugNow() - debug_start_time); |
@@ -80,14 +81,33 @@ void HunspellEngine::InitializeHunspell() { |
} |
} |
-void HunspellEngine::AddWordToHunspell(const std::string& word) { |
- if (!word.empty() && word.length() < MAXWORDLEN) |
- hunspell_->add(word.c_str()); |
+void HunspellEngine::AddWordsToHunspell(const std::vector<std::string>& words) { |
+ std::string word; |
+ for (chrome::spellcheck_common::WordList::const_iterator it = words.begin(); |
+ it != words.end(); |
+ ++it) { |
+ word = *it; |
+ if (!word.empty() && |
+ word.length() <= |
+ chrome::spellcheck_common::MAX_CUSTOM_DICTIONARY_WORD_BYTES) { |
+ hunspell_->add(word.c_str()); |
+ } |
+ } |
} |
-void HunspellEngine::RemoveWordFromHunspell(const std::string& word) { |
- if (!word.empty() && word.length() < MAXWORDLEN) |
- hunspell_->remove(word.c_str()); |
+void HunspellEngine::RemoveWordsFromHunspell( |
+ const std::vector<std::string>& words) { |
+ std::string word; |
+ for (std::vector<std::string>::const_iterator it = words.begin(); |
+ it != words.end(); |
+ ++it) { |
+ word = *it; |
+ if (!word.empty() && |
+ word.length() <= |
+ chrome::spellcheck_common::MAX_CUSTOM_DICTIONARY_WORD_BYTES) { |
+ hunspell_->remove(word.c_str()); |
+ } |
+ } |
} |
bool HunspellEngine::CheckSpelling(const string16& word_to_check, int tag) { |
@@ -97,7 +117,7 @@ bool HunspellEngine::CheckSpelling(const string16& word_to_check, int tag) { |
if (word_to_check_utf8.length() < kMaxCheckedLen) { |
if (hunspell_.get()) { |
// |hunspell_->spell| returns 0 if the word is spelled correctly and |
- // non-zero otherwsie. |
+ // non-zero otherwise. |
word_correct = (hunspell_->spell(word_to_check_utf8.c_str()) != 0); |
} else { |
// If |hunspell_| is NULL here, an error has occurred, but it's better |
@@ -136,23 +156,28 @@ void HunspellEngine::FillSuggestionList( |
free(suggestions); |
} |
-void HunspellEngine::OnWordAdded(const std::string& word) { |
+void HunspellEngine::OnCustomDictionaryChanged( |
+ const std::vector<std::string>& words_added, |
+ const std::vector<std::string>& words_removed) { |
if (!hunspell_.get()) { |
// Save it for later---add it when hunspell is initialized. |
- custom_words_.push_back(word); |
- } else { |
- AddWordToHunspell(word); |
- } |
-} |
- |
-void HunspellEngine::OnWordRemoved(const std::string& word) { |
- if (!hunspell_.get()) { |
- chrome::spellcheck_common::WordList::iterator it = std::find( |
- custom_words_.begin(), custom_words_.end(), word); |
- if (it != custom_words_.end()) |
- custom_words_.erase(it); |
+ custom_words_.insert(custom_words_.end(), |
+ words_added.begin(), |
+ words_added.end()); |
+ // Remove words. |
+ std::vector<std::string> words_removed_copy(words_removed); |
+ std::sort(words_removed_copy.begin(), words_removed_copy.end()); |
+ std::sort(custom_words_.begin(), custom_words_.end()); |
+ std::vector<std::string> updated_custom_words; |
+ std::set_difference(custom_words_.begin(), |
+ custom_words_.end(), |
+ words_removed_copy.begin(), |
+ words_removed_copy.end(), |
+ std::back_inserter(updated_custom_words)); |
+ std::swap(custom_words_, updated_custom_words); |
} else { |
- RemoveWordFromHunspell(word); |
+ AddWordsToHunspell(words_added); |
+ RemoveWordsFromHunspell(words_removed); |
} |
} |