Chromium Code Reviews| Index: third_party/libaddressinput/chromium/cpp/src/lookup_key.cc |
| diff --git a/third_party/libaddressinput/chromium/cpp/src/lookup_key.cc b/third_party/libaddressinput/chromium/cpp/src/lookup_key.cc |
| index 278d8940e9e8c2a1eb2aecf13bf328fdbd674716..f595cb19fe9c84dd4dd644b3d3260aa167b51711 100644 |
| --- a/third_party/libaddressinput/chromium/cpp/src/lookup_key.cc |
| +++ b/third_party/libaddressinput/chromium/cpp/src/lookup_key.cc |
| @@ -14,12 +14,14 @@ |
| #include "lookup_key.h" |
| +#include <libaddressinput/address_data.h> |
| #include <libaddressinput/address_field.h> |
| #include <libaddressinput/util/basictypes.h> |
| #include <libaddressinput/util/scoped_ptr.h> |
| #include <cassert> |
| #include <cstddef> |
| +#include <map> |
| #include <string> |
| #include <utility> |
| @@ -29,6 +31,28 @@ |
| namespace i18n { |
| namespace addressinput { |
| +namespace { |
| + |
| +// Returns the value of the |field| in the |address|. The |field| can be only |
| +// ADMIN_AREA, LOCALITY, and DEPENDENT_LOCALITY. |
| +const std::string& GetAddressFieldValue(const AddressData& address, |
|
Evan Stade
2013/12/19 02:12:38
this seems like it should be an AddressData method
please use gerrit instead
2014/01/08 00:55:52
Done.
|
| + AddressField field) { |
| + switch (field) { |
| + case ADMIN_AREA: |
| + return address.administrative_area; |
| + case LOCALITY: |
| + return address.locality; |
| + case DEPENDENT_LOCALITY: |
| + return address.dependent_locality; |
| + default: |
| + assert(false); |
| + static const std::string kEmptyString; |
| + return kEmptyString; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| LookupKey::LookupKey(const std::string& country_code) |
| : value_("data/" + country_code), |
| level_(COUNTRY), |
| @@ -77,6 +101,43 @@ LookupKey* LookupKey::AddLanguageCodeKey(const std::string& language_code) { |
| return language_code_key; |
| } |
| +const LookupKey* LookupKey::GetSubKey(const std::string& sub_region) const { |
| + LookupKeys::const_iterator it = sub_keys_.find(sub_region); |
| + return it == sub_keys_.end() ? NULL : it->second; |
| +} |
| + |
| +const LookupKey* LookupKey::GetLanguageCodeKey( |
| + const std::string& language_code) const { |
| + LookupKeys::const_iterator it = language_code_keys_.find(language_code); |
| + return it == language_code_keys_.end() ? NULL : it->second; |
| +} |
| + |
| +std::map<AddressField, const Rule*> LookupKey::BuildFieldRuleMap( |
|
Evan Stade
2013/12/19 02:12:38
this actually feels like it should be a method on
please use gerrit instead
2014/01/08 00:55:52
AddressData is public API, but Rule is not. Moving
|
| + const AddressData& address) const { |
| + static const int kNumberOfLevels = 4; |
|
Evan Stade
2013/12/19 02:12:38
use arraysize()
please use gerrit instead
2014/01/08 00:55:52
No longer applicable.
|
| + static const AddressField kLevels[kNumberOfLevels] = { |
| + COUNTRY, |
| + ADMIN_AREA, |
| + LOCALITY, |
| + DEPENDENT_LOCALITY |
| + }; |
| + std::map<AddressField, const Rule*> result; |
| + const LookupKey* key = this; |
|
Evan Stade
2013/12/19 02:12:38
does this assume |this| is a country level LookupK
please use gerrit instead
2014/01/08 00:55:52
No longer applicable.
|
| + for (int i = 0; i < kNumberOfLevels && key != NULL; ++i) { |
| + const LookupKey* language_code_key = |
| + key->GetLanguageCodeKey(address.language_code); |
| + result.insert(std::make_pair( |
| + kLevels[i], |
| + language_code_key == NULL |
| + ? key->GetRule() |
| + : language_code_key->GetRule())); |
| + key = i == kNumberOfLevels - 1 |
| + ? NULL |
| + : key->GetSubKey(GetAddressFieldValue(address, kLevels[i + 1])); |
| + } |
| + return result; |
| +} |
| + |
| LookupKey::LookupKey(const std::string& key_value, AddressField level) |
| : value_(key_value), |
| level_(level), |