Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/address_data.cc

Issue 368243007: Reland of "Use address_data.h from upstream libaddressinput". (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update BUILD.gn, fix a test. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (C) 2013 Google Inc. 1 // Copyright (C) 2014 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 <libaddressinput/address_data.h> 15 #include <libaddressinput/address_data.h>
16 16
17 #include <libaddressinput/address_field.h> 17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/util/basictypes.h>
18 19
19 #include <algorithm> 20 #include <algorithm>
20 #include <cassert> 21 #include <cassert>
21 #include <cstddef> 22 #include <cstddef>
23 #include <functional>
24 #include <ostream>
22 #include <string> 25 #include <string>
23 #include <vector> 26 #include <vector>
24 27
25 #include "region_data_constants.h" 28 #include <re2/re2.h>
26 #include "rule.h"
27 #include "util/string_util.h"
28 29
29 namespace i18n { 30 namespace i18n {
30 namespace addressinput { 31 namespace addressinput {
31 32
32 namespace { 33 namespace {
33 34
34 const std::string* GetMemberForField(const AddressData& address, 35 // Mapping from AddressField value to pointer to AddressData member.
35 AddressField field) { 36 std::string AddressData::*kStringField[] = {
36 switch (field) { 37 &AddressData::region_code,
37 case COUNTRY: 38 &AddressData::administrative_area,
38 return &address.country_code; 39 &AddressData::locality,
39 case ADMIN_AREA: 40 &AddressData::dependent_locality,
40 return &address.administrative_area; 41 &AddressData::sorting_code,
41 case LOCALITY: 42 &AddressData::postal_code,
42 return &address.locality; 43 NULL,
43 case DEPENDENT_LOCALITY: 44 &AddressData::recipient
44 return &address.dependent_locality; 45 };
45 case SORTING_CODE:
46 return &address.sorting_code;
47 case POSTAL_CODE:
48 return &address.postal_code;
49 case RECIPIENT:
50 return &address.recipient;
51 case STREET_ADDRESS:
52 break;
53 }
54 46
55 assert(false); 47 // Mapping from AddressField value to pointer to AddressData member.
56 return NULL; 48 const std::vector<std::string> AddressData::*kVectorStringField[] = {
49 NULL,
50 NULL,
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 &AddressData::address_line,
56 NULL
57 };
58
59 COMPILE_ASSERT(arraysize(kStringField) == arraysize(kVectorStringField),
60 field_mapping_array_size_mismatch);
61
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);
57 } 67 }
58 68
59 } // namespace 69 } // namespace
60 70
61 void AddressData::FormatForDisplay(std::vector<std::string>* lines) const { 71 bool AddressData::IsFieldEmpty(AddressField field) const {
62 assert(lines != NULL); 72 assert(field >= 0);
63 lines->clear(); 73 assert(static_cast<size_t>(field) < arraysize(kStringField));
64 74 if (kStringField[field] != NULL) {
65 Rule rule; 75 const std::string& value = GetFieldValue(field);
66 rule.CopyFrom(Rule::GetDefault()); 76 return IsStringEmpty(value);
67 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); 77 } else {
68 78 const std::vector<std::string>& value = GetRepeatedFieldValue(field);
69 // If latinized rules are available and the |language_code| of this address is 79 return std::find_if(value.begin(), value.end(),
70 // not the primary language code for the region, then use the latinized 80 std::not1(std::ptr_fun(&IsStringEmpty))) ==
71 // formatting rules. 81 value.end();
72 const std::vector<std::vector<FormatElement> >& format =
73 rule.GetLatinFormat().empty() ||
74 language_code.empty() ||
75 NormalizeLanguageCode(language_code) ==
76 NormalizeLanguageCode(rule.GetLanguage())
77 ? rule.GetFormat() : rule.GetLatinFormat();
78
79 for (size_t i = 0; i < format.size(); ++i) {
80 std::string line;
81 for (size_t j = 0; j < format[i].size(); ++j) {
82 const FormatElement& element = format[i][j];
83 if (element.IsField()) {
84 if (element.field == STREET_ADDRESS) {
85 // Street address field can contain multiple values.
86 for (size_t k = 0; k < address_lines.size(); ++k) {
87 line += address_lines[k];
88 if (k < address_lines.size() - 1) {
89 lines->push_back(line);
90 line.clear();
91 }
92 }
93 } else {
94 line += GetFieldValue(element.field);
95 }
96 } else {
97 line += element.literal;
98 }
99 }
100
101 if (!line.empty()) {
102 lines->push_back(line);
103 }
104 } 82 }
105 } 83 }
106 84
107 const std::string& AddressData::GetFieldValue(AddressField field) const { 85 const std::string& AddressData::GetFieldValue(
108 const std::string* field_value = GetMemberForField(*this, field); 86 AddressField field) const {
109 return field_value != NULL ? *field_value : country_code; 87 assert(field >= 0);
88 assert(static_cast<size_t>(field) < arraysize(kStringField));
89 assert(kStringField[field] != NULL);
90 return this->*kStringField[field];
110 } 91 }
111 92
112 void AddressData::SetFieldValue(AddressField field, const std::string& value) { 93 void AddressData::SetFieldValue(AddressField field, const std::string& value) {
113 std::string* field_value = 94 assert(field >= 0);
114 const_cast<std::string*>(GetMemberForField(*this, field)); 95 assert(static_cast<size_t>(field) < arraysize(kStringField));
115 if (field_value != NULL) { 96 assert(kStringField[field] != NULL);
116 *field_value = value; 97 (this->*kStringField[field]).assign(value);
117 }
118 } 98 }
119 99
120 bool AddressData::HasAllRequiredFields() const { 100 const std::vector<std::string>& AddressData::GetRepeatedFieldValue(
121 if (country_code.empty()) 101 AddressField field) const {
122 return false; 102 assert(IsRepeatedFieldValue(field));
103 return this->*kVectorStringField[field];
104 }
123 105
124 Rule rule; 106 bool AddressData::operator==(const AddressData& other) const {
125 rule.CopyFrom(Rule::GetDefault()); 107 return
126 if (!rule.ParseSerializedRule( 108 region_code == other.region_code &&
127 RegionDataConstants::GetRegionData(country_code))) { 109 address_line == other.address_line &&
128 return false; 110 administrative_area == other.administrative_area &&
129 } 111 locality == other.locality &&
112 dependent_locality == other.dependent_locality &&
113 postal_code == other.postal_code &&
114 sorting_code == other.sorting_code &&
115 language_code == other.language_code &&
116 recipient == other.recipient;
117 }
130 118
131 std::vector< ::i18n::addressinput::AddressField> required_fields = 119 // static
132 rule.GetRequired(); 120 bool AddressData::IsRepeatedFieldValue(AddressField field) {
133 for (size_t i = 0; i < required_fields.size(); ++i) { 121 assert(field >= 0);
134 if (required_fields[i] == STREET_ADDRESS) { 122 assert(static_cast<size_t>(field) < arraysize(kVectorStringField));
135 if (address_lines.empty() || address_lines[0].empty()) { 123 return kVectorStringField[field] != NULL;
136 return false;
137 }
138 } else if (GetFieldValue(required_fields[i]).empty()) {
139 return false;
140 }
141 }
142
143 return true;
144 } 124 }
145 125
146 } // namespace addressinput 126 } // namespace addressinput
147 } // namespace i18n 127 } // namespace i18n
128
129 std::ostream& operator<<(std::ostream& o,
130 const i18n::addressinput::AddressData& address) {
131 o << "region_code: \"" << address.region_code << "\"\n"
132 "administrative_area: \"" << address.administrative_area << "\"\n"
133 "locality: \"" << address.locality << "\"\n"
134 "dependent_locality: \"" << address.dependent_locality << "\"\n"
135 "postal_code: \"" << address.postal_code << "\"\n"
136 "sorting_code: \"" << address.sorting_code << "\"\n";
137
138 // TODO: Update the field order in the .h file to match the order they are
139 // printed out here, for consistency.
140 for (std::vector<std::string>::const_iterator it =
141 address.address_line.begin();
142 it != address.address_line.end(); ++it) {
143 o << "address_line: \"" << *it << "\"\n";
144 }
145
146 o << "language_code: \"" << address.language_code << "\"\n"
147 "recipient: \"" << address.recipient << "\"\n";
148
149 return o;
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698