OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/common/android/address_parser_internal.h" | 5 #include "content/common/android/address_parser_internal.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Number of digits for a valid zip code. | 14 // Number of digits for a valid zip code. |
15 const size_t kZipDigits = 5; | 15 const size_t kZipDigits = 5; |
16 | 16 |
17 // Number of digits for a valid zip code in the Zip Plus 4 format. | 17 // Number of digits for a valid zip code in the Zip Plus 4 format. |
18 const size_t kZipPlus4Digits = 9; | 18 const size_t kZipPlus4Digits = 9; |
19 | 19 |
20 // Maximum number of digits of a house number, including possible hyphens. | 20 // Maximum number of digits of a house number, including possible hyphens. |
21 const size_t kMaxHouseDigits = 5; | 21 const size_t kMaxHouseDigits = 5; |
22 | 22 |
23 char16 SafePreviousChar(const string16::const_iterator& it, | 23 char16 SafePreviousChar(const base::string16::const_iterator& it, |
24 const string16::const_iterator& begin) { | 24 const base::string16::const_iterator& begin) { |
25 if (it == begin) | 25 if (it == begin) |
26 return ' '; | 26 return ' '; |
27 return *(it - 1); | 27 return *(it - 1); |
28 } | 28 } |
29 | 29 |
30 char16 SafeNextChar(const string16::const_iterator& it, | 30 char16 SafeNextChar(const base::string16::const_iterator& it, |
31 const string16::const_iterator& end) { | 31 const base::string16::const_iterator& end) { |
32 if (it == end) | 32 if (it == end) |
33 return ' '; | 33 return ' '; |
34 return *(it + 1); | 34 return *(it + 1); |
35 } | 35 } |
36 | 36 |
37 bool WordLowerCaseEqualsASCII(string16::const_iterator word_begin, | 37 bool WordLowerCaseEqualsASCII(base::string16::const_iterator word_begin, |
38 string16::const_iterator word_end, const char* ascii_to_match) { | 38 base::string16::const_iterator word_end, const char* ascii_to_match) { |
39 for (string16::const_iterator it = word_begin; it != word_end; | 39 for (base::string16::const_iterator it = word_begin; it != word_end; |
40 ++it, ++ascii_to_match) { | 40 ++it, ++ascii_to_match) { |
41 if (!*ascii_to_match || base::ToLowerASCII(*it) != *ascii_to_match) | 41 if (!*ascii_to_match || base::ToLowerASCII(*it) != *ascii_to_match) |
42 return false; | 42 return false; |
43 } | 43 } |
44 return *ascii_to_match == 0 || *ascii_to_match == ' '; | 44 return *ascii_to_match == 0 || *ascii_to_match == ' '; |
45 } | 45 } |
46 | 46 |
47 bool LowerCaseEqualsASCIIWithPlural(string16::const_iterator word_begin, | 47 bool LowerCaseEqualsASCIIWithPlural(base::string16::const_iterator word_begin, |
48 string16::const_iterator word_end, const char* ascii_to_match, | 48 base::string16::const_iterator word_end, const char* ascii_to_match, |
49 bool allow_plural) { | 49 bool allow_plural) { |
50 for (string16::const_iterator it = word_begin; it != word_end; | 50 for (base::string16::const_iterator it = word_begin; it != word_end; |
51 ++it, ++ascii_to_match) { | 51 ++it, ++ascii_to_match) { |
52 if (!*ascii_to_match && allow_plural && *it == 's' && it + 1 == word_end) | 52 if (!*ascii_to_match && allow_plural && *it == 's' && it + 1 == word_end) |
53 return true; | 53 return true; |
54 | 54 |
55 if (!*ascii_to_match || base::ToLowerASCII(*it) != *ascii_to_match) | 55 if (!*ascii_to_match || base::ToLowerASCII(*it) != *ascii_to_match) |
56 return false; | 56 return false; |
57 } | 57 } |
58 return *ascii_to_match == 0; | 58 return *ascii_to_match == 0; |
59 } | 59 } |
60 | 60 |
61 } // anonymous namespace | 61 } // anonymous namespace |
62 | 62 |
63 namespace content { | 63 namespace content { |
64 | 64 |
65 namespace address_parser { | 65 namespace address_parser { |
66 | 66 |
67 namespace internal { | 67 namespace internal { |
68 | 68 |
69 Word::Word(const string16::const_iterator& begin, | 69 Word::Word(const base::string16::const_iterator& begin, |
70 const string16::const_iterator& end) | 70 const base::string16::const_iterator& end) |
71 : begin(begin), | 71 : begin(begin), |
72 end(end) { | 72 end(end) { |
73 DCHECK(begin <= end); | 73 DCHECK(begin <= end); |
74 } | 74 } |
75 | 75 |
76 bool HouseNumberParser::IsPreDelimiter(char16 character) { | 76 bool HouseNumberParser::IsPreDelimiter(char16 character) { |
77 return character == ':' || IsPostDelimiter(character); | 77 return character == ':' || IsPostDelimiter(character); |
78 } | 78 } |
79 | 79 |
80 bool HouseNumberParser::IsPostDelimiter(char16 character) { | 80 bool HouseNumberParser::IsPostDelimiter(char16 character) { |
(...skipping 27 matching lines...) Expand all Loading... |
108 return false; | 108 return false; |
109 | 109 |
110 if (word) { | 110 if (word) { |
111 word->begin = it_ - result_chars_; | 111 word->begin = it_ - result_chars_; |
112 word->end = it_; | 112 word->end = it_; |
113 } | 113 } |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 bool HouseNumberParser::Parse( | 117 bool HouseNumberParser::Parse( |
118 const string16::const_iterator& begin, | 118 const base::string16::const_iterator& begin, |
119 const string16::const_iterator& end, Word* word) { | 119 const base::string16::const_iterator& end, Word* word) { |
120 it_ = begin_ = begin; | 120 it_ = begin_ = begin; |
121 end_ = end; | 121 end_ = end; |
122 ResetState(); | 122 ResetState(); |
123 | 123 |
124 // Iterations only used as a fail-safe against any buggy infinite loops. | 124 // Iterations only used as a fail-safe against any buggy infinite loops. |
125 size_t iterations = 0; | 125 size_t iterations = 0; |
126 size_t max_iterations = end - begin + 1; | 126 size_t max_iterations = end - begin + 1; |
127 for (; it_ != end_ && iterations < max_iterations; ++iterations) { | 127 for (; it_ != end_ && iterations < max_iterations; ++iterations) { |
128 | 128 |
129 // Word finished case. | 129 // Word finished case. |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 } | 410 } |
411 | 411 |
412 return false; | 412 return false; |
413 } | 413 } |
414 | 414 |
415 bool IsZipValid(const Word& word, size_t state_index) { | 415 bool IsZipValid(const Word& word, size_t state_index) { |
416 size_t length = word.end - word.begin; | 416 size_t length = word.end - word.begin; |
417 if (length != kZipDigits && length != kZipPlus4Digits + 1) | 417 if (length != kZipDigits && length != kZipPlus4Digits + 1) |
418 return false; | 418 return false; |
419 | 419 |
420 for (string16::const_iterator it = word.begin; it != word.end; ++it) { | 420 for (base::string16::const_iterator it = word.begin; it != word.end; ++it) { |
421 size_t pos = it - word.begin; | 421 size_t pos = it - word.begin; |
422 if (IsAsciiDigit(*it) || (*it == '-' && pos == kZipDigits)) | 422 if (IsAsciiDigit(*it) || (*it == '-' && pos == kZipDigits)) |
423 continue; | 423 continue; |
424 return false; | 424 return false; |
425 } | 425 } |
426 return IsZipValidForState(word, state_index); | 426 return IsZipValidForState(word, state_index); |
427 } | 427 } |
428 | 428 |
429 bool IsZipValidForState(const Word& word, size_t state_index) { | 429 bool IsZipValidForState(const Word& word, size_t state_index) { |
430 // List of valid zip code ranges. | 430 // List of valid zip code ranges. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 } | 619 } |
620 | 620 |
621 return false; | 621 return false; |
622 } | 622 } |
623 | 623 |
624 } // namespace internal | 624 } // namespace internal |
625 | 625 |
626 } // namespace address_parser | 626 } // namespace address_parser |
627 | 627 |
628 } // namespace content | 628 } // namespace content |
OLD | NEW |