| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/text/bytes_formatting.h" | 5 #include "ui/base/text/bytes_formatting.h" |
| 6 | 6 |
| 7 #include "base/i18n/number_formatting.h" | 7 #include "base/i18n/number_formatting.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 const int kSpeedStrings[] = { | 28 const int kSpeedStrings[] = { |
| 29 IDS_APP_BYTES_PER_SECOND, | 29 IDS_APP_BYTES_PER_SECOND, |
| 30 IDS_APP_KIBIBYTES_PER_SECOND, | 30 IDS_APP_KIBIBYTES_PER_SECOND, |
| 31 IDS_APP_MEBIBYTES_PER_SECOND, | 31 IDS_APP_MEBIBYTES_PER_SECOND, |
| 32 IDS_APP_GIBIBYTES_PER_SECOND, | 32 IDS_APP_GIBIBYTES_PER_SECOND, |
| 33 IDS_APP_TEBIBYTES_PER_SECOND, | 33 IDS_APP_TEBIBYTES_PER_SECOND, |
| 34 IDS_APP_PEBIBYTES_PER_SECOND | 34 IDS_APP_PEBIBYTES_PER_SECOND |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 string16 FormatBytesInternal(int64 bytes, | 37 base::string16 FormatBytesInternal(int64 bytes, |
| 38 DataUnits units, | 38 DataUnits units, |
| 39 bool show_units, | 39 bool show_units, |
| 40 const int* const suffix) { | 40 const int* const suffix) { |
| 41 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE); | 41 DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_PEBIBYTE); |
| 42 if (bytes < 0) { | 42 if (bytes < 0) { |
| 43 NOTREACHED() << "Negative bytes value"; | 43 NOTREACHED() << "Negative bytes value"; |
| 44 return string16(); | 44 return base::string16(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Put the quantity in the right units. | 47 // Put the quantity in the right units. |
| 48 double unit_amount = static_cast<double>(bytes); | 48 double unit_amount = static_cast<double>(bytes); |
| 49 for (int i = 0; i < units; ++i) | 49 for (int i = 0; i < units; ++i) |
| 50 unit_amount /= 1024.0; | 50 unit_amount /= 1024.0; |
| 51 | 51 |
| 52 int fractional_digits = 0; | 52 int fractional_digits = 0; |
| 53 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) | 53 if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100) |
| 54 fractional_digits = 1; | 54 fractional_digits = 1; |
| 55 | 55 |
| 56 string16 result = base::FormatDouble(unit_amount, fractional_digits); | 56 base::string16 result = base::FormatDouble(unit_amount, fractional_digits); |
| 57 | 57 |
| 58 if (show_units) | 58 if (show_units) |
| 59 result = l10n_util::GetStringFUTF16(suffix[units], result); | 59 result = l10n_util::GetStringFUTF16(suffix[units], result); |
| 60 | 60 |
| 61 return result; | 61 return result; |
| 62 } | 62 } |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 DataUnits GetByteDisplayUnits(int64 bytes) { | 66 DataUnits GetByteDisplayUnits(int64 bytes) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 int unit_index = arraysize(kUnitThresholds); | 84 int unit_index = arraysize(kUnitThresholds); |
| 85 while (--unit_index > 0) { | 85 while (--unit_index > 0) { |
| 86 if (bytes >= kUnitThresholds[unit_index]) | 86 if (bytes >= kUnitThresholds[unit_index]) |
| 87 break; | 87 break; |
| 88 } | 88 } |
| 89 | 89 |
| 90 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_PEBIBYTE); | 90 DCHECK(unit_index >= DATA_UNITS_BYTE && unit_index <= DATA_UNITS_PEBIBYTE); |
| 91 return DataUnits(unit_index); | 91 return DataUnits(unit_index); |
| 92 } | 92 } |
| 93 | 93 |
| 94 string16 FormatBytesWithUnits(int64 bytes, DataUnits units, bool show_units) { | 94 base::string16 FormatBytesWithUnits(int64 bytes, |
| 95 DataUnits units, |
| 96 bool show_units) { |
| 95 return FormatBytesInternal(bytes, units, show_units, kByteStrings); | 97 return FormatBytesInternal(bytes, units, show_units, kByteStrings); |
| 96 } | 98 } |
| 97 | 99 |
| 98 string16 FormatSpeedWithUnits(int64 bytes, DataUnits units, bool show_units) { | 100 base::string16 FormatSpeedWithUnits(int64 bytes, |
| 101 DataUnits units, |
| 102 bool show_units) { |
| 99 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); | 103 return FormatBytesInternal(bytes, units, show_units, kSpeedStrings); |
| 100 } | 104 } |
| 101 | 105 |
| 102 string16 FormatBytes(int64 bytes) { | 106 base::string16 FormatBytes(int64 bytes) { |
| 103 return FormatBytesWithUnits(bytes, GetByteDisplayUnits(bytes), true); | 107 return FormatBytesWithUnits(bytes, GetByteDisplayUnits(bytes), true); |
| 104 } | 108 } |
| 105 | 109 |
| 106 string16 FormatSpeed(int64 bytes) { | 110 base::string16 FormatSpeed(int64 bytes) { |
| 107 return FormatSpeedWithUnits(bytes, GetByteDisplayUnits(bytes), true); | 111 return FormatSpeedWithUnits(bytes, GetByteDisplayUnits(bytes), true); |
| 108 } | 112 } |
| 109 | 113 |
| 110 } // namespace ui | 114 } // namespace ui |
| OLD | NEW |