diff options
30 files changed, 554 insertions, 276 deletions
diff --git a/base/base.gyp b/base/base.gyp index 668171d..3b7b085 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -130,6 +130,7 @@ 'i18n/case_conversion_unittest.cc', 'i18n/file_util_icu_unittest.cc', 'i18n/icu_string_conversions_unittest.cc', + 'i18n/number_formatting_unittest.cc', 'i18n/rtl_unittest.cc', 'i18n/time_formatting_unittest.cc', 'json/json_reader_unittest.cc', diff --git a/base/i18n/number_formatting.cc b/base/i18n/number_formatting.cc index a529a84..b2eeb16 100644 --- a/base/i18n/number_formatting.cc +++ b/base/i18n/number_formatting.cc @@ -20,6 +20,10 @@ namespace { struct NumberFormatWrapper { NumberFormatWrapper() { + Reset(); + } + + void Reset() { // There's no ICU call to destroy a NumberFormat object other than // operator delete, so use the default Delete, which calls operator delete. // This can cause problems if a different allocator is used by this file @@ -32,12 +36,14 @@ struct NumberFormatWrapper { scoped_ptr<icu::NumberFormat> number_format; }; -} // namespace +LazyInstance<NumberFormatWrapper> g_number_format_int(LINKER_INITIALIZED); +LazyInstance<NumberFormatWrapper> g_number_format_float(LINKER_INITIALIZED); -static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED); +} // namespace string16 FormatNumber(int64 number) { - icu::NumberFormat* number_format = g_number_format.Get().number_format.get(); + icu::NumberFormat* number_format = + g_number_format_int.Get().number_format.get(); if (!number_format) { // As a fallback, just return the raw number in a string. @@ -49,4 +55,29 @@ string16 FormatNumber(int64 number) { return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); } +string16 FormatDouble(double number, int fractional_digits) { + icu::NumberFormat* number_format = + g_number_format_float.Get().number_format.get(); + + if (!number_format) { + // As a fallback, just return the raw number in a string. + return UTF8ToUTF16(StringPrintf("%f", number)); + } + number_format->setMaximumFractionDigits(fractional_digits); + number_format->setMinimumFractionDigits(fractional_digits); + icu::UnicodeString ustr; + number_format->format(number, ustr); + + return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); +} + +namespace testing { + +void ResetFormatters() { + g_number_format_int.Get().Reset(); + g_number_format_float.Get().Reset(); +} + +} // namespace testing + } // namespace base diff --git a/base/i18n/number_formatting.h b/base/i18n/number_formatting.h index f5ec083..5df7f17 100644 --- a/base/i18n/number_formatting.h +++ b/base/i18n/number_formatting.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,8 +11,24 @@ namespace base { +// Return a number formatted with separators in the user's locale. +// Ex: FormatNumber(1234567) +// => "1,234,567" in English, "1.234.567" in German string16 FormatNumber(int64 number); +// Return a number formatted with separators in the user's locale. +// Ex: FormatDouble(1234567.8, 1) +// => "1,234,567.8" in English, "1.234.567,8" in German +string16 FormatDouble(double number, int fractional_digits); + +namespace testing { + +// Causes cached formatters to be discarded and recreated. Only useful for +// testing. +void ResetFormatters(); + +} // namespace testing + } // namespace base #endif // BASE_I18N_NUMBER_FORMATTING_H_ diff --git a/base/i18n/number_formatting_unittest.cc b/base/i18n/number_formatting_unittest.cc new file mode 100644 index 0000000..e6a54b5 --- /dev/null +++ b/base/i18n/number_formatting_unittest.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <limits> + +#include "base/i18n/number_formatting.h" +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "unicode/locid.h" + +namespace { + +void SetICUDefaultLocale(const std::string& locale_string) { + icu::Locale locale(locale_string.c_str()); + UErrorCode error_code = U_ZERO_ERROR; + icu::Locale::setDefault(locale, error_code); + EXPECT_TRUE(U_SUCCESS(error_code)); +} + +} // namespace + +TEST(NumberFormattingTest, FormatNumber) { + static const struct { + int64 number; + const char* expected_english; + const char* expected_german; + } cases[] = { + {0, "0", "0"}, + {1024, "1,024", "1.024"}, + {std::numeric_limits<int64>::max(), + "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"}, + {std::numeric_limits<int64>::min(), + "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"}, + {-42, "-42", "-42"}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + SetICUDefaultLocale("en"); + base::testing::ResetFormatters(); + EXPECT_EQ(cases[i].expected_english, + UTF16ToUTF8(base::FormatNumber(cases[i].number))); + SetICUDefaultLocale("de"); + base::testing::ResetFormatters(); + EXPECT_EQ(cases[i].expected_german, + UTF16ToUTF8(base::FormatNumber(cases[i].number))); + } +} + +TEST(NumberFormattingTest, FormatDouble) { + static const struct { + double number; + int frac_digits; + const char* expected_english; + const char* expected_german; + } cases[] = { + {0.0, 0, "0", "0"}, + {-0.0, 4, "-0.0000", "-0,0000"}, + {1024.2, 0, "1,024", "1.024"}, + {-1024.223, 2, "-1,024.22", "-1.024,22"}, + {std::numeric_limits<double>::max(), 6, + "179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000," + "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000," + "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000," + "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000," + "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000," + "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000," + "000.000000", + "179.769.313.486.232.000.000.000.000.000.000.000.000.000.000.000.000." + "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." + "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." + "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." + "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." + "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000." + "000,000000"}, + {std::numeric_limits<double>::min(), 2, "0.00", "0,00"}, + {-42.7, 3, "-42.700", "-42,700"}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + SetICUDefaultLocale("en"); + base::testing::ResetFormatters(); + EXPECT_EQ(cases[i].expected_english, + UTF16ToUTF8(base::FormatDouble(cases[i].number, + cases[i].frac_digits))); + SetICUDefaultLocale("de"); + base::testing::ResetFormatters(); + EXPECT_EQ(cases[i].expected_german, + UTF16ToUTF8(base::FormatDouble(cases[i].number, + cases[i].frac_digits))); + } +} diff --git a/base/string_util.cc b/base/string_util.cc index 81b9ee7..31c5e1a 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -604,85 +604,35 @@ bool EndsWith(const string16& str, const string16& search, } #endif -DataUnits GetByteDisplayUnits(int64 bytes) { - // The byte thresholds at which we display amounts. A byte count is displayed - // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. - // This must match the DataUnits enum. - static const int64 kUnitThresholds[] = { - 0, // DATA_UNITS_BYTE, - 3*1024, // DATA_UNITS_KIBIBYTE, - 2*1024*1024, // DATA_UNITS_MEBIBYTE, - 1024*1024*1024 // DATA_UNITS_GIBIBYTE, - }; - - if (bytes < 0) { - NOTREACHED() << "Negative bytes value"; - return DATA_UNITS_BYTE; - } - - int unit_index = arraysize(kUnitThresholds); - while (--unit_index > 0) { - if (bytes >= kUnitThresholds[unit_index]) - break; - } - - DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_GIBIBYTE); - return DataUnits(unit_index); -} - -// TODO(mpcomplete): deal with locale -// Byte suffixes. This must match the DataUnits enum. -static const char* const kByteStrings[] = { - "B", - "kB", - "MB", - "GB" +static const char* const kByteStringsUnlocalized[] = { + " B", + " kB", + " MB", + " GB", + " TB", + " PB" }; -static const char* const kSpeedStrings[] = { - "B/s", - "kB/s", - "MB/s", - "GB/s" -}; - -string16 FormatBytesInternal(int64 bytes, - DataUnits units, - bool show_units, - const char* const* suffix) { - if (bytes < 0) { - NOTREACHED() << "Negative bytes value"; - return string16(); - } - - DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_GIBIBYTE); - - // Put the quantity in the right units. +string16 FormatBytesUnlocalized(int64 bytes) { double unit_amount = static_cast<double>(bytes); - for (int i = 0; i < units; ++i) - unit_amount /= 1024.0; + size_t dimension = 0; + const int kKilo = 1024; + while (unit_amount >= kKilo && + dimension < arraysize(kByteStringsUnlocalized) - 1) { + unit_amount /= kKilo; + dimension++; + } char buf[64]; - if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) - base::snprintf(buf, arraysize(buf), "%.1lf", unit_amount); - else - base::snprintf(buf, arraysize(buf), "%.0lf", unit_amount); - - std::string ret(buf); - if (show_units) { - ret += " "; - ret += suffix[units]; + if (bytes != 0 && dimension > 0 && unit_amount < 100) { + base::snprintf(buf, arraysize(buf), "%.1lf%s", unit_amount, + kByteStringsUnlocalized[dimension]); + } else { + base::snprintf(buf, arraysize(buf), "%.0lf%s", unit_amount, + kByteStringsUnlocalized[dimension]); } - return ASCIIToUTF16(ret); -} - -string16 FormatBytes(int64 bytes, DataUnits units, bool show_units) { - return FormatBytesInternal(bytes, units, show_units, kByteStrings); -} - -string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units) { - return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); + return ASCIIToUTF16(buf); } template<class StringType> diff --git a/base/string_util.h b/base/string_util.h index 0cb439c..f731c34 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -407,31 +407,11 @@ inline bool IsWhitespace(wchar_t c) { return wcschr(kWhitespaceWide, c) != NULL; } -enum DataUnits { - DATA_UNITS_BYTE = 0, - DATA_UNITS_KIBIBYTE, - DATA_UNITS_MEBIBYTE, - DATA_UNITS_GIBIBYTE, -}; - -// Return the unit type that is appropriate for displaying the amount of bytes -// passed in. -BASE_API DataUnits GetByteDisplayUnits(int64 bytes); - -// Return a byte string in human-readable format, displayed in units appropriate -// specified by 'units', with an optional unit suffix. -// Ex: FormatBytes(512, DATA_UNITS_KIBIBYTE, true) => "0.5 KB" -// Ex: FormatBytes(10*1024, DATA_UNITS_MEBIBYTE, false) => "0.1" -BASE_API string16 FormatBytes(int64 bytes, DataUnits units, bool show_units); - -// As above, but with "/s" units. -// Ex: FormatSpeed(512, DATA_UNITS_KIBIBYTE, true) => "0.5 KB/s" -// Ex: FormatSpeed(10*1024, DATA_UNITS_MEBIBYTE, false) => "0.1" -BASE_API string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units); - -// Return a number formated with separators in the user's locale way. -// Ex: FormatNumber(1234567) => 1,234,567 -BASE_API string16 FormatNumber(int64 number); +// Return a byte string in human-readable format with a unit suffix. Not +// appropriate for use in any UI; use of FormatBytes and friends in ui/base is +// highly recommended instead. TODO(avi): Figure out how to get callers to use +// FormatBytes instead; remove this. +BASE_API string16 FormatBytesUnlocalized(int64 bytes); // Starting at |start_offset| (usually 0), replace the first instance of // |find_this| with |replace_with|. diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 8bdeaf5..0911aae 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -491,32 +491,10 @@ TEST(StringUtilTest, LowerCaseEqualsASCII) { } } -TEST(StringUtilTest, GetByteDisplayUnits) { +TEST(StringUtilTest, FormatBytesUnlocalized) { static const struct { int64 bytes; - DataUnits expected; - } cases[] = { - {0, DATA_UNITS_BYTE}, - {512, DATA_UNITS_BYTE}, - {10*1024, DATA_UNITS_KIBIBYTE}, - {10*1024*1024, DATA_UNITS_MEBIBYTE}, - {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE}, - {~(1LL<<63), DATA_UNITS_GIBIBYTE}, -#ifdef NDEBUG - {-1, DATA_UNITS_BYTE}, -#endif - }; - - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) - EXPECT_EQ(cases[i].expected, GetByteDisplayUnits(cases[i].bytes)); -} - -TEST(StringUtilTest, FormatBytes) { - static const struct { - int64 bytes; - DataUnits units; const char* expected; - const char* expected_with_units; } cases[] = { // Expected behavior: we show one post-decimal digit when we have // under two pre-decimal digits, except in cases where it makes no @@ -524,39 +502,29 @@ TEST(StringUtilTest, FormatBytes) { // Since we switch units once we cross the 1000 mark, this keeps // the display of file sizes or bytes consistently around three // digits. - {0, DATA_UNITS_BYTE, "0", "0 B"}, - {512, DATA_UNITS_BYTE, "512", "512 B"}, - {512, DATA_UNITS_KIBIBYTE, "0.5", "0.5 kB"}, - {1024*1024, DATA_UNITS_KIBIBYTE, "1024", "1024 kB"}, - {1024*1024, DATA_UNITS_MEBIBYTE, "1.0", "1.0 MB"}, - {1024*1024*1024, DATA_UNITS_GIBIBYTE, "1.0", "1.0 GB"}, - {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, - {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "99.0", "99.0 GB"}, - {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "105", "105 GB"}, - {105LL*1024*1024*1024 + 500LL*1024*1024, DATA_UNITS_GIBIBYTE, - "105", "105 GB"}, - {~(1LL<<63), DATA_UNITS_GIBIBYTE, "8589934592", "8589934592 GB"}, - - {99*1024 + 103, DATA_UNITS_KIBIBYTE, "99.1", "99.1 kB"}, - {1024*1024 + 103, DATA_UNITS_KIBIBYTE, "1024", "1024 kB"}, - {1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, "1.2", "1.2 MB"}, - {1024*1024*1024 + (927 * 1024*1024), DATA_UNITS_GIBIBYTE, - "1.9", "1.9 GB"}, - {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, - {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "100", "100 GB"}, -#ifdef NDEBUG - {-1, DATA_UNITS_BYTE, "", ""}, -#endif + {0, "0 B"}, + {512, "512 B"}, + {1024*1024, "1.0 MB"}, + {1024*1024*1024, "1.0 GB"}, + {10LL*1024*1024*1024, "10.0 GB"}, + {99LL*1024*1024*1024, "99.0 GB"}, + {105LL*1024*1024*1024, "105 GB"}, + {105LL*1024*1024*1024 + 500LL*1024*1024, "105 GB"}, + {~(1LL<<63), "8192 PB"}, + + {99*1024 + 103, "99.1 kB"}, + {1024*1024 + 103, "1.0 MB"}, + {1024*1024 + 205 * 1024, "1.2 MB"}, + {1024*1024*1024 + (927 * 1024*1024), "1.9 GB"}, + {10LL*1024*1024*1024, "10.0 GB"}, + {100LL*1024*1024*1024, "100 GB"}, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { EXPECT_EQ(ASCIIToUTF16(cases[i].expected), - FormatBytes(cases[i].bytes, cases[i].units, false)); - EXPECT_EQ(ASCIIToUTF16(cases[i].expected_with_units), - FormatBytes(cases[i].bytes, cases[i].units, true)); + FormatBytesUnlocalized(cases[i].bytes)); } } - TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { static const struct { const char* str; diff --git a/chrome/browser/chromeos/cros/network_library.cc b/chrome/browser/chromeos/cros/network_library.cc index 03ad81b..2a4ac3c 100644 --- a/chrome/browser/chromeos/cros/network_library.cc +++ b/chrome/browser/chromeos/cros/network_library.cc @@ -28,6 +28,7 @@ #include "crypto/nss_util.h" // crypto::GetTPMTokenInfo() for 802.1X and VPN. #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" //////////////////////////////////////////////////////////////////////////////// // Implementation notes. @@ -1530,17 +1531,13 @@ string16 CellularDataPlan::GetPlanDesciption() const { case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: { return l10n_util::GetStringFUTF16( IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_DATA, - FormatBytes(plan_data_bytes, - GetByteDisplayUnits(plan_data_bytes), - true), + ui::FormatBytes(plan_data_bytes), base::TimeFormatFriendlyDate(plan_start_time)); } case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: { return l10n_util::GetStringFUTF16( IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_RECEIVED_FREE_DATA, - FormatBytes(plan_data_bytes, - GetByteDisplayUnits(plan_data_bytes), - true), + ui::FormatBytes(plan_data_bytes), base::TimeFormatFriendlyDate(plan_start_time)); default: break; @@ -1576,14 +1573,10 @@ string16 CellularDataPlan::GetDataRemainingDesciption() const { IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_UNLIMITED); } case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: { - return FormatBytes(remaining_bytes, - GetByteDisplayUnits(remaining_bytes), - true); + return ui::FormatBytes(remaining_bytes); } case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: { - return FormatBytes(remaining_bytes, - GetByteDisplayUnits(remaining_bytes), - true); + return ui::FormatBytes(remaining_bytes); } default: break; diff --git a/chrome/browser/diagnostics/recon_diagnostics.cc b/chrome/browser/diagnostics/recon_diagnostics.cc index 390d2aa..77bc6fb 100644 --- a/chrome/browser/diagnostics/recon_diagnostics.cc +++ b/chrome/browser/diagnostics/recon_diagnostics.cc @@ -20,6 +20,7 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_version_info.h" #include "content/common/json_value_serializer.h" +#include "ui/base/text/bytes_formatting.h" #if defined(OS_WIN) #include "base/win/windows_version.h" @@ -251,8 +252,7 @@ class PathTest : public DiagnosticTest { dir_or_file.LossyDisplayName()); return true; } - DataUnits units = GetByteDisplayUnits(dir_or_file_size); - string16 printable_size = FormatBytes(dir_or_file_size, units, true); + string16 printable_size = ui::FormatBytes(dir_or_file_size); if (path_info_.max_size > 0) { if (dir_or_file_size > path_info_.max_size) { @@ -299,8 +299,7 @@ class DiskSpaceTest : public DiagnosticTest { RecordFailure(ASCIIToUTF16("Unable to query free space")); return true; } - DataUnits units = GetByteDisplayUnits(disk_space); - string16 printable_size = FormatBytes(disk_space, units, true); + string16 printable_size = ui::FormatBytes(disk_space); if (disk_space < 80 * kOneMeg) { RecordFailure(ASCIIToUTF16("Low disk space : ") + printable_size); return true; diff --git a/chrome/browser/download/download_item_model.cc b/chrome/browser/download/download_item_model.cc index 9eae34f..f37cb1f 100644 --- a/chrome/browser/download/download_item_model.cc +++ b/chrome/browser/download/download_item_model.cc @@ -13,6 +13,7 @@ #include "chrome/common/time_format.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" using base::TimeDelta; @@ -31,8 +32,8 @@ string16 DownloadItemModel::GetStatusText() { int64 size = download_->received_bytes(); int64 total = download_->total_bytes(); - DataUnits amount_units = GetByteDisplayUnits(total); - const string16 simple_size = FormatBytes(size, amount_units, false); + ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); + string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); // In RTL locales, we render the text "size/total" in an RTL context. This // is problematic since a string such as "123/456 MB" is displayed @@ -40,7 +41,7 @@ string16 DownloadItemModel::GetStatusText() { // we mark the total string as an LTR string if the UI layout is // right-to-left so that the string "456 MB" is treated as an LTR run. string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( - FormatBytes(total, amount_units, true)); + ui::FormatBytesWithUnits(total, amount_units, true)); TimeDelta remaining; string16 simple_time; @@ -71,9 +72,9 @@ string16 DownloadItemModel::GetStatusText() { } else { if (simple_time.empty()) { // Instead of displaying "0 B" we keep the "Starting..." string. - status_text = (size == 0) ? - l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) : - FormatBytes(size, GetByteDisplayUnits(size), true); + status_text = (size == 0) + ? l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) + : ui::FormatBytes(size); } else { status_text = l10n_util::GetStringFUTF16( IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total, diff --git a/chrome/browser/download/download_manager_unittest.cc b/chrome/browser/download/download_manager_unittest.cc index 8b6fcfe..1b80445 100644 --- a/chrome/browser/download/download_manager_unittest.cc +++ b/chrome/browser/download/download_manager_unittest.cc @@ -33,6 +33,7 @@ #include "testing/gmock_mutant.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" class DownloadManagerTest : public testing::Test { public: @@ -447,10 +448,11 @@ TEST_F(DownloadManagerTest, DownloadInterruptTest) { EXPECT_FALSE(observer->was_opened()); EXPECT_FALSE(download->file_externally_removed()); EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); - DataUnits amount_units = GetByteDisplayUnits(kTestDataLen); - const string16 simple_size = FormatBytes(error_size, amount_units, false); + ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen); + string16 simple_size = + ui::FormatBytesWithUnits(error_size, amount_units, false); string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( - FormatBytes(kTestDataLen, amount_units, true)); + ui::FormatBytesWithUnits(kTestDataLen, amount_units, true)); EXPECT_EQ(download_item_model->GetStatusText(), l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED, simple_size, diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc index bce087b..83acdb7 100644 --- a/chrome/browser/download/download_util.cc +++ b/chrome/browser/download/download_util.cc @@ -51,6 +51,7 @@ #include "third_party/skia/include/core/SkShader.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +#include "ui/base/text/bytes_formatting.h" #include "ui/gfx/canvas_skia.h" #include "ui/gfx/image/image.h" #include "ui/gfx/rect.h" @@ -724,8 +725,7 @@ DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { string16 GetProgressStatusText(DownloadItem* download) { int64 total = download->total_bytes(); int64 size = download->received_bytes(); - DataUnits amount_units = GetByteDisplayUnits(size); - string16 received_size = FormatBytes(size, amount_units, true); + string16 received_size = ui::FormatBytes(size); string16 amount = received_size; // Adjust both strings for the locale direction since we don't yet know which @@ -733,8 +733,7 @@ string16 GetProgressStatusText(DownloadItem* download) { base::i18n::AdjustStringForLocaleDirection(&amount); if (total) { - amount_units = GetByteDisplayUnits(total); - string16 total_text = FormatBytes(total, amount_units, true); + string16 total_text = ui::FormatBytes(total); base::i18n::AdjustStringForLocaleDirection(&total_text); base::i18n::AdjustStringForLocaleDirection(&received_size); @@ -745,8 +744,7 @@ string16 GetProgressStatusText(DownloadItem* download) { amount.assign(received_size); } int64 current_speed = download->CurrentSpeed(); - amount_units = GetByteDisplayUnits(current_speed); - string16 speed_text = FormatSpeed(current_speed, amount_units, true); + string16 speed_text = ui::FormatSpeed(current_speed); base::i18n::AdjustStringForLocaleDirection(&speed_text); base::TimeDelta remaining; diff --git a/chrome/browser/task_manager/task_manager.cc b/chrome/browser/task_manager/task_manager.cc index 35c8e96..f568f01 100644 --- a/chrome/browser/task_manager/task_manager.cc +++ b/chrome/browser/task_manager/task_manager.cc @@ -36,6 +36,7 @@ #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +#include "ui/base/text/bytes_formatting.h" #include "unicode/coll.h" #if defined(OS_MACOSX) @@ -63,8 +64,8 @@ int ValueCompare(T value1, T value2) { string16 FormatStatsSize(const WebKit::WebCache::ResourceTypeStat& stat) { return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_CACHE_SIZE_CELL_TEXT, - FormatBytes(stat.size, DATA_UNITS_KIBIBYTE, false), - FormatBytes(stat.liveSize, DATA_UNITS_KIBIBYTE, false)); + ui::FormatBytesWithUnits(stat.size, ui::DATA_UNITS_KIBIBYTE, false), + ui::FormatBytesWithUnits(stat.liveSize, ui::DATA_UNITS_KIBIBYTE, false)); } } // namespace @@ -128,8 +129,7 @@ string16 TaskManagerModel::GetResourceNetworkUsage(int index) const { return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT); if (net_usage == 0) return ASCIIToUTF16("0"); - string16 net_byte = FormatSpeed(net_usage, GetByteDisplayUnits(net_usage), - true); + string16 net_byte = ui::FormatSpeed(net_usage); // Force number string to have LTR directionality. return base::i18n::GetDisplayStringInLTRDirectionality(net_byte); } @@ -237,12 +237,12 @@ string16 TaskManagerModel::GetResourceV8MemoryAllocatedSize( if (!resources_[index]->ReportsV8MemoryStats()) return l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT); return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_CACHE_SIZE_CELL_TEXT, - FormatBytes(resources_[index]->GetV8MemoryAllocated(), - DATA_UNITS_KIBIBYTE, - false), - FormatBytes(resources_[index]->GetV8MemoryUsed(), - DATA_UNITS_KIBIBYTE, - false)); + ui::FormatBytesWithUnits(resources_[index]->GetV8MemoryAllocated(), + ui::DATA_UNITS_KIBIBYTE, + false), + ui::FormatBytesWithUnits(resources_[index]->GetV8MemoryUsed(), + ui::DATA_UNITS_KIBIBYTE, + false)); } bool TaskManagerModel::IsResourceFirstInGroup(int index) const { @@ -489,9 +489,9 @@ string16 TaskManagerModel::GetMemCellText(int64 number) const { base::i18n::AdjustStringForLocaleDirection(&str); return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_MEM_CELL_TEXT, str); #else - // System expectation is to show "100 KB", "200 MB", etc. + // System expectation is to show "100 kB", "200 MB", etc. // TODO(thakis): Switch to metric units (as opposed to powers of two). - return FormatBytes(number, GetByteDisplayUnits(number), /*show_units=*/true); + return ui::FormatBytes(number); #endif } diff --git a/chrome/browser/ui/cocoa/content_settings/cookie_details.mm b/chrome/browser/ui/cocoa/content_settings/cookie_details.mm index 6b87412..8b9bd8e 100644 --- a/chrome/browser/ui/cocoa/content_settings/cookie_details.mm +++ b/chrome/browser/ui/cocoa/content_settings/cookie_details.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -9,6 +9,7 @@ #include "grit/generated_resources.h" #include "chrome/browser/cookies_tree_model.h" #include "ui/base/l10n/l10n_util_mac.h" +#include "ui/base/text/bytes_formatting.h" #include "webkit/appcache/appcache_service.h" #pragma mark Cocoa Cookie Details @@ -156,8 +157,8 @@ canEditExpiration_ = NO; databaseDescription_.reset([base::SysUTF8ToNSString( databaseInfo->description) retain]); - fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(databaseInfo->size, - GetByteDisplayUnits(databaseInfo->size), true)) retain]); + fileSize_.reset([base::SysUTF16ToNSString( + ui::FormatBytes(databaseInfo->size)) retain]); lastModified_.reset([base::SysUTF16ToNSString( base::TimeFormatFriendlyDateAndTime( databaseInfo->last_modified)) retain]); @@ -171,8 +172,8 @@ type_ = kCocoaCookieDetailsTypeTreeLocalStorage; canEditExpiration_ = NO; domain_.reset([base::SysUTF8ToNSString(storageInfo->origin) retain]); - fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(storageInfo->size, - GetByteDisplayUnits(storageInfo->size), true)) retain]); + fileSize_.reset( + [base::SysUTF16ToNSString(ui::FormatBytes(storageInfo->size)) retain]); lastModified_.reset([base::SysUTF16ToNSString( base::TimeFormatFriendlyDateAndTime( storageInfo->last_modified)) retain]); @@ -186,8 +187,8 @@ canEditExpiration_ = NO; manifestURL_.reset([base::SysUTF8ToNSString( appcacheInfo->manifest_url.spec()) retain]); - fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(appcacheInfo->size, - GetByteDisplayUnits(appcacheInfo->size), true)) retain]); + fileSize_.reset([base::SysUTF16ToNSString( + ui::FormatBytes(appcacheInfo->size)) retain]); created_.reset([base::SysUTF16ToNSString( base::TimeFormatFriendlyDateAndTime( appcacheInfo->creation_time)) retain]); @@ -209,8 +210,8 @@ domain_.reset([base::SysUTF8ToNSString(domain) retain]); databaseDescription_.reset( [base::SysUTF16ToNSString(databaseDescription) retain]); - fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(fileSize, - GetByteDisplayUnits(fileSize), true)) retain]); + fileSize_.reset( + [base::SysUTF16ToNSString(ui::FormatBytes(fileSize)) retain]); } return self; } @@ -243,8 +244,8 @@ type_ = kCocoaCookieDetailsTypeTreeIndexedDB; canEditExpiration_ = NO; domain_.reset([base::SysUTF8ToNSString(indexedDBInfo->origin) retain]); - fileSize_.reset([base::SysUTF16ToNSString(FormatBytes(indexedDBInfo->size, - GetByteDisplayUnits(indexedDBInfo->size), true)) retain]); + fileSize_.reset([base::SysUTF16ToNSString( + ui::FormatBytes(indexedDBInfo->size)) retain]); lastModified_.reset([base::SysUTF16ToNSString( base::TimeFormatFriendlyDateAndTime( indexedDBInfo->last_modified)) retain]); diff --git a/chrome/browser/ui/gtk/gtk_chrome_cookie_view.cc b/chrome/browser/ui/gtk/gtk_chrome_cookie_view.cc index a7bac57..e1844ba 100644 --- a/chrome/browser/ui/gtk/gtk_chrome_cookie_view.cc +++ b/chrome/browser/ui/gtk/gtk_chrome_cookie_view.cc @@ -9,6 +9,7 @@ #include "chrome/browser/ui/gtk/gtk_util.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" namespace { @@ -540,10 +541,7 @@ void gtk_chrome_cookie_view_display_database( gtk_entry_set_text(GTK_ENTRY(self->database_description_entry_), database_info.description.c_str()); gtk_entry_set_text(GTK_ENTRY(self->database_size_entry_), - UTF16ToUTF8(FormatBytes( - database_info.size, - GetByteDisplayUnits(database_info.size), - true)).c_str()); + UTF16ToUTF8(ui::FormatBytes(database_info.size)).c_str()); gtk_entry_set_text(GTK_ENTRY(self->database_last_modified_entry_), UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( database_info.last_modified)).c_str()); @@ -560,10 +558,8 @@ void gtk_chrome_cookie_view_display_local_storage( gtk_entry_set_text(GTK_ENTRY(self->local_storage_origin_entry_), local_storage_info.origin.c_str()); gtk_entry_set_text(GTK_ENTRY(self->local_storage_size_entry_), - UTF16ToUTF8(FormatBytes( - local_storage_info.size, - GetByteDisplayUnits(local_storage_info.size), - true)).c_str()); + UTF16ToUTF8(ui::FormatBytes( + local_storage_info.size)).c_str()); gtk_entry_set_text(GTK_ENTRY(self->local_storage_last_modified_entry_), UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( local_storage_info.last_modified)).c_str()); @@ -579,10 +575,7 @@ void gtk_chrome_cookie_view_display_app_cache( gtk_entry_set_text(GTK_ENTRY(self->appcache_manifest_entry_), info.manifest_url.spec().c_str()); gtk_entry_set_text(GTK_ENTRY(self->appcache_size_entry_), - UTF16ToUTF8(FormatBytes( - info.size, - GetByteDisplayUnits(info.size), - true)).c_str()); + UTF16ToUTF8(ui::FormatBytes(info.size)).c_str()); gtk_entry_set_text(GTK_ENTRY(self->appcache_created_entry_), UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( info.creation_time)).c_str()); @@ -601,10 +594,8 @@ void gtk_chrome_cookie_view_display_indexed_db( gtk_entry_set_text(GTK_ENTRY(self->indexed_db_origin_entry_), indexed_db_info.origin.c_str()); gtk_entry_set_text(GTK_ENTRY(self->indexed_db_size_entry_), - UTF16ToUTF8(FormatBytes( - indexed_db_info.size, - GetByteDisplayUnits(indexed_db_info.size), - true)).c_str()); + UTF16ToUTF8(ui::FormatBytes( + indexed_db_info.size)).c_str()); gtk_entry_set_text(GTK_ENTRY(self->indexed_db_last_modified_entry_), UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( indexed_db_info.last_modified)).c_str()); @@ -642,10 +633,7 @@ void gtk_chrome_cookie_view_display_database_accessed( gtk_entry_set_text(GTK_ENTRY(self->database_accessed_description_entry_), UTF16ToUTF8(display_name).c_str()); gtk_entry_set_text(GTK_ENTRY(self->database_accessed_size_entry_), - UTF16ToUTF8(FormatBytes( - estimated_size, - GetByteDisplayUnits(estimated_size), - true)).c_str()); + UTF16ToUTF8(ui::FormatBytes(estimated_size)).c_str()); SetDatabaseAccessedSensitivity(self, TRUE); } diff --git a/chrome/browser/ui/views/appcache_info_view.cc b/chrome/browser/ui/views/appcache_info_view.cc index 199d256..b4add97 100644 --- a/chrome/browser/ui/views/appcache_info_view.cc +++ b/chrome/browser/ui/views/appcache_info_view.cc @@ -8,6 +8,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "grit/generated_resources.h" +#include "ui/base/text/bytes_formatting.h" namespace { const int kInfoLabelIds[] = { @@ -27,7 +28,7 @@ void AppCacheInfoView::SetAppCacheInfo(const appcache::AppCacheInfo* info) { string16 manifest_url = UTF8ToUTF16(info->manifest_url.spec()); string16 size = - FormatBytes(info->size, GetByteDisplayUnits(info->size), true); + ui::FormatBytes(info->size); string16 creation_date = base::TimeFormatFriendlyDateAndTime(info->creation_time); string16 last_access_date = diff --git a/chrome/browser/ui/views/database_info_view.cc b/chrome/browser/ui/views/database_info_view.cc index 0f0d6e4..8f1e7c2 100644 --- a/chrome/browser/ui/views/database_info_view.cc +++ b/chrome/browser/ui/views/database_info_view.cc @@ -10,6 +10,7 @@ #include "base/utf_string_conversions.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" #include "ui/gfx/color_utils.h" #include "views/controls/label.h" #include "views/controls/textfield/textfield.h" @@ -39,10 +40,7 @@ void DatabaseInfoView::SetDatabaseInfo( l10n_util::GetStringUTF16(IDS_COOKIES_WEB_DATABASE_UNNAMED_NAME)) : UTF8ToWide(database_info.database_name)); description_value_field_->SetText(UTF8ToWide(database_info.description)); - size_value_field_->SetText( - FormatBytes(database_info.size, - GetByteDisplayUnits(database_info.size), - true)); + size_value_field_->SetText(ui::FormatBytes(database_info.size)); last_modified_value_field_->SetText( base::TimeFormatFriendlyDateAndTime(database_info.last_modified)); EnableDatabaseDisplay(true); diff --git a/chrome/browser/ui/views/database_open_info_view.cc b/chrome/browser/ui/views/database_open_info_view.cc index 7bdbbef..4d23a95 100644 --- a/chrome/browser/ui/views/database_open_info_view.cc +++ b/chrome/browser/ui/views/database_open_info_view.cc @@ -7,6 +7,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "grit/generated_resources.h" +#include "ui/base/text/bytes_formatting.h" namespace { const int kInfoLabelIds[] = { @@ -29,9 +30,7 @@ void DatabaseOpenInfoView::SetFields(const std::string& host, const string16& display_name, unsigned long estimated_size) { string16 url = UTF8ToUTF16(host); - string16 size = FormatBytes(estimated_size, - GetByteDisplayUnits(estimated_size), - true); + string16 size = ui::FormatBytes(estimated_size); int row = 0; SetValue(row++, url); SetValue(row++, database_name); diff --git a/chrome/browser/ui/views/indexed_db_info_view.cc b/chrome/browser/ui/views/indexed_db_info_view.cc index 34fd9eb6..d033dca 100644 --- a/chrome/browser/ui/views/indexed_db_info_view.cc +++ b/chrome/browser/ui/views/indexed_db_info_view.cc @@ -10,6 +10,7 @@ #include "base/utf_string_conversions.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" #include "ui/gfx/color_utils.h" #include "views/controls/label.h" #include "views/controls/textfield/textfield.h" @@ -34,10 +35,7 @@ IndexedDBInfoView::~IndexedDBInfoView() { void IndexedDBInfoView::SetIndexedDBInfo( const BrowsingDataIndexedDBHelper::IndexedDBInfo& indexed_db_info) { origin_value_field_->SetText(UTF8ToWide(indexed_db_info.origin)); - size_value_field_->SetText( - FormatBytes(indexed_db_info.size, - GetByteDisplayUnits(indexed_db_info.size), - true)); + size_value_field_->SetText(ui::FormatBytes(indexed_db_info.size)); last_modified_value_field_->SetText( base::TimeFormatFriendlyDateAndTime(indexed_db_info.last_modified)); EnableIndexedDBDisplay(true); diff --git a/chrome/browser/ui/views/local_storage_info_view.cc b/chrome/browser/ui/views/local_storage_info_view.cc index 894d7c9..8bbc580 100644 --- a/chrome/browser/ui/views/local_storage_info_view.cc +++ b/chrome/browser/ui/views/local_storage_info_view.cc @@ -10,6 +10,7 @@ #include "base/utf_string_conversions.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" #include "ui/gfx/color_utils.h" #include "views/controls/label.h" #include "views/controls/textfield/textfield.h" @@ -35,10 +36,7 @@ void LocalStorageInfoView::SetLocalStorageInfo( const BrowsingDataLocalStorageHelper::LocalStorageInfo& local_storage_info) { origin_value_field_->SetText(UTF8ToWide(local_storage_info.origin)); - size_value_field_->SetText( - FormatBytes(local_storage_info.size, - GetByteDisplayUnits(local_storage_info.size), - true)); + size_value_field_->SetText(ui::FormatBytes(local_storage_info.size)); last_modified_value_field_->SetText( base::TimeFormatFriendlyDateAndTime(local_storage_info.last_modified)); EnableLocalStorageDisplay(true); diff --git a/chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.cc b/chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.cc index 61ef476..dabfc2c 100644 --- a/chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.cc +++ b/chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.cc @@ -20,6 +20,7 @@ #include "grit/locale_settings.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" +#include "ui/base/text/bytes_formatting.h" namespace imageburner { @@ -596,8 +597,7 @@ void WebUIHandler::GetProgressTimeLeftText(int message_id, } void WebUIHandler::GetDataSizeText(int64 size, string16* size_text) { - DataUnits size_units = GetByteDisplayUnits(size); - *size_text = FormatBytes(size, size_units, true); + *size_text = ui::FormatBytes(size); base::i18n::AdjustStringForLocaleDirection(size_text); } diff --git a/chrome/browser/ui/webui/cookies_tree_model_util.cc b/chrome/browser/ui/webui/cookies_tree_model_util.cc index 438aa2b..7c8a566 100644 --- a/chrome/browser/ui/webui/cookies_tree_model_util.cc +++ b/chrome/browser/ui/webui/cookies_tree_model_util.cc @@ -12,6 +12,7 @@ #include "chrome/browser/cookies_tree_model.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/text/bytes_formatting.h" namespace { @@ -116,10 +117,7 @@ void GetCookieTreeNodeDictionary(const CookieTreeNode& node, l10n_util::GetStringUTF8(IDS_COOKIES_WEB_DATABASE_UNNAMED_NAME) : database_info.database_name); dict->SetString(kKeyDesc, database_info.description); - dict->SetString(kKeySize, - FormatBytes(database_info.size, - GetByteDisplayUnits(database_info.size), - true)); + dict->SetString(kKeySize, ui::FormatBytes(database_info.size)); dict->SetString(kKeyModified, UTF16ToUTF8( base::TimeFormatFriendlyDateAndTime(database_info.last_modified))); @@ -133,10 +131,7 @@ void GetCookieTreeNodeDictionary(const CookieTreeNode& node, local_storage_info = *node.GetDetailedInfo().local_storage_info; dict->SetString(kKeyOrigin, local_storage_info.origin); - dict->SetString(kKeySize, - FormatBytes(local_storage_info.size, - GetByteDisplayUnits(local_storage_info.size), - true)); + dict->SetString(kKeySize, ui::FormatBytes(local_storage_info.size)); dict->SetString(kKeyModified, UTF16ToUTF8( base::TimeFormatFriendlyDateAndTime( local_storage_info.last_modified))); @@ -151,10 +146,7 @@ void GetCookieTreeNodeDictionary(const CookieTreeNode& node, *node.GetDetailedInfo().appcache_info; dict->SetString(kKeyManifest, appcache_info.manifest_url.spec()); - dict->SetString(kKeySize, - FormatBytes(appcache_info.size, - GetByteDisplayUnits(appcache_info.size), - true)); + dict->SetString(kKeySize, ui::FormatBytes(appcache_info.size)); dict->SetString(kKeyCreated, UTF16ToUTF8( base::TimeFormatFriendlyDateAndTime(appcache_info.creation_time))); dict->SetString(kKeyAccessed, UTF16ToUTF8( @@ -170,10 +162,7 @@ void GetCookieTreeNodeDictionary(const CookieTreeNode& node, *node.GetDetailedInfo().indexed_db_info; dict->SetString(kKeyOrigin, indexed_db_info.origin); - dict->SetString(kKeySize, - FormatBytes(indexed_db_info.size, - GetByteDisplayUnits(indexed_db_info.size), - true)); + dict->SetString(kKeySize, ui::FormatBytes(indexed_db_info.size)); dict->SetString(kKeyModified, UTF16ToUTF8( base::TimeFormatFriendlyDateAndTime(indexed_db_info.last_modified))); @@ -189,20 +178,14 @@ void GetCookieTreeNodeDictionary(const CookieTreeNode& node, dict->SetString(kKeyOrigin, file_system_info.origin.spec()); dict->SetString(kKeyPersistent, file_system_info.has_persistent ? - UTF16ToUTF8(FormatBytes( - file_system_info.usage_persistent, - GetByteDisplayUnits( - file_system_info.usage_persistent), - true)) : + UTF16ToUTF8(ui::FormatBytes( + file_system_info.usage_persistent)) : l10n_util::GetStringUTF8( IDS_COOKIES_FILE_SYSTEM_USAGE_NONE)); dict->SetString(kKeyTemporary, file_system_info.has_temporary ? - UTF16ToUTF8(FormatBytes( - file_system_info.usage_temporary, - GetByteDisplayUnits( - file_system_info.usage_temporary), - true)) : + UTF16ToUTF8(ui::FormatBytes( + file_system_info.usage_temporary)) : l10n_util::GetStringUTF8( IDS_COOKIES_FILE_SYSTEM_USAGE_NONE)); break; diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 6b706d1..d2b742b 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -1225,10 +1225,7 @@ std::string GetDirectoryListingEntry(const string16& name, result.append(",0,"); } - base::JsonDoubleQuote( - FormatBytes(size, GetByteDisplayUnits(size), true), - true, - &result); + base::JsonDoubleQuote(FormatBytesUnlocalized(size), true, &result); result.append(","); diff --git a/ui/base/strings/app_strings.grd b/ui/base/strings/app_strings.grd index 0f8d128..6ca00e2 100644 --- a/ui/base/strings/app_strings.grd +++ b/ui/base/strings/app_strings.grd @@ -379,6 +379,44 @@ need to be translated for each locale.--> <message name="IDS_APP_SHIFT_MODIFIER" desc="Shift key shortcut modifier"> Shift+<ph name="KEY_COMBO_NAME">$1<ex>C</ex></ph> </message> + + <!-- Byte size units --> + <message name="IDS_APP_BYTES" desc="Units tag indicating a quantity of bytes"> + <ph name="QUANTITY">$1<ex>42</ex></ph> B + </message> + <message name="IDS_APP_KIBIBYTES" desc="Units tag indicating a quantity of kilobytes"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> kB + </message> + <message name="IDS_APP_MEBIBYTES" desc="Units tag indicating a quantity of megabytes"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> MB + </message> + <message name="IDS_APP_GIBIBYTES" desc="Units tag indicating a quantity of gigabytes"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> GB + </message> + <message name="IDS_APP_TEBIBYTES" desc="Units tag indicating a quantity of terabytes"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> TB + </message> + <message name="IDS_APP_PEBIBYTES" desc="Units tag indicating a quantity of petabytes"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> PB + </message> + <message name="IDS_APP_BYTES_PER_SECOND" desc="Units tag indicating a speed of bytes/second"> + <ph name="QUANTITY">$1<ex>42</ex></ph> B/s + </message> + <message name="IDS_APP_KIBIBYTES_PER_SECOND" desc="Units tag indicating a speed of kilobytes/second"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> kB/s + </message> + <message name="IDS_APP_MEBIBYTES_PER_SECOND" desc="Units tag indicating a speed of megabytes/second"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> MB/s + </message> + <message name="IDS_APP_GIBIBYTES_PER_SECOND" desc="Units tag indicating a speed of gigabytes/second"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> GB/s + </message> + <message name="IDS_APP_TEBIBYTES_PER_SECOND" desc="Units tag indicating a speed of terabytes/second"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> TB/s + </message> + <message name="IDS_APP_PEBIBYTES_PER_SECOND" desc="Units tag indicating a speed of petabytes/second"> + <ph name="QUANTITY">$1<ex>42.0</ex></ph> PB/s + </message> </messages> </release> </grit> diff --git a/ui/base/text/bytes_formatting.cc b/ui/base/text/bytes_formatting.cc new file mode 100644 index 0000000..6399b3c --- /dev/null +++ b/ui/base/text/bytes_formatting.cc @@ -0,0 +1,110 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/base/text/bytes_formatting.h" + +#include "base/i18n/number_formatting.h" +#include "base/logging.h" +#include "base/string_util.h" +#include "base/utf_string_conversions.h" +#include "grit/app_strings.h" +#include "ui/base/l10n/l10n_util.h" + +namespace ui { + +namespace { + +// Byte suffix string constants. These both must match the DataUnits enum. +const int kByteStrings[] = { + IDS_APP_BYTES, + IDS_APP_KIBIBYTES, + IDS_APP_MEBIBYTES, + IDS_APP_GIBIBYTES, + IDS_APP_TEBIBYTES, + IDS_APP_PEBIBYTES +}; + +const int kSpeedStrings[] = { + IDS_APP_BYTES_PER_SECOND, + IDS_APP_KIBIBYTES_PER_SECOND, + IDS_APP_MEBIBYTES_PER_SECOND, + IDS_APP_GIBIBYTES_PER_SECOND, + IDS_APP_TEBIBYTES_PER_SECOND, + IDS_APP_PEBIBYTES_PER_SECOND +}; + +string16 FormatBytesInternal(int64 bytes, + DataUnits units, + bool show_units, + const int* const suffix) { + DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE); + if (bytes < 0) { + NOTREACHED() << "Negative bytes value"; + return string16(); + } + + // Put the quantity in the right units. + double unit_amount = static_cast<double>(bytes); + for (int i = 0; i < units; ++i) + unit_amount /= 1024.0; + + int fractional_digits = 0; + if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) + fractional_digits = 1; + + string16 result = base::FormatDouble(unit_amount, fractional_digits); + + if (show_units) + result = l10n_util::GetStringFUTF16(suffix[units], result); + + return result; +} + +} // namespace + +DataUnits GetByteDisplayUnits(int64 bytes) { + // The byte thresholds at which we display amounts. A byte count is displayed + // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. + // This must match the DataUnits enum. + static const int64 kUnitThresholds[] = { + 0, // DATA_UNITS_BYTE, + 3 * (1LL << 10), // DATA_UNITS_KIBIBYTE, + 2 * (1LL << 20), // DATA_UNITS_MEBIBYTE, + 1LL << 30, // DATA_UNITS_GIBIBYTE, + 1LL << 40, // DATA_UNITS_TEBIBYTE, + 1LL << 50 // DATA_UNITS_PEBIBYTE, + }; + + if (bytes < 0) { + NOTREACHED() << "Negative bytes value"; + return DATA_UNITS_BYTE; + } + + int unit_index = arraysize(kUnitThresholds); + while (--unit_index > 0) { + if (bytes >= kUnitThresholds[unit_index]) + break; + } + + DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_PEBIBYTE); + return DataUnits(unit_index); +} + +string16 FormatBytesWithUnits(int64 bytes, DataUnits units, bool show_units) { + return FormatBytesInternal(bytes, units, show_units, kByteStrings); +} + +string16 FormatSpeedWithUnits(int64 bytes, DataUnits units, bool show_units) { + return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); +} + +string16 FormatBytes(int64 bytes) { + return FormatBytesWithUnits(bytes, GetByteDisplayUnits(bytes), true); +} + +string16 FormatSpeed(int64 bytes) { + return FormatSpeedWithUnits(bytes, GetByteDisplayUnits(bytes), true); +} + +} // namespace ui diff --git a/ui/base/text/bytes_formatting.h b/ui/base/text/bytes_formatting.h new file mode 100644 index 0000000..aef8f01 --- /dev/null +++ b/ui/base/text/bytes_formatting.h @@ -0,0 +1,55 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_BASE_TEXT_BYTES_FORMATTING_H_ +#define UI_BASE_TEXT_BYTES_FORMATTING_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/string16.h" + +namespace ui { + +// Simple API ------------------------------------------------------------------ + +// Simple call to return a byte quantity as a string in human-readable format. +// Ex: FormatBytes(512) => "512 B" +// Ex: FormatBytes(101479) => "99.1 kB" +string16 FormatBytes(int64 bytes); + +// Simple call to return a speed as a string in human-readable format. +// Ex: FormatSpeed(512) => "512 B/s" +// Ex: FormatSpeed(101479) => "99.1 kB/s" +string16 FormatSpeed(int64 bytes); + +// Less-Simple API ------------------------------------------------------------- + +enum DataUnits { + DATA_UNITS_BYTE = 0, + DATA_UNITS_KIBIBYTE, + DATA_UNITS_MEBIBYTE, + DATA_UNITS_GIBIBYTE, + DATA_UNITS_TEBIBYTE, + DATA_UNITS_PEBIBYTE +}; + +// Return the unit type that is appropriate for displaying the amount of bytes +// passed in. Most of the time, an explicit call to this isn't necessary; just +// use FormatBytes()/FormatSpeed() above. +DataUnits GetByteDisplayUnits(int64 bytes); + +// Return a byte quantity as a string in human-readable format with an optional +// unit suffix. Specify in the |units| argument the units to be used. +// Ex: FormatBytes(512, DATA_UNITS_KIBIBYTE, true) => "0.5 kB" +// Ex: FormatBytes(10*1024, DATA_UNITS_MEBIBYTE, false) => "0.1" +string16 FormatBytesWithUnits(int64 bytes, DataUnits units, bool show_units); + +// As above, but with "/s" units for speed values. +// Ex: FormatSpeed(512, DATA_UNITS_KIBIBYTE, true) => "0.5 kB/s" +// Ex: FormatSpeed(10*1024, DATA_UNITS_MEBIBYTE, false) => "0.1" +string16 FormatSpeedWithUnits(int64 bytes, DataUnits units, bool show_units); + +} // namespace ui + +#endif // UI_BASE_TEXT_BYTES_FORMATTING_H_ diff --git a/ui/base/text/bytes_formatting_unittest.cc b/ui/base/text/bytes_formatting_unittest.cc new file mode 100644 index 0000000..a214259 --- /dev/null +++ b/ui/base/text/bytes_formatting_unittest.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/base/text/bytes_formatting.h" + +namespace ui { + +TEST(BytesFormattingTest, GetByteDisplayUnits) { + static const struct { + int64 bytes; + DataUnits expected; + } cases[] = { + {0, DATA_UNITS_BYTE}, + {512, DATA_UNITS_BYTE}, + {10*1024, DATA_UNITS_KIBIBYTE}, + {10*1024*1024, DATA_UNITS_MEBIBYTE}, + {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE}, + {10LL*1024*1024*1024*1024, DATA_UNITS_TEBIBYTE}, + {~(1LL<<63), DATA_UNITS_PEBIBYTE}, +#ifdef NDEBUG + {-1, DATA_UNITS_BYTE}, +#endif + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) + EXPECT_EQ(cases[i].expected, GetByteDisplayUnits(cases[i].bytes)); +} + +TEST(BytesFormattingTest, FormatBytes) { + static const struct { + int64 bytes; + DataUnits units; + const char* expected; + const char* expected_with_units; + } cases[] = { + // Expected behavior: we show one post-decimal digit when we have + // under two pre-decimal digits, except in cases where it makes no + // sense (zero or bytes). + // Since we switch units once we cross the 1000 mark, this keeps + // the display of file sizes or bytes consistently around three + // digits. + {0, DATA_UNITS_BYTE, "0", "0 B"}, + {512, DATA_UNITS_BYTE, "512", "512 B"}, + {512, DATA_UNITS_KIBIBYTE, "0.5", "0.5 kB"}, + {1024*1024, DATA_UNITS_KIBIBYTE, "1,024", "1,024 kB"}, + {1024*1024, DATA_UNITS_MEBIBYTE, "1.0", "1.0 MB"}, + {1024*1024*1024, DATA_UNITS_GIBIBYTE, "1.0", "1.0 GB"}, + {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, + {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "99.0", "99.0 GB"}, + {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "105", "105 GB"}, + {105LL*1024*1024*1024 + 500LL*1024*1024, DATA_UNITS_GIBIBYTE, + "105", "105 GB"}, + {~(1LL<<63), DATA_UNITS_GIBIBYTE, "8,589,934,592", "8,589,934,592 GB"}, + {~(1LL<<63), DATA_UNITS_PEBIBYTE, "8,192", "8,192 PB"}, + + {99*1024 + 103, DATA_UNITS_KIBIBYTE, "99.1", "99.1 kB"}, + {1024*1024 + 103, DATA_UNITS_KIBIBYTE, "1,024", "1,024 kB"}, + {1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, "1.2", "1.2 MB"}, + {1024*1024*1024 + (927 * 1024*1024), DATA_UNITS_GIBIBYTE, + "1.9", "1.9 GB"}, + {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "10.0", "10.0 GB"}, + {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, "100", "100 GB"}, +#ifdef NDEBUG + {-1, DATA_UNITS_BYTE, "", ""}, +#endif + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + EXPECT_EQ(ASCIIToUTF16(cases[i].expected), + FormatBytesWithUnits(cases[i].bytes, cases[i].units, false)); + EXPECT_EQ(ASCIIToUTF16(cases[i].expected_with_units), + FormatBytesWithUnits(cases[i].bytes, cases[i].units, true)); + } +} + +} // namespace ui diff --git a/ui/ui_base.gypi b/ui/ui_base.gypi index 115ea7f..b602b16 100644 --- a/ui/ui_base.gypi +++ b/ui/ui_base.gypi @@ -136,6 +136,8 @@ 'base/resource/resource_bundle_mac.mm', 'base/resource/resource_bundle_posix.cc', 'base/resource/resource_bundle_win.cc', + 'base/text/bytes_formatting.cc', + 'base/text/bytes_formatting.h', 'base/text/text_elider.cc', 'base/text/text_elider.h', 'base/theme_provider.cc', diff --git a/ui/ui_unittests.gypi b/ui/ui_unittests.gypi index 3e976b4..8ea6cb2 100644 --- a/ui/ui_unittests.gypi +++ b/ui/ui_unittests.gypi @@ -32,6 +32,7 @@ 'base/range/range_unittest.mm', 'base/resource/data_pack_unittest.cc', 'base/resource/resource_bundle_unittest.cc', + 'base/text/bytes_formatting_unittest.cc', 'base/test/data/resource.h', 'base/text/text_elider_unittest.cc', 'gfx/blit_unittest.cc', diff --git a/webkit/appcache/view_appcache_internals_job.cc b/webkit/appcache/view_appcache_internals_job.cc index 2f61ce9..7666a46 100644 --- a/webkit/appcache/view_appcache_internals_job.cc +++ b/webkit/appcache/view_appcache_internals_job.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -116,8 +116,7 @@ void AddHTMLFromAppCacheToOutput( out->append("<ul>"); AddLiTag(kSize, - UTF16ToUTF8(FormatBytes( - info->size, GetByteDisplayUnits(info->size), true)), + UTF16ToUTF8(FormatBytesUnlocalized(info->size)), out); AddLiTag(kCreationTime, UTF16ToUTF8(TimeFormatFriendlyDateAndTime(info->creation_time)), |