Chromium Code Reviews| Index: ui/base/text/bytes_formatting.cc |
| diff --git a/ui/base/text/bytes_formatting.cc b/ui/base/text/bytes_formatting.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7f058ee14d815f51a20d38974a697981436e9858 |
| --- /dev/null |
| +++ b/ui/base/text/bytes_formatting.cc |
| @@ -0,0 +1,105 @@ |
| +// 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_KILOBYTES, |
| + IDS_APP_MEGABYTES, |
| + IDS_APP_GIGABYTES, |
| + IDS_APP_TERABYTES, |
| + IDS_APP_PETABYTES |
| +}; |
| + |
| +const int kSpeedStrings[] = { |
| + IDS_APP_BYTES_PER_SECOND, |
| + IDS_APP_KILOBYTES_PER_SECOND, |
| + IDS_APP_MEGABYTES_PER_SECOND, |
| + IDS_APP_GIGABYTES_PER_SECOND, |
| + IDS_APP_TERABYTES_PER_SECOND, |
| + IDS_APP_PETABYTES_PER_SECOND |
| +}; |
| + |
| +string16 FormatBytesInternal(int64 bytes, |
| + DataUnits units, |
| + bool show_units, |
| + const int* const suffix) { |
| + if (bytes < 0) { |
| + NOTREACHED() << "Negative bytes value"; |
| + return string16(); |
| + } |
| + |
| + if (units == DATA_UNITS_NATURAL) |
| + units = GetByteDisplayUnits(bytes); |
| + DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE); |
| + |
| + // 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); |
|
jungshik at Google
2011/06/22 00:35:01
FYI: When plural format is used later (not now) fo
Avi (use Gerrit)
2011/06/22 16:48:25
Moving back to "B" for now will fix this. BTW, "by
|
| + |
| + 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 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); |
| +} |
| + |
| +} // namespace ui |