OLD | NEW |
---|---|
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/singleton.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/scoped_ptr.h" | |
10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
12 #include "unicode/numfmt.h" | 13 #include "unicode/numfmt.h" |
13 #include "unicode/ustring.h" | 14 #include "unicode/ustring.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 struct NumberFormatSingletonTraits | 20 struct NumberFormatWrapper { |
20 : public DefaultSingletonTraits<icu::NumberFormat> { | 21 NumberFormatWrapper() { |
21 static icu::NumberFormat* New() { | 22 // There's no ICU call to destroy a NumberFormat object other than |
23 // operator delete, so use the default Delete, which calls operator delete. | |
24 // This can cause problems if a different allocator is used by this file tha n | |
M-A Ruel
2010/12/10 14:52:18
80 cols
Satish
2010/12/10 17:13:48
Done.
| |
25 // by ICU. | |
22 UErrorCode status = U_ZERO_ERROR; | 26 UErrorCode status = U_ZERO_ERROR; |
23 icu::NumberFormat* formatter = icu::NumberFormat::createInstance(status); | 27 number_format.reset(icu::NumberFormat::createInstance(status)); |
24 DCHECK(U_SUCCESS(status)); | 28 DCHECK(U_SUCCESS(status)); |
25 return formatter; | |
26 } | 29 } |
27 // There's no ICU call to destroy a NumberFormat object other than | 30 |
28 // operator delete, so use the default Delete, which calls operator delete. | 31 scoped_ptr<icu::NumberFormat> number_format; |
29 // This can cause problems if a different allocator is used by this file than | |
30 // by ICU. | |
31 }; | 32 }; |
32 | 33 |
33 } // namespace | 34 } // namespace |
34 | 35 |
36 static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED); | |
37 | |
35 string16 FormatNumber(int64 number) { | 38 string16 FormatNumber(int64 number) { |
36 icu::NumberFormat* number_format = | 39 icu::NumberFormat* number_format = g_number_format.Get().number_format.get(); |
37 Singleton<icu::NumberFormat, NumberFormatSingletonTraits>::get(); | |
38 | 40 |
39 if (!number_format) { | 41 if (!number_format) { |
40 // As a fallback, just return the raw number in a string. | 42 // As a fallback, just return the raw number in a string. |
41 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); | 43 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); |
42 } | 44 } |
43 icu::UnicodeString ustr; | 45 icu::UnicodeString ustr; |
44 number_format->format(number, ustr); | 46 number_format->format(number, ustr); |
45 | 47 |
46 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 48 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
47 } | 49 } |
48 | 50 |
49 } // namespace base | 51 } // namespace base |
OLD | NEW |