1 // Copyright (C) 2014 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <libaddressinput/address_data.h>
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/util/basictypes.h>
31 namespace addressinput {
35 // Mapping from AddressField value to pointer to AddressData member.
36 std::string AddressData::*kStringField[] = {
37 &AddressData::region_code,
38 &AddressData::administrative_area,
39 &AddressData::locality,
40 &AddressData::dependent_locality,
41 &AddressData::sorting_code,
42 &AddressData::postal_code,
44 &AddressData::recipient
47 // Mapping from AddressField value to pointer to AddressData member.
48 const std::vector<std::string> AddressData::*kVectorStringField[] = {
55 &AddressData::address_line,
59 COMPILE_ASSERT(arraysize(kStringField) == arraysize(kVectorStringField),
60 field_mapping_array_size_mismatch);
62 // A string is considered to be "empty" not only if it actually is empty, but
63 // also if it contains nothing but whitespace.
64 bool IsStringEmpty(const std::string& str) {
65 static const RE2 kMatcher("\\S");
66 return str.empty() || !RE2::PartialMatch(str, kMatcher);
71 bool AddressData::IsFieldEmpty(AddressField field) const {
73 assert(static_cast<size_t>(field) < arraysize(kStringField));
74 if (kStringField[field] != NULL) {
75 const std::string& value = GetFieldValue(field);
76 return IsStringEmpty(value);
78 const std::vector<std::string>& value = GetRepeatedFieldValue(field);
79 return std::find_if(value.begin(),
81 std::not1(std::ptr_fun(&IsStringEmpty))) == value.end();
85 const std::string& AddressData::GetFieldValue(
86 AddressField field) const {
88 assert(static_cast<size_t>(field) < arraysize(kStringField));
89 assert(kStringField[field] != NULL);
90 return this->*kStringField[field];
93 void AddressData::SetFieldValue(AddressField field, const std::string& value) {
95 assert(static_cast<size_t>(field) < arraysize(kStringField));
96 assert(kStringField[field] != NULL);
97 (this->*kStringField[field]).assign(value);
100 const std::vector<std::string>& AddressData::GetRepeatedFieldValue(
101 AddressField field) const {
102 assert(IsRepeatedFieldValue(field));
103 return this->*kVectorStringField[field];
106 bool AddressData::operator==(const AddressData& other) const {
107 return region_code == other.region_code &&
108 address_line == other.address_line &&
109 administrative_area == other.administrative_area &&
110 locality == other.locality &&
111 dependent_locality == other.dependent_locality &&
112 postal_code == other.postal_code &&
113 sorting_code == other.sorting_code &&
114 language_code == other.language_code && recipient == other.recipient;
118 bool AddressData::IsRepeatedFieldValue(AddressField field) {
120 assert(static_cast<size_t>(field) < arraysize(kVectorStringField));
121 return kVectorStringField[field] != NULL;
124 } // namespace addressinput
127 std::ostream& operator<<(std::ostream& o,
128 const i18n::addressinput::AddressData& address) {
129 o << "region_code: \"" << address.region_code << "\"\n"
130 "administrative_area: \"" << address.administrative_area << "\"\n"
131 "locality: \"" << address.locality << "\"\n"
132 "dependent_locality: \"" << address.dependent_locality << "\"\n"
133 "postal_code: \"" << address.postal_code << "\"\n"
134 "sorting_code: \"" << address.sorting_code << "\"\n";
136 // TODO: Update the field order in the .h file to match the order they are
137 // printed out here, for consistency.
138 for (std::vector<std::string>::const_iterator it =
139 address.address_line.begin();
140 it != address.address_line.end(); ++it) {
141 o << "address_line: \"" << *it << "\"\n";
144 o << "language_code: \"" << address.language_code << "\"\n"
145 "recipient: \"" << address.recipient << "\"\n";