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

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

Issue 2629883004: [Payment Request] Update the CurrencyStringFormatter to call the native impl. (Closed)
Patch Set: CurrencyStringFormatter -> CurrencyFormatter 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"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "third_party/icu/source/common/unicode/stringpiece.h" 12 #include "third_party/icu/source/common/unicode/stringpiece.h"
13 #include "third_party/icu/source/common/unicode/uchar.h" 13 #include "third_party/icu/source/common/unicode/uchar.h"
14 #include "third_party/icu/source/common/unicode/unistr.h" 14 #include "third_party/icu/source/common/unicode/unistr.h"
15 #include "third_party/icu/source/common/unicode/utypes.h" 15 #include "third_party/icu/source/common/unicode/utypes.h"
16 16
17 namespace payments { 17 namespace payments {
18 18
19 const char kIso4217CurrencySystem[] = "urn:iso:std:iso:4217"; 19 const char kIso4217CurrencySystem[] = "urn:iso:std:iso:4217";
20
20 namespace { 21 namespace {
21 22
22 // Support a maximum of 10 fractional digits, similar to the ISO20022 standard. 23 // Support a maximum of 10 fractional digits, similar to the ISO20022 standard.
23 // https://www.iso20022.org/standardsrepository/public/wqt/Description/mx/dico/ 24 // https://www.iso20022.org/standardsrepository/public/wqt/Description/mx/dico/
24 // datatypes/_L8ZcEp0gEeOo48XfssNw8w 25 // datatypes/_L8ZcEp0gEeOo48XfssNw8w
25 const int kMaximumNumFractionalDigits = 10; 26 const int kMaximumNumFractionalDigits = 10;
26 27
27 // Max currency code length. Length of currency code can be at most 2048. 28 // Max currency code length. Length of currency code can be at most 2048.
28 const static size_t kMaxCurrencyCodeLength = 2048; 29 const static size_t kMaxCurrencyCodeLength = 2048;
29 30
31 // Currency codes longer than 6 characters get truncated to 5 + ellipsis.
32 const static size_t kMaxCurrencyCodeDisplayedChars = 6;
33
34 // Used to truncate long currency codes.
35 const char kEllipsis[] = "\xE2\x80\xA6";
36
30 // 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.
31 bool ShouldUseCurrencyCode(const std::string& currency_code, 38 bool ShouldUseCurrencyCode(const std::string& currency_code,
32 const base::Optional<std::string> currency_system) { 39 const base::Optional<std::string> currency_system) {
33 return currency_system.value_or(kIso4217CurrencySystem) == 40 return currency_system.value_or(kIso4217CurrencySystem) ==
34 kIso4217CurrencySystem && 41 kIso4217CurrencySystem &&
35 !currency_code.empty() && 42 !currency_code.empty() &&
36 currency_code.size() <= kMaxCurrencyCodeLength; 43 currency_code.size() <= kMaxCurrencyCodeLength;
37 } 44 }
38 45
46 std::string FormatCurrencyCode(const std::string& currency_code) {
47 return currency_code.length() < kMaxCurrencyCodeDisplayedChars
48 ? currency_code
49 : currency_code.substr(0, kMaxCurrencyCodeDisplayedChars - 1) +
50 kEllipsis;
51 }
52
39 } // namespace 53 } // namespace
40 54
41 CurrencyFormatter::CurrencyFormatter( 55 CurrencyFormatter::CurrencyFormatter(
42 const std::string& currency_code, 56 const std::string& currency_code,
43 const base::Optional<std::string> currency_system, 57 const base::Optional<std::string> currency_system,
44 const std::string& locale_name) 58 const std::string& locale_name)
45 : locale_(locale_name.c_str()) { 59 : locale_(locale_name.c_str()),
60 formatted_currency_code_(FormatCurrencyCode(currency_code)) {
46 UErrorCode error_code = U_ZERO_ERROR; 61 UErrorCode error_code = U_ZERO_ERROR;
47 icu_formatter_.reset( 62 icu_formatter_.reset(
48 icu::NumberFormat::createCurrencyInstance(locale_, error_code)); 63 icu::NumberFormat::createCurrencyInstance(locale_, error_code));
49 if (U_FAILURE(error_code)) { 64 if (U_FAILURE(error_code)) {
50 icu::UnicodeString name;
51 std::string locale_str;
52 locale_.getDisplayName(name).toUTF8String(locale_str);
53 LOG(ERROR) << "Failed to initialize the currency formatter for " 65 LOG(ERROR) << "Failed to initialize the currency formatter for "
54 << locale_str; 66 << locale_name;
55 return; 67 return;
56 } 68 }
57 69
58 if (ShouldUseCurrencyCode(currency_code, currency_system)) { 70 if (ShouldUseCurrencyCode(currency_code, currency_system)) {
59 currency_code_.reset(new icu::UnicodeString( 71 currency_code_.reset(new icu::UnicodeString(
60 currency_code.c_str(), 72 currency_code.c_str(),
61 base::checked_cast<int32_t>(currency_code.size()))); 73 base::checked_cast<int32_t>(currency_code.size())));
62 } else { 74 } else {
63 // For non-ISO4217 currency system/code, we use a dummy code which is not 75 // For non-ISO4217 currency system/code, we use a dummy code which is not
64 // going to appear in the output (stripped in Format()). This is because ICU 76 // going to appear in the output (stripped in Format()). This is because ICU
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 if (u_isUWhiteSpace(output[output.length() - 1])) { 127 if (u_isUWhiteSpace(output[output.length() - 1])) {
116 output.remove(output.length() - 1, 1); 128 output.remove(output.length() - 1, 1);
117 } 129 }
118 130
119 std::string output_str; 131 std::string output_str;
120 output.toUTF8String(output_str); 132 output.toUTF8String(output_str);
121 return base::UTF8ToUTF16(output_str); 133 return base::UTF8ToUTF16(output_str);
122 } 134 }
123 135
124 } // namespace payments 136 } // 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