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 "third_party/re2/re2/re2.h" |
| 12 #include "third_party/icu/source/i18n/unicode/regex.h" | |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 // A singleton class that serves as a cache of compiled regex patterns. | 15 // A singleton class that serves as a cache of compiled regex patterns. |
| 17 class AutofillRegexes { | 16 class AutofillRegexes { |
| 18 public: | 17 public: |
| 19 static AutofillRegexes* GetInstance(); | 18 static AutofillRegexes* GetInstance(); |
| 20 | 19 |
| 21 // Returns the compiled regex matcher corresponding to |pattern|. | 20 // Returns the compiled regex matcher corresponding to |pattern|. |
| 22 icu::RegexMatcher* GetMatcher(const base::string16& pattern); | 21 re2::RE2* GetMatcher(const std::string& pattern); |
| 23 | 22 |
| 24 private: | 23 private: |
| 25 AutofillRegexes(); | 24 AutofillRegexes(); |
| 26 ~AutofillRegexes(); | 25 ~AutofillRegexes(); |
| 27 friend struct base::DefaultSingletonTraits<AutofillRegexes>; | 26 friend struct base::DefaultSingletonTraits<AutofillRegexes>; |
| 28 | 27 |
| 29 // Maps patterns to their corresponding regex matchers. | 28 // Maps patterns to their corresponding regex matchers. |
| 30 base::ScopedPtrHashMap<base::string16, scoped_ptr<icu::RegexMatcher>> | 29 base::ScopedPtrHashMap<std::string, scoped_ptr<re2::RE2>> matchers_; |
| 31 matchers_; | |
| 32 | 30 |
| 33 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); | 31 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); |
| 34 }; | 32 }; |
| 35 | 33 |
| 36 // static | 34 // static |
| 37 AutofillRegexes* AutofillRegexes::GetInstance() { | 35 AutofillRegexes* AutofillRegexes::GetInstance() { |
| 38 return base::Singleton<AutofillRegexes>::get(); | 36 return base::Singleton<AutofillRegexes>::get(); |
| 39 } | 37 } |
| 40 | 38 |
| 41 AutofillRegexes::AutofillRegexes() { | 39 AutofillRegexes::AutofillRegexes() { |
| 42 } | 40 } |
| 43 | 41 |
| 44 AutofillRegexes::~AutofillRegexes() { | 42 AutofillRegexes::~AutofillRegexes() { |
| 45 } | 43 } |
| 46 | 44 |
| 47 icu::RegexMatcher* AutofillRegexes::GetMatcher(const base::string16& pattern) { | 45 re2::RE2* AutofillRegexes::GetMatcher(const std::string& pattern) { |
| 48 auto it = matchers_.find(pattern); | 46 auto it = matchers_.find(pattern); |
| 49 if (it == matchers_.end()) { | 47 if (it == matchers_.end()) { |
| 50 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); | 48 scoped_ptr<re2::RE2> matcher(new re2::RE2(pattern)); |
| 51 | 49 DCHECK(matcher->ok()); |
| 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()); | 50 auto result = matchers_.add(pattern, matcher.Pass()); |
| 58 DCHECK(result.second); | 51 DCHECK(result.second); |
| 59 it = result.first; | 52 it = result.first; |
| 60 } | 53 } |
| 61 return it->second; | 54 return it->second; |
| 62 } | 55 } |
| 63 | 56 |
| 64 } // namespace | 57 } // namespace |
| 65 | 58 |
| 66 namespace autofill { | 59 namespace autofill { |
| 67 | 60 |
| 68 bool MatchesPattern(const base::string16& input, | 61 bool MatchesPattern(const std::string& input, const std::string& pattern) { |
| 69 const base::string16& pattern) { | 62 re2::RE2* matcher = AutofillRegexes::GetInstance()->GetMatcher(pattern); |
|
Ilya Sherman
2015/11/19 02:09:00
Please add a TODO to verify whether it's still wor
tfarina
2015/11/20 19:03:37
Done.
| |
| 70 icu::RegexMatcher* matcher = | 63 return re2::RE2::FullMatch(input, *matcher); |
|
Ilya Sherman
2015/11/19 02:09:00
Do the tests pass with this change? I would have
tfarina
2015/11/20 19:03:37
Now they do. Sorry for not getting to test this lo
| |
| 71 AutofillRegexes::GetInstance()->GetMatcher(pattern); | |
| 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 } | 64 } |
| 80 | 65 |
| 81 } // namespace autofill | 66 } // namespace autofill |
| OLD | NEW |