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, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include <libaddressinput/address_data.h> | 15 #include <libaddressinput/address_data.h> |
| 16 | 16 |
| 17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
| 18 | 18 |
| 19 #include <cassert> | 19 #include <cassert> |
| 20 #include <cstddef> | |
| 20 #include <string> | 21 #include <string> |
| 22 #include <vector> | |
| 23 | |
| 24 #include "region_data_constants.h" | |
| 25 #include "rule.h" | |
| 21 | 26 |
| 22 namespace i18n { | 27 namespace i18n { |
| 23 namespace addressinput { | 28 namespace addressinput { |
| 24 | 29 |
| 30 void AddressData::BuildDisplayLines(std::vector<std::string>* lines) const { | |
| 31 assert(lines != NULL); | |
| 32 lines->clear(); | |
| 33 | |
| 34 Rule rule; | |
| 35 rule.CopyFrom(Rule::GetDefault()); | |
| 36 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); | |
| 37 | |
| 38 for (size_t i = 0; i < rule.GetFormat().size(); ++i) { | |
| 39 std::string line; | |
| 40 for (size_t j = 0; j < rule.GetFormat()[i].size(); ++j) { | |
| 41 const FormatElement& element = rule.GetFormat()[i][j]; | |
|
Evan Stade
2014/01/14 18:05:22
rule.GetFormat()[i] is used enough to get a local
please use gerrit instead
2014/01/14 23:21:12
Done.
| |
| 42 switch (element.type) { | |
| 43 case FormatElement::LITERAL: | |
| 44 line += element.literal; | |
| 45 break; | |
| 46 | |
| 47 case FormatElement::FIELD: | |
| 48 if (element.field == STREET_ADDRESS) { | |
| 49 // A street address is always surrounded by either newlines or | |
| 50 // spaces. | |
| 51 if (rule.GetFormat()[i].size() == 1) { | |
| 52 lines->insert(lines->end(), | |
| 53 address_lines.begin(), | |
| 54 address_lines.end()); | |
| 55 } else { | |
| 56 for (size_t k = 0; k < address_lines.size(); ++k) { | |
|
Evan Stade
2014/01/14 18:05:22
I don't understand why street address line 2 might
please use gerrit instead
2014/01/14 23:21:12
Take the https://i18napis.appspot.com/ssl-address/
Evan Stade
2014/01/14 23:45:36
I think that would imply there's only one street a
please use gerrit instead
2014/01/15 00:05:47
Combined the cases and simplified the code. If Chr
| |
| 57 line += address_lines[k]; | |
| 58 if (k < address_lines.size() - 1) { | |
| 59 line += " "; | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 } else { | |
| 64 const std::string& field = GetField(element.field); | |
| 65 if (!field.empty()) { | |
| 66 line += field; | |
| 67 } | |
| 68 } | |
| 69 break; | |
| 70 | |
| 71 default: | |
| 72 assert(false); | |
| 73 break; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 if (!line.empty()) { | |
| 78 lines->push_back(line); | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 25 const std::string& AddressData::GetField(AddressField field) const { | 83 const std::string& AddressData::GetField(AddressField field) const { |
|
Evan Stade
2014/01/14 18:05:22
nit: should be called GetFieldValue
please use gerrit instead
2014/01/14 23:21:12
Done.
| |
| 26 switch (field) { | 84 switch (field) { |
| 27 case COUNTRY: | 85 case COUNTRY: |
| 28 return country_code; | 86 return country_code; |
| 29 case ADMIN_AREA: | 87 case ADMIN_AREA: |
| 30 return administrative_area; | 88 return administrative_area; |
| 31 case LOCALITY: | 89 case LOCALITY: |
| 32 return locality; | 90 return locality; |
| 33 case DEPENDENT_LOCALITY: | 91 case DEPENDENT_LOCALITY: |
| 34 return dependent_locality; | 92 return dependent_locality; |
| 35 case SORTING_CODE: | 93 case SORTING_CODE: |
| 36 return sorting_code; | 94 return sorting_code; |
| 37 case POSTAL_CODE: | 95 case POSTAL_CODE: |
| 38 return postal_code; | 96 return postal_code; |
| 39 case ORGANIZATION: | 97 case ORGANIZATION: |
| 40 return organization; | 98 return organization; |
| 41 case RECIPIENT: | 99 case RECIPIENT: |
| 42 return recipient; | 100 return recipient; |
| 43 default: | 101 default: |
| 44 assert(false); | 102 assert(false); |
| 45 return recipient; | 103 return recipient; |
| 46 } | 104 } |
| 47 } | 105 } |
| 48 | 106 |
| 49 } // namespace addressinput | 107 } // namespace addressinput |
| 50 } // namespace i18n | 108 } // namespace i18n |
| OLD | NEW |