OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/i18n/number_formatting.h" | 5 #include "base/i18n/number_formatting.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "third_party/icu/source/common/unicode/ustring.h" | 17 #include "third_party/icu/source/common/unicode/ustring.h" |
18 #include "third_party/icu/source/i18n/unicode/numfmt.h" | 18 #include "third_party/icu/source/i18n/unicode/numfmt.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // A simple wrapper around icu::NumberFormat that allows for resetting it | 24 // A simple wrapper around icu::NumberFormat that allows for resetting it |
25 // (as LazyInstance does not). | 25 // (as LazyInstance does not). |
26 struct NumberFormatWrapper { | 26 struct FormatWrapper { |
27 NumberFormatWrapper() { | 27 void Reset(UNumberFormatStyle kind) { |
28 Reset(); | |
29 } | |
30 | |
31 void Reset() { | |
32 // There's no ICU call to destroy a NumberFormat object other than | 28 // There's no ICU call to destroy a NumberFormat object other than |
33 // operator delete, so use the default Delete, which calls operator delete. | 29 // operator delete, so use the default Delete, which calls operator delete. |
34 // This can cause problems if a different allocator is used by this file | 30 // This can cause problems if a different allocator is used by this file |
35 // than by ICU. | 31 // than by ICU. |
36 UErrorCode status = U_ZERO_ERROR; | 32 UErrorCode status = U_ZERO_ERROR; |
37 number_format.reset(icu::NumberFormat::createInstance(status)); | 33 number_format.reset(icu::NumberFormat::createInstance( |
34 icu::Locale::getDefault(), kind, status)); | |
38 DCHECK(U_SUCCESS(status)); | 35 DCHECK(U_SUCCESS(status)); |
39 } | 36 } |
40 | 37 |
41 std::unique_ptr<icu::NumberFormat> number_format; | 38 std::unique_ptr<icu::NumberFormat> number_format; |
42 }; | 39 }; |
43 | 40 |
41 struct NumberFormatWrapper : FormatWrapper { | |
42 NumberFormatWrapper() { Reset(UNUM_DECIMAL); } | |
43 }; | |
44 | |
45 struct PercentFormatWrapper : FormatWrapper { | |
46 PercentFormatWrapper() { | |
47 Reset(UNUM_PERCENT); | |
48 number_format.get()->setMaximumFractionDigits(0); | |
49 number_format.get()->setMinimumFractionDigits(0); | |
50 } | |
51 }; | |
52 | |
44 LazyInstance<NumberFormatWrapper> g_number_format_int = | 53 LazyInstance<NumberFormatWrapper> g_number_format_int = |
45 LAZY_INSTANCE_INITIALIZER; | 54 LAZY_INSTANCE_INITIALIZER; |
46 LazyInstance<NumberFormatWrapper> g_number_format_float = | 55 LazyInstance<NumberFormatWrapper> g_number_format_float = |
47 LAZY_INSTANCE_INITIALIZER; | 56 LAZY_INSTANCE_INITIALIZER; |
57 LazyInstance<PercentFormatWrapper> g_number_format_percent = | |
58 LAZY_INSTANCE_INITIALIZER; | |
48 | 59 |
49 } // namespace | 60 } // namespace |
50 | 61 |
51 string16 FormatNumber(int64_t number) { | 62 string16 FormatNumber(int64_t number) { |
52 icu::NumberFormat* number_format = | 63 icu::NumberFormat* number_format = |
53 g_number_format_int.Get().number_format.get(); | 64 g_number_format_int.Get().number_format.get(); |
54 | 65 |
55 if (!number_format) { | 66 if (!number_format) { |
56 // As a fallback, just return the raw number in a string. | 67 // As a fallback, just return the raw number in a string. |
57 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); | 68 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); |
(...skipping 13 matching lines...) Expand all Loading... | |
71 return UTF8ToUTF16(StringPrintf("%f", number)); | 82 return UTF8ToUTF16(StringPrintf("%f", number)); |
72 } | 83 } |
73 number_format->setMaximumFractionDigits(fractional_digits); | 84 number_format->setMaximumFractionDigits(fractional_digits); |
74 number_format->setMinimumFractionDigits(fractional_digits); | 85 number_format->setMinimumFractionDigits(fractional_digits); |
75 icu::UnicodeString ustr; | 86 icu::UnicodeString ustr; |
76 number_format->format(number, ustr); | 87 number_format->format(number, ustr); |
77 | 88 |
78 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 89 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
79 } | 90 } |
80 | 91 |
92 string16 FormatPercent(int number) { | |
93 icu::NumberFormat* percent_format = | |
94 g_number_format_percent.Get().number_format.get(); | |
95 | |
Peter Kasting
2016/05/19 09:35:52
Nit: I'd remove this blank line and instead add on
Greg Levin
2016/05/19 15:42:13
Function rewritten, no blank lines.
| |
96 if (!percent_format) { | |
97 // As a fallback, just return the default percent formatting in a string. | |
98 return UTF8ToUTF16(StringPrintf("%d%%", number)); | |
jungshik at Google
2016/05/18 22:35:03
nit: ASCIIToUTF16()
Greg Levin
2016/05/19 15:42:13
Function no longer uses cached lazy instance, so n
| |
99 } | |
100 icu::UnicodeString ustr; | |
101 percent_format->format(static_cast<double>(number) / 100.0, ustr); | |
102 | |
103 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | |
104 } | |
105 | |
81 namespace testing { | 106 namespace testing { |
82 | 107 |
83 void ResetFormatters() { | 108 void ResetFormatters() { |
84 g_number_format_int.Get().Reset(); | 109 g_number_format_int.Get().Reset(UNUM_DECIMAL); |
85 g_number_format_float.Get().Reset(); | 110 g_number_format_float.Get().Reset(UNUM_DECIMAL); |
111 g_number_format_percent.Get().Reset(UNUM_PERCENT); | |
86 } | 112 } |
87 | 113 |
88 } // namespace testing | 114 } // namespace testing |
89 | 115 |
90 } // namespace base | 116 } // namespace base |
OLD | NEW |