OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "base/string_util.h" | |
6 | |
7 #include <string.h> | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 #include "base/singleton.h" | |
13 #include "unicode/numfmt.h" | |
14 #include "unicode/ustring.h" | |
15 | |
16 // Number formatting ----------------------------------------------------------- | |
17 | |
18 namespace { | |
19 | |
20 struct NumberFormatSingletonTraits | |
21 : public DefaultSingletonTraits<icu::NumberFormat> { | |
22 static icu::NumberFormat* New() { | |
23 UErrorCode status = U_ZERO_ERROR; | |
24 icu::NumberFormat* formatter = icu::NumberFormat::createInstance(status); | |
25 DCHECK(U_SUCCESS(status)); | |
26 return formatter; | |
27 } | |
28 // There's no ICU call to destroy a NumberFormat object other than | |
29 // operator delete, so use the default Delete, which calls operator delete. | |
30 // This can cause problems if a different allocator is used by this file than | |
31 // by ICU. | |
32 }; | |
33 | |
34 } // namespace | |
35 | |
36 std::wstring FormatNumber(int64 number) { | |
37 icu::NumberFormat* number_format = | |
38 Singleton<icu::NumberFormat, NumberFormatSingletonTraits>::get(); | |
39 | |
40 if (!number_format) { | |
41 // As a fallback, just return the raw number in a string. | |
42 return StringPrintf(L"%lld", number); | |
43 } | |
44 icu::UnicodeString ustr; | |
45 number_format->format(number, ustr); | |
46 | |
47 #if defined(WCHAR_T_IS_UTF16) | |
48 return std::wstring(ustr.getBuffer(), | |
49 static_cast<std::wstring::size_type>(ustr.length())); | |
50 #elif defined(WCHAR_T_IS_UTF32) | |
51 wchar_t buffer[64]; // A int64 is less than 20 chars long, so 64 chars | |
52 // leaves plenty of room for formating stuff. | |
53 int length = 0; | |
54 UErrorCode error = U_ZERO_ERROR; | |
55 u_strToWCS(buffer, 64, &length, ustr.getBuffer(), ustr.length() , &error); | |
56 if (U_FAILURE(error)) { | |
57 NOTREACHED(); | |
58 // As a fallback, just return the raw number in a string. | |
59 return StringPrintf(L"%lld", number); | |
60 } | |
61 return std::wstring(buffer, static_cast<std::wstring::size_type>(length)); | |
62 #endif // defined(WCHAR_T_IS_UTF32) | |
63 } | |
64 | |
65 // Although this function isn't specific to ICU, we implemented it here so | |
66 // that chrome.exe won't pull it in. Moving this function to string_util.cc | |
67 // causes chrome.exe to grow by 400k because of more ICU being pulled in. | |
68 TrimPositions TrimWhitespaceUTF8(const std::string& input, | |
69 TrimPositions positions, | |
70 std::string* output) { | |
71 // This implementation is not so fast since it converts the text encoding | |
72 // twice. Please feel free to file a bug if this function hurts the | |
73 // performance of Chrome. | |
74 DCHECK(IsStringUTF8(input)); | |
75 std::wstring input_wide = UTF8ToWide(input); | |
76 std::wstring output_wide; | |
77 TrimPositions result = TrimWhitespace(input_wide, positions, &output_wide); | |
78 *output = WideToUTF8(output_wide); | |
79 return result; | |
80 } | |
OLD | NEW |