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

Side by Side Diff: components/autofill/core/browser/country_names.cc

Issue 1859453002: components/autofill: scoped_ptr -> unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments addressed Created 4 years, 8 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/browser/country_names.h" 5 #include "components/autofill/core/browser/country_names.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory>
10
9 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
13 #include "base/stl_util.h" 14 #include "base/stl_util.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "components/autofill/core/browser/country_data.h" 17 #include "components/autofill/core/browser/country_data.h"
17 #include "components/autofill/core/common/autofill_l10n_util.h" 18 #include "components/autofill/core/common/autofill_l10n_util.h"
18 #include "third_party/icu/source/common/unicode/unistr.h" 19 #include "third_party/icu/source/common/unicode/unistr.h"
19 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
20 21
21 namespace autofill { 22 namespace autofill {
22 namespace { 23 namespace {
23 24
24 // A copy of the application locale string, which should be ready for 25 // A copy of the application locale string, which should be ready for
25 // CountryName's construction. 26 // CountryName's construction.
26 static base::LazyInstance<std::string> g_application_locale = 27 static base::LazyInstance<std::string> g_application_locale =
27 LAZY_INSTANCE_INITIALIZER; 28 LAZY_INSTANCE_INITIALIZER;
28 29
29 // Returns the ICU sort key corresponding to |str| for the given |collator|. 30 // Returns the ICU sort key corresponding to |str| for the given |collator|.
30 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- 31 // Uses |buffer| as temporary storage, and might resize |buffer| as a side-
31 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if 32 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if
32 // the |buffer| is resized. 33 // the |buffer| is resized.
33 const std::string GetSortKey(const icu::Collator& collator, 34 const std::string GetSortKey(const icu::Collator& collator,
34 const base::string16& str, 35 const base::string16& str,
35 scoped_ptr<uint8_t[]>* buffer, 36 std::unique_ptr<uint8_t[]>* buffer,
36 int32_t* buffer_size) { 37 int32_t* buffer_size) {
37 DCHECK(buffer); 38 DCHECK(buffer);
38 DCHECK(buffer_size); 39 DCHECK(buffer_size);
39 40
40 icu::UnicodeString icu_str(str.c_str(), str.length()); 41 icu::UnicodeString icu_str(str.c_str(), str.length());
41 int32_t expected_size = 42 int32_t expected_size =
42 collator.getSortKey(icu_str, buffer->get(), *buffer_size); 43 collator.getSortKey(icu_str, buffer->get(), *buffer_size);
43 if (expected_size > *buffer_size) { 44 if (expected_size > *buffer_size) {
44 // If there wasn't enough space, grow the buffer and try again. 45 // If there wasn't enough space, grow the buffer and try again.
45 *buffer_size = expected_size; 46 *buffer_size = expected_size;
(...skipping 27 matching lines...) Expand all
73 common_names.insert(std::make_pair("UNITED STATES OF AMERICA", "US")); 74 common_names.insert(std::make_pair("UNITED STATES OF AMERICA", "US"));
74 common_names.insert(std::make_pair("U.S.A.", "US")); 75 common_names.insert(std::make_pair("U.S.A.", "US"));
75 common_names.insert(std::make_pair("GREAT BRITAIN", "GB")); 76 common_names.insert(std::make_pair("GREAT BRITAIN", "GB"));
76 common_names.insert(std::make_pair("UK", "GB")); 77 common_names.insert(std::make_pair("UK", "GB"));
77 common_names.insert(std::make_pair("BRASIL", "BR")); 78 common_names.insert(std::make_pair("BRASIL", "BR"));
78 common_names.insert(std::make_pair("DEUTSCHLAND", "DE")); 79 common_names.insert(std::make_pair("DEUTSCHLAND", "DE"));
79 return common_names; 80 return common_names;
80 } 81 }
81 82
82 // Creates collator for |locale| and sets its attributes as needed. 83 // Creates collator for |locale| and sets its attributes as needed.
83 scoped_ptr<icu::Collator> CreateCollator(const icu::Locale& locale) { 84 std::unique_ptr<icu::Collator> CreateCollator(const icu::Locale& locale) {
84 scoped_ptr<icu::Collator> collator( 85 std::unique_ptr<icu::Collator> collator(
85 autofill::l10n::GetCollatorForLocale(locale)); 86 autofill::l10n::GetCollatorForLocale(locale));
86 if (!collator) 87 if (!collator)
87 return nullptr; 88 return nullptr;
88 89
89 // Compare case-insensitively and ignoring punctuation. 90 // Compare case-insensitively and ignoring punctuation.
90 UErrorCode ignored = U_ZERO_ERROR; 91 UErrorCode ignored = U_ZERO_ERROR;
91 collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored); 92 collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored);
92 ignored = U_ZERO_ERROR; 93 ignored = U_ZERO_ERROR;
93 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); 94 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored);
94 95
95 return collator; 96 return collator;
96 } 97 }
97 98
98 // If |locale| is different from "en_US", returns a collator for "en_US" and 99 // If |locale| is different from "en_US", returns a collator for "en_US" and
99 // sets its attributes as appropriate. Otherwise returns null. 100 // sets its attributes as appropriate. Otherwise returns null.
100 scoped_ptr<icu::Collator> CreateDefaultCollator(const icu::Locale& locale) { 101 std::unique_ptr<icu::Collator> CreateDefaultCollator(
102 const icu::Locale& locale) {
101 icu::Locale default_locale("en_US"); 103 icu::Locale default_locale("en_US");
102 104
103 if (default_locale != locale) 105 if (default_locale != locale)
104 return CreateCollator(default_locale); 106 return CreateCollator(default_locale);
105 107
106 return nullptr; 108 return nullptr;
107 } 109 }
108 110
109 // Returns the mapping of country names localized to |locale| to their 111 // Returns the mapping of country names localized to |locale| to their
110 // corresponding country codes. The provided |collator| should be suitable for 112 // corresponding country codes. The provided |collator| should be suitable for
111 // the locale. The collator being null is handled gracefully by returning an 113 // the locale. The collator being null is handled gracefully by returning an
112 // empty map, to account for the very rare cases when the collator fails to 114 // empty map, to account for the very rare cases when the collator fails to
113 // initialize. 115 // initialize.
114 std::map<std::string, std::string> GetLocalizedNames( 116 std::map<std::string, std::string> GetLocalizedNames(
115 const std::string& locale, 117 const std::string& locale,
116 const icu::Collator* collator) { 118 const icu::Collator* collator) {
117 if (!collator) 119 if (!collator)
118 return std::map<std::string, std::string>(); 120 return std::map<std::string, std::string>();
119 121
120 std::map<std::string, std::string> localized_names; 122 std::map<std::string, std::string> localized_names;
121 int32_t buffer_size = 1000; 123 int32_t buffer_size = 1000;
122 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); 124 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
123 125
124 for (const std::string& country_code : 126 for (const std::string& country_code :
125 CountryDataMap::GetInstance()->country_codes()) { 127 CountryDataMap::GetInstance()->country_codes()) {
126 base::string16 country_name = 128 base::string16 country_name =
127 l10n_util::GetDisplayNameForCountry(country_code, locale); 129 l10n_util::GetDisplayNameForCountry(country_code, locale);
128 std::string sort_key = 130 std::string sort_key =
129 GetSortKey(*collator, country_name, &buffer, &buffer_size); 131 GetSortKey(*collator, country_name, &buffer, &buffer_size);
130 132
131 localized_names.insert(std::make_pair(sort_key, country_code)); 133 localized_names.insert(std::make_pair(sort_key, country_code));
132 } 134 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 193 }
192 194
193 const std::string CountryNames::GetCountryCodeForLocalizedName( 195 const std::string CountryNames::GetCountryCodeForLocalizedName(
194 const base::string16& country_name, 196 const base::string16& country_name,
195 const std::map<std::string, std::string>& localized_names, 197 const std::map<std::string, std::string>& localized_names,
196 const icu::Collator& collator) { 198 const icu::Collator& collator) {
197 // As recommended[1] by ICU, initialize the buffer size to four times the 199 // As recommended[1] by ICU, initialize the buffer size to four times the
198 // source string length. 200 // source string length.
199 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples 201 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples
200 int32_t buffer_size = country_name.size() * 4; 202 int32_t buffer_size = country_name.size() * 4;
201 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); 203 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
202 std::string sort_key = 204 std::string sort_key =
203 GetSortKey(collator, country_name, &buffer, &buffer_size); 205 GetSortKey(collator, country_name, &buffer, &buffer_size);
204 206
205 auto result = localized_names.find(sort_key); 207 auto result = localized_names.find(sort_key);
206 208
207 if (result != localized_names.end()) 209 if (result != localized_names.end())
208 return result->second; 210 return result->second;
209 211
210 return std::string(); 212 return std::string();
211 } 213 }
212 214
213 } // namespace autofill 215 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/country_names.h ('k') | components/autofill/core/browser/credit_card_field.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698