Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Implements a custom word iterator used for our spellchecker. | 5 // Implements a custom word iterator used for our spellchecker. |
| 6 | 6 |
| 7 #include "components/spellcheck/renderer/spellcheck_worditerator.h" | 7 #include "components/spellcheck/renderer/spellcheck_worditerator.h" |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/i18n/break_iterator.h" | 14 #include "base/i18n/break_iterator.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "components/spellcheck/renderer/spellcheck.h" | 19 #include "components/spellcheck/renderer/spellcheck.h" |
| 20 #include "third_party/icu/source/common/unicode/normlzr.h" | 20 #include "third_party/icu/source/common/unicode/normlzr.h" |
| 21 #include "third_party/icu/source/common/unicode/schriter.h" | 21 #include "third_party/icu/source/common/unicode/schriter.h" |
| 22 #include "third_party/icu/source/common/unicode/uscript.h" | 22 #include "third_party/icu/source/common/unicode/uscript.h" |
| 23 #include "third_party/icu/source/i18n/unicode/ulocdata.h" | 23 #include "third_party/icu/source/i18n/unicode/ulocdata.h" |
| 24 | 24 |
| 25 using base::i18n::BreakIterator; | |
| 26 | |
| 27 // SpellcheckCharAttribute implementation: | 25 // SpellcheckCharAttribute implementation: |
| 28 | 26 |
| 29 SpellcheckCharAttribute::SpellcheckCharAttribute() | 27 SpellcheckCharAttribute::SpellcheckCharAttribute() |
| 30 : script_code_(USCRIPT_LATIN) { | 28 : script_code_(USCRIPT_LATIN) { |
| 31 } | 29 } |
| 32 | 30 |
| 33 SpellcheckCharAttribute::~SpellcheckCharAttribute() { | 31 SpellcheckCharAttribute::~SpellcheckCharAttribute() { |
| 34 } | 32 } |
| 35 | 33 |
| 36 void SpellcheckCharAttribute::SetDefaultLanguage(const std::string& language) { | 34 void SpellcheckCharAttribute::SetDefaultLanguage(const std::string& language) { |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 bool allow_contraction) { | 323 bool allow_contraction) { |
| 326 // Create a custom ICU break iterator with empty text used in this object. (We | 324 // Create a custom ICU break iterator with empty text used in this object. (We |
| 327 // allow setting text later so we can re-use this iterator.) | 325 // allow setting text later so we can re-use this iterator.) |
| 328 DCHECK(attribute); | 326 DCHECK(attribute); |
| 329 const base::string16 rule(attribute->GetRuleSet(allow_contraction)); | 327 const base::string16 rule(attribute->GetRuleSet(allow_contraction)); |
| 330 | 328 |
| 331 // If there is no rule set, the attributes were invalid. | 329 // If there is no rule set, the attributes were invalid. |
| 332 if (rule.empty()) | 330 if (rule.empty()) |
| 333 return false; | 331 return false; |
| 334 | 332 |
| 335 std::unique_ptr<BreakIterator> iterator( | 333 std::unique_ptr<base::i18n::BreakIterator> iterator( |
|
groby-ooo-7-16
2016/08/29 17:37:12
Tiny nit: While you're here, would you mind switch
Paweł Hajdan Jr.
2016/08/30 08:29:17
Done.
| |
| 336 new BreakIterator(base::string16(), rule)); | 334 new base::i18n::BreakIterator(base::string16(), rule)); |
| 337 if (!iterator->Init()) { | 335 if (!iterator->Init()) { |
| 338 // Since we're not passing in any text, the only reason this could fail | 336 // Since we're not passing in any text, the only reason this could fail |
| 339 // is if we fail to parse the rules. Since the rules are hardcoded, | 337 // is if we fail to parse the rules. Since the rules are hardcoded, |
| 340 // that would be a bug in this class. | 338 // that would be a bug in this class. |
| 341 NOTREACHED() << "failed to open iterator (broken rules)"; | 339 NOTREACHED() << "failed to open iterator (broken rules)"; |
| 342 return false; | 340 return false; |
| 343 } | 341 } |
| 344 iterator_ = std::move(iterator); | 342 iterator_ = std::move(iterator); |
| 345 | 343 |
| 346 // Set the character attributes so we can normalize the words extracted by | 344 // Set the character attributes so we can normalize the words extracted by |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 return IS_END_OF_TEXT; | 379 return IS_END_OF_TEXT; |
| 382 } | 380 } |
| 383 | 381 |
| 384 // Find a word that can be checked for spelling or a character that can be | 382 // Find a word that can be checked for spelling or a character that can be |
| 385 // skipped over. Rather than moving past a skippable character this returns | 383 // skipped over. Rather than moving past a skippable character this returns |
| 386 // IS_SKIPPABLE and defers handling the character to the calling function. | 384 // IS_SKIPPABLE and defers handling the character to the calling function. |
| 387 while (iterator_->Advance()) { | 385 while (iterator_->Advance()) { |
| 388 const size_t start = iterator_->prev(); | 386 const size_t start = iterator_->prev(); |
| 389 const size_t length = iterator_->pos() - start; | 387 const size_t length = iterator_->pos() - start; |
| 390 switch (iterator_->GetWordBreakStatus()) { | 388 switch (iterator_->GetWordBreakStatus()) { |
| 391 case BreakIterator::IS_WORD_BREAK: { | 389 case base::i18n::BreakIterator::IS_WORD_BREAK: { |
| 392 if (Normalize(start, length, word_string)) { | 390 if (Normalize(start, length, word_string)) { |
| 393 *word_start = start; | 391 *word_start = start; |
| 394 *word_length = length; | 392 *word_length = length; |
| 395 return IS_WORD; | 393 return IS_WORD; |
| 396 } | 394 } |
| 397 break; | 395 break; |
| 398 } | 396 } |
| 399 case BreakIterator::IS_SKIPPABLE_WORD: { | 397 case base::i18n::BreakIterator::IS_SKIPPABLE_WORD: { |
| 400 *word_string = iterator_->GetString(); | 398 *word_string = iterator_->GetString(); |
| 401 *word_start = start; | 399 *word_start = start; |
| 402 *word_length = length; | 400 *word_length = length; |
| 403 return IS_SKIPPABLE; | 401 return IS_SKIPPABLE; |
| 404 } | 402 } |
| 405 // |iterator_| is RULE_BASED so the break status should never be | 403 // |iterator_| is RULE_BASED so the break status should never be |
| 406 // IS_LINE_OR_CHAR_BREAK. | 404 // IS_LINE_OR_CHAR_BREAK. |
| 407 case BreakIterator::IS_LINE_OR_CHAR_BREAK: { | 405 case base::i18n::BreakIterator::IS_LINE_OR_CHAR_BREAK: { |
| 408 NOTREACHED(); | 406 NOTREACHED(); |
| 409 break; | 407 break; |
| 410 } | 408 } |
| 411 } | 409 } |
| 412 } | 410 } |
| 413 | 411 |
| 414 // There aren't any more words in the given text. | 412 // There aren't any more words in the given text. |
| 415 return IS_END_OF_TEXT; | 413 return IS_END_OF_TEXT; |
| 416 } | 414 } |
| 417 | 415 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 436 if (status != U_ZERO_ERROR && status != U_STRING_NOT_TERMINATED_WARNING) | 434 if (status != U_ZERO_ERROR && status != U_STRING_NOT_TERMINATED_WARNING) |
| 437 return false; | 435 return false; |
| 438 | 436 |
| 439 // Copy the normalized text to the output. | 437 // Copy the normalized text to the output. |
| 440 icu::StringCharacterIterator it(output); | 438 icu::StringCharacterIterator it(output); |
| 441 for (UChar c = it.first(); c != icu::CharacterIterator::DONE; c = it.next()) | 439 for (UChar c = it.first(); c != icu::CharacterIterator::DONE; c = it.next()) |
| 442 attribute_->OutputChar(c, output_string); | 440 attribute_->OutputChar(c, output_string); |
| 443 | 441 |
| 444 return !output_string->empty(); | 442 return !output_string->empty(); |
| 445 } | 443 } |
| OLD | NEW |