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

Side by Side Diff: components/payments/currency_formatter.cc

Issue 2649143003: PaymentRequest: The currencySystem should be non-nullable. (Closed)
Patch Set: PaymentRequest: The currencySystem should be non-nullable. Created 3 years, 11 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/payments/currency_formatter.h" 5 #include "components/payments/currency_formatter.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/numerics/safe_conversions.h" 9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 18 matching lines...) Expand all
29 const static size_t kMaxCurrencyCodeLength = 2048; 29 const static size_t kMaxCurrencyCodeLength = 2048;
30 30
31 // Currency codes longer than 6 characters get truncated to 5 + ellipsis. 31 // Currency codes longer than 6 characters get truncated to 5 + ellipsis.
32 const static size_t kMaxCurrencyCodeDisplayedChars = 6; 32 const static size_t kMaxCurrencyCodeDisplayedChars = 6;
33 33
34 // Used to truncate long currency codes. 34 // Used to truncate long currency codes.
35 const char kEllipsis[] = "\xE2\x80\xA6"; 35 const char kEllipsis[] = "\xE2\x80\xA6";
36 36
37 // Returns whether the |currency_code| is valid to be used in ICU. 37 // Returns whether the |currency_code| is valid to be used in ICU.
38 bool ShouldUseCurrencyCode(const std::string& currency_code, 38 bool ShouldUseCurrencyCode(const std::string& currency_code,
39 const base::Optional<std::string> currency_system) { 39 const std::string& currency_system) {
40 return currency_system.value_or(kIso4217CurrencySystem) == 40 return (currency_system.empty() ||
41 kIso4217CurrencySystem && 41 currency_system == kIso4217CurrencySystem) &&
42 !currency_code.empty() && 42 !currency_code.empty() &&
43 currency_code.size() <= kMaxCurrencyCodeLength; 43 currency_code.size() <= kMaxCurrencyCodeLength;
44 } 44 }
45 45
46 std::string FormatCurrencyCode(const std::string& currency_code) { 46 std::string FormatCurrencyCode(const std::string& currency_code) {
47 return currency_code.length() < kMaxCurrencyCodeDisplayedChars 47 return currency_code.length() < kMaxCurrencyCodeDisplayedChars
48 ? currency_code 48 ? currency_code
49 : currency_code.substr(0, kMaxCurrencyCodeDisplayedChars - 1) + 49 : currency_code.substr(0, kMaxCurrencyCodeDisplayedChars - 1) +
50 kEllipsis; 50 kEllipsis;
51 } 51 }
52 52
53 } // namespace 53 } // namespace
54 54
55 CurrencyFormatter::CurrencyFormatter( 55 CurrencyFormatter::CurrencyFormatter(const std::string& currency_code,
56 const std::string& currency_code, 56 const std::string& currency_system,
57 const base::Optional<std::string> currency_system, 57 const std::string& locale_name)
58 const std::string& locale_name)
59 : locale_(locale_name.c_str()), 58 : locale_(locale_name.c_str()),
60 formatted_currency_code_(FormatCurrencyCode(currency_code)) { 59 formatted_currency_code_(FormatCurrencyCode(currency_code)) {
61 UErrorCode error_code = U_ZERO_ERROR; 60 UErrorCode error_code = U_ZERO_ERROR;
62 icu_formatter_.reset( 61 icu_formatter_.reset(
63 icu::NumberFormat::createCurrencyInstance(locale_, error_code)); 62 icu::NumberFormat::createCurrencyInstance(locale_, error_code));
64 if (U_FAILURE(error_code)) { 63 if (U_FAILURE(error_code)) {
65 LOG(ERROR) << "Failed to initialize the currency formatter for " 64 LOG(ERROR) << "Failed to initialize the currency formatter for "
66 << locale_name; 65 << locale_name;
67 return; 66 return;
68 } 67 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 if (u_isUWhiteSpace(output[output.length() - 1])) { 126 if (u_isUWhiteSpace(output[output.length() - 1])) {
128 output.remove(output.length() - 1, 1); 127 output.remove(output.length() - 1, 1);
129 } 128 }
130 129
131 std::string output_str; 130 std::string output_str;
132 output.toUTF8String(output_str); 131 output.toUTF8String(output_str);
133 return base::UTF8ToUTF16(output_str); 132 return base::UTF8ToUTF16(output_str);
134 } 133 }
135 134
136 } // namespace payments 135 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/currency_formatter.h ('k') | components/payments/currency_formatter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698