• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2014 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "lookup_key.h"
16 
17 #include <libaddressinput/address_data.h>
18 #include <libaddressinput/address_field.h>
19 #include <libaddressinput/util/basictypes.h>
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <cstddef>
24 #include <functional>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include "language.h"
31 #include "region_data_constants.h"
32 #include "rule.h"
33 #include "util/cctype_tolower_equal.h"
34 
35 namespace i18n {
36 namespace addressinput {
37 
38 namespace {
39 
40 const char kSlashDelim[] = "/";
41 const char kDashDelim[] = "--";
42 const char kData[] = "data";
43 const char kUnknown[] = "ZZ";
44 
45 // Assume the language_tag has had "Latn" script removed when this is called.
ShouldSetLanguageForKey(const std::string & language_tag,const std::string & region_code)46 bool ShouldSetLanguageForKey(const std::string& language_tag,
47                              const std::string& region_code) {
48   // We only need a language in the key if there is subregion data at all.
49   if (RegionDataConstants::GetMaxLookupKeyDepth(region_code) == 0) {
50     return false;
51   }
52   Rule rule;
53   rule.CopyFrom(Rule::GetDefault());
54   // TODO: Pre-parse the rules and have a map from region code to rule.
55   if (!rule.ParseSerializedRule(
56           RegionDataConstants::GetRegionData(region_code))) {
57     return false;
58   }
59   const std::vector<std::string>& languages = rule.GetLanguages();
60   // Do not add the default language (we want "data/US", not "data/US--en").
61   // (empty should not happen here because we have some sub-region data).
62   if (languages.empty() || languages[0] == language_tag) {
63     return false;
64   }
65   // Finally, only return true if the language is one of the remaining ones.
66   return std::find_if(languages.begin() + 1, languages.end(),
67                       std::bind2nd(EqualToTolowerString(), language_tag)) !=
68          languages.end();
69 }
70 
71 }  // namespace
72 
73 const AddressField LookupKey::kHierarchy[] = {
74   COUNTRY,
75   ADMIN_AREA,
76   LOCALITY,
77   DEPENDENT_LOCALITY
78 };
79 
LookupKey()80 LookupKey::LookupKey() {
81 }
82 
~LookupKey()83 LookupKey::~LookupKey() {
84 }
85 
FromAddress(const AddressData & address)86 void LookupKey::FromAddress(const AddressData& address) {
87   nodes_.clear();
88   if (address.region_code.empty()) {
89     nodes_.insert(std::make_pair(COUNTRY, kUnknown));
90   } else {
91     for (size_t i = 0; i < arraysize(kHierarchy); ++i) {
92       AddressField field = kHierarchy[i];
93       const std::string& value = address.GetFieldValue(field);
94       if (value.empty()) {
95         break;
96       }
97       nodes_.insert(std::make_pair(field, value));
98     }
99   }
100   Language address_language(address.language_code);
101   std::string language_tag_no_latn = address_language.has_latin_script
102                                          ? address_language.base
103                                          : address_language.tag;
104   if (ShouldSetLanguageForKey(language_tag_no_latn, address.region_code)) {
105     language_ = language_tag_no_latn;
106   }
107 }
108 
FromLookupKey(const LookupKey & parent,const std::string & child_node)109 void LookupKey::FromLookupKey(const LookupKey& parent,
110                               const std::string& child_node) {
111   assert(parent.nodes_.size() < arraysize(kHierarchy));
112   assert(!child_node.empty());
113 
114   // Copy its nodes if this isn't the parent object.
115   if (this != &parent) nodes_ = parent.nodes_;
116   AddressField child_field = kHierarchy[nodes_.size()];
117   nodes_.insert(std::make_pair(child_field, child_node));
118 }
119 
ToKeyString(size_t max_depth) const120 std::string LookupKey::ToKeyString(size_t max_depth) const {
121   assert(max_depth < arraysize(kHierarchy));
122   std::string key_string(kData);
123 
124   for (size_t i = 0; i <= max_depth; ++i) {
125     AddressField field = kHierarchy[i];
126     std::map<AddressField, std::string>::const_iterator it = nodes_.find(field);
127     if (it == nodes_.end()) {
128       break;
129     }
130     key_string.append(kSlashDelim);
131     key_string.append(it->second);
132   }
133   if (!language_.empty()) {
134     key_string.append(kDashDelim);
135     key_string.append(language_);
136   }
137   return key_string;
138 }
139 
GetRegionCode() const140 const std::string& LookupKey::GetRegionCode() const {
141   std::map<AddressField, std::string>::const_iterator it = nodes_.find(COUNTRY);
142   assert(it != nodes_.end());
143   return it->second;
144 }
145 
GetDepth() const146 size_t LookupKey::GetDepth() const {
147   size_t depth = nodes_.size() - 1;
148   assert(depth < arraysize(kHierarchy));
149   return depth;
150 }
151 
152 }  // namespace addressinput
153 }  // namespace i18n
154