Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/rule.cc

Issue 131223004: [rac] Format an address for display. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 %.
89 for (size_t i = 1; i < format_parts.size(); ++i) { 101 for (size_t i = 1; i < format_parts.size(); ++i) {
102 if (format_parts[i].empty()) {
103 continue;
104 }
105
106 // The first character after % denotes a field or a newline token.
107 const char control_character = format_parts[i][0];
108
109 // The rest of the string after the token is a literal.
110 const std::string literal = format_parts[i].substr(1);
111
90 AddressField field = COUNTRY; 112 AddressField field = COUNTRY;
91 if (ParseToken(format_parts[i][0], &field)) { 113 if (ParseToken(control_character, &field)) {
92 fields->back().push_back(field); 114 lines->back().push_back(FormatElement(field));
93 } else if (format_parts[i][0] == 'n') { 115 } else if (control_character == 'n') {
94 fields->push_back(std::vector<AddressField>()); 116 lines->push_back(std::vector<FormatElement>());
117 }
118
119 if (!literal.empty()) {
120 lines->back().push_back(FormatElement(literal));
95 } 121 }
96 } 122 }
97 } 123 }
98 124
99 // Clears |fields|, parses |required|, and adds the required fields to |fields|. 125 // Clears |fields|, parses |required|, and adds the required fields to |fields|.
100 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, 126 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY,
101 // SORTING_CODE}. 127 // SORTING_CODE}.
102 void ParseAddressFieldsRequired(const std::string& required, 128 void ParseAddressFieldsRequired(const std::string& required,
103 std::vector<AddressField>* fields) { 129 std::vector<AddressField>* fields) {
104 assert(fields != NULL); 130 assert(fields != NULL);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 192 }
167 if (postal_code_type == "zip") { 193 if (postal_code_type == "zip") {
168 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL 194 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL
169 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; 195 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL;
170 } 196 }
171 return INVALID_MESSAGE_ID; 197 return INVALID_MESSAGE_ID;
172 } 198 }
173 199
174 } // namespace 200 } // namespace
175 201
202 FormatElement::FormatElement(AddressField field)
203 : field(field), literal() {}
204
205 FormatElement::FormatElement(const std::string& literal)
206 : field(COUNTRY), literal(literal) {
207 assert(!literal.empty());
208 }
209
210 FormatElement::~FormatElement() {}
211
212 bool FormatElement::operator==(const FormatElement& other) const {
213 return field == other.field && literal == other.literal;
214 }
215
176 Rule::Rule() 216 Rule::Rule()
177 : format_(), 217 : format_(),
178 required_(), 218 required_(),
179 sub_keys_(), 219 sub_keys_(),
180 languages_(), 220 languages_(),
181 language_(), 221 language_(),
182 postal_code_format_(), 222 postal_code_format_(),
183 admin_area_name_message_id_(INVALID_MESSAGE_ID), 223 admin_area_name_message_id_(INVALID_MESSAGE_ID),
184 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} 224 postal_code_name_message_id_(INVALID_MESSAGE_ID) {}
185 225
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; 307 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL;
268 case POSTAL_CODE: 308 case POSTAL_CODE:
269 return invalid_postal_code_message_id_; 309 return invalid_postal_code_message_id_;
270 default: 310 default:
271 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; 311 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY;
272 } 312 }
273 } 313 }
274 314
275 } // namespace addressinput 315 } // namespace addressinput
276 } // namespace i18n 316 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698