OLD | NEW |
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 |
(...skipping 10 matching lines...) Expand all Loading... |
40 | 41 |
41 CurrencyFormatter::CurrencyFormatter( | 42 CurrencyFormatter::CurrencyFormatter( |
42 const std::string& currency_code, | 43 const std::string& currency_code, |
43 const base::Optional<std::string> currency_system, | 44 const base::Optional<std::string> currency_system, |
44 const std::string& locale_name) | 45 const std::string& locale_name) |
45 : locale_(locale_name.c_str()) { | 46 : locale_(locale_name.c_str()) { |
46 UErrorCode error_code = U_ZERO_ERROR; | 47 UErrorCode error_code = U_ZERO_ERROR; |
47 icu_formatter_.reset( | 48 icu_formatter_.reset( |
48 icu::NumberFormat::createCurrencyInstance(locale_, error_code)); | 49 icu::NumberFormat::createCurrencyInstance(locale_, error_code)); |
49 if (U_FAILURE(error_code)) { | 50 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 " | 51 LOG(ERROR) << "Failed to initialize the currency formatter for " |
54 << locale_str; | 52 << locale_name; |
55 return; | 53 return; |
56 } | 54 } |
57 | 55 |
58 if (ShouldUseCurrencyCode(currency_code, currency_system)) { | 56 if (ShouldUseCurrencyCode(currency_code, currency_system)) { |
59 currency_code_.reset(new icu::UnicodeString( | 57 currency_code_.reset(new icu::UnicodeString( |
60 currency_code.c_str(), | 58 currency_code.c_str(), |
61 base::checked_cast<int32_t>(currency_code.size()))); | 59 base::checked_cast<int32_t>(currency_code.size()))); |
62 } else { | 60 } else { |
63 // For non-ISO4217 currency system/code, we use a dummy code which is not | 61 // 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 | 62 // 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 Loading... |
115 if (u_isUWhiteSpace(output[output.length() - 1])) { | 113 if (u_isUWhiteSpace(output[output.length() - 1])) { |
116 output.remove(output.length() - 1, 1); | 114 output.remove(output.length() - 1, 1); |
117 } | 115 } |
118 | 116 |
119 std::string output_str; | 117 std::string output_str; |
120 output.toUTF8String(output_str); | 118 output.toUTF8String(output_str); |
121 return base::UTF8ToUTF16(output_str); | 119 return base::UTF8ToUTF16(output_str); |
122 } | 120 } |
123 | 121 |
124 } // namespace payments | 122 } // namespace payments |
OLD | NEW |