OLD | NEW |
---|---|
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 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_ui.h> | 15 #include <libaddressinput/address_ui.h> |
16 | 16 |
17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
18 #include <libaddressinput/address_ui_component.h> | 18 #include <libaddressinput/address_ui_component.h> |
19 #include <libaddressinput/localization.h> | 19 #include <libaddressinput/localization.h> |
20 | 20 |
21 #include <string> | 21 #include <string> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "address_field_util.h" | |
25 #include "grit.h" | 24 #include "grit.h" |
26 #include "messages.h" | 25 #include "messages.h" |
27 #include "region_data_constants.h" | 26 #include "region_data_constants.h" |
28 #include "rule.h" | 27 #include "rule.h" |
29 | 28 |
30 namespace i18n { | 29 namespace i18n { |
31 namespace addressinput { | 30 namespace addressinput { |
32 | 31 |
33 namespace { | 32 namespace { |
34 | 33 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; | 69 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL; |
71 case ORGANIZATION: | 70 case ORGANIZATION: |
72 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; | 71 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL; |
73 case RECIPIENT: | 72 case RECIPIENT: |
74 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; | 73 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL; |
75 default: | 74 default: |
76 return INVALID_MESSAGE_ID; | 75 return INVALID_MESSAGE_ID; |
77 } | 76 } |
78 } | 77 } |
79 | 78 |
80 bool IsNewline(AddressField field) { | |
81 // NEWLINE is an extension for AddressField enum that's used only internally. | |
82 return field == static_cast<AddressField>(NEWLINE); | |
83 } | |
84 | |
85 } // namespace | 79 } // namespace |
86 | 80 |
87 const std::vector<std::string>& GetRegionCodes() { | 81 const std::vector<std::string>& GetRegionCodes() { |
88 return RegionDataConstants::GetRegionCodes(); | 82 return RegionDataConstants::GetRegionCodes(); |
89 } | 83 } |
90 | 84 |
91 std::vector<AddressUiComponent> BuildComponents( | 85 std::vector<AddressUiComponent> BuildComponents( |
92 const std::string& region_code, | 86 const std::string& region_code, |
93 const Localization& localization) { | 87 const Localization& localization) { |
94 std::vector<AddressUiComponent> result; | 88 std::vector<AddressUiComponent> result; |
95 | 89 |
96 Rule rule; | 90 Rule rule; |
97 rule.CopyFrom(GetDefaultRule()); | 91 rule.CopyFrom(GetDefaultRule()); |
98 if (!rule.ParseSerializedRule( | 92 if (!rule.ParseSerializedRule( |
99 RegionDataConstants::GetRegionData(region_code))) { | 93 RegionDataConstants::GetRegionData(region_code))) { |
100 return result; | 94 return result; |
101 } | 95 } |
102 | 96 |
103 bool previous_field_is_newline = true; | 97 for (std::vector<std::vector<AddressField> >::const_iterator |
104 bool next_field_is_newline = true; | 98 line_it = rule.GetFormat().begin(); |
105 for (std::vector<AddressField>::const_iterator field_it = | 99 line_it != rule.GetFormat().end(); |
106 rule.GetFormat().begin(); | 100 ++line_it) { |
107 field_it != rule.GetFormat().end(); ++field_it) { | 101 for (std::vector<AddressField>::const_iterator field_it = line_it->begin(); |
108 if (IsNewline(*field_it)) { | 102 field_it != line_it->end(); ++field_it) { |
109 previous_field_is_newline = true; | 103 AddressUiComponent component; |
110 continue; | 104 component.length_hint = |
105 line_it->size() == 1 ? AddressUiComponent::HINT_LONG | |
please use gerrit instead
2013/12/20 22:03:33
I like that this logic is now quite clear :-)
Evan Stade
2013/12/21 00:27:13
what happened to SHORT_EOL or whatever we were goi
please use gerrit instead
2014/01/02 19:49:39
I've added a test that verifies that no consecutiv
| |
106 : AddressUiComponent::HINT_SHORT; | |
107 component.field = *field_it; | |
108 component.name = localization.GetString( | |
109 GetMessageIdForField(*field_it, rule.GetAdminAreaNameMessageId(), | |
110 rule.GetPostalCodeNameMessageId())); | |
111 result.push_back(component); | |
111 } | 112 } |
112 AddressUiComponent component; | |
113 std::vector<AddressField>::const_iterator next_field_it = field_it + 1; | |
114 next_field_is_newline = | |
115 next_field_it == rule.GetFormat().end() || IsNewline(*next_field_it); | |
116 component.length_hint = previous_field_is_newline && next_field_is_newline | |
117 ? AddressUiComponent::HINT_LONG | |
118 : AddressUiComponent::HINT_SHORT; | |
119 previous_field_is_newline = false; | |
120 component.field = *field_it; | |
121 component.name = localization.GetString( | |
122 GetMessageIdForField(*field_it, rule.GetAdminAreaNameMessageId(), | |
123 rule.GetPostalCodeNameMessageId())); | |
124 result.push_back(component); | |
125 } | 113 } |
126 | 114 |
127 return result; | 115 return result; |
128 } | 116 } |
129 | 117 |
130 } // namespace addressinput | 118 } // namespace addressinput |
131 } // namespace i18n | 119 } // namespace i18n |
OLD | NEW |