summaryrefslogtreecommitdiffstats
path: root/base/string_util.cc
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-22 20:40:22 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-22 20:40:22 +0000
commit7a3b263af6356885628add7a8843a40dbee5b8ab (patch)
treeeff6140c44d29b6904b90366df65161364ea555f /base/string_util.cc
parentefaba4815ff6786082583393ad06f8c22ef11fa0 (diff)
downloadchromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.zip
chromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.tar.gz
chromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.tar.bz2
Localize strings, speeds.
BUG=86527 TEST=run in non-English. For European languages, during a download the decimal separators should be commas (e.g. "0,0 MB"). (The speeds are in strings files and might take a little time to run through the translation machinery.) Review URL: http://codereview.chromium.org/7189076 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90092 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r--base/string_util.cc94
1 files changed, 22 insertions, 72 deletions
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>