summaryrefslogtreecommitdiffstats
path: root/chrome/browser/spellchecker.cc
diff options
context:
space:
mode:
authorsidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-12 19:30:21 +0000
committersidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-12 19:30:21 +0000
commiteda2b5a29df212809fb3ee5a8e8c42017b635f1c (patch)
treee0b9116beca023c3400cee5e28baa7114dee9cd2 /chrome/browser/spellchecker.cc
parent01804b55e214f78aec2618b811a5ffa7079104dd (diff)
downloadchromium_src-eda2b5a29df212809fb3ee5a8e8c42017b635f1c.zip
chromium_src-eda2b5a29df212809fb3ee5a8e8c42017b635f1c.tar.gz
chromium_src-eda2b5a29df212809fb3ee5a8e8c42017b635f1c.tar.bz2
Add Automatic spell correction support in Chrome.
Issue=7624 Review URL: http://codereview.chromium.org/42608 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15888 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/spellchecker.cc')
-rw-r--r--chrome/browser/spellchecker.cc44
1 files changed, 44 insertions, 0 deletions
diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc
index 1a28cf1..75ef204 100644
--- a/chrome/browser/spellchecker.cc
+++ b/chrome/browser/spellchecker.cc
@@ -30,6 +30,7 @@ using base::TimeTicks;
static const int kMaxSuggestions = 5; // Max number of dictionary suggestions.
+static const int kMaxAutoCorrectWordSize = 8;
namespace {
@@ -475,6 +476,49 @@ bool SpellChecker::Initialize() {
return false;
}
+void SpellChecker::GetAutoCorrectionWord(const std::wstring& word,
+ std::wstring* autocorrect_word) {
+ autocorrect_word->clear();
+ int word_length = static_cast<int>(word.size());
+ if (word_length < 2 || word_length > kMaxAutoCorrectWordSize)
+ return;
+
+ wchar_t misspelled_word[kMaxAutoCorrectWordSize + 1];
+ const wchar_t* word_char = word.c_str();
+ for (int i = 0; i <= kMaxAutoCorrectWordSize; i++) {
+ if (i >= word_length)
+ misspelled_word[i] = NULL;
+ else
+ misspelled_word[i] = word_char[i];
+ }
+
+ // Swap adjacent characters and spellcheck.
+ int misspelling_start, misspelling_len;
+ for (int i = 0; i < word_length - 1; i++) {
+ // Swap.
+ std::swap(misspelled_word[i], misspelled_word[i + 1]);
+
+ // Check spelling.
+ misspelling_start = misspelling_len = 0;
+ SpellCheckWord(misspelled_word, word_length, &misspelling_start,
+ &misspelling_len, NULL);
+
+ // Make decision: if only one swap produced a valid word, then we want to
+ // return it. If we found two or more, we don't do autocorrection.
+ if (misspelling_len == 0) {
+ if (autocorrect_word->empty()) {
+ autocorrect_word->assign(misspelled_word);
+ } else {
+ autocorrect_word->clear();
+ return;
+ }
+ }
+
+ // Restore the swapped characters.
+ std::swap(misspelled_word[i], misspelled_word[i + 1]);
+ }
+}
+
void SpellChecker::AddCustomWordsToHunspell() {
// Add custom words to Hunspell.
// This should be done in File Loop, but since Hunspell is in this IO Loop,