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

Side by Side Diff: components/autofill/core/common/autofill_regexes.cc

Issue 1453193002: autofill: switch autofill_regexes to RE2 library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address reviews Created 5 years 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/autofill/core/common/autofill_regexes.h" 5 #include "components/autofill/core/common/autofill_regexes.h"
6 6
7 #include "base/containers/scoped_ptr_hash_map.h" 7 #include "base/containers/scoped_ptr_hash_map.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/singleton.h" 10 #include "base/memory/singleton.h"
11 #include "base/strings/string16.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "third_party/icu/source/i18n/unicode/regex.h" 12 #include "third_party/re2/re2/re2.h"
13 13
14 namespace { 14 namespace {
15 15
16 // A singleton class that serves as a cache of compiled regex patterns. 16 // A singleton class that serves as a cache of compiled regex patterns.
17 class AutofillRegexes { 17 class AutofillRegexes {
18 public: 18 public:
19 static AutofillRegexes* GetInstance(); 19 static AutofillRegexes* GetInstance();
20 20
21 // Returns the compiled regex matcher corresponding to |pattern|. 21 // Returns the compiled regex matcher corresponding to |pattern|.
22 icu::RegexMatcher* GetMatcher(const base::string16& pattern); 22 re2::RE2* GetMatcher(const std::string& pattern);
23 23
24 private: 24 private:
25 AutofillRegexes(); 25 AutofillRegexes();
26 ~AutofillRegexes(); 26 ~AutofillRegexes();
27 friend struct base::DefaultSingletonTraits<AutofillRegexes>; 27 friend struct base::DefaultSingletonTraits<AutofillRegexes>;
28 28
29 // Maps patterns to their corresponding regex matchers. 29 // Maps patterns to their corresponding regex matchers.
30 base::ScopedPtrHashMap<base::string16, scoped_ptr<icu::RegexMatcher>> 30 base::ScopedPtrHashMap<std::string, scoped_ptr<re2::RE2>> matchers_;
31 matchers_;
32 31
33 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); 32 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes);
34 }; 33 };
35 34
36 // static 35 // static
37 AutofillRegexes* AutofillRegexes::GetInstance() { 36 AutofillRegexes* AutofillRegexes::GetInstance() {
38 return base::Singleton<AutofillRegexes>::get(); 37 return base::Singleton<AutofillRegexes>::get();
39 } 38 }
40 39
41 AutofillRegexes::AutofillRegexes() { 40 AutofillRegexes::AutofillRegexes() {
42 } 41 }
43 42
44 AutofillRegexes::~AutofillRegexes() { 43 AutofillRegexes::~AutofillRegexes() {
45 } 44 }
46 45
47 icu::RegexMatcher* AutofillRegexes::GetMatcher(const base::string16& pattern) { 46 re2::RE2* AutofillRegexes::GetMatcher(const std::string& pattern) {
48 auto it = matchers_.find(pattern); 47 auto it = matchers_.find(pattern);
49 if (it == matchers_.end()) { 48 if (it == matchers_.end()) {
50 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); 49 re2::RE2::Options options;
51 50 options.set_case_sensitive(false);
52 UErrorCode status = U_ZERO_ERROR; 51 scoped_ptr<re2::RE2> matcher(new re2::RE2(pattern, options));
53 scoped_ptr<icu::RegexMatcher> matcher( 52 DCHECK(matcher->ok());
54 new icu::RegexMatcher(icu_pattern, UREGEX_CASE_INSENSITIVE, status));
55 DCHECK(U_SUCCESS(status));
56
57 auto result = matchers_.add(pattern, matcher.Pass()); 53 auto result = matchers_.add(pattern, matcher.Pass());
58 DCHECK(result.second); 54 DCHECK(result.second);
59 it = result.first; 55 it = result.first;
60 } 56 }
61 return it->second; 57 return it->second;
62 } 58 }
63 59
64 } // namespace 60 } // namespace
65 61
66 namespace autofill { 62 namespace autofill {
67 63
68 bool MatchesPattern(const base::string16& input, 64 bool MatchesPattern(const base::string16& input, const std::string& pattern) {
69 const base::string16& pattern) { 65 // TODO(isherman): Run performance tests to determine whether caching regex
70 icu::RegexMatcher* matcher = 66 // matchers is still useful now that we've switched from ICU to RE2.
71 AutofillRegexes::GetInstance()->GetMatcher(pattern); 67 // http://crbug.com/470065
72 icu::UnicodeString icu_input(input.data(), input.length()); 68 re2::RE2* matcher = AutofillRegexes::GetInstance()->GetMatcher(pattern);
73 matcher->reset(icu_input); 69 return re2::RE2::PartialMatch(base::UTF16ToUTF8(input), *matcher);
74
75 UErrorCode status = U_ZERO_ERROR;
76 UBool match = matcher->find(0, status);
77 DCHECK(U_SUCCESS(status));
78 return match == TRUE;
79 } 70 }
80 71
81 } // namespace autofill 72 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698