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

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

Issue 110533002: [rac] Temporarily copy libaddressinput to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Avoid redefinition of basictypes.h Created 7 years 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
(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 <libaddressinput/address_ui.h>
16
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/address_ui_component.h>
19 #include <libaddressinput/localization.h>
20
21 #include <string>
22 #include <vector>
23
24 #include "address_field_util.h"
25 #include "grit.h"
26 #include "messages.h"
27 #include "region_data_constants.h"
28 #include "rule.h"
29
30 namespace i18n {
31 namespace addressinput {
32
33 namespace {
34
35 // Parses the default region data into the static Rule object and returns a
36 // constant reference to this object. Cannot return a copy of the object,
37 // because Rule objects are not copyable.
38 const Rule& InitDefaultRule() {
39 static Rule rule;
40 rule.ParseSerializedRule(RegionDataConstants::GetDefaultRegionData());
41 return rule;
42 }
43
44 // Returns the constant reference to the Rule object from InitDefaultRule(). The
45 // static object is in InitDefaultRule(), but this function maintains a constant
46 // static reference to it. The constant static reference avoids re-parsing the
47 // default region data.
48 const Rule& GetDefaultRule() {
49 static const Rule& kDefaultRule(InitDefaultRule());
50 return kDefaultRule;
51 }
52
53 int GetMessageIdForField(AddressField field,
54 int admin_area_name_message_id,
55 int postal_code_name_message_id) {
56 switch (field) {
57 case COUNTRY:
58 return IDS_LIBADDRESSINPUT_I18N_COUNTRY_LABEL;
59 case ADMIN_AREA:
60 return admin_area_name_message_id;
61 case LOCALITY:
62 return IDS_LIBADDRESSINPUT_I18N_LOCALITY_LABEL;
63 case DEPENDENT_LOCALITY:
64 return IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL;
65 case SORTING_CODE:
66 return IDS_LIBADDRESSINPUT_I18N_CEDEX_LABEL;
67 case POSTAL_CODE:
68 return postal_code_name_message_id;
69 case STREET_ADDRESS:
70 return IDS_LIBADDRESSINPUT_I18N_ADDRESS_LINE1_LABEL;
71 case ORGANIZATION:
72 return IDS_LIBADDRESSINPUT_I18N_ORGANIZATION_LABEL;
73 case RECIPIENT:
74 return IDS_LIBADDRESSINPUT_I18N_RECIPIENT_LABEL;
75 default:
76 return INVALID_MESSAGE_ID;
77 }
78 }
79
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
86
87 const std::vector<std::string>& GetRegionCodes() {
88 return RegionDataConstants::GetRegionCodes();
89 }
90
91 std::vector<AddressUiComponent> BuildComponents(
92 const std::string& region_code,
93 const Localization& localization) {
94 std::vector<AddressUiComponent> result;
95
96 Rule rule;
97 rule.CopyFrom(GetDefaultRule());
98 if (!rule.ParseSerializedRule(
99 RegionDataConstants::GetRegionData(region_code))) {
100 return result;
101 }
102
103 bool previous_field_is_newline = true;
104 bool next_field_is_newline = true;
105 for (std::vector<AddressField>::const_iterator field_it =
106 rule.GetFormat().begin();
107 field_it != rule.GetFormat().end(); ++field_it) {
108 if (IsNewline(*field_it)) {
109 previous_field_is_newline = true;
110 continue;
111 }
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 }
126
127 return result;
128 }
129
130 } // namespace addressinput
131 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698