| 6 |
| 7 | #include "base/i18n/string_search.h" |
| 8 | #include "base/logging.h" |
| 9 |
| 10 | #include "third_party/icu/source/i18n/unicode/usearch.h" |
| 11 |
| 12 | namespace base { |
| 13 | namespace i18n { |
| 14 |
| 15 | FixedPatternStringSearch::FixedPatternStringSearch(const string16& find_this, |
| 16 | bool case_sensitive) |
| 17 | : find_this_(find_this) { |
| 18 | // usearch_open requires a valid string argument to be searched, even if we |
| 19 | // want to set it by usearch_setText afterwards. So, supplying a dummy text. |
| 20 | const string16& dummy = find_this_; |
| 21 |
| 22 | UErrorCode status = U_ZERO_ERROR; |
| 23 | search_ = usearch_open(find_this_.data(), find_this_.size(), dummy.data(), |
| 24 | dummy.size(), uloc_getDefault(), |
| 25 | nullptr, // breakiter |
| 26 | &status); |
| 27 | if (U_SUCCESS(status)) { |
| 28 | // http://icu-project.org/apiref/icu4c40/ucol_8h.html#6a967f36248b0a1bc7654f538ee8ba96 |
| 29 | // Set comparison level to UCOL_PRIMARY to ignore secondary and tertiary |
| 30 | // differences. Set comparison level to UCOL_TERTIARY to include all |
| 31 | // comparison differences. |
| 32 | // Diacritical differences on the same base letter represent a |
| 33 | // secondary difference. |
| 34 | // Uppercase and lowercase versions of the same character represents a |
| 35 | // tertiary difference. |
| 36 | UCollator* collator = usearch_getCollator(search_); |
| 37 | ucol_setStrength(collator, case_sensitive ? UCOL_TERTIARY : UCOL_PRIMARY); |
| 38 | usearch_reset(search_); |
| 39 | } |
| 40 | } |
| 41 |
| 42 | FixedPatternStringSearch::~FixedPatternStringSearch() { |
| 43 | if (search_) |
| 44 | usearch_close(search_); |
| 45 | } |
| 46 |
| 47 | bool FixedPatternStringSearch::Search(const string16& in_this, |
| 48 | size_t* match_index, |
| 49 | size_t* match_length, |
| 50 | bool forward_search) { |
| 51 | UErrorCode status = U_ZERO_ERROR; |
| 52 | usearch_setText(search_, in_this.data(), in_this.size(), &status); |
| 53 |
| 54 | // Default to basic substring search if usearch fails. According to |
| 55 | // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail |
| 56 | // if either |find_this| or |in_this| are empty. In either case basic |
| 57 | // substring search will give the correct return value. |
| 58 | if (!U_SUCCESS(status)) { |
| 59 | size_t index = in_this.find(find_this_); |
| 60 | if (index == string16::npos) |
| 61 | return false; |
| 62 | if (match_index) |
| 63 | *match_index = index; |
| 64 | if (match_length) |
| 65 | *match_length = find_this_.size(); |
| 66 | return true; |
| 67 | } |
| 68 |
| 69 | int32_t index = forward_search ? usearch_first(search_, &status) |
| 70 | : usearch_last(search_, &status); |
| 71 | if (!U_SUCCESS(status) || index == USEARCH_DONE) |
| 72 | return false; |
| 73 | if (match_index) |
| 74 | *match_index = static_cast<size_t>(index); |
| 75 | if (match_length) |
| 76 | *match_length = static_cast<size_t>(usearch_getMatchedLength(search_)); |
| 77 | return true; |
| 78 | } |
| 79 |
| 80 | FixedPatternStringSearchIgnoringCaseAndAccents:: |
| 81 | FixedPatternStringSearchIgnoringCaseAndAccents(const string16& find_this) |
| 82 | : base_search_(find_this, /*case_sensitive=*/false) {} |
| 83 |
| 84 | bool FixedPatternStringSearchIgnoringCaseAndAccents::Search( |
| 85 | const string16& in_this, |
| 86 | size_t* match_index, |
| 87 | size_t* match_length) { |
| 88 | return base_search_.Search(in_this, match_index, match_length, |
| 89 | /*forward_search=*/true); |
| 90 | } |
| 91 |
| 92 | bool StringSearchIgnoringCaseAndAccents(const string16& find_this, |
| 93 | const string16& in_this, |
| 94 | size_t* match_index, |
| 95 | size_t* match_length) { |
| 96 | return FixedPatternStringSearchIgnoringCaseAndAccents(find_this).Search( |
| 97 | in_this, match_index, match_length); |
| 98 | } |
| 99 |
| 100 | bool StringSearch(const string16& find_this, |
| 101 | const string16& in_this, |
| 102 | size_t* match_index, |
| 103 | size_t* match_length, |
| 104 | bool case_sensitive, |
| 105 | bool forward_search) { |
| 106 | return FixedPatternStringSearch(find_this, case_sensitive) |
| 107 | .Search(in_this, match_index, match_length, forward_search); |
| 108 | } |
| 109 |
| 110 | } // namespace i18n |
| 111 | } // namespace base |
| 112 |