• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "base/i18n/number_formatting.h"
6 
7 #include "base/format_macros.h"
8 #include "base/logging.h"
9 #include "base/lazy_instance.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h"
13 #include "unicode/numfmt.h"
14 #include "unicode/ustring.h"
15 
16 namespace base {
17 
18 namespace {
19 
20 struct NumberFormatWrapper {
NumberFormatWrapperbase::__anonfad8f7170111::NumberFormatWrapper21   NumberFormatWrapper() {
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
25     // than by ICU.
26     UErrorCode status = U_ZERO_ERROR;
27     number_format.reset(icu::NumberFormat::createInstance(status));
28     DCHECK(U_SUCCESS(status));
29   }
30 
31   scoped_ptr<icu::NumberFormat> number_format;
32 };
33 
34 }  // namespace
35 
36 static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED);
37 
FormatNumber(int64 number)38 string16 FormatNumber(int64 number) {
39   icu::NumberFormat* number_format = g_number_format.Get().number_format.get();
40 
41   if (!number_format) {
42     // As a fallback, just return the raw number in a string.
43     return UTF8ToUTF16(StringPrintf("%" PRId64, number));
44   }
45   icu::UnicodeString ustr;
46   number_format->format(number, ustr);
47 
48   return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
49 }
50 
51 }  // namespace base
52