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
3 // found in the LICENSE file.
5 #include "base/i18n/string_search.h"
6 #include "base/logging.h"
8 #include "third_party/icu/source/i18n/unicode/usearch.h"
13 FixedPatternStringSearchIgnoringCaseAndAccents::
14 FixedPatternStringSearchIgnoringCaseAndAccents(const string16
& find_this
)
15 : find_this_(find_this
) {
16 // usearch_open requires a valid string argument to be searched, even if we
17 // want to set it by usearch_setText afterwards. So, supplying a dummy text.
18 const string16
& dummy
= find_this_
;
20 UErrorCode status
= U_ZERO_ERROR
;
21 search_
= usearch_open(find_this_
.data(), find_this_
.size(),
22 dummy
.data(), dummy
.size(),
26 if (U_SUCCESS(status
)) {
27 UCollator
* collator
= usearch_getCollator(search_
);
28 ucol_setStrength(collator
, UCOL_PRIMARY
);
29 usearch_reset(search_
);
33 FixedPatternStringSearchIgnoringCaseAndAccents::
34 ~FixedPatternStringSearchIgnoringCaseAndAccents() {
36 usearch_close(search_
);
39 bool FixedPatternStringSearchIgnoringCaseAndAccents::Search(
40 const string16
& in_this
, size_t* match_index
, size_t* match_length
) {
41 UErrorCode status
= U_ZERO_ERROR
;
42 usearch_setText(search_
, in_this
.data(), in_this
.size(), &status
);
44 // Default to basic substring search if usearch fails. According to
45 // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail
46 // if either |find_this| or |in_this| are empty. In either case basic
47 // substring search will give the correct return value.
48 if (!U_SUCCESS(status
)) {
49 size_t index
= in_this
.find(find_this_
);
50 if (index
== string16::npos
) {
56 *match_length
= find_this_
.size();
61 int32_t index
= usearch_first(search_
, &status
);
62 if (!U_SUCCESS(status
) || index
== USEARCH_DONE
)
65 *match_index
= static_cast<size_t>(index
);
67 *match_length
= static_cast<size_t>(usearch_getMatchedLength(search_
));
71 bool StringSearchIgnoringCaseAndAccents(const string16
& find_this
,
72 const string16
& in_this
,
74 size_t* match_length
) {
75 return FixedPatternStringSearchIgnoringCaseAndAccents(find_this
).Search(
76 in_this
, match_index
, match_length
);