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 "lookup_key.h" | 15 #include "lookup_key.h" |
| 16 | 16 |
| 17 #include <libaddressinput/address_data.h> | |
| 17 #include <libaddressinput/address_field.h> | 18 #include <libaddressinput/address_field.h> |
| 18 #include <libaddressinput/util/basictypes.h> | 19 #include <libaddressinput/util/basictypes.h> |
| 19 #include <libaddressinput/util/scoped_ptr.h> | 20 #include <libaddressinput/util/scoped_ptr.h> |
| 20 | 21 |
| 21 #include <cassert> | 22 #include <cassert> |
| 22 #include <cstddef> | 23 #include <cstddef> |
| 24 #include <map> | |
| 23 #include <string> | 25 #include <string> |
| 24 #include <utility> | 26 #include <utility> |
| 25 | 27 |
| 26 #include "rule.h" | 28 #include "rule.h" |
| 27 #include "util/stl_util.h" | 29 #include "util/stl_util.h" |
| 28 | 30 |
| 29 namespace i18n { | 31 namespace i18n { |
| 30 namespace addressinput { | 32 namespace addressinput { |
| 31 | 33 |
| 34 namespace { | |
| 35 | |
| 36 // Returns the value of the |field| in the |address|. The |field| can be only | |
| 37 // ADMIN_AREA, LOCALITY, and DEPENDENT_LOCALITY. | |
| 38 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.
| |
| 39 AddressField field) { | |
| 40 switch (field) { | |
| 41 case ADMIN_AREA: | |
| 42 return address.administrative_area; | |
| 43 case LOCALITY: | |
| 44 return address.locality; | |
| 45 case DEPENDENT_LOCALITY: | |
| 46 return address.dependent_locality; | |
| 47 default: | |
| 48 assert(false); | |
| 49 static const std::string kEmptyString; | |
| 50 return kEmptyString; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 32 LookupKey::LookupKey(const std::string& country_code) | 56 LookupKey::LookupKey(const std::string& country_code) |
| 33 : value_("data/" + country_code), | 57 : value_("data/" + country_code), |
| 34 level_(COUNTRY), | 58 level_(COUNTRY), |
| 35 rule_(), | 59 rule_(), |
| 36 sub_keys_(), | 60 sub_keys_(), |
| 37 language_code_keys_() {} | 61 language_code_keys_() {} |
| 38 | 62 |
| 39 LookupKey::~LookupKey() { | 63 LookupKey::~LookupKey() { |
| 40 STLDeleteContainerPairSecondPointers(sub_keys_.begin(), sub_keys_.end()); | 64 STLDeleteContainerPairSecondPointers(sub_keys_.begin(), sub_keys_.end()); |
| 41 STLDeleteContainerPairSecondPointers( | 65 STLDeleteContainerPairSecondPointers( |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 70 } | 94 } |
| 71 | 95 |
| 72 LookupKey* LookupKey::AddLanguageCodeKey(const std::string& language_code) { | 96 LookupKey* LookupKey::AddLanguageCodeKey(const std::string& language_code) { |
| 73 assert(language_code_keys_.find(language_code) == language_code_keys_.end()); | 97 assert(language_code_keys_.find(language_code) == language_code_keys_.end()); |
| 74 LookupKey* language_code_key = | 98 LookupKey* language_code_key = |
| 75 new LookupKey(value_ + "--" + language_code, level_); | 99 new LookupKey(value_ + "--" + language_code, level_); |
| 76 language_code_keys_.insert(std::make_pair(language_code, language_code_key)); | 100 language_code_keys_.insert(std::make_pair(language_code, language_code_key)); |
| 77 return language_code_key; | 101 return language_code_key; |
| 78 } | 102 } |
| 79 | 103 |
| 104 const LookupKey* LookupKey::GetSubKey(const std::string& sub_region) const { | |
| 105 LookupKeys::const_iterator it = sub_keys_.find(sub_region); | |
| 106 return it == sub_keys_.end() ? NULL : it->second; | |
| 107 } | |
| 108 | |
| 109 const LookupKey* LookupKey::GetLanguageCodeKey( | |
| 110 const std::string& language_code) const { | |
| 111 LookupKeys::const_iterator it = language_code_keys_.find(language_code); | |
| 112 return it == language_code_keys_.end() ? NULL : it->second; | |
| 113 } | |
| 114 | |
| 115 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
| |
| 116 const AddressData& address) const { | |
| 117 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.
| |
| 118 static const AddressField kLevels[kNumberOfLevels] = { | |
| 119 COUNTRY, | |
| 120 ADMIN_AREA, | |
| 121 LOCALITY, | |
| 122 DEPENDENT_LOCALITY | |
| 123 }; | |
| 124 std::map<AddressField, const Rule*> result; | |
| 125 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.
| |
| 126 for (int i = 0; i < kNumberOfLevels && key != NULL; ++i) { | |
| 127 const LookupKey* language_code_key = | |
| 128 key->GetLanguageCodeKey(address.language_code); | |
| 129 result.insert(std::make_pair( | |
| 130 kLevels[i], | |
| 131 language_code_key == NULL | |
| 132 ? key->GetRule() | |
| 133 : language_code_key->GetRule())); | |
| 134 key = i == kNumberOfLevels - 1 | |
| 135 ? NULL | |
| 136 : key->GetSubKey(GetAddressFieldValue(address, kLevels[i + 1])); | |
| 137 } | |
| 138 return result; | |
| 139 } | |
| 140 | |
| 80 LookupKey::LookupKey(const std::string& key_value, AddressField level) | 141 LookupKey::LookupKey(const std::string& key_value, AddressField level) |
| 81 : value_(key_value), | 142 : value_(key_value), |
| 82 level_(level), | 143 level_(level), |
| 83 rule_(), | 144 rule_(), |
| 84 sub_keys_(), | 145 sub_keys_(), |
| 85 language_code_keys_() {} | 146 language_code_keys_() {} |
| 86 | 147 |
| 87 } // namespace addressinput | 148 } // namespace addressinput |
| 88 } // namespace i18n | 149 } // namespace i18n |
| OLD | NEW |