summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-18 21:30:36 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-18 21:30:36 +0000
commit016d78de3eb0ee784728c99836cb42753e8dea60 (patch)
tree398679128dd10491063194548ae93fb3115fad19
parent7b0ac2b15ccf3652097cb87d60f34f6bfda6388a (diff)
downloadchromium_src-016d78de3eb0ee784728c99836cb42753e8dea60.zip
chromium_src-016d78de3eb0ee784728c99836cb42753e8dea60.tar.gz
chromium_src-016d78de3eb0ee784728c99836cb42753e8dea60.tar.bz2
Convert FormatBytes to string16.
I considered ASCII, but it's producing what is intended to be a human-readable string. For example, in theory the "bytes/s" annotation could be localized into a language where the "b" is non-ASCII. Review URL: http://codereview.chromium.org/3107021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56603 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/string_util.cc46
-rw-r--r--base/string_util.h6
-rw-r--r--base/string_util_unittest.cc44
-rw-r--r--chrome/browser/diagnostics/recon_diagnostics.cc6
-rw-r--r--chrome/browser/download/download_item_model.cc11
-rw-r--r--chrome/browser/download/download_util.cc11
-rw-r--r--chrome/browser/gtk/gtk_chrome_cookie_view.cc8
-rw-r--r--chrome/browser/task_manager.cc20
-rw-r--r--net/base/net_util.cc3
-rw-r--r--webkit/appcache/view_appcache_internals_job.cc2
10 files changed, 78 insertions, 79 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 917d9a9..4dbcd7b 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -652,27 +652,27 @@ DataUnits GetByteDisplayUnits(int64 bytes) {
// TODO(mpcomplete): deal with locale
// Byte suffixes. This must match the DataUnits enum.
-static const wchar_t* const kByteStrings[] = {
- L"B",
- L"kB",
- L"MB",
- L"GB"
+static const char* const kByteStrings[] = {
+ "B",
+ "kB",
+ "MB",
+ "GB"
};
-static const wchar_t* const kSpeedStrings[] = {
- L"B/s",
- L"kB/s",
- L"MB/s",
- L"GB/s"
+static const char* const kSpeedStrings[] = {
+ "B/s",
+ "kB/s",
+ "MB/s",
+ "GB/s"
};
-std::wstring FormatBytesInternal(int64 bytes,
- DataUnits units,
- bool show_units,
- const wchar_t* const* suffix) {
+string16 FormatBytesInternal(int64 bytes,
+ DataUnits units,
+ bool show_units,
+ const char* const* suffix) {
if (bytes < 0) {
NOTREACHED() << "Negative bytes value";
- return std::wstring();
+ return string16();
}
DCHECK(units >= DATA_UNITS_BYTE && units <= DATA_UNITS_GIBIBYTE);
@@ -682,26 +682,26 @@ std::wstring FormatBytesInternal(int64 bytes,
for (int i = 0; i < units; ++i)
unit_amount /= 1024.0;
- wchar_t buf[64];
+ char buf[64];
if (bytes != 0 && units != DATA_UNITS_BYTE && unit_amount < 100)
- base::swprintf(buf, arraysize(buf), L"%.1lf", unit_amount);
+ base::snprintf(buf, arraysize(buf), "%.1lf", unit_amount);
else
- base::swprintf(buf, arraysize(buf), L"%.0lf", unit_amount);
+ base::snprintf(buf, arraysize(buf), "%.0lf", unit_amount);
- std::wstring ret(buf);
+ std::string ret(buf);
if (show_units) {
- ret += L" ";
+ ret += " ";
ret += suffix[units];
}
- return ret;
+ return ASCIIToUTF16(ret);
}
-std::wstring FormatBytes(int64 bytes, DataUnits units, bool show_units) {
+string16 FormatBytes(int64 bytes, DataUnits units, bool show_units) {
return FormatBytesInternal(bytes, units, show_units, kByteStrings);
}
-std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units) {
+string16 FormatSpeed(int64 bytes, DataUnits units, bool show_units) {
return FormatBytesInternal(bytes, units, show_units, kSpeedStrings);
}
diff --git a/base/string_util.h b/base/string_util.h
index c69b2de..4419b79 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -417,16 +417,16 @@ DataUnits GetByteDisplayUnits(int64 bytes);
// 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"
-std::wstring FormatBytes(int64 bytes, DataUnits units, bool show_units);
+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"
-std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units);
+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
-std::wstring FormatNumber(int64 number);
+string16 FormatNumber(int64 number);
// 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 9f72b1f..8a8fc39 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -530,8 +530,8 @@ TEST(StringUtilTest, FormatBytes) {
static const struct {
int64 bytes;
DataUnits units;
- const wchar_t* expected;
- const wchar_t* expected_with_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
@@ -539,35 +539,35 @@ 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, L"0", L"0 B"},
- {512, DATA_UNITS_BYTE, L"512", L"512 B"},
- {512, DATA_UNITS_KIBIBYTE, L"0.5", L"0.5 kB"},
- {1024*1024, DATA_UNITS_KIBIBYTE, L"1024", L"1024 kB"},
- {1024*1024, DATA_UNITS_MEBIBYTE, L"1.0", L"1.0 MB"},
- {1024*1024*1024, DATA_UNITS_GIBIBYTE, L"1.0", L"1.0 GB"},
- {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10.0", L"10.0 GB"},
- {99LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"99.0", L"99.0 GB"},
- {105LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"105", L"105 GB"},
+ {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,
- L"105", L"105 GB"},
- {~(1LL<<63), DATA_UNITS_GIBIBYTE, L"8589934592", L"8589934592 GB"},
+ "105", "105 GB"},
+ {~(1LL<<63), DATA_UNITS_GIBIBYTE, "8589934592", "8589934592 GB"},
- {99*1024 + 103, DATA_UNITS_KIBIBYTE, L"99.1", L"99.1 kB"},
- {1024*1024 + 103, DATA_UNITS_KIBIBYTE, L"1024", L"1024 kB"},
- {1024*1024 + 205 * 1024, DATA_UNITS_MEBIBYTE, L"1.2", L"1.2 MB"},
+ {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,
- L"1.9", L"1.9 GB"},
- {10LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"10.0", L"10.0 GB"},
- {100LL*1024*1024*1024, DATA_UNITS_GIBIBYTE, L"100", L"100 GB"},
+ "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, L"", L""},
+ {-1, DATA_UNITS_BYTE, "", ""},
#endif
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- EXPECT_EQ(cases[i].expected,
+ EXPECT_EQ(ASCIIToUTF16(cases[i].expected),
FormatBytes(cases[i].bytes, cases[i].units, false));
- EXPECT_EQ(cases[i].expected_with_units,
+ EXPECT_EQ(ASCIIToUTF16(cases[i].expected_with_units),
FormatBytes(cases[i].bytes, cases[i].units, true));
}
}
diff --git a/chrome/browser/diagnostics/recon_diagnostics.cc b/chrome/browser/diagnostics/recon_diagnostics.cc
index eb76c2b..bbe23f7 100644
--- a/chrome/browser/diagnostics/recon_diagnostics.cc
+++ b/chrome/browser/diagnostics/recon_diagnostics.cc
@@ -203,8 +203,7 @@ class PathTest : public DiagnosticTest {
return true;
}
DataUnits units = GetByteDisplayUnits(dir_or_file_size);
- string16 printable_size =
- WideToUTF16(FormatBytes(dir_or_file_size, units, true));
+ string16 printable_size = FormatBytes(dir_or_file_size, units, true);
if (path_info_.max_size > 0) {
if (dir_or_file_size > path_info_.max_size) {
@@ -248,8 +247,7 @@ class DiskSpaceTest : public DiagnosticTest {
return true;
}
DataUnits units = GetByteDisplayUnits(disk_space);
- string16 printable_size =
- WideToUTF16(FormatBytes(disk_space, units, true));
+ string16 printable_size = FormatBytes(disk_space, units, true);
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 c360273..48c75ad 100644
--- a/chrome/browser/download/download_item_model.cc
+++ b/chrome/browser/download/download_item_model.cc
@@ -32,17 +32,15 @@ std::wstring DownloadItemModel::GetStatusText() {
int64 total = download_->total_bytes();
DataUnits amount_units = GetByteDisplayUnits(total);
- const string16 simple_size = WideToUTF16Hack(FormatBytes(size, amount_units,
- false));
+ const string16 simple_size = FormatBytes(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
// as "MB 123/456" because it ends with an LTR run. In order to solve this,
// 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 = WideToUTF16Hack(FormatBytes(total, amount_units,
- true));
- simple_total = base::i18n::GetDisplayStringInLTRDirectionality(simple_total);
+ string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
+ FormatBytes(total, amount_units, true));
TimeDelta remaining;
string16 simple_time;
@@ -71,8 +69,7 @@ std::wstring DownloadItemModel::GetStatusText() {
// Instead of displaying "0 B" we keep the "Starting..." string.
status_text = (size == 0) ?
l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING) :
- WideToUTF16Hack(FormatBytes(size, GetByteDisplayUnits(size),
- true));
+ FormatBytes(size, GetByteDisplayUnits(size), true);
} else {
status_text = l10n_util::GetStringFUTF16(
IDS_DOWNLOAD_STATUS_IN_PROGRESS, simple_size, simple_total,
diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc
index e76bf6e..e7f75c9 100644
--- a/chrome/browser/download/download_util.cc
+++ b/chrome/browser/download/download_util.cc
@@ -591,7 +591,8 @@ std::wstring GetProgressStatusText(DownloadItem* download) {
int64 total = download->total_bytes();
int64 size = download->received_bytes();
DataUnits amount_units = GetByteDisplayUnits(size);
- std::wstring received_size = FormatBytes(size, amount_units, true);
+ std::wstring received_size = UTF16ToWideHack(FormatBytes(size, amount_units,
+ true));
std::wstring amount = received_size;
// Adjust both strings for the locale direction since we don't yet know which
@@ -604,7 +605,8 @@ std::wstring GetProgressStatusText(DownloadItem* download) {
if (total) {
amount_units = GetByteDisplayUnits(total);
- std::wstring total_text = FormatBytes(total, amount_units, true);
+ std::wstring total_text =
+ UTF16ToWideHack(FormatBytes(total, amount_units, true));
std::wstring total_text_localized;
if (base::i18n::AdjustStringForLocaleDirection(total_text,
&total_text_localized))
@@ -617,8 +619,9 @@ std::wstring GetProgressStatusText(DownloadItem* download) {
amount.assign(received_size);
}
amount_units = GetByteDisplayUnits(download->CurrentSpeed());
- std::wstring speed_text = FormatSpeed(download->CurrentSpeed(),
- amount_units, true);
+ std::wstring speed_text =
+ UTF16ToWideHack(FormatSpeed(download->CurrentSpeed(), amount_units,
+ true));
std::wstring speed_text_localized;
if (base::i18n::AdjustStringForLocaleDirection(speed_text,
&speed_text_localized))
diff --git a/chrome/browser/gtk/gtk_chrome_cookie_view.cc b/chrome/browser/gtk/gtk_chrome_cookie_view.cc
index 359cdca..5ecba7e 100644
--- a/chrome/browser/gtk/gtk_chrome_cookie_view.cc
+++ b/chrome/browser/gtk/gtk_chrome_cookie_view.cc
@@ -512,7 +512,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_),
- WideToUTF8(FormatBytes(
+ UTF16ToUTF8(FormatBytes(
database_info.size,
GetByteDisplayUnits(database_info.size),
true)).c_str());
@@ -532,7 +532,7 @@ 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_),
- WideToUTF8(FormatBytes(
+ UTF16ToUTF8(FormatBytes(
local_storage_info.size,
GetByteDisplayUnits(local_storage_info.size),
true)).c_str());
@@ -551,7 +551,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_),
- WideToUTF8(FormatBytes(
+ UTF16ToUTF8(FormatBytes(
info.size,
GetByteDisplayUnits(info.size),
true)).c_str());
@@ -595,7 +595,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_),
- WideToUTF8(FormatBytes(
+ UTF16ToUTF8(FormatBytes(
estimated_size,
GetByteDisplayUnits(estimated_size),
true)).c_str());
diff --git a/chrome/browser/task_manager.cc b/chrome/browser/task_manager.cc
index fb236e3..f8ffc10 100644
--- a/chrome/browser/task_manager.cc
+++ b/chrome/browser/task_manager.cc
@@ -59,8 +59,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,
- WideToUTF16Hack(FormatBytes(stat.size, DATA_UNITS_KIBIBYTE, false)),
- WideToUTF16Hack(FormatBytes(stat.liveSize, DATA_UNITS_KIBIBYTE, false)));
+ FormatBytes(stat.size, DATA_UNITS_KIBIBYTE, false),
+ FormatBytes(stat.liveSize, DATA_UNITS_KIBIBYTE, false));
}
} // namespace
@@ -126,8 +126,8 @@ 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 = WideToUTF16(
- FormatSpeed(net_usage, GetByteDisplayUnits(net_usage), true));
+ string16 net_byte = FormatSpeed(net_usage, GetByteDisplayUnits(net_usage),
+ true);
// Force number string to have LTR directionality.
return base::i18n::GetDisplayStringInLTRDirectionality(net_byte);
}
@@ -217,12 +217,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,
- WideToUTF16Hack(FormatBytes(resources_[index]->GetV8MemoryAllocated(),
- DATA_UNITS_KIBIBYTE,
- false)),
- WideToUTF16Hack(FormatBytes(resources_[index]->GetV8MemoryUsed(),
- DATA_UNITS_KIBIBYTE,
- false)));
+ FormatBytes(resources_[index]->GetV8MemoryAllocated(),
+ DATA_UNITS_KIBIBYTE,
+ false),
+ FormatBytes(resources_[index]->GetV8MemoryUsed(),
+ DATA_UNITS_KIBIBYTE,
+ false));
}
bool TaskManagerModel::IsResourceFirstInGroup(int index) const {
diff --git a/net/base/net_util.cc b/net/base/net_util.cc
index a125664..d0b034b 100644
--- a/net/base/net_util.cc
+++ b/net/base/net_util.cc
@@ -1055,7 +1055,8 @@ std::string GetDirectoryListingEntry(const string16& name,
}
base::JsonDoubleQuote(
- WideToUTF16Hack(FormatBytes(size, GetByteDisplayUnits(size), true)), true,
+ FormatBytes(size, GetByteDisplayUnits(size), true),
+ true,
&result);
result.append(",");
diff --git a/webkit/appcache/view_appcache_internals_job.cc b/webkit/appcache/view_appcache_internals_job.cc
index f96fe37..93390cc 100644
--- a/webkit/appcache/view_appcache_internals_job.cc
+++ b/webkit/appcache/view_appcache_internals_job.cc
@@ -109,7 +109,7 @@ void AddHTMLFromAppCacheToOutput(
out->append("<ul>");
AddLiTag(kSize,
- WideToUTF8(FormatBytes(
+ UTF16ToUTF8(FormatBytes(
info->size, GetByteDisplayUnits(info->size), true)),
out);
AddLiTag(kCreationTime,