Commit da77fd43 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Use std::wstring for CommandLine on Windows

This change modifies base::CommandLine::StringType to be std::wstring on
Windows. It relies on changing base::FilePath::StringType to
std::wstring on Windows as well, in order to compile once base::string16
is no longer std::wstring.

Bug: 911896
Change-Id: I07accc7e043fadf2b584799b699881f300e18bb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1875748Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709226}
parent 1901fc96
This diff is collapsed.
......@@ -34,13 +34,12 @@ class BASE_EXPORT CommandLine {
public:
#if defined(OS_WIN)
// The native command line string type.
using StringType = string16;
using StringPieceType = base::StringPiece16;
using StringType = std::wstring;
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
using StringType = std::string;
using StringPieceType = base::StringPiece;
#endif
using StringPieceType = base::BasicStringPiece<StringType>;
using CharType = StringType::value_type;
using StringVector = std::vector<StringType>;
using SwitchMap = std::map<std::string, StringType, std::less<>>;
......@@ -104,7 +103,7 @@ class BASE_EXPORT CommandLine {
static bool InitializedForCurrentProcess();
#if defined(OS_WIN)
static CommandLine FromString(StringPiece16 command_line);
static CommandLine FromString(StringPieceType command_line);
#endif
// Initialize from an argv vector.
......@@ -217,7 +216,7 @@ class BASE_EXPORT CommandLine {
#if defined(OS_WIN)
// Initialize by parsing the given command line string.
// The program name is assumed to be the first item in the string.
void ParseFromString(StringPiece16 command_line);
void ParseFromString(StringPieceType command_line);
#endif
private:
......
......@@ -108,14 +108,14 @@ TEST(CommandLineTest, CommandLineConstructor) {
TEST(CommandLineTest, CommandLineFromString) {
#if defined(OS_WIN)
CommandLine cl = CommandLine::FromString(StrCat(
{STRING16_LITERAL("program --foo= -bAr /Spaetzel=pierogi /Baz flim "),
STRING16_LITERAL("--other-switches=\"--dog=canine --cat=feline\" "),
STRING16_LITERAL("-spaetzle=Crepe -=loosevalue FLAN "),
STRING16_LITERAL("--input-translation=\"45\"--output-rotation "),
STRING16_LITERAL("--quotes="), kTrickyQuoted, STRING16_LITERAL(" "),
STRING16_LITERAL("-- -- --not-a-switch "),
STRING16_LITERAL("\"in the time of submarines...\"")}));
CommandLine cl = CommandLine::FromString(
L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
L"--other-switches=\"--dog=canine --cat=feline\" "
L"-spaetzle=Crepe -=loosevalue FLAN "
L"--input-translation=\"45\"--output-rotation "
L"--quotes=" +
kTrickyQuoted +
L" -- -- --not-a-switch \"in the time of submarines...\"");
EXPECT_FALSE(cl.GetCommandLineString().empty());
EXPECT_FALSE(cl.HasSwitch("cruller"));
......@@ -172,7 +172,7 @@ TEST(CommandLineTest, CommandLineFromString) {
// Tests behavior with an empty input string.
TEST(CommandLineTest, EmptyString) {
#if defined(OS_WIN)
CommandLine cl_from_string = CommandLine::FromString(string16());
CommandLine cl_from_string = CommandLine::FromString(std::wstring());
EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
EXPECT_TRUE(cl_from_string.GetProgram().empty());
EXPECT_EQ(1U, cl_from_string.argv().size());
......@@ -205,11 +205,11 @@ TEST(CommandLineTest, GetArgumentsString) {
cl.AppendArg(kFifthArgName);
#if defined(OS_WIN)
CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName));
CommandLine::StringType expected_first_arg(UTF8ToWide(kFirstArgName));
CommandLine::StringType expected_second_arg(UTF8ToWide(kSecondArgName));
CommandLine::StringType expected_third_arg(UTF8ToWide(kThirdArgName));
CommandLine::StringType expected_fourth_arg(UTF8ToWide(kFourthArgName));
CommandLine::StringType expected_fifth_arg(UTF8ToWide(kFifthArgName));
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
CommandLine::StringType expected_first_arg(kFirstArgName);
CommandLine::StringType expected_second_arg(kSecondArgName);
......@@ -293,15 +293,16 @@ TEST(CommandLineTest, AppendSwitches) {
#if defined(OS_WIN)
EXPECT_EQ(
StrCat({STRING16_LITERAL("Program "), STRING16_LITERAL("--switch1 "),
STRING16_LITERAL("--switch2=value "),
STRING16_LITERAL("--switch3=\"a value with spaces\" "),
STRING16_LITERAL("--switch4=\"\\\"a value with quotes\\\"\" "),
// Even though the switches are unique, appending can add
// repeat switches to argv.
STRING16_LITERAL("--quotes=\"\\\"a value with quotes\\\"\" "),
STRING16_LITERAL("--quotes=\""), kTrickyQuoted,
STRING16_LITERAL("\"")}),
L"Program "
L"--switch1 "
L"--switch2=value "
L"--switch3=\"a value with spaces\" "
L"--switch4=\"\\\"a value with quotes\\\"\" "
// Even though the switches are unique, appending can add repeat
// switches to argv.
L"--quotes=\"\\\"a value with quotes\\\"\" "
L"--quotes=\"" +
kTrickyQuoted + L"\"",
cl.GetCommandLineString());
#endif
}
......@@ -358,12 +359,12 @@ TEST(CommandLineTest, AppendArguments) {
// against regressions.
TEST(CommandLineTest, ProgramQuotes) {
// Check that quotes are not added for paths without spaces.
const FilePath kProgram(STRING16_LITERAL("Program"));
const FilePath kProgram(L"Program");
CommandLine cl_program(kProgram);
EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
const FilePath kProgramPath(STRING16_LITERAL("Program Path"));
const FilePath kProgramPath(L"Program Path");
// Check that quotes are not returned from GetProgram().
CommandLine cl_program_path(kProgramPath);
......@@ -371,13 +372,12 @@ TEST(CommandLineTest, ProgramQuotes) {
// Check that quotes are added to command line string paths containing spaces.
CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
EXPECT_EQ(STRING16_LITERAL("\"Program Path\""), cmd_string);
EXPECT_EQ(L"\"Program Path\"", cmd_string);
// Check the optional quoting of placeholders in programs.
CommandLine cl_quote_placeholder(FilePath(STRING16_LITERAL("%1")));
EXPECT_EQ(STRING16_LITERAL("%1"),
cl_quote_placeholder.GetCommandLineString());
EXPECT_EQ(STRING16_LITERAL("\"%1\""),
CommandLine cl_quote_placeholder(FilePath(L"%1"));
EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
EXPECT_EQ(L"\"%1\"",
cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
}
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment