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 // | |
| 15 // The language-to-script and country-to-script mapping is loosely based on: | |
| 16 // http://unicode.org/cldr/trac/browser/tags/release-24/common/supplemental/supp lementalData.xml | |
| 14 | 17 |
| 15 #include <libaddressinput/address_data.h> | 18 #include <libaddressinput/address_data.h> |
| 16 | 19 |
| 17 #include <libaddressinput/address_field.h> | 20 #include <libaddressinput/address_field.h> |
| 18 | 21 |
| 22 #include <algorithm> | |
| 19 #include <cassert> | 23 #include <cassert> |
| 24 #include <cstddef> | |
| 25 #include <ostream> | |
| 26 #include <sstream> | |
| 20 #include <string> | 27 #include <string> |
| 28 #include <vector> | |
| 29 | |
| 30 #include "region_data_constants.h" | |
| 31 #include "rule.h" | |
| 21 | 32 |
| 22 namespace i18n { | 33 namespace i18n { |
| 23 namespace addressinput { | 34 namespace addressinput { |
| 24 | 35 |
| 36 namespace { | |
| 37 | |
| 38 enum Script { | |
| 39 ARAB, | |
| 40 HANG, | |
| 41 HANS, | |
| 42 HANT, | |
| 43 JPAN, | |
| 44 THAI, | |
| 45 OTHER | |
| 46 }; | |
| 47 | |
| 48 std::string GetAddressLanguageCode(const AddressData& address, | |
| 49 const Rule& rule) { | |
| 50 std::vector<std::string>::const_iterator it = | |
| 51 std::find(rule.GetLanguages().begin(), | |
| 52 rule.GetLanguages().end(), | |
| 53 address.language_code); | |
| 54 if (it != rule.GetLanguages().end()) { | |
| 55 return *it; | |
| 56 } else if (!rule.GetLanguage().empty()) { | |
| 57 return rule.GetLanguage(); | |
| 58 } else { | |
| 59 return std::string(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 Script GetCountryScript(const std::string& country_code) { | |
| 64 if (country_code == "JP") { | |
| 65 return JPAN; | |
| 66 } else if (country_code == "HK" || | |
| 67 country_code == "MO" || | |
| 68 country_code == "TW") { | |
| 69 return HANT; | |
| 70 } else if (country_code == "CN") { | |
| 71 return HANS; | |
| 72 } else if (country_code == "AE" || | |
| 73 country_code == "AF" || | |
| 74 country_code == "BH" || | |
| 75 country_code == "DZ" || | |
| 76 country_code == "EG" || | |
| 77 country_code == "EH" || | |
| 78 country_code == "IQ" || | |
| 79 country_code == "IR" || | |
| 80 country_code == "JO" || | |
| 81 country_code == "KM" || | |
| 82 country_code == "KW" || | |
| 83 country_code == "LB" || | |
| 84 country_code == "LY" || | |
| 85 country_code == "MA" || | |
| 86 country_code == "MR" || | |
| 87 country_code == "OM" || | |
| 88 country_code == "PK" || | |
| 89 country_code == "PS" || | |
| 90 country_code == "QA" || | |
| 91 country_code == "SA" || | |
| 92 country_code == "SD" || | |
| 93 country_code == "SY" || | |
| 94 country_code == "TN" || | |
| 95 country_code == "YE") { | |
| 96 return ARAB; | |
| 97 } else if (country_code == "KP" || | |
| 98 country_code == "KR") { | |
| 99 return HANG; | |
| 100 } else if (country_code == "TH") { | |
| 101 return THAI; | |
| 102 } | |
| 103 return OTHER; | |
| 104 } | |
| 105 | |
| 106 Script GetLanguageScript(const std::string& language_code) { | |
| 107 if (language_code == "ja") { | |
| 108 return JPAN; | |
| 109 } else if (language_code == "zh" || | |
| 110 language_code == "zh-hant") { | |
| 111 return HANT; | |
| 112 } else if (language_code == "zh-hans") { | |
| 113 return HANS; | |
| 114 } else if (language_code == "ar" || | |
| 115 language_code == "cjm" || | |
| 116 language_code == "doi" || | |
| 117 language_code == "fa" || | |
| 118 language_code == "lah" || | |
| 119 language_code == "prd" || | |
| 120 language_code == "ps" || | |
| 121 language_code == "swb" || | |
| 122 language_code == "ug" || | |
| 123 language_code == "ur") { | |
| 124 return ARAB; | |
| 125 } else if (language_code == "ko") { | |
| 126 return HANG; | |
| 127 } else if (language_code == "kdt" || | |
| 128 language_code == "lcp" || | |
| 129 language_code == "lwl" || | |
| 130 language_code == "th" || | |
| 131 language_code == "tts") { | |
| 132 return THAI; | |
| 133 } | |
| 134 return OTHER; | |
| 135 } | |
| 136 | |
| 137 std::string GetCompactLineSeparator(Script script) { | |
| 138 switch (script) { | |
| 139 case JPAN: | |
| 140 case HANT: | |
| 141 case HANS: | |
| 142 return ""; | |
| 143 case ARAB: | |
| 144 return "، "; | |
| 145 case HANG: | |
| 146 case THAI: | |
| 147 return " "; | |
| 148 default: | |
| 149 return ", "; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 std::string GetCompactLineSeparator(const AddressData& address, | |
| 154 const Rule& rule) { | |
| 155 std::string language_code = GetAddressLanguageCode(address, rule); | |
| 156 return GetCompactLineSeparator( | |
| 157 !language_code.empty() ? GetLanguageScript(language_code) | |
| 158 : GetCountryScript(address.country_code)); | |
| 159 } | |
| 160 | |
| 161 void BuildLines(const std::vector<std::vector<FormatElement> >& format, | |
| 162 const AddressData& address, | |
| 163 std::vector<std::string>* lines) { | |
| 164 assert(lines != NULL); | |
| 165 lines->clear(); | |
| 166 | |
| 167 for (size_t i = 0; i < format.size(); ++i) { | |
| 168 if (format[i].empty()) { | |
| 169 continue; | |
| 170 } | |
| 171 | |
| 172 std::stringstream line; | |
| 173 for (size_t j = 0; j < format[i].size(); ++j) { | |
| 174 const FormatElement& element = format[i][j]; | |
| 175 switch (element.type) { | |
| 176 case FormatElement::LITERAL: | |
| 177 line << element.literal; | |
| 178 break; | |
| 179 | |
| 180 case FormatElement::FIELD: | |
| 181 if (element.field == STREET_ADDRESS) { | |
| 182 for (size_t k = 0; k < address.address_lines.size(); ++k) { | |
| 183 if (address.address_lines.empty()) { | |
| 184 continue; | |
| 185 } | |
| 186 // A street address is always surrounded by either newlines or | |
| 187 // spaces. | |
| 188 if (format[i].size() == 1) { | |
| 189 lines->push_back(address.address_lines[k]); | |
| 190 } else { | |
| 191 line << address.address_lines[k]; | |
| 192 if (k < address.address_lines.size() - 1) { | |
| 193 line << " "; | |
| 194 } | |
| 195 } | |
| 196 } | |
| 197 } else { | |
| 198 const std::string& field = address.GetField(element.field); | |
| 199 if (!field.empty()) { | |
| 200 line << field; | |
| 201 } | |
| 202 } | |
| 203 break; | |
| 204 | |
| 205 default: | |
| 206 assert(false); | |
| 207 break; | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 if (line.tellp() > 0) { | |
| 212 lines->push_back(line.str()); | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 } // namespace | |
| 218 | |
| 219 std::string AddressData::ToString(FormatType format_type) const { | |
| 220 Rule rule; | |
| 221 rule.CopyFrom(Rule::GetDefault()); | |
| 222 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); | |
| 223 | |
| 224 std::vector<std::string> lines; | |
| 225 BuildLines(rule.GetFormat(), *this, &lines); | |
| 226 | |
| 227 std::string line_separator = "\n"; | |
| 228 std::string mid_address_separator = "\n"; | |
| 229 if (format_type != FORMAT_FULL) { | |
| 230 line_separator = GetCompactLineSeparator(*this, rule); | |
| 231 if (format_type == FORMAT_ONE_LINE) { | |
| 232 mid_address_separator = line_separator; | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 std::stringstream ss; | |
|
Evan Stade
2014/01/13 22:04:39
nit: use std::string and concatenation
please use gerrit instead
2014/01/14 01:17:47
Done.
| |
| 237 for (size_t i = 0; i < lines.size(); ++i) { | |
| 238 ss << lines[i]; | |
| 239 if (i + 1 == lines.size() / 2) { | |
| 240 ss << mid_address_separator; | |
| 241 } else if (i != lines.size() - 1) { | |
| 242 ss << line_separator; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 return ss.str(); | |
| 247 } | |
| 248 | |
| 25 const std::string& AddressData::GetField(AddressField field) const { | 249 const std::string& AddressData::GetField(AddressField field) const { |
| 26 switch (field) { | 250 switch (field) { |
| 27 case COUNTRY: | 251 case COUNTRY: |
| 28 return country_code; | 252 return country_code; |
| 29 case ADMIN_AREA: | 253 case ADMIN_AREA: |
| 30 return administrative_area; | 254 return administrative_area; |
| 31 case LOCALITY: | 255 case LOCALITY: |
| 32 return locality; | 256 return locality; |
| 33 case DEPENDENT_LOCALITY: | 257 case DEPENDENT_LOCALITY: |
| 34 return dependent_locality; | 258 return dependent_locality; |
| 35 case SORTING_CODE: | 259 case SORTING_CODE: |
| 36 return sorting_code; | 260 return sorting_code; |
| 37 case POSTAL_CODE: | 261 case POSTAL_CODE: |
| 38 return postal_code; | 262 return postal_code; |
| 39 case ORGANIZATION: | 263 case ORGANIZATION: |
| 40 return organization; | 264 return organization; |
| 41 case RECIPIENT: | 265 case RECIPIENT: |
| 42 return recipient; | 266 return recipient; |
| 43 default: | 267 default: |
| 44 assert(false); | 268 assert(false); |
| 45 return recipient; | 269 return recipient; |
| 46 } | 270 } |
| 47 } | 271 } |
| 48 | 272 |
| 49 } // namespace addressinput | 273 } // namespace addressinput |
| 50 } // namespace i18n | 274 } // namespace i18n |
| OLD | NEW |