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

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

Issue 2621033003: [Payments] Currency formatter for order amounts. (Closed)
Patch Set: checked_cast 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/payments/currency_formatter.h"
6
7 #include <memory>
8
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "third_party/icu/source/common/unicode/stringpiece.h"
13 #include "third_party/icu/source/common/unicode/uchar.h"
14 #include "third_party/icu/source/common/unicode/unistr.h"
15 #include "third_party/icu/source/common/unicode/utypes.h"
16
17 namespace payments {
18
19 const char kIso4217CurrencySystem[] = "urn:iso:std:iso:4217";
20 namespace {
21
22 // Support a maximum of 10 fractional digits, similar to the ISO20022 standard.
23 // https://www.iso20022.org/standardsrepository/public/wqt/Description/mx/dico/
24 // datatypes/_L8ZcEp0gEeOo48XfssNw8w
25 const int kMaximumNumFractionalDigits = 10;
26
27 // Max currency code length. Length of currency code can be at most 2048.
28 const static size_t kMaxCurrencyCodeLength = 2048;
29
30 // Returns whether the |currency_code| is valid to be used in ICU.
31 bool ShouldUseCurrencyCode(const std::string& currency_code,
32 const base::Optional<std::string> currency_system) {
33 return currency_system.value_or(kIso4217CurrencySystem) ==
34 kIso4217CurrencySystem &&
35 !currency_code.empty() &&
36 currency_code.size() <= kMaxCurrencyCodeLength;
37 }
38
39 } // namespace
40
41 CurrencyFormatter::CurrencyFormatter(
42 const std::string& currency_code,
43 const base::Optional<std::string> currency_system,
44 const std::string& locale_name)
45 : locale_(locale_name.c_str()) {
46 UErrorCode error_code = U_ZERO_ERROR;
47 icu_formatter_.reset(
48 icu::NumberFormat::createCurrencyInstance(locale_, error_code));
49 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 "
54 << locale_str;
55 return;
56 }
57
58 if (ShouldUseCurrencyCode(currency_code, currency_system)) {
59 currency_code_.reset(new icu::UnicodeString(
60 currency_code.c_str(),
61 base::checked_cast<int32_t>(currency_code.size())));
62 } else {
63 // 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
65 // NumberFormat will not accept an empty currency code. Under these
66 // circumstances, the number amount will be formatted according to locale,
67 // which is desirable (e.g. "55.00" -> "55,00" in fr_FR).
68 currency_code_.reset(new icu::UnicodeString("DUM", 3));
69 }
70
71 icu_formatter_->setCurrency(currency_code_->getBuffer(), error_code);
72 if (U_FAILURE(error_code)) {
73 std::string currency_code_str;
74 currency_code_->toUTF8String(currency_code_str);
75 LOG(ERROR) << "Could not set currency code on currency formatter: "
76 << currency_code_str;
77 return;
78 }
79
80 icu_formatter_->setMaximumFractionDigits(kMaximumNumFractionalDigits);
81 }
82
83 CurrencyFormatter::~CurrencyFormatter() {}
84
85 base::string16 CurrencyFormatter::Format(const std::string& amount) {
86 // It's possible that the ICU formatter didn't initialize properly.
87 if (!icu_formatter_ || !icu_formatter_->getCurrency())
88 return base::UTF8ToUTF16(amount);
89
90 icu::UnicodeString output;
91 UErrorCode error_code = U_ZERO_ERROR;
92 icu_formatter_->format(icu::StringPiece(amount.c_str()), output, nullptr,
93 error_code);
94
95 if (output.isEmpty())
96 return base::UTF8ToUTF16(amount);
97
98 // Explicitly removes the currency code (truncated to its 3-letter and
99 // 2-letter versions) from the output, because callers are expected to
100 // display the currency code alongside this result.
101 //
102 // 3+ letters: If currency code is "ABCDEF" or "BTX", this code will
103 // transform "ABC55.00"/"BTX55.00" to "55.00".
104 // 2 letters: If currency code is "CAD", this code will transform "CA$55.00"
105 // to "$55.00" (en_US) or "55,00 $ CA" to "55,00 $" (fr_FR).
106 icu::UnicodeString tmp_currency_code(*currency_code_);
107 tmp_currency_code.truncate(3);
108 output.findAndReplace(tmp_currency_code, "");
109 tmp_currency_code.truncate(2);
110 output.findAndReplace(tmp_currency_code, "");
111 // Trims any unicode whitespace (including non-breaking space).
112 if (u_isUWhiteSpace(output[0])) {
113 output.remove(0, 1);
114 }
115 if (u_isUWhiteSpace(output[output.length() - 1])) {
116 output.remove(output.length() - 1, 1);
117 }
118
119 std::string output_str;
120 output.toUTF8String(output_str);
121 return base::UTF8ToUTF16(output_str);
122 }
123
124 } // 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