| 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 <memory> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/containers/scoped_ptr_hash_map.h" | 10 #include "base/containers/scoped_ptr_hash_map.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "third_party/icu/source/i18n/unicode/regex.h" | 15 #include "third_party/icu/source/i18n/unicode/regex.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // A singleton class that serves as a cache of compiled regex patterns. | 19 // A singleton class that serves as a cache of compiled regex patterns. |
| 20 class AutofillRegexes { | 20 class AutofillRegexes { |
| 21 public: | 21 public: |
| 22 static AutofillRegexes* GetInstance(); | 22 static AutofillRegexes* GetInstance(); |
| 23 | 23 |
| 24 // Returns the compiled regex matcher corresponding to |pattern|. | 24 // Returns the compiled regex matcher corresponding to |pattern|. |
| 25 icu::RegexMatcher* GetMatcher(const base::string16& pattern); | 25 icu::RegexMatcher* GetMatcher(const base::string16& pattern); |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 AutofillRegexes(); | 28 AutofillRegexes(); |
| 29 ~AutofillRegexes(); | 29 ~AutofillRegexes(); |
| 30 friend struct base::DefaultSingletonTraits<AutofillRegexes>; | 30 friend struct base::DefaultSingletonTraits<AutofillRegexes>; |
| 31 | 31 |
| 32 // Maps patterns to their corresponding regex matchers. | 32 // Maps patterns to their corresponding regex matchers. |
| 33 base::ScopedPtrHashMap<base::string16, scoped_ptr<icu::RegexMatcher>> | 33 base::ScopedPtrHashMap<base::string16, std::unique_ptr<icu::RegexMatcher>> |
| 34 matchers_; | 34 matchers_; |
| 35 | 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); | 36 DISALLOW_COPY_AND_ASSIGN(AutofillRegexes); |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 // static | 39 // static |
| 40 AutofillRegexes* AutofillRegexes::GetInstance() { | 40 AutofillRegexes* AutofillRegexes::GetInstance() { |
| 41 return base::Singleton<AutofillRegexes>::get(); | 41 return base::Singleton<AutofillRegexes>::get(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 AutofillRegexes::AutofillRegexes() { | 44 AutofillRegexes::AutofillRegexes() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 AutofillRegexes::~AutofillRegexes() { | 47 AutofillRegexes::~AutofillRegexes() { |
| 48 } | 48 } |
| 49 | 49 |
| 50 icu::RegexMatcher* AutofillRegexes::GetMatcher(const base::string16& pattern) { | 50 icu::RegexMatcher* AutofillRegexes::GetMatcher(const base::string16& pattern) { |
| 51 auto it = matchers_.find(pattern); | 51 auto it = matchers_.find(pattern); |
| 52 if (it == matchers_.end()) { | 52 if (it == matchers_.end()) { |
| 53 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); | 53 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); |
| 54 | 54 |
| 55 UErrorCode status = U_ZERO_ERROR; | 55 UErrorCode status = U_ZERO_ERROR; |
| 56 scoped_ptr<icu::RegexMatcher> matcher( | 56 std::unique_ptr<icu::RegexMatcher> matcher( |
| 57 new icu::RegexMatcher(icu_pattern, UREGEX_CASE_INSENSITIVE, status)); | 57 new icu::RegexMatcher(icu_pattern, UREGEX_CASE_INSENSITIVE, status)); |
| 58 DCHECK(U_SUCCESS(status)); | 58 DCHECK(U_SUCCESS(status)); |
| 59 | 59 |
| 60 auto result = matchers_.add(pattern, std::move(matcher)); | 60 auto result = matchers_.add(pattern, std::move(matcher)); |
| 61 DCHECK(result.second); | 61 DCHECK(result.second); |
| 62 it = result.first; | 62 it = result.first; |
| 63 } | 63 } |
| 64 return it->second; | 64 return it->second; |
| 65 } | 65 } |
| 66 | 66 |
| 67 } // namespace | 67 } // namespace |
| 68 | 68 |
| 69 namespace autofill { | 69 namespace autofill { |
| 70 | 70 |
| 71 bool MatchesPattern(const base::string16& input, | 71 bool MatchesPattern(const base::string16& input, |
| 72 const base::string16& pattern) { | 72 const base::string16& pattern) { |
| 73 icu::RegexMatcher* matcher = | 73 icu::RegexMatcher* matcher = |
| 74 AutofillRegexes::GetInstance()->GetMatcher(pattern); | 74 AutofillRegexes::GetInstance()->GetMatcher(pattern); |
| 75 icu::UnicodeString icu_input(input.data(), input.length()); | 75 icu::UnicodeString icu_input(input.data(), input.length()); |
| 76 matcher->reset(icu_input); | 76 matcher->reset(icu_input); |
| 77 | 77 |
| 78 UErrorCode status = U_ZERO_ERROR; | 78 UErrorCode status = U_ZERO_ERROR; |
| 79 UBool match = matcher->find(0, status); | 79 UBool match = matcher->find(0, status); |
| 80 DCHECK(U_SUCCESS(status)); | 80 DCHECK(U_SUCCESS(status)); |
| 81 return match == TRUE; | 81 return match == TRUE; |
| 82 } | 82 } |
| 83 | 83 |
| 84 } // namespace autofill | 84 } // namespace autofill |
| OLD | NEW |