Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/string16.h" |
| 12 #include "third_party/icu/source/i18n/unicode/regex.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "third_party/re2/re2/re2.h" | |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // A singleton class that serves as a cache of compiled regex patterns. | 17 // A singleton class that serves as a cache of compiled regex patterns. |
| 17 class AutofillRegexes { | 18 class AutofillRegexes { |
| 18 public: | 19 public: |
| 19 static AutofillRegexes* GetInstance(); | 20 static AutofillRegexes* GetInstance(); |
| 20 | 21 |
| 21 // Returns the compiled regex matcher corresponding to |pattern|. | 22 // Returns the compiled regex matcher corresponding to |pattern|. |
| 22 icu::RegexMatcher* GetMatcher(const base::string16& pattern); | 23 re2::RE2* GetMatcher(const base::string16& pattern); |
| 23 | 24 |
| 24 private: | 25 private: |
| 25 AutofillRegexes(); | 26 AutofillRegexes(); |
| 26 ~AutofillRegexes(); | 27 ~AutofillRegexes(); |
| 27 friend struct base::DefaultSingletonTraits<AutofillRegexes>; | 28 friend struct base::DefaultSingletonTraits<AutofillRegexes>; |
| 28 | 29 |
| 29 // Maps patterns to their corresponding regex matchers. | 30 // Maps patterns to their corresponding regex matchers. |
| 30 base::ScopedPtrHashMap<base::string16, scoped_ptr<icu::RegexMatcher>> | 31 base::ScopedPtrHashMap<base::string16, scoped_ptr<re2::RE2>> matchers_; |
| 31 matchers_; | |
| 32 | 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); | 33 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 // static | 36 // static |
| 37 AutofillRegexes* AutofillRegexes::GetInstance() { | 37 AutofillRegexes* AutofillRegexes::GetInstance() { |
| 38 return base::Singleton<AutofillRegexes>::get(); | 38 return base::Singleton<AutofillRegexes>::get(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 AutofillRegexes::AutofillRegexes() { | 41 AutofillRegexes::AutofillRegexes() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 AutofillRegexes::~AutofillRegexes() { | 44 AutofillRegexes::~AutofillRegexes() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 icu::RegexMatcher* AutofillRegexes::GetMatcher(const base::string16& pattern) { | 47 re2::RE2* AutofillRegexes::GetMatcher(const base::string16& pattern) { |
|
vabr (Chromium)
2015/11/18 09:26:36
Tracing this to the callsites, it appears that we
tfarina
2015/11/18 18:03:10
I don't think that was a good suggestion. That req
Ilya Sherman
2015/11/19 02:08:59
I think you might have misunderstood Václav's sugg
vabr (Chromium)
2015/11/19 08:09:04
@tfarina -- sorry that I was unclear; I really mea
tfarina
2015/11/20 19:03:37
Ilya, can I do this API change in a follow up? I d
Ilya Sherman
2015/11/20 22:27:59
I'd prefer that you make the change as part of thi
| |
| 48 auto it = matchers_.find(pattern); | 48 auto it = matchers_.find(pattern); |
| 49 if (it == matchers_.end()) { | 49 if (it == matchers_.end()) { |
| 50 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); | 50 scoped_ptr<re2::RE2> matcher(new re2::RE2(base::UTF16ToUTF8(pattern))); |
| 51 | |
| 52 UErrorCode status = U_ZERO_ERROR; | |
| 53 scoped_ptr<icu::RegexMatcher> matcher( | |
| 54 new icu::RegexMatcher(icu_pattern, UREGEX_CASE_INSENSITIVE, status)); | |
| 55 DCHECK(U_SUCCESS(status)); | |
| 56 | |
| 57 auto result = matchers_.add(pattern, matcher.Pass()); | 51 auto result = matchers_.add(pattern, matcher.Pass()); |
| 58 DCHECK(result.second); | 52 DCHECK(result.second); |
| 59 it = result.first; | 53 it = result.first; |
| 60 } | 54 } |
| 61 return it->second; | 55 return it->second; |
| 62 } | 56 } |
| 63 | 57 |
| 64 } // namespace | 58 } // namespace |
| 65 | 59 |
| 66 namespace autofill { | 60 namespace autofill { |
| 67 | 61 |
| 68 bool MatchesPattern(const base::string16& input, | 62 bool MatchesPattern(const base::string16& input, |
| 69 const base::string16& pattern) { | 63 const base::string16& pattern) { |
| 70 icu::RegexMatcher* matcher = | 64 re2::RE2* matcher = AutofillRegexes::GetInstance()->GetMatcher(pattern); |
| 71 AutofillRegexes::GetInstance()->GetMatcher(pattern); | 65 return matcher->ok(); |
|
Ilya Sherman
2015/11/18 06:48:50
Hmm, it looks like you aren't using the |input| at
vabr (Chromium)
2015/11/18 09:26:36
+1, ok() only checks if the matcher was created wi
tfarina
2015/11/18 18:03:10
Done.
| |
| 72 icu::UnicodeString icu_input(input.data(), input.length()); | |
| 73 matcher->reset(icu_input); | |
| 74 | |
| 75 UErrorCode status = U_ZERO_ERROR; | |
| 76 UBool match = matcher->find(0, status); | |
| 77 DCHECK(U_SUCCESS(status)); | |
| 78 return match == TRUE; | |
| 79 } | 66 } |
| 80 | 67 |
| 81 } // namespace autofill | 68 } // namespace autofill |
| OLD | NEW |