Chromium Code Reviews| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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) { |
|
Evan Stade
2014/01/14 23:45:36
nit: s/elements/lines
please use gerrit instead
2014/01/15 00:05:47
Done.
| |
| 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 | |
| 94 // If the address format starts with a literal, then it will be in the first | |
| 95 // element of |data_parts|. This literal does not begin with % and should not | |
| 96 // be parsed as a token. | |
| 97 if (!data_parts.empty() && !data_parts[0].empty()) { | |
| 98 elements->back().push_back(FormatElement(data_parts[0])); | |
| 99 } | |
| 100 | |
| 101 | |
|
Evan Stade
2014/01/14 23:45:36
^H
please use gerrit instead
2014/01/15 00:05:47
Done.
| |
| 102 // The rest of the elements in |data_parts| begin with %. The first character | |
| 103 // after % denotes a field or a newline token. The rest of the string after | |
| 104 // the token is a literal. | |
|
Evan Stade
2014/01/14 23:45:36
thanks for the explanation. To make the code more
please use gerrit instead
2014/01/15 00:05:47
Done.
| |
| 105 for (size_t i = 1; i < data_parts.size(); ++i) { | |
| 90 AddressField field = COUNTRY; | 106 AddressField field = COUNTRY; |
| 91 if (ParseToken(format_parts[i][0], &field)) { | 107 if (ParseToken(data_parts[i][0], &field)) { |
| 92 fields->back().push_back(field); | 108 elements->back().push_back(FormatElement(field)); |
| 93 } else if (format_parts[i][0] == 'n') { | 109 } else if (data_parts[i][0] == 'n') { |
| 94 fields->push_back(std::vector<AddressField>()); | 110 elements->push_back(std::vector<FormatElement>()); |
| 111 } | |
| 112 if (data_parts[i].length() > 1) { | |
| 113 elements->back().push_back(FormatElement(data_parts[i].substr(1))); | |
| 95 } | 114 } |
| 96 } | 115 } |
| 97 } | 116 } |
| 98 | 117 |
| 99 // Clears |fields|, parses |required|, and adds the required fields to |fields|. | 118 // Clears |fields|, parses |required|, and adds the required fields to |fields|. |
| 100 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, | 119 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY, |
| 101 // SORTING_CODE}. | 120 // SORTING_CODE}. |
| 102 void ParseAddressFieldsRequired(const std::string& required, | 121 void ParseAddressFieldsRequired(const std::string& required, |
| 103 std::vector<AddressField>* fields) { | 122 std::vector<AddressField>* fields) { |
| 104 assert(fields != NULL); | 123 assert(fields != NULL); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 } | 185 } |
| 167 if (postal_code_type == "zip") { | 186 if (postal_code_type == "zip") { |
| 168 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL | 187 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL |
| 169 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; | 188 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; |
| 170 } | 189 } |
| 171 return INVALID_MESSAGE_ID; | 190 return INVALID_MESSAGE_ID; |
| 172 } | 191 } |
| 173 | 192 |
| 174 } // namespace | 193 } // namespace |
| 175 | 194 |
| 195 FormatElement::FormatElement(AddressField field) | |
| 196 : field(field), literal() {} | |
| 197 | |
| 198 FormatElement::FormatElement(const std::string& literal) | |
| 199 : field(COUNTRY), literal(literal) { | |
| 200 assert(!literal.empty()); | |
| 201 } | |
| 202 | |
| 203 FormatElement::~FormatElement() {} | |
| 204 | |
| 205 bool FormatElement::operator==(const FormatElement& other) const { | |
| 206 return field == other.field && literal == other.literal; | |
| 207 } | |
| 208 | |
| 176 Rule::Rule() | 209 Rule::Rule() |
| 177 : format_(), | 210 : format_(), |
| 178 required_(), | 211 required_(), |
| 179 sub_keys_(), | 212 sub_keys_(), |
| 180 languages_(), | 213 languages_(), |
| 181 language_(), | 214 language_(), |
| 182 postal_code_format_(), | 215 postal_code_format_(), |
| 183 admin_area_name_message_id_(INVALID_MESSAGE_ID), | 216 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
| 184 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} | 217 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} |
| 185 | 218 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; | 300 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; |
| 268 case POSTAL_CODE: | 301 case POSTAL_CODE: |
| 269 return invalid_postal_code_message_id_; | 302 return invalid_postal_code_message_id_; |
| 270 default: | 303 default: |
| 271 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; | 304 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; |
| 272 } | 305 } |
| 273 } | 306 } |
| 274 | 307 |
| 275 } // namespace addressinput | 308 } // namespace addressinput |
| 276 } // namespace i18n | 309 } // namespace i18n |
| OLD | NEW |