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

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: Address comments. 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 |fields|, parses |format|, and adds the format address fields to
72 // |fields|. 72 // |fields|.
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 void ParseAddressFieldsFormat(const std::string& format, 82 // {"ÅLAND"}
83 std::vector<std::vector<AddressField> >* fields) { 83 // }
84 assert(fields != NULL); 84 void ParseAddressFieldsFormat(
85 fields->clear(); 85 const std::string& data,
86 fields->resize(1); 86 std::vector<std::vector<FormatElement> >* elements) {
87 std::vector<std::string> format_parts; 87 assert(elements != NULL);
88 SplitString(format, '%', &format_parts); 88 elements->clear();
89 for (size_t i = 1; i < format_parts.size(); ++i) { 89 elements->resize(1);
90
91 std::vector<std::string> data_parts;
92 SplitString(data, '%', &data_parts);
93 if (!data_parts.empty() && !data_parts[0].empty()) {
94 elements->back().push_back(FormatElement(data_parts[0]));
Evan Stade 2014/01/14 18:05:22 don't really understand what's going on here, and
please use gerrit instead 2014/01/14 23:21:12 Added a comment.
95 }
96
97 for (size_t i = 1; i < data_parts.size(); ++i) {
90 AddressField field = COUNTRY; 98 AddressField field = COUNTRY;
91 if (ParseToken(format_parts[i][0], &field)) { 99 if (ParseToken(data_parts[i][0], &field)) {
92 fields->back().push_back(field); 100 elements->back().push_back(FormatElement(field));
93 } else if (format_parts[i][0] == 'n') { 101 } else if (data_parts[i][0] == 'n') {
94 fields->push_back(std::vector<AddressField>()); 102 elements->push_back(std::vector<FormatElement>());
103 }
104 if (data_parts[i].length() > 1) {
105 elements->back().push_back(FormatElement(data_parts[i].substr(1)));
95 } 106 }
96 } 107 }
97 } 108 }
98 109
99 // Clears |fields|, parses |required|, and adds the required fields to |fields|. 110 // Clears |fields|, parses |required|, and adds the required fields to |fields|.
100 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, 111 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY,
101 // SORTING_CODE}. 112 // SORTING_CODE}.
102 void ParseAddressFieldsRequired(const std::string& required, 113 void ParseAddressFieldsRequired(const std::string& required,
103 std::vector<AddressField>* fields) { 114 std::vector<AddressField>* fields) {
104 assert(fields != NULL); 115 assert(fields != NULL);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 177 }
167 if (postal_code_type == "zip") { 178 if (postal_code_type == "zip") {
168 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL 179 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL
169 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; 180 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL;
170 } 181 }
171 return INVALID_MESSAGE_ID; 182 return INVALID_MESSAGE_ID;
172 } 183 }
173 184
174 } // namespace 185 } // namespace
175 186
187 FormatElement::FormatElement(AddressField field)
188 : type(FIELD), field(field), literal() {}
189
190 FormatElement::FormatElement(const std::string& literal)
191 : type(LITERAL), field(COUNTRY), literal(literal) {}
192
193 FormatElement::~FormatElement() {}
194
195 bool FormatElement::operator==(const FormatElement& other) const {
196 return type == other.type &&
197 field == other.field &&
198 literal == other.literal;
199 }
200
176 Rule::Rule() 201 Rule::Rule()
177 : format_(), 202 : format_(),
178 required_(), 203 required_(),
179 sub_keys_(), 204 sub_keys_(),
180 languages_(), 205 languages_(),
181 language_(), 206 language_(),
182 postal_code_format_(), 207 postal_code_format_(),
183 admin_area_name_message_id_(INVALID_MESSAGE_ID), 208 admin_area_name_message_id_(INVALID_MESSAGE_ID),
184 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} 209 postal_code_name_message_id_(INVALID_MESSAGE_ID) {}
185 210
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; 292 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL;
268 case POSTAL_CODE: 293 case POSTAL_CODE:
269 return invalid_postal_code_message_id_; 294 return invalid_postal_code_message_id_;
270 default: 295 default:
271 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; 296 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY;
272 } 297 }
273 } 298 }
274 299
275 } // namespace addressinput 300 } // namespace addressinput
276 } // namespace i18n 301 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698