OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "unicode/numfmt.h" | 14 #include "unicode/numfmt.h" |
15 #include "unicode/ustring.h" | 15 #include "unicode/ustring.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 struct NumberFormatWrapper { | 21 struct NumberFormatWrapper { |
22 NumberFormatWrapper() { | 22 NumberFormatWrapper() { |
23 Reset(); | |
24 } | |
25 | |
26 void Reset() { | |
23 // There's no ICU call to destroy a NumberFormat object other than | 27 // There's no ICU call to destroy a NumberFormat object other than |
24 // operator delete, so use the default Delete, which calls operator delete. | 28 // operator delete, so use the default Delete, which calls operator delete. |
25 // This can cause problems if a different allocator is used by this file | 29 // This can cause problems if a different allocator is used by this file |
26 // than by ICU. | 30 // than by ICU. |
27 UErrorCode status = U_ZERO_ERROR; | 31 UErrorCode status = U_ZERO_ERROR; |
28 number_format.reset(icu::NumberFormat::createInstance(status)); | 32 number_format.reset(icu::NumberFormat::createInstance(status)); |
29 DCHECK(U_SUCCESS(status)); | 33 DCHECK(U_SUCCESS(status)); |
30 } | 34 } |
31 | 35 |
32 scoped_ptr<icu::NumberFormat> number_format; | 36 scoped_ptr<icu::NumberFormat> number_format; |
33 }; | 37 }; |
34 | 38 |
35 } // namespace | 39 } // namespace |
36 | 40 |
37 static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED); | 41 static |
Mark Mentovai
2011/06/22 20:24:11
Rather than staticating these, just move them up a
Avi (use Gerrit)
2011/06/22 20:27:37
Done.
| |
42 LazyInstance<NumberFormatWrapper> g_number_format_int(LINKER_INITIALIZED); | |
43 | |
44 static | |
45 LazyInstance<NumberFormatWrapper> g_number_format_float(LINKER_INITIALIZED); | |
38 | 46 |
39 string16 FormatNumber(int64 number) { | 47 string16 FormatNumber(int64 number) { |
40 icu::NumberFormat* number_format = g_number_format.Get().number_format.get(); | 48 icu::NumberFormat* number_format = |
49 g_number_format_int.Get().number_format.get(); | |
41 | 50 |
42 if (!number_format) { | 51 if (!number_format) { |
43 // As a fallback, just return the raw number in a string. | 52 // As a fallback, just return the raw number in a string. |
44 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); | 53 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); |
45 } | 54 } |
46 icu::UnicodeString ustr; | 55 icu::UnicodeString ustr; |
47 number_format->format(number, ustr); | 56 number_format->format(number, ustr); |
48 | 57 |
49 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 58 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
50 } | 59 } |
51 | 60 |
61 string16 FormatDouble(double number, int fractional_digits) { | |
62 icu::NumberFormat* number_format = | |
63 g_number_format_float.Get().number_format.get(); | |
64 | |
65 if (!number_format) { | |
66 // As a fallback, just return the raw number in a string. | |
67 return UTF8ToUTF16(StringPrintf("%f", number)); | |
68 } | |
69 number_format->setMaximumFractionDigits(fractional_digits); | |
70 number_format->setMinimumFractionDigits(fractional_digits); | |
71 icu::UnicodeString ustr; | |
72 number_format->format(number, ustr); | |
73 | |
74 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | |
75 } | |
76 | |
77 namespace testing { | |
78 | |
79 void ResetFormatters() { | |
80 g_number_format_int.Get().Reset(); | |
81 g_number_format_float.Get().Reset(); | |
82 } | |
83 | |
84 } // namespace testing | |
85 | |
52 } // namespace base | 86 } // namespace base |
OLD | NEW |