diff options
author | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-28 01:00:25 +0000 |
---|---|---|
committer | dhollowa@chromium.org <dhollowa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-28 01:00:25 +0000 |
commit | 0f5164f1a101a9b89d2343b865f23162a0ad1c19 (patch) | |
tree | cab305ee89230c987a26ae88ae55c9ed7905f297 | |
parent | 20095e2e5da3c4dd5c4de9c0a97247d86884a461 (diff) | |
download | chromium_src-0f5164f1a101a9b89d2343b865f23162a0ad1c19.zip chromium_src-0f5164f1a101a9b89d2343b865f23162a0ad1c19.tar.gz chromium_src-0f5164f1a101a9b89d2343b865f23162a0ad1c19.tar.bz2 |
Autofill should use unicode/regex.h instead of WebKit::WebRegularExpression
Switches Autofill over to use ICU regex facilities.
BUG=84199, 84158
TEST=unit tests.
Review URL: http://codereview.chromium.org/7071048
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87126 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/autofill/autofill_scanner.cc | 19 | ||||
-rw-r--r-- | chrome/browser/autofill/autofill_scanner.h | 10 | ||||
-rw-r--r-- | chrome/browser/autofill/credit_card.cc | 34 | ||||
-rw-r--r-- | chrome/browser/autofill/form_field.cc | 35 | ||||
-rw-r--r-- | chrome/browser/autofill/form_structure_unittest.cc | 4 | ||||
-rw-r--r-- | chrome/browser/autofill/personal_data_manager.cc | 14 |
6 files changed, 58 insertions, 58 deletions
diff --git a/chrome/browser/autofill/autofill_scanner.cc b/chrome/browser/autofill/autofill_scanner.cc index 1fd8a7e..6526fcc 100644 --- a/chrome/browser/autofill/autofill_scanner.cc +++ b/chrome/browser/autofill/autofill_scanner.cc @@ -6,6 +6,7 @@ #include "base/logging.h" #include "chrome/browser/autofill/autofill_field.h" +#include "unicode/regex.h" AutofillScanner::AutofillScanner( const std::vector<const AutofillField*>& fields) @@ -43,3 +44,21 @@ void AutofillScanner::Rewind() { void AutofillScanner::SaveCursor() { saved_cursors_.push_back(cursor_); } + +namespace autofill { + +bool MatchString(const string16& input, const string16& pattern) { + UErrorCode status = U_ZERO_ERROR; + icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); + icu::UnicodeString icu_input(input.data(), input.length()); + icu::RegexMatcher matcher(icu_pattern, icu_input, + UREGEX_CASE_INSENSITIVE, status); + DCHECK(U_SUCCESS(status)); + + UBool match = matcher.find(0, status); + DCHECK(U_SUCCESS(status)); + return !!match; +} + +} // namespace autofill + diff --git a/chrome/browser/autofill/autofill_scanner.h b/chrome/browser/autofill/autofill_scanner.h index 8c582dd..7eca0ce 100644 --- a/chrome/browser/autofill/autofill_scanner.h +++ b/chrome/browser/autofill/autofill_scanner.h @@ -9,6 +9,7 @@ #include <vector> #include "base/basictypes.h" +#include "base/string16.h" class AutofillField; @@ -48,4 +49,13 @@ class AutofillScanner { DISALLOW_COPY_AND_ASSIGN(AutofillScanner); }; +// Parsing utilities. +namespace autofill { + +// Case-insensitive regular expression matching. Returns true if |pattern| is +// found in |input|. +bool MatchString(const string16& input, const string16& pattern); + +} // namespace autofill + #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_SCANNER_H_ diff --git a/chrome/browser/autofill/credit_card.cc b/chrome/browser/autofill/credit_card.cc index d6ea407..4e487da 100644 --- a/chrome/browser/autofill/credit_card.cc +++ b/chrome/browser/autofill/credit_card.cc @@ -14,13 +14,11 @@ #include "base/string_split.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/autofill/autofill_scanner.h" #include "chrome/browser/autofill/autofill_type.h" #include "chrome/browser/autofill/field_types.h" #include "chrome/common/guid.h" #include "grit/generated_resources.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCaseSensitivity.h" #include "ui/base/l10n/l10n_util.h" namespace { @@ -322,22 +320,20 @@ const string16 CreditCard::Label() const { void CreditCard::SetInfoForMonthInputType(const string16& value) { // Check if |text| is "yyyy-mm" format first, and check normal month format. - WebKit::WebRegularExpression re(WebKit::WebString("^[0-9]{4}\\-[0-9]{1,2}$"), - WebKit::WebTextCaseInsensitive); - bool match = re.match(WebKit::WebString(StringToLowerASCII(value))) != -1; - if (match) { - std::vector<string16> year_month; - base::SplitString(value, L'-', &year_month); - DCHECK_EQ((int)year_month.size(), 2); - int num = 0; - bool converted = false; - converted = base::StringToInt(year_month[0], &num); - DCHECK(converted); - SetExpirationYear(num); - converted = base::StringToInt(year_month[1], &num); - DCHECK(converted); - SetExpirationMonth(num); - } + if (!autofill::MatchString(value, UTF8ToUTF16("^[0-9]{4}-[0-9]{1,2}$"))) + return; + + std::vector<string16> year_month; + base::SplitString(value, L'-', &year_month); + DCHECK_EQ((int)year_month.size(), 2); + int num = 0; + bool converted = false; + converted = base::StringToInt(year_month[0], &num); + DCHECK(converted); + SetExpirationYear(num); + converted = base::StringToInt(year_month[1], &num); + DCHECK(converted); + SetExpirationMonth(num); } string16 CreditCard::ObfuscatedNumber() const { diff --git a/chrome/browser/autofill/form_field.cc b/chrome/browser/autofill/form_field.cc index 5cd6ae6..01f2ee1 100644 --- a/chrome/browser/autofill/form_field.cc +++ b/chrome/browser/autofill/form_field.cc @@ -24,9 +24,6 @@ #include "chrome/browser/autofill/name_field.h" #include "chrome/browser/autofill/phone_field.h" #include "grit/autofill_resources.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCaseSensitivity.h" #include "ui/base/l10n/l10n_util.h" namespace { @@ -34,26 +31,6 @@ namespace { using autofill::GetEcmlPattern; using autofill::HasECMLField; -bool MatchName(const AutofillField* field, const string16& pattern) { - // TODO(jhawkins): Remove StringToLowerASCII. WebRegularExpression needs to - // be fixed to take WebTextCaseInsensitive into account. - WebKit::WebRegularExpression re(WebKit::WebString(pattern), - WebKit::WebTextCaseInsensitive); - bool match = re.match( - WebKit::WebString(StringToLowerASCII(field->name))) != -1; - return match; -} - -bool MatchLabel(const AutofillField* field, const string16& pattern) { - // TODO(jhawkins): Remove StringToLowerASCII. WebRegularExpression needs to - // be fixed to take WebTextCaseInsensitive into account. - WebKit::WebRegularExpression re(WebKit::WebString(pattern), - WebKit::WebTextCaseInsensitive); - bool match = re.match( - WebKit::WebString(StringToLowerASCII(field->label))) != -1; - return match; -} - bool IsTextField(const string16& type) { return type == ASCIIToUTF16("text"); } @@ -173,11 +150,15 @@ bool FormField::MatchAndAdvance(AutofillScanner* scanner, bool FormField::Match(const AutofillField* field, const string16& pattern, int match_type) { - if ((match_type & FormField::MATCH_LABEL) && MatchLabel(field, pattern)) - return true; + if ((match_type & FormField::MATCH_LABEL) && + autofill::MatchString(field->label, pattern)) { + return true; + } - if ((match_type & FormField::MATCH_NAME) && MatchName(field, pattern)) - return true; + if ((match_type & FormField::MATCH_NAME) && + autofill::MatchString(field->name, pattern)) { + return true; + } return false; } diff --git a/chrome/browser/autofill/form_structure_unittest.cc b/chrome/browser/autofill/form_structure_unittest.cc index 983f8e8..7f80582 100644 --- a/chrome/browser/autofill/form_structure_unittest.cc +++ b/chrome/browser/autofill/form_structure_unittest.cc @@ -833,7 +833,7 @@ TEST(FormStructureTest, ThreeAddressLinesExpedia) { form_structure->DetermineHeuristicTypes(); EXPECT_TRUE(form_structure->IsAutofillable(true)); ASSERT_EQ(4U, form_structure->field_count()); - ASSERT_EQ(3U, form_structure->autofill_count()); + EXPECT_EQ(3U, form_structure->autofill_count()); // Address Line 1. EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); @@ -1125,7 +1125,7 @@ TEST(FormStructureTest, HeuristicsInfernoCC) { // Expect the correct number of fields. ASSERT_EQ(5U, form_structure->field_count()); - ASSERT_EQ(5U, form_structure->autofill_count()); + EXPECT_EQ(5U, form_structure->autofill_count()); // Name on Card. EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); diff --git a/chrome/browser/autofill/personal_data_manager.cc b/chrome/browser/autofill/personal_data_manager.cc index 03eb64e..d3d78ad 100644 --- a/chrome/browser/autofill/personal_data_manager.cc +++ b/chrome/browser/autofill/personal_data_manager.cc @@ -13,6 +13,7 @@ #include "chrome/browser/autofill/autofill-inl.h" #include "chrome/browser/autofill/autofill_field.h" #include "chrome/browser/autofill/autofill_metrics.h" +#include "chrome/browser/autofill/autofill_scanner.h" #include "chrome/browser/autofill/form_structure.h" #include "chrome/browser/autofill/phone_number.h" #include "chrome/browser/autofill/phone_number_i18n.h" @@ -23,8 +24,6 @@ #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/pref_names.h" #include "content/browser/browser_thread.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" namespace { @@ -76,20 +75,15 @@ T* address_of(T& v) { bool IsValidEmail(const string16& value) { // This regex is more permissive than the official rfc2822 spec on the // subject, but it does reject obvious non-email addresses. - const string16 kEmailPattern = - ASCIIToUTF16("^[^@]+@[^@]+\\.[a-z]{2,6}$"); - WebKit::WebRegularExpression re(WebKit::WebString(kEmailPattern), - WebKit::WebTextCaseInsensitive); - return re.match(WebKit::WebString(StringToLowerASCII(value))) != -1; + const string16 kEmailPattern = ASCIIToUTF16("^[^@]+@[^@]+\\.[a-z]{2,6}$"); + return autofill::MatchString(value, kEmailPattern); } // Valid for US zip codes only. bool IsValidZip(const string16& value) { // Basic US zip code matching. const string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); - WebKit::WebRegularExpression re(WebKit::WebString(kZipPattern), - WebKit::WebTextCaseInsensitive); - return re.match(WebKit::WebString(StringToLowerASCII(value))) != -1; + return autofill::MatchString(value, kZipPattern); } // Returns true if minimum requirements for import of a given |profile| have |