Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/i18n/string_search.h" | 5 #include "base/i18n/string_search.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 | 7 |
| 8 #include "third_party/icu/public/i18n/unicode/usearch.h" | 8 #include "third_party/icu/public/i18n/unicode/usearch.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace base { |
| 11 namespace i18n { | |
| 11 | 12 |
| 12 bool CollationSensitiveStringSearch(const string16& find_this, | 13 FixedPatternStringSearchIgnoringCaseAndAccents:: |
| 13 const string16& in_this, | 14 FixedPatternStringSearchIgnoringCaseAndAccents(const string16& find_this) |
| 14 UCollationStrength strength, | 15 : find_this_(find_this) { |
| 15 size_t* match_index, | 16 // usearch_open requires a valid string argument to be searched, even if we |
| 16 size_t* match_length) { | 17 // want to set it by usearch_setText afterwards. So, supplying a dummy text. |
| 18 const string16& dummy = find_this_; | |
| 19 | |
| 17 UErrorCode status = U_ZERO_ERROR; | 20 UErrorCode status = U_ZERO_ERROR; |
| 21 search_ = usearch_open(find_this_.data(), find_this_.size(), | |
| 22 dummy.data(), dummy.size(), | |
| 23 uloc_getDefault(), | |
| 24 NULL, // breakiter | |
| 25 &status); | |
| 26 if (U_SUCCESS(status)) { | |
| 27 UCollator* collator = usearch_getCollator(search_); | |
| 28 ucol_setStrength(collator, UCOL_PRIMARY); | |
| 29 usearch_reset(search_); | |
| 30 } | |
| 31 } | |
| 18 | 32 |
| 19 UStringSearch* search = usearch_open(find_this.data(), -1, | 33 FixedPatternStringSearchIgnoringCaseAndAccents:: |
| 20 in_this.data(), -1, | 34 ~FixedPatternStringSearchIgnoringCaseAndAccents() { |
| 21 uloc_getDefault(), | 35 if (search_) |
| 22 NULL, // breakiter | 36 usearch_close(search_); |
| 23 &status); | 37 } |
| 38 | |
| 39 bool FixedPatternStringSearchIgnoringCaseAndAccents::Search( | |
| 40 const string16& in_this, size_t* match_index, size_t* match_length) { | |
| 41 UErrorCode status = U_ZERO_ERROR; | |
| 42 if (!search_) | |
| 43 status = U_ILLEGAL_ARGUMENT_ERROR; | |
| 44 else | |
| 45 usearch_setText(search_, in_this.data(), in_this.size(), &status); | |
|
jungshik at Google
2013/07/10 21:27:52
You don't need to check for |search_| != NULL here
kinaba
2013/07/11 03:14:22
Done. Thanks,
| |
| 24 | 46 |
| 25 // Default to basic substring search if usearch fails. According to | 47 // Default to basic substring search if usearch fails. According to |
| 26 // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail | 48 // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail |
| 27 // if either |find_this| or |in_this| are empty. In either case basic | 49 // if either |find_this| or |in_this| are empty. In either case basic |
| 28 // substring search will give the correct return value. | 50 // substring search will give the correct return value. |
| 29 if (!U_SUCCESS(status)) { | 51 if (!U_SUCCESS(status)) { |
| 30 size_t index = in_this.find(find_this); | 52 size_t index = in_this.find(find_this_); |
| 31 if (index == string16::npos) { | 53 if (index == string16::npos) { |
| 32 return false; | 54 return false; |
| 33 } else { | 55 } else { |
| 34 if (match_index) | 56 if (match_index) |
| 35 *match_index = index; | 57 *match_index = index; |
| 36 if (match_length) | 58 if (match_length) |
| 37 *match_length = find_this.size(); | 59 *match_length = find_this_.size(); |
| 38 return true; | 60 return true; |
| 39 } | 61 } |
| 40 } | 62 } |
| 41 | 63 |
| 42 UCollator* collator = usearch_getCollator(search); | 64 int32_t index = usearch_first(search_, &status); |
| 43 ucol_setStrength(collator, strength); | 65 if (!U_SUCCESS(status) || index == USEARCH_DONE) |
| 44 usearch_reset(search); | |
| 45 | |
| 46 int32_t index = usearch_first(search, &status); | |
| 47 if (!U_SUCCESS(status) || index == USEARCH_DONE) { | |
| 48 usearch_close(search); | |
| 49 return false; | 66 return false; |
| 50 } | |
| 51 | |
| 52 if (match_index) | 67 if (match_index) |
| 53 *match_index = static_cast<size_t>(index); | 68 *match_index = static_cast<size_t>(index); |
| 54 if (match_length) | 69 if (match_length) |
| 55 *match_length = static_cast<size_t>(usearch_getMatchedLength(search)); | 70 *match_length = static_cast<size_t>(usearch_getMatchedLength(search_)); |
| 56 | |
| 57 usearch_close(search); | |
| 58 return true; | 71 return true; |
| 59 } | 72 } |
| 60 | 73 |
| 61 } // namespace | |
| 62 | |
| 63 namespace base { | |
| 64 namespace i18n { | |
| 65 | |
| 66 bool StringSearchIgnoringCaseAndAccents(const string16& find_this, | 74 bool StringSearchIgnoringCaseAndAccents(const string16& find_this, |
| 67 const string16& in_this, | 75 const string16& in_this, |
| 68 size_t* match_index, | 76 size_t* match_index, |
| 69 size_t* match_length) { | 77 size_t* match_length) { |
| 70 return CollationSensitiveStringSearch(find_this, | 78 return FixedPatternStringSearchIgnoringCaseAndAccents(find_this).Search( |
| 71 in_this, | 79 in_this, match_index, match_length); |
| 72 UCOL_PRIMARY, | |
| 73 match_index, | |
| 74 match_length); | |
| 75 } | 80 } |
| 76 | 81 |
| 77 } // namespace i18n | 82 } // namespace i18n |
| 78 } // namespace base | 83 } // namespace base |
| OLD | NEW |