Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: base/i18n/number_formatting.cc

Issue 267001: Separate out some more ICU from base and into base/i18n.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/i18n/number_formatting.h ('k') | base/string_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:mergeinfo
Merged /branches/chrome_webkit_merge_branch/base/string_util_icu.cc:r69-2775
OLDNEW
1 // Copyright (c) 2006-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/string_util.h" 5 #include "base/i18n/number_formatting.h"
6 6
7 #include <string.h>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h" 7 #include "base/logging.h"
12 #include "base/singleton.h" 8 #include "base/singleton.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
13 #include "unicode/numfmt.h" 11 #include "unicode/numfmt.h"
14 #include "unicode/ustring.h" 12 #include "unicode/ustring.h"
15 13
16 // Number formatting ----------------------------------------------------------- 14 namespace base {
17 15
18 namespace { 16 namespace {
19 17
20 struct NumberFormatSingletonTraits 18 struct NumberFormatSingletonTraits
21 : public DefaultSingletonTraits<icu::NumberFormat> { 19 : public DefaultSingletonTraits<icu::NumberFormat> {
22 static icu::NumberFormat* New() { 20 static icu::NumberFormat* New() {
23 UErrorCode status = U_ZERO_ERROR; 21 UErrorCode status = U_ZERO_ERROR;
24 icu::NumberFormat* formatter = icu::NumberFormat::createInstance(status); 22 icu::NumberFormat* formatter = icu::NumberFormat::createInstance(status);
25 DCHECK(U_SUCCESS(status)); 23 DCHECK(U_SUCCESS(status));
26 return formatter; 24 return formatter;
27 } 25 }
28 // There's no ICU call to destroy a NumberFormat object other than 26 // There's no ICU call to destroy a NumberFormat object other than
29 // operator delete, so use the default Delete, which calls operator delete. 27 // 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 28 // This can cause problems if a different allocator is used by this file than
31 // by ICU. 29 // by ICU.
32 }; 30 };
33 31
34 } // namespace 32 } // namespace
35 33
36 std::wstring FormatNumber(int64 number) { 34 string16 FormatNumber(int64 number) {
37 icu::NumberFormat* number_format = 35 icu::NumberFormat* number_format =
38 Singleton<icu::NumberFormat, NumberFormatSingletonTraits>::get(); 36 Singleton<icu::NumberFormat, NumberFormatSingletonTraits>::get();
39 37
40 if (!number_format) { 38 if (!number_format) {
41 // As a fallback, just return the raw number in a string. 39 // As a fallback, just return the raw number in a string.
42 return StringPrintf(L"%lld", number); 40 return UTF8ToUTF16(StringPrintf("%lld", number));
43 } 41 }
44 icu::UnicodeString ustr; 42 icu::UnicodeString ustr;
45 number_format->format(number, ustr); 43 number_format->format(number, ustr);
46 44
47 #if defined(WCHAR_T_IS_UTF16) 45 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
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 } 46 }
64 47
65 // Although this function isn't specific to ICU, we implemented it here so 48 } // namespace base
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 }
OLDNEW
« no previous file with comments | « base/i18n/number_formatting.h ('k') | base/string_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698