Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "ui/base/text/bytes_formatting.h" | |
| 6 | |
| 7 #include "base/i18n/number_formatting.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/string_util.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "grit/app_strings.h" | |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 13 | |
| 14 namespace ui { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Byte suffix string constants. These both must match the DataUnits enum. | |
| 19 const int kByteStrings[] = { | |
| 20 IDS_APP_BYTES, | |
| 21 IDS_APP_KILOBYTES, | |
| 22 IDS_APP_MEGABYTES, | |
| 23 IDS_APP_GIGABYTES, | |
| 24 IDS_APP_TERABYTES, | |
| 25 IDS_APP_PETABYTES | |
| 26 }; | |
| 27 | |
| 28 const int kSpeedStrings[] = { | |
| 29 IDS_APP_BYTES_PER_SECOND, | |
| 30 IDS_APP_KILOBYTES_PER_SECOND, | |
| 31 IDS_APP_MEGABYTES_PER_SECOND, | |
| 32 IDS_APP_GIGABYTES_PER_SECOND, | |
| 33 IDS_APP_TERABYTES_PER_SECOND, | |
| 34 IDS_APP_PETABYTES_PER_SECOND | |
| 35 }; | |
| 36 | |
| 37 string16 FormatBytesInternal(int64 bytes, | |
| 38 DataUnits units, | |
| 39 bool show_units, | |
| 40 const int* const suffix) { | |
| 41 if (bytes < 0) { | |
| 42 NOTREACHED() << "Negative bytes value"; | |
| 43 return string16(); | |
| 44 } | |
| 45 | |
| 46 if (units == DATA_UNITS_NATURAL) | |
| 47 units = GetByteDisplayUnits(bytes); | |
| 48 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE); | |
| 49 | |
| 50 // Put the quantity in the right units. | |
| 51 double unit_amount = static_cast<double>(bytes); | |
| 52 for (int i = 0; i < units; ++i) | |
| 53 unit_amount /= 1024.0; | |
| 54 | |
| 55 int fractional_digits = 0; | |
| 56 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) | |
| 57 fractional_digits = 1; | |
| 58 | |
| 59 string16 result = base::FormatDouble(unit_amount, fractional_digits); | |
| 60 | |
| 61 if (show_units) | |
| 62 result = l10n_util::GetStringFUTF16(suffix[units], result); | |
| 63 | |
| 64 return result; | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 DataUnits GetByteDisplayUnits(int64 bytes) { | |
| 70 // The byte thresholds at which we display amounts. A byte count is displayed | |
| 71 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. | |
| 72 // This must match the DataUnits enum. | |
| 73 static const int64 kUnitThresholds[] = { | |
| 74 0, // DATA_UNITS_BYTE, | |
| 75 3*1024, // DATA_UNITS_KIBIBYTE, | |
| 76 2*1024*1024, // DATA_UNITS_MEBIBYTE, | |
| 77 1024*1024*1024, // DATA_UNITS_GIBIBYTE, | |
| 78 1024LL*1024*1024*1024, // DATA_UNITS_TEBIBYTE, | |
| 79 1024LL*1024*1024*1024*1024 // DATA_UNITS_PEBIBYTE, | |
|
Evan Stade
2011/06/21 03:18:41
spaces around operators (also you might do 1 << 20
Avi (use Gerrit)
2011/06/21 14:30:19
Done.
| |
| 80 }; | |
| 81 | |
| 82 if (bytes < 0) { | |
| 83 NOTREACHED() << "Negative bytes value"; | |
| 84 return DATA_UNITS_BYTE; | |
| 85 } | |
| 86 | |
| 87 int unit_index = arraysize(kUnitThresholds); | |
| 88 while (--unit_index > 0) { | |
| 89 if (bytes >= kUnitThresholds[unit_index]) | |
| 90 break; | |
| 91 } | |
| 92 | |
| 93 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_PEBIBYTE); | |
| 94 return DataUnits(unit_index); | |
| 95 } | |
| 96 | |
| 97 string16 FormatBytes(int64 bytes, DataUnits units, bool show_units) { | |
| 98 return FormatBytesInternal(bytes, units, show_units, kByteStrings); | |
| 99 } | |
| 100 | |
| 101 string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units) { | |
| 102 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); | |
| 103 } | |
| 104 | |
| 105 } // namespace ui | |
| OLD | NEW |