OLD | NEW |
---|---|
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 *field = ORGANIZATION; | 61 *field = ORGANIZATION; |
62 return true; | 62 return true; |
63 case 'N': | 63 case 'N': |
64 *field = RECIPIENT; | 64 *field = RECIPIENT; |
65 return true; | 65 return true; |
66 default: | 66 default: |
67 return false; | 67 return false; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 // Clears |fields|, parses |format|, and adds the format address fields to | 71 // Clears |lines|, parses |format|, and adds the address fields and literals to |
72 // |fields|. | 72 // |lines|. |
73 // | 73 // |
74 // For example, the address format in Switzerland is "%O%n%N%n%A%nAX-%Z | 74 // For example, the address format in Finland is "%O%n%N%n%A%nAX-%Z %C%nÅLAND". |
75 // %C%nÅLAND". It includes the allowed fields prefixed with %, newlines denoted | 75 // It includes the allowed fields prefixed with %, newlines denoted %n, and the |
76 // %n, and the extra text that should be included on an envelope. This function | 76 // extra text that should be included on an envelope. It is parsed into: |
77 // parses only the tokens denoted % to determine how an address input form | 77 // { |
78 // should be laid out. | 78 // {ORGANIZATION}, |
79 // | 79 // {RECIPIENT}, |
80 // The format string "%O%n%N%n%A%nAX-%Z%C%nÅLAND" is parsed into | 80 // {STREET_ADDRESS}, |
81 // {{ORGANIZATION}, {RECIPIENT}, {STREET_ADDRESS}, {POSTAL_CODE, LOCALITY}, {}}. | 81 // {"AX-", POSTAL_CODE, " ", LOCALITY}, |
82 // {"ÅLAND"} | |
83 // } | |
82 void ParseAddressFieldsFormat(const std::string& format, | 84 void ParseAddressFieldsFormat(const std::string& format, |
83 std::vector<std::vector<AddressField> >* fields) { | 85 std::vector<std::vector<FormatElement> >* lines) { |
84 assert(fields != NULL); | 86 assert(lines != NULL); |
85 fields->clear(); | 87 lines->clear(); |
86 fields->resize(1); | 88 lines->resize(1); |
89 | |
87 std::vector<std::string> format_parts; | 90 std::vector<std::string> format_parts; |
88 SplitString(format, '%', &format_parts); | 91 SplitString(format, '%', &format_parts); |
92 | |
93 // If the address format starts with a literal, then it will be in the first | |
94 // element of |format_parts|. This literal does not begin with % and should | |
95 // not be parsed as a token. | |
96 if (!format_parts.empty() && !format_parts[0].empty()) { | |
97 lines->back().push_back(FormatElement(format_parts[0])); | |
98 } | |
99 | |
100 // The rest of the elements in |format_parts| begin with %. The first | |
101 // character after % denotes a field or a newline token. The rest of the | |
102 // string after the token is a literal. | |
89 for (size_t i = 1; i < format_parts.size(); ++i) { | 103 for (size_t i = 1; i < format_parts.size(); ++i) { |
104 const char control_character = format_parts[i][0]; | |
Evan Stade
2014/01/16 00:37:40
should maybe:
if (format_parts[i].empty())
cont
please use gerrit instead
2014/01/16 01:22:04
Done.
| |
90 AddressField field = COUNTRY; | 105 AddressField field = COUNTRY; |
91 if (ParseToken(format_parts[i][0], &field)) { | 106 |
92 fields->back().push_back(field); | 107 if (ParseToken(control_character, &field)) { |
93 } else if (format_parts[i][0] == 'n') { | 108 lines->back().push_back(FormatElement(field)); |
94 fields->push_back(std::vector<AddressField>()); | 109 } else if (control_character == 'n') { |
110 lines->push_back(std::vector<FormatElement>()); | |
111 } | |
112 | |
113 if (format_parts[i].length() > 1) { | |
114 std::string literal = format_parts[i].substr(1); | |
Evan Stade
2014/01/16 00:37:40
std::string literal = format_parts[i].substr(1);
i
please use gerrit instead
2014/01/16 01:22:04
Done.
| |
115 lines->back().push_back(FormatElement(literal)); | |
95 } | 116 } |
96 } | 117 } |
97 } | 118 } |
98 | 119 |
99 // Clears |fields|, parses |required|, and adds the required fields to |fields|. | 120 // Clears |fields|, parses |required|, and adds the required fields to |fields|. |
100 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, | 121 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, |
101 // SORTING_CODE}. | 122 // SORTING_CODE}. |
102 void ParseAddressFieldsRequired(const std::string& required, | 123 void ParseAddressFieldsRequired(const std::string& required, |
103 std::vector<AddressField>* fields) { | 124 std::vector<AddressField>* fields) { |
104 assert(fields != NULL); | 125 assert(fields != NULL); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 } | 187 } |
167 if (postal_code_type == "zip") { | 188 if (postal_code_type == "zip") { |
168 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL | 189 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL |
169 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; | 190 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; |
170 } | 191 } |
171 return INVALID_MESSAGE_ID; | 192 return INVALID_MESSAGE_ID; |
172 } | 193 } |
173 | 194 |
174 } // namespace | 195 } // namespace |
175 | 196 |
197 FormatElement::FormatElement(AddressField field) | |
198 : field(field), literal() {} | |
199 | |
200 FormatElement::FormatElement(const std::string& literal) | |
201 : field(COUNTRY), literal(literal) { | |
202 assert(!literal.empty()); | |
203 } | |
204 | |
205 FormatElement::~FormatElement() {} | |
206 | |
207 bool FormatElement::operator==(const FormatElement& other) const { | |
208 return field == other.field && literal == other.literal; | |
209 } | |
210 | |
176 Rule::Rule() | 211 Rule::Rule() |
177 : format_(), | 212 : format_(), |
178 required_(), | 213 required_(), |
179 sub_keys_(), | 214 sub_keys_(), |
180 languages_(), | 215 languages_(), |
181 language_(), | 216 language_(), |
182 postal_code_format_(), | 217 postal_code_format_(), |
183 admin_area_name_message_id_(INVALID_MESSAGE_ID), | 218 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
184 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} | 219 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} |
185 | 220 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; | 302 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; |
268 case POSTAL_CODE: | 303 case POSTAL_CODE: |
269 return invalid_postal_code_message_id_; | 304 return invalid_postal_code_message_id_; |
270 default: | 305 default: |
271 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; | 306 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; |
272 } | 307 } |
273 } | 308 } |
274 | 309 |
275 } // namespace addressinput | 310 } // namespace addressinput |
276 } // namespace i18n | 311 } // namespace i18n |
OLD | NEW |