OLD | NEW |
| (Empty) |
1 // Copyright (C) 2013 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 "address_field_util.h" | |
16 | |
17 #include <libaddressinput/address_field.h> | |
18 | |
19 #include <cassert> | |
20 #include <cstddef> | |
21 #include <map> | |
22 #include <string> | |
23 #include <utility> | |
24 #include <vector> | |
25 | |
26 namespace i18n { | |
27 namespace addressinput { | |
28 | |
29 namespace { | |
30 | |
31 std::map<char, AddressField> InitFields() { | |
32 std::map<char, AddressField> fields; | |
33 fields.insert(std::make_pair('R', COUNTRY)); | |
34 fields.insert(std::make_pair('S', ADMIN_AREA)); | |
35 fields.insert(std::make_pair('C', LOCALITY)); | |
36 fields.insert(std::make_pair('D', DEPENDENT_LOCALITY)); | |
37 fields.insert(std::make_pair('x', SORTING_CODE)); | |
38 fields.insert(std::make_pair('Z', POSTAL_CODE)); | |
39 fields.insert(std::make_pair('A', STREET_ADDRESS)); | |
40 fields.insert(std::make_pair('O', ORGANIZATION)); | |
41 fields.insert(std::make_pair('N', RECIPIENT)); | |
42 // An extension of AddressField enum used only internally: | |
43 fields.insert(std::make_pair( | |
44 'n', static_cast<AddressField>(NEWLINE))); | |
45 return fields; | |
46 } | |
47 | |
48 const std::map<char, AddressField>& GetFields() { | |
49 static const std::map<char, AddressField> kFields(InitFields()); | |
50 return kFields; | |
51 } | |
52 | |
53 bool IsTokenPrefix(char c) { | |
54 return c == '%'; | |
55 } | |
56 | |
57 bool IsToken(char c) { | |
58 return GetFields().find(c) != GetFields().end(); | |
59 } | |
60 | |
61 AddressField ParseToken(char c) { | |
62 std::map<char, AddressField>::const_iterator it = GetFields().find(c); | |
63 assert(it != GetFields().end()); | |
64 return it->second; | |
65 } | |
66 | |
67 } // namespace | |
68 | |
69 void ParseAddressFieldsFormat(const std::string& format, | |
70 std::vector<AddressField>* fields) { | |
71 assert(fields != NULL); | |
72 fields->clear(); | |
73 for (std::string::const_iterator current = format.begin(), | |
74 next = format.begin() + 1; | |
75 current != format.end() && next != format.end(); | |
76 ++current, ++next) { | |
77 if (IsTokenPrefix(*current) && IsToken(*next)) { | |
78 fields->push_back(ParseToken(*next)); | |
79 } | |
80 } | |
81 } | |
82 | |
83 } // namespace addressinput | |
84 } // namespace i18n | |
OLD | NEW |