Skip to content

Commit

Permalink
Add utf_string_conversions to base namespace.
Browse files Browse the repository at this point in the history
This adds "using"s for all functions so those can be fixed in a separate pass.

This converts the "Wide" versions of the functions in the Chrome directory as a first pass on the changeover.

BUG=

Review URL: https://codereview.chromium.org/12314090

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@184352 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Feb 24, 2013
1 parent 20023a7 commit 5f0cebb
Show file tree
Hide file tree
Showing 170 changed files with 534 additions and 472 deletions.
4 changes: 2 additions & 2 deletions apps/app_host/update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const wchar_t kBrowserAppGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
Version GetAppHostVersion() {
scoped_ptr<FileVersionInfo> version_info(
FileVersionInfo::CreateFileVersionInfoForCurrentModule());
Version app_host_version(WideToASCII(version_info->product_version()));
Version app_host_version(base::WideToASCII(version_info->product_version()));
DCHECK(app_host_version.IsValid());
return app_host_version;
}
Expand All @@ -58,7 +58,7 @@ Version GetAppVersionFromRegistry(const wchar_t* app_guid) {
if ((reg_key.Open(root_key, client_key.c_str(),
KEY_QUERY_VALUE) == ERROR_SUCCESS) &&
(reg_key.ReadValue(kRegVersionField, &version_str) == ERROR_SUCCESS)) {
return Version(WideToASCII(version_str));
return Version(base::WideToASCII(version_str));
}
return Version();
}
Expand Down
17 changes: 8 additions & 9 deletions base/utf_string_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
#include "base/string_util.h"
#include "base/strings/utf_string_conversion_utils.h"

using base::PrepareForUTF8Output;
using base::PrepareForUTF16Or32Output;
using base::ReadUnicodeCharacter;
using base::WriteUnicodeCharacter;
namespace base {

namespace {

Expand Down Expand Up @@ -63,7 +60,7 @@ bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
return ConvertUnicode(src, src_len, output);
}

std::wstring UTF8ToWide(const base::StringPiece& utf8) {
std::wstring UTF8ToWide(const StringPiece& utf8) {
std::wstring ret;
UTF8ToWide(utf8.data(), utf8.length(), &ret);
return ret;
Expand Down Expand Up @@ -133,7 +130,7 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
return ConvertUnicode(src, src_len, output);
}

string16 UTF8ToUTF16(const base::StringPiece& utf8) {
string16 UTF8ToUTF16(const StringPiece& utf8) {
string16 ret;
// Ignore the success flag of this call, it will do the best it can for
// invalid input, which is what we want here.
Expand Down Expand Up @@ -161,7 +158,7 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
return UTF8ToWide(src, src_len, output);
}

string16 UTF8ToUTF16(const base::StringPiece& utf8) {
string16 UTF8ToUTF16(const StringPiece& utf8) {
return UTF8ToWide(utf8);
}

Expand All @@ -175,12 +172,14 @@ std::string UTF16ToUTF8(const string16& utf16) {

#endif

std::wstring ASCIIToWide(const base::StringPiece& ascii) {
std::wstring ASCIIToWide(const StringPiece& ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return std::wstring(ascii.begin(), ascii.end());
}

string16 ASCIIToUTF16(const base::StringPiece& ascii) {
string16 ASCIIToUTF16(const StringPiece& ascii) {
DCHECK(IsStringASCII(ascii)) << ascii;
return string16(ascii.begin(), ascii.end());
}

} // namespace base
21 changes: 21 additions & 0 deletions base/utf_string_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "base/string16.h"
#include "base/string_piece.h"

namespace base {

// These convert between UTF-8, -16, and -32 strings. They are potentially slow,
// so avoid unnecessary conversions. The low-level versions return a boolean
// indicating whether the conversion was 100% valid. In this case, it will still
Expand Down Expand Up @@ -55,4 +57,23 @@ BASE_EXPORT std::string UTF16ToUTF8(const string16& utf16);
BASE_EXPORT std::wstring ASCIIToWide(const base::StringPiece& ascii);
BASE_EXPORT string16 ASCIIToUTF16(const base::StringPiece& ascii);

} // namespace base

// TODO(brettw) remove these usings once all code has been converted to using
// the namespaces.
using base::WideToUTF8;
using base::WideToUTF8;
using base::UTF8ToWide;
using base::UTF8ToWide;
using base::WideToUTF16;
using base::WideToUTF16;
using base::UTF16ToWide;
using base::UTF16ToWide;
using base::UTF8ToUTF16;
using base::UTF8ToUTF16;
using base::UTF16ToUTF8;
using base::UTF16ToUTF8;
using base::ASCIIToWide;
using base::ASCIIToUTF16;

#endif // BASE_UTF_STRING_CONVERSIONS_H_
2 changes: 1 addition & 1 deletion base/utf_string_conversions_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ TEST(UTFStringConversionsTest, ConvertMultiString) {
EXPECT_EQ(expected, converted);
}

} // base
} // namespace base
21 changes: 11 additions & 10 deletions chrome/app/breakpad_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ void SetCrashKeyValue(const base::StringPiece& key,
entry = it->second;
}

entry->set(UTF8ToWide(key).data(), UTF8ToWide(value).data());
entry->set(base::UTF8ToWide(key).data(), base::UTF8ToWide(value).data());
}

void ClearCrashKeyValue(const base::StringPiece& key) {
Expand Down Expand Up @@ -743,8 +743,8 @@ bool WrapMessageBoxWithSEH(const wchar_t* text, const wchar_t* caption,
// spawned and basically just shows the 'chrome has crashed' dialog if
// the CHROME_CRASHED environment variable is present.
bool ShowRestartDialogIfCrashed(bool* exit_now) {
if (!::GetEnvironmentVariableW(ASCIIToWide(env_vars::kShowRestart).c_str(),
NULL, 0)) {
if (!::GetEnvironmentVariableW(
base::ASCIIToWide(env_vars::kShowRestart).c_str(), NULL, 0)) {
return false;
}

Expand All @@ -757,12 +757,12 @@ bool ShowRestartDialogIfCrashed(bool* exit_now) {
}

DWORD len = ::GetEnvironmentVariableW(
ASCIIToWide(env_vars::kRestartInfo).c_str(), NULL, 0);
base::ASCIIToWide(env_vars::kRestartInfo).c_str(), NULL, 0);
if (!len)
return true;

wchar_t* restart_data = new wchar_t[len + 1];
::GetEnvironmentVariableW(ASCIIToWide(env_vars::kRestartInfo).c_str(),
::GetEnvironmentVariableW(base::ASCIIToWide(env_vars::kRestartInfo).c_str(),
restart_data, len);
restart_data[len] = 0;
// The CHROME_RESTART var contains the dialog strings separated by '|'.
Expand All @@ -777,7 +777,7 @@ bool ShowRestartDialogIfCrashed(bool* exit_now) {
// If the UI layout is right-to-left, we need to pass the appropriate MB_XXX
// flags so that an RTL message box is displayed.
UINT flags = MB_OKCANCEL | MB_ICONWARNING;
if (dlg_strings[2] == ASCIIToWide(env_vars::kRtlLocale))
if (dlg_strings[2] == base::ASCIIToWide(env_vars::kRtlLocale))
flags |= MB_RIGHT | MB_RTLREADING;

return WrapMessageBoxWithSEH(dlg_strings[1].c_str(), dlg_strings[0].c_str(),
Expand All @@ -804,7 +804,8 @@ extern "C" int __declspec(dllexport) CrashForException(
// indicates whether policy data was successfully read. If it is true, |result|
// contains the value set by policy.
static bool MetricsReportingControlledByPolicy(bool* result) {
std::wstring key_name = UTF8ToWide(policy::key::kMetricsReportingEnabled);
std::wstring key_name =
base::UTF8ToWide(policy::key::kMetricsReportingEnabled);
DWORD value = 0;
base::win::RegKey hklm_policy_key(HKEY_LOCAL_MACHINE,
policy::kRegistryMandatorySubKey, KEY_READ);
Expand Down Expand Up @@ -851,7 +852,7 @@ static void InitPipeNameEnvVar(bool is_per_user_install) {
bool use_crash_service = !controlled_by_policy &&
((command.HasSwitch(switches::kNoErrorDialogs) ||
GetEnvironmentVariable(
ASCIIToWide(env_vars::kHeadless).c_str(), NULL, 0)));
base::ASCIIToWide(env_vars::kHeadless).c_str(), NULL, 0)));

std::wstring pipe_name;
if (use_crash_service) {
Expand Down Expand Up @@ -887,7 +888,7 @@ static void InitPipeNameEnvVar(bool is_per_user_install) {
pipe_name = kGoogleUpdatePipeName;
pipe_name += user_sid;
}
env->SetVar(kPipeNameVar, WideToASCII(pipe_name));
env->SetVar(kPipeNameVar, base::WideToASCII(pipe_name));
}

void InitCrashReporter() {
Expand Down Expand Up @@ -946,7 +947,7 @@ void InitCrashReporter() {
InitDefaultCrashCallback(default_filter);
return;
}
std::wstring pipe_name = ASCIIToWide(pipe_name_ascii);
std::wstring pipe_name = base::ASCIIToWide(pipe_name_ascii);

#ifdef _WIN64
// The protocol for connecting to the out-of-process Breakpad crash
Expand Down
10 changes: 5 additions & 5 deletions chrome/app/client_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ HMODULE MainDllLoader::Load(string16* out_version, string16* out_file) {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
if (cmd_line.HasSwitch(switches::kChromeVersion)) {
version_string = cmd_line.GetSwitchValueNative(switches::kChromeVersion);
version = Version(WideToASCII(version_string));
version = Version(base::WideToASCII(version_string));

if (!version.IsValid()) {
// If a bogus command line flag was given, then abort.
Expand All @@ -377,7 +377,7 @@ HMODULE MainDllLoader::Load(string16* out_version, string16* out_file) {
FileVersionInfo::CreateFileVersionInfoForCurrentModule());
if (file_version_info.get()) {
version_string = file_version_info->file_version();
version = Version(WideToASCII(version_string));
version = Version(base::WideToASCII(version_string));
}
}

Expand All @@ -387,9 +387,9 @@ HMODULE MainDllLoader::Load(string16* out_version, string16* out_file) {

// If no version in the current module, then look in the environment.
if (!version.IsValid()) {
if (EnvQueryStr(ASCIIToWide(chrome::kChromeVersionEnvVar).c_str(),
if (EnvQueryStr(base::ASCIIToWide(chrome::kChromeVersionEnvVar).c_str(),
&version_string)) {
version = Version(WideToASCII(version_string));
version = Version(base::WideToASCII(version_string));
LOG_IF(ERROR, !version.IsValid()) << "Invalid environment version: "
<< version_string;
}
Expand Down Expand Up @@ -428,7 +428,7 @@ int MainDllLoader::Launch(HINSTANCE instance,
return chrome::RESULT_CODE_MISSING_DATA;

scoped_ptr<base::Environment> env(base::Environment::Create());
env->SetVar(chrome::kChromeVersionEnvVar, WideToUTF8(version));
env->SetVar(chrome::kChromeVersionEnvVar, base::WideToUTF8(version));
// TODO(erikwright): Remove this when http://crbug.com/174953 is fixed and
// widely deployed.
env->UnSetVar(env_vars::kGoogleUpdateIsMachineEnvVar);
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/about_flags_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ TEST_F(AboutFlagsTest, CheckValues) {
#if defined(OS_WIN)
EXPECT_EQ(std::wstring::npos,
command_line.GetCommandLineString().find(
ASCIIToWide(switch1_with_equals)));
base::ASCIIToWide(switch1_with_equals)));
#else
EXPECT_EQ(std::string::npos,
command_line.GetCommandLineString().find(switch1_with_equals));
Expand All @@ -264,7 +264,7 @@ TEST_F(AboutFlagsTest, CheckValues) {
#if defined(OS_WIN)
EXPECT_NE(std::wstring::npos,
command_line.GetCommandLineString().find(
ASCIIToWide(switch2_with_equals)));
base::ASCIIToWide(switch2_with_equals)));
#else
EXPECT_NE(std::string::npos,
command_line.GetCommandLineString().find(switch2_with_equals));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void RecursiveFindNodeInAccessibilityTree(
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("role=%d name=%s\n", role.lVal, WideToUTF8(name).c_str());
printf("role=%d name=%s\n", role.lVal, base::WideToUTF8(name).c_str());

if (expected_role == role.lVal && expected_name == name) {
*found = true;
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/autocomplete/autocomplete_input_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TEST(AutocompleteInputTest, InputType) {
{ ASCIIToUTF16("127.0.1"), AutocompleteInput::UNKNOWN },
{ ASCIIToUTF16("127.0.1/"), AutocompleteInput::URL },
{ ASCIIToUTF16("browser.tabs.closeButtons"), AutocompleteInput::UNKNOWN },
{ WideToUTF16(L"\u6d4b\u8bd5"), AutocompleteInput::UNKNOWN },
{ base::WideToUTF16(L"\u6d4b\u8bd5"), AutocompleteInput::UNKNOWN },
{ ASCIIToUTF16("[2001:]"), AutocompleteInput::QUERY },
{ ASCIIToUTF16("[2001:dB8::1]"), AutocompleteInput::URL },
{ ASCIIToUTF16("192.168.0.256"), AutocompleteInput::QUERY },
Expand Down Expand Up @@ -150,8 +150,9 @@ TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) {
// This tests for a regression where certain input in the omnibox caused us to
// crash. As long as the test completes without crashing, we're fine.
TEST(AutocompleteInputTest, InputCrash) {
AutocompleteInput input(WideToUTF16(L"\uff65@s"), string16::npos, string16(),
true, false, true, AutocompleteInput::ALL_MATCHES);
AutocompleteInput input(base::WideToUTF16(L"\uff65@s"), string16::npos,
string16(), true, false, true,
AutocompleteInput::ALL_MATCHES);
}

TEST(AutocompleteInputTest, ParseForEmphasizeComponent) {
Expand All @@ -170,7 +171,7 @@ TEST(AutocompleteInputTest, ParseForEmphasizeComponent) {
{ ASCIIToUTF16("http://foo/bar baz"), Component(0, 4), Component(7, 3) },
{ ASCIIToUTF16("link:foo.com"), Component(0, 4), kInvalidComponent },
{ ASCIIToUTF16("www.foo.com:81"), kInvalidComponent, Component(0, 11) },
{ WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) },
{ base::WideToUTF16(L"\u6d4b\u8bd5"), kInvalidComponent, Component(0, 2) },
{ ASCIIToUTF16("view-source:http://www.foo.com/"), Component(12, 4),
Component(19, 11) },
{ ASCIIToUTF16("view-source:https://example.com/"),
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/autocomplete/history_url_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,10 @@ TEST_F(HistoryURLProviderTest, Fixup) {
RunTest(ASCIIToUTF16("#"), string16(), false, NULL, 0);
RunTest(ASCIIToUTF16("%20"), string16(), false, NULL, 0);
const std::string fixup_crash[] = {"http://%EF%BD%A5@s/"};
RunTest(WideToUTF16(L"\uff65@s"), string16(), false, fixup_crash,
RunTest(base::WideToUTF16(L"\uff65@s"), string16(), false, fixup_crash,
arraysize(fixup_crash));
RunTest(WideToUTF16(L"\u2015\u2015@ \uff7c"), string16(), false, NULL, 0);
RunTest(base::WideToUTF16(L"\u2015\u2015@ \uff7c"), string16(), false,
NULL, 0);

// Fixing up "file:" should result in an inline autocomplete offset of just
// after "file:", not just after "file://".
Expand Down Expand Up @@ -528,7 +529,7 @@ TEST_F(HistoryURLProviderTest, Fixup) {
}

TEST_F(HistoryURLProviderTest, AdjustOffset) {
RunAdjustOffsetTest(WideToUTF16(L"http://www.\uAD50\uC721"), 13);
RunAdjustOffsetTest(base::WideToUTF16(L"http://www.\uAD50\uC721"), 13);
RunAdjustOffsetTest(ASCIIToUTF16("http://spaces.com/path%20with%20spa"), 31);
RunAdjustOffsetTest(ASCIIToUTF16("http://ms/c++ s"), 15);
}
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/autofill/autofill_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/keycodes/keyboard_codes.h"

using base::WideToUTF16;
using content::RenderViewHost;
using content::RenderViewHostTester;
using content::WebContents;
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/automation/testing_automation_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ void TestingAutomationProvider::GetTabTitle(int handle,
NavigationController* tab = tab_tracker_->GetResource(handle);
NavigationEntry* entry = tab->GetActiveEntry();
if (entry != NULL) {
*title = UTF16ToWideHack(entry->GetTitleForDisplay(""));
*title = base::UTF16ToWideHack(entry->GetTitleForDisplay(""));
} else {
*title = std::wstring();
}
Expand Down Expand Up @@ -1050,8 +1050,9 @@ void TestingAutomationProvider::ExecuteJavascript(
}

new DomOperationMessageSender(this, reply_message, false);
ExecuteJavascriptInRenderViewFrame(WideToUTF16Hack(frame_xpath),
WideToUTF16Hack(script), reply_message,
ExecuteJavascriptInRenderViewFrame(base::WideToUTF16Hack(frame_xpath),
base::WideToUTF16Hack(script),
reply_message,
web_contents->GetRenderViewHost());
}

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/bookmarks/bookmark_index_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ TEST_F(BookmarkIndexTest, HonorMax) {
// than the upper case string no match positions are returned.
TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) {
const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0,
WideToUTF16(L"\u0130 i"),
base::WideToUTF16(L"\u0130 i"),
GURL("http://www.google.com"));

std::vector<bookmark_utils::TitleMatch> matches;
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/bookmarks/bookmark_model_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ TEST_F(BookmarkModelTest, AddURL) {

TEST_F(BookmarkModelTest, AddURLWithUnicodeTitle) {
const BookmarkNode* root = model_.bookmark_bar_node();
const string16 title(WideToUTF16(
const string16 title(base::WideToUTF16(
L"\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053"));
const GURL url("https://www.baidu.com/");

Expand Down
16 changes: 8 additions & 8 deletions chrome/browser/bookmarks/bookmark_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TEST_F(BookmarkUtilsTest, DoesBookmarkContainText) {
EXPECT_FALSE(DoesBookmarkContainText(node, ASCIIToUTF16("cnn"), string()));

// Tests for a Japanese IDN.
const string16 kDecodedIdn = WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB");
const string16 kDecodedIdn = base::WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB");
node = model.AddURL(model.other_node(),
0,
ASCIIToUTF16("foo bar"),
Expand Down Expand Up @@ -108,24 +108,24 @@ TEST_F(BookmarkUtilsTest, DoesBookmarkContainText) {
// Test with accents.
node = model.AddURL(model.other_node(),
0,
WideToUTF16(L"fr\u00E4n\u00E7\u00F3s\u00EA"),
base::WideToUTF16(L"fr\u00E4n\u00E7\u00F3s\u00EA"),
GURL("http://www.google.com/search?q=FBA"));
EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("francose"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("FrAnCoSe"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"fr\u00E4ncose"),
EXPECT_TRUE(DoesBookmarkContainText(node, base::WideToUTF16(L"fr\u00E4ncose"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"fran\u00E7ose"),
EXPECT_TRUE(DoesBookmarkContainText(node, base::WideToUTF16(L"fran\u00E7ose"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"franc\u00F3se"),
EXPECT_TRUE(DoesBookmarkContainText(node, base::WideToUTF16(L"franc\u00F3se"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"francos\u00EA"),
EXPECT_TRUE(DoesBookmarkContainText(node, base::WideToUTF16(L"francos\u00EA"),
string()));
EXPECT_TRUE(DoesBookmarkContainText(
node, WideToUTF16(L"Fr\u00C4n\u00C7\u00F3S\u00EA"), string()));
node, base::WideToUTF16(L"Fr\u00C4n\u00C7\u00F3S\u00EA"), string()));
EXPECT_TRUE(DoesBookmarkContainText(
node, WideToUTF16(L"fr\u00C4n\u00C7\u00D3s\u00CA"), string()));
node, base::WideToUTF16(L"fr\u00C4n\u00C7\u00D3s\u00CA"), string()));
EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("fba"), string()));
EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("FBA"), string()));
}
Expand Down
Loading

0 comments on commit 5f0cebb

Please sign in to comment.