Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: base/i18n/string_search.cc

Issue 18031015: base/i18n: Class for efficiently searching the same query over many texts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments (#5) Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/i18n/string_search.h ('k') | base/i18n/string_search_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 usearch_setText(search_, in_this.data(), in_this.size(), &status);
24 43
25 // Default to basic substring search if usearch fails. According to 44 // Default to basic substring search if usearch fails. According to
26 // http://icu-project.org/apiref/icu4c/usearch_8h.html, usearch_open will fail 45 // 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 46 // if either |find_this| or |in_this| are empty. In either case basic
28 // substring search will give the correct return value. 47 // substring search will give the correct return value.
29 if (!U_SUCCESS(status)) { 48 if (!U_SUCCESS(status)) {
30 size_t index = in_this.find(find_this); 49 size_t index = in_this.find(find_this_);
31 if (index == string16::npos) { 50 if (index == string16::npos) {
32 return false; 51 return false;
33 } else { 52 } else {
34 if (match_index) 53 if (match_index)
35 *match_index = index; 54 *match_index = index;
36 if (match_length) 55 if (match_length)
37 *match_length = find_this.size(); 56 *match_length = find_this_.size();
38 return true; 57 return true;
39 } 58 }
40 } 59 }
41 60
42 UCollator* collator = usearch_getCollator(search); 61 int32_t index = usearch_first(search_, &status);
43 ucol_setStrength(collator, strength); 62 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; 63 return false;
50 }
51
52 if (match_index) 64 if (match_index)
53 *match_index = static_cast<size_t>(index); 65 *match_index = static_cast<size_t>(index);
54 if (match_length) 66 if (match_length)
55 *match_length = static_cast<size_t>(usearch_getMatchedLength(search)); 67 *match_length = static_cast<size_t>(usearch_getMatchedLength(search_));
56
57 usearch_close(search);
58 return true; 68 return true;
59 } 69 }
60 70
61 } // namespace
62
63 namespace base {
64 namespace i18n {
65
66 bool StringSearchIgnoringCaseAndAccents(const string16& find_this, 71 bool StringSearchIgnoringCaseAndAccents(const string16& find_this,
67 const string16& in_this, 72 const string16& in_this,
68 size_t* match_index, 73 size_t* match_index,
69 size_t* match_length) { 74 size_t* match_length) {
70 return CollationSensitiveStringSearch(find_this, 75 return FixedPatternStringSearchIgnoringCaseAndAccents(find_this).Search(
71 in_this, 76 in_this, match_index, match_length);
72 UCOL_PRIMARY,
73 match_index,
74 match_length);
75 } 77 }
76 78
77 } // namespace i18n 79 } // namespace i18n
78 } // namespace base 80 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/string_search.h ('k') | base/i18n/string_search_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698