aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/address_data.cc
blob: 40d3ee76535bb97bae7c5eb0b94b27e5db4f2eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2014 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libaddressinput/address_data.h>

#include <libaddressinput/address_field.h>
#include <libaddressinput/util/basictypes.h>

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <functional>
#include <ostream>
#include <string>
#include <vector>

#include <re2/re2.h>

namespace i18n {
namespace addressinput {

namespace {

// Mapping from AddressField value to pointer to AddressData member.
std::string AddressData::*kStringField[] = {
  &AddressData::region_code,
  &AddressData::administrative_area,
  &AddressData::locality,
  &AddressData::dependent_locality,
  &AddressData::sorting_code,
  &AddressData::postal_code,
  NULL,
  &AddressData::organization,
  &AddressData::recipient
};

// Mapping from AddressField value to pointer to AddressData member.
const std::vector<std::string> AddressData::*kVectorStringField[] = {
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  &AddressData::address_line,
  NULL,
  NULL
};

COMPILE_ASSERT(arraysize(kStringField) == arraysize(kVectorStringField),
               field_mapping_array_size_mismatch);

// A string is considered to be "empty" not only if it actually is empty, but
// also if it contains nothing but whitespace.
bool IsStringEmpty(const std::string& str) {
  static const RE2 kMatcher("\\S");
  return str.empty() || !RE2::PartialMatch(str, kMatcher);
}

}  // namespace

bool AddressData::IsFieldEmpty(AddressField field) const {
  assert(field >= 0);
  assert(static_cast<size_t>(field) < arraysize(kStringField));
  if (kStringField[field] != NULL) {
    const std::string& value = GetFieldValue(field);
    return IsStringEmpty(value);
  } else {
    const std::vector<std::string>& value = GetRepeatedFieldValue(field);
    return std::find_if(value.begin(),
                        value.end(),
                        std::not1(std::ptr_fun(&IsStringEmpty))) == value.end();
  }
}

const std::string& AddressData::GetFieldValue(
    AddressField field) const {
  assert(field >= 0);
  assert(static_cast<size_t>(field) < arraysize(kStringField));
  assert(kStringField[field] != NULL);
  return this->*kStringField[field];
}

void AddressData::SetFieldValue(AddressField field, const std::string& value) {
  assert(field >= 0);
  assert(static_cast<size_t>(field) < arraysize(kStringField));
  assert(kStringField[field] != NULL);
  (this->*kStringField[field]).assign(value);
}

const std::vector<std::string>& AddressData::GetRepeatedFieldValue(
    AddressField field) const {
  assert(IsRepeatedFieldValue(field));
  return this->*kVectorStringField[field];
}

bool AddressData::operator==(const AddressData& other) const {
  return region_code == other.region_code &&
         address_line == other.address_line &&
         administrative_area == other.administrative_area &&
         locality == other.locality &&
         dependent_locality == other.dependent_locality &&
         postal_code == other.postal_code &&
         sorting_code == other.sorting_code &&
         language_code == other.language_code &&
         organization == other.organization &&
         recipient == other.recipient;
}

// static
bool AddressData::IsRepeatedFieldValue(AddressField field) {
  assert(field >= 0);
  assert(static_cast<size_t>(field) < arraysize(kVectorStringField));
  return kVectorStringField[field] != NULL;
}

}  // namespace addressinput
}  // namespace i18n

std::ostream& operator<<(std::ostream& o,
                         const i18n::addressinput::AddressData& address) {
  o << "region_code: \"" << address.region_code << "\"\n"
    "administrative_area: \"" << address.administrative_area << "\"\n"
    "locality: \"" << address.locality << "\"\n"
    "dependent_locality: \"" << address.dependent_locality << "\"\n"
    "postal_code: \"" << address.postal_code << "\"\n"
    "sorting_code: \"" << address.sorting_code << "\"\n";

  // TODO: Update the field order in the .h file to match the order they are
  // printed out here, for consistency.
  for (std::vector<std::string>::const_iterator it =
           address.address_line.begin();
       it != address.address_line.end(); ++it) {
    o << "address_line: \"" << *it << "\"\n";
  }

  o << "language_code: \"" << address.language_code << "\"\n"
    "organization: \"" << address.organization << "\"\n"
    "recipient: \"" << address.recipient << "\"\n";

  return o;
}