diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-16 22:58:11 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-16 22:58:11 +0000 |
commit | d3a94165c9dc2f5142613304efa7c8e13c69fc8e (patch) | |
tree | 1bfdffdf1b9351df144ca77e454c92d761a8da8d /chrome/browser | |
parent | aa573b305e7350bbfa6f9659dc36ce62d877e574 (diff) | |
download | chromium_src-d3a94165c9dc2f5142613304efa7c8e13c69fc8e.zip chromium_src-d3a94165c9dc2f5142613304efa7c8e13c69fc8e.tar.gz chromium_src-d3a94165c9dc2f5142613304efa7c8e13c69fc8e.tar.bz2 |
Port the spell checker to posix.
It all builds but does not link yet.
Review URL: http://codereview.chromium.org/14408
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7109 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser.scons | 4 | ||||
-rw-r--r-- | chrome/browser/spellcheck_unittest.cc | 12 | ||||
-rw-r--r-- | chrome/browser/spellcheck_worditerator.cc | 24 | ||||
-rw-r--r-- | chrome/browser/spellcheck_worditerator.h | 15 | ||||
-rw-r--r-- | chrome/browser/spellchecker.cc | 46 | ||||
-rw-r--r-- | chrome/browser/spellchecker.h | 2 |
6 files changed, 52 insertions, 51 deletions
diff --git a/chrome/browser/browser.scons b/chrome/browser/browser.scons index 40599fb4..cf6a6cb 100644 --- a/chrome/browser/browser.scons +++ b/chrome/browser/browser.scons @@ -106,6 +106,8 @@ if env['PLATFORM'] in ('posix', 'win32'): 'safe_browsing/safe_browsing_service.cc', 'safe_browsing/safe_browsing_util.cc', 'session_startup_pref.cc', + 'spellcheck_worditerator.cc', + 'spellchecker.cc', 'template_url_parser.cc', 'url_fetcher_protect.cc', 'user_metrics.cc', @@ -257,8 +259,6 @@ if env['PLATFORM'] == 'win32': 'sessions/tab_restore_service.cc', 'shell_integration.cc', 'site_instance.cc', - 'spellcheck_worditerator.cc', - 'spellchecker.cc', 'ssl_blocking_page.cc', 'ssl_error_info.cc', 'ssl_manager.cc', diff --git a/chrome/browser/spellcheck_unittest.cc b/chrome/browser/spellcheck_unittest.cc index 7bc95c1..745cd6e 100644 --- a/chrome/browser/spellcheck_unittest.cc +++ b/chrome/browser/spellcheck_unittest.cc @@ -256,7 +256,7 @@ TEST_F(SpellCheckTest, SpellCheckStrings_EN_US) { scoped_refptr<SpellChecker> spell_checker(new SpellChecker( hunspell_directory, L"en-US", NULL, std::wstring())); - for (int i = 0; i < arraysize(kTestCases); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { size_t input_length = 0; if (kTestCases[i].input != NULL) { input_length = wcslen(kTestCases[i].input); @@ -310,7 +310,7 @@ TEST_F(SpellCheckTest, SpellCheckSuggestions_EN_US) { scoped_refptr<SpellChecker> spell_checker(new SpellChecker( hunspell_directory, L"en-US", NULL, std::wstring())); - for (int i = 0; i < arraysize(kTestCases); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { std::vector<std::wstring> suggestions; size_t input_length = 0; if (kTestCases[i].input != NULL) { @@ -358,7 +358,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckAddToDictionary_EN_US) { scoped_refptr<SpellChecker> spell_checker(new SpellChecker( hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile)); - for (int i = 0; i < arraysize(kTestCases); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { // Add the word to spellchecker. spell_checker->AddWord(std::wstring(kTestCases[i].word_to_add)); @@ -384,7 +384,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckAddToDictionary_EN_US) { scoped_refptr<SpellChecker> spell_checker_new(new SpellChecker( hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile)); - for (int i = 0; i < arraysize(kTestCases); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { // Now check whether it is added to Spellchecker. std::vector<std::wstring> suggestions; size_t input_length = 0; @@ -426,7 +426,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckSuggestionsAddToDictionary_EN_US) { scoped_refptr<SpellChecker> spell_checker(new SpellChecker( hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile)); - for (int i = 0; i < arraysize(kTestCases); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { // Add the word to spellchecker. spell_checker->AddWord(std::wstring(kTestCases[i].word_to_add)); } @@ -452,7 +452,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckSuggestionsAddToDictionary_EN_US) { {L"oogleplex", false, 0, 0, L"Googleplex"}, }; - for (int i = 0; i < arraysize(kTestCasesToBeTested); i++) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCasesToBeTested); ++i) { std::vector<std::wstring> suggestions; size_t input_length = 0; if (kTestCasesToBeTested[i].input != NULL) { diff --git a/chrome/browser/spellcheck_worditerator.cc b/chrome/browser/spellcheck_worditerator.cc index 770a833..7dc5b4f 100644 --- a/chrome/browser/spellcheck_worditerator.cc +++ b/chrome/browser/spellcheck_worditerator.cc @@ -48,7 +48,7 @@ SpellcheckCharAttribute::SpellcheckCharAttribute() { L'\xFF07', // MidNumLet # FULLWIDTH APOSTROPHE L'\xFF0E', // MidNumLet # FULLWIDTH FULL STOP }; - for (int i = 0; i < arraysize(kMidLetters); i++) + for (size_t i = 0; i < arraysize(kMidLetters); ++i) middle_letters_[kMidLetters[i]] = true; } @@ -76,7 +76,7 @@ void SpellcheckCharAttribute::SetDefaultLanguage(const std::wstring& language) { ulocdata_close(locale_data); if (U_SUCCESS(status)) { int length = uset_size(exemplar_set); - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; ++i) { UChar32 character = uset_charAt(exemplar_set, i); SetWordScript(GetScriptCode(character), true); } @@ -102,7 +102,7 @@ bool SpellcheckCharAttribute::IsContractionChar(UChar32 character) const { // Initializes the mapping table. void SpellcheckCharAttribute::InitializeScriptTable() { - for (int i = 0; i < arraysize(script_attributes_); i++) + for (size_t i = 0; i < arraysize(script_attributes_); ++i) script_attributes_[i] = false; } @@ -117,7 +117,8 @@ UScriptCode SpellcheckCharAttribute::GetScriptCode(UChar32 character) const { // whether not a script is used by the selected dictionary. void SpellcheckCharAttribute::SetWordScript(const int script_code, bool in_use) { - if (script_code < 0 || script_code >= arraysize(script_attributes_)) + if (script_code < 0 || + static_cast<size_t>(script_code) >= arraysize(script_attributes_)) return; script_attributes_[script_code] = in_use; } @@ -126,15 +127,16 @@ void SpellcheckCharAttribute::SetWordScript(const int script_code, // dictionary. bool SpellcheckCharAttribute::IsWordScript( const UScriptCode script_code) const { - if (script_code < 0 || script_code >= arraysize(script_attributes_)) + if (script_code < 0 || + static_cast<size_t>(script_code) >= arraysize(script_attributes_)) return false; return script_attributes_[script_code]; } SpellcheckWordIterator::SpellcheckWordIterator() : word_(NULL), - position_(0), length_(0), + position_(0), allow_contraction_(false), attribute_(NULL) { } @@ -145,7 +147,7 @@ SpellcheckWordIterator::~SpellcheckWordIterator() { // Initialize a word-iterator object. void SpellcheckWordIterator::Initialize( const SpellcheckCharAttribute* attribute, - const wchar_t* word, + const char16* word, size_t length, bool allow_contraction) { word_ = word; @@ -162,7 +164,7 @@ void SpellcheckWordIterator::Initialize( // To handle this case easily, we should firstly extract a segment consisting // of word characters and contraction characters, and discard contraction // characters at the beginning and the end of the extracted segment. -bool SpellcheckWordIterator::GetNextWord(std::wstring* word_string, +bool SpellcheckWordIterator::GetNextWord(string16* word_string, int* word_start, int* word_length) { word_string->empty(); @@ -239,7 +241,7 @@ void SpellcheckWordIterator::TrimSegment(int segment_start, // "http://www.unicode.org/Public/UNIDATA/Scripts.txt". bool SpellcheckWordIterator::Normalize(int input_start, int input_length, - std::wstring* output_string) const { + string16* output_string) const { // Unicode Standard Annex #15 "http://www.unicode.org/unicode/reports/tr15/" // does not only write NFKD and NFKC can compose ligatures into their ASCII // alternatives, but also write NFKC keeps accents of characters. @@ -251,10 +253,10 @@ bool SpellcheckWordIterator::Normalize(int input_start, // and call the function with it. We re-allocate the output string // only if it cannot store the normalized string, i.e. the output string is // longer than the input one. - const wchar_t* input_string = &word_[input_start]; + const char16* input_string = &word_[input_start]; UErrorCode error_code = U_ZERO_ERROR; int output_length = input_length + 1; - wchar_t *output_buffer = WriteInto(output_string, output_length); + char16* output_buffer = WriteInto(output_string, output_length); output_length = unorm_normalize(input_string, input_length, UNORM_NFKC, 0, output_buffer, output_length, &error_code); if (error_code == U_BUFFER_OVERFLOW_ERROR) { diff --git a/chrome/browser/spellcheck_worditerator.h b/chrome/browser/spellcheck_worditerator.h index 57f32eb..ce15aec 100644 --- a/chrome/browser/spellcheck_worditerator.h +++ b/chrome/browser/spellcheck_worditerator.h @@ -9,6 +9,7 @@ #include <string> #include "base/basictypes.h" +#include "base/string16.h" #include "unicode/uscript.h" @@ -93,7 +94,7 @@ class SpellcheckWordIterator { // * attribute [in] (const SpellcheckCharAttribute*) // Represents a set of character attributes used for filtering out // non-word characters. - // * word [in] (const wchar_t*) + // * word [in] (const char16*) // Represents a string from which this object extracts words. // (This string does not have to be NUL-terminated.) // * length [in] (size_t) @@ -108,20 +109,20 @@ class SpellcheckWordIterator { // * false // An error occured while initializing this object. void Initialize(const SpellcheckCharAttribute* attribute, - const wchar_t* word, + const char16* word, size_t length, bool allow_contraction); // Retrieves a word (or a contraction). // Parameters - // * word_string [out] (std::wstring*) + // * word_string [out] (string16*) // Represents a word (or a contraction) to be checked its spelling. // This |word_string| has been already normalized to its canonical form // (i.e. decomposed ligatures, replaced full-width latin characters to // its ASCII alternatives, etc.) so that a SpellChecker object can check // its spelling without any additional operations. // On the other hand, a substring of the input string - // std::wstring str(&word[word_start], word_length); + // string16 str(&word[word_start], word_length); // represents the non-normalized version of this extracted word. // * word_start [out] (int*) // Represents the offset of this word from the beginning of the input @@ -136,7 +137,7 @@ class SpellcheckWordIterator { // Found a word (or a contraction) to be checked its spelling. // * false // Not found any more words or contractions to be checked their spellings. - bool GetNextWord(std::wstring* word_string, + bool GetNextWord(string16* word_string, int* word_start, int* word_length); @@ -157,11 +158,11 @@ class SpellcheckWordIterator { // canonical form to the |output_string|. bool Normalize(int input_start, int input_length, - std::wstring* output_string) const; + string16* output_string) const; private: // The pointer to the input string from which we are extracting words. - const wchar_t* word_; + const char16* word_; // The length of the original string. int length_; diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc index 6fc9d4c..09a2e95 100644 --- a/chrome/browser/spellchecker.cc +++ b/chrome/browser/spellchecker.cc @@ -2,17 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <io.h> - #include "chrome/browser/spellchecker.h" #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/file_util.h" #include "base/histogram.h" #include "base/logging.h" #include "base/path_service.h" #include "base/string_util.h" #include "base/thread.h" -#include "base/win_util.h" #include "chrome/app/locales/locale_settings.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profile.h" @@ -23,8 +21,6 @@ #include "chrome/common/l10n_util.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" -#include "chrome/common/render_messages.h" -#include "chrome/common/win_util.h" #include "chrome/third_party/hunspell/src/hunspell/hunspell.hxx" #include "net/url_request/url_request.h" @@ -82,7 +78,7 @@ void SpellChecker::SpellCheckLanguages(Languages* languages) { SpellChecker::Language SpellChecker::GetCorrespondingSpellCheckLanguage( const Language& language) { // Look for exact match in the Spell Check language list. - for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { + for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { Language spellcheck_language(g_supported_spellchecker_languages[i]); if (spellcheck_language == language) return language; @@ -96,7 +92,7 @@ SpellChecker::Language SpellChecker::GetCorrespondingSpellCheckLanguage( // locale ids with a script code in the middle, yet. // TODO(jungshik): Add a better fallback. Language language_part(language, 0, language.find(L'-')); - for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { + for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { Language spellcheck_language(g_supported_spellchecker_languages[i]); if (spellcheck_language.substr(0, spellcheck_language.find(L'-')) == language_part) @@ -212,11 +208,11 @@ class SpellChecker::DictionaryDownloadController const std::wstring& dic_file_path, URLRequestContext* url_request_context, MessageLoop* ui_loop) - : url_request_context_(url_request_context), + : spellchecker_flag_set_task_(spellchecker_flag_set_task), + url_request_context_(url_request_context), download_server_url_( L"http://cache.pack.google.com/chrome/dict/"), - ui_loop_(ui_loop), - spellchecker_flag_set_task_(spellchecker_flag_set_task) { + ui_loop_(ui_loop) { // Determine dictionary file path and name. fetcher_.reset(NULL); dic_zip_file_path_ = file_util::GetDirectoryFromPath(dic_file_path); @@ -306,7 +302,7 @@ std::wstring SpellChecker::GetVersionedFileName(const Language& language, const std::wstring& dict_dir) { // The default version string currently in use. static const wchar_t kDefaultVersionString[] = L"-1-1"; - + // Use this struct to insert version strings for dictionary files which have // special version strings, other than the default version string. // For de-DE, we are currently using de-DE-1-1-1 for versioning, because @@ -320,15 +316,15 @@ std::wstring SpellChecker::GetVersionedFileName(const Language& language, // The corresponding version. const char* version; } special_version_string[] = { - "de-DE", "-1-1-1", + {"de-DE", "-1-1-1"}, }; - + // Generate the bdict file name using default version string or special // version string, depending on the language. std::wstring versioned_bdict_file_name(language + kDefaultVersionString + L".bdic"); std::string language_string(WideToUTF8(language)); - for (int i = 0; i < arraysize(special_version_string); ++i) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(special_version_string); ++i) { if (language_string == special_version_string[i].language) { versioned_bdict_file_name = language + UTF8ToWide(special_version_string[i].version) + L".bdic"; @@ -351,11 +347,10 @@ SpellChecker::SpellChecker(const std::wstring& dict_dir, worker_loop_(NULL), #endif tried_to_download_(false), - url_request_context_(request_context), file_loop_(NULL), -#pragma warning(suppress: 4355) // Okay to pass "this" here. - dic_download_state_changer_factory_(this), - dic_is_downloading_(false) { + url_request_context_(request_context), + dic_is_downloading_(false), + ALLOW_THIS_IN_INTIALIZER_LIST(dic_download_state_changer_factory_(this)) { // Remember UI loop to later use this as a proxy to get IO loop. ui_loop_ = MessageLoop::current(); @@ -460,16 +455,16 @@ void SpellChecker::AddCustomWordsToHunspell() { // This function is a fall-back when the SpellcheckWordIterator class // returns a concatenated word which is not in the selected dictionary // (e.g. "in'n'out") but each word is valid. -bool SpellChecker::IsValidContraction(const std::wstring& contraction) { +bool SpellChecker::IsValidContraction(const string16& contraction) { SpellcheckWordIterator word_iterator; word_iterator.Initialize(&character_attributes_, contraction.c_str(), contraction.length(), false); - std::wstring word; + string16 word; int word_start; int word_length; while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { - if (!hunspell_->spell(WideToUTF8(word).c_str())) + if (!hunspell_->spell(UTF16ToUTF8(word).c_str())) return false; } return true; @@ -505,13 +500,16 @@ bool SpellChecker::SpellCheckWord( return true; // unable to spellcheck, return word is OK SpellcheckWordIterator word_iterator; - std::wstring word; + string16 word; + string16 in_word_utf16; + WideToUTF16(in_word, in_word_len, &in_word_utf16); int word_start; int word_length; - word_iterator.Initialize(&character_attributes_, in_word, in_word_len, true); + word_iterator.Initialize(&character_attributes_, in_word_utf16.c_str(), + in_word_len, true); while (word_iterator.GetNextWord(&word, &word_start, &word_length)) { // Found a word (or a contraction) that hunspell can check its spelling. - std::string encoded_word = WideToUTF8(word); + std::string encoded_word = UTF16ToUTF8(word); { TimeTicks begin_time = TimeTicks::Now(); diff --git a/chrome/browser/spellchecker.h b/chrome/browser/spellchecker.h index 921a08e..1e97605 100644 --- a/chrome/browser/spellchecker.h +++ b/chrome/browser/spellchecker.h @@ -107,7 +107,7 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> { // Returns whether or not the given word is a contraction of valid words // (e.g. "word:word"). - bool IsValidContraction(const Language& word); + bool IsValidContraction(const string16& word); // Return the file name of the dictionary, including the path and the version // numbers. |