Chromium Code Reviews| Index: base/command_line.cc |
| diff --git a/base/command_line.cc b/base/command_line.cc |
| index 61ff5c10c3813e74ffdedece08cc0bf14638716a..586c189ba35fb31ec346a2b65e4dff6bad2287ae 100644 |
| --- a/base/command_line.cc |
| +++ b/base/command_line.cc |
| @@ -93,18 +93,6 @@ void AppendSwitchesAndArguments(CommandLine* command_line, |
| } |
| } |
| -// Lowercase switches for backwards compatiblity *on Windows*. |
| -#if defined(OS_WIN) |
| -std::string LowerASCIIOnWindows(const std::string& string) { |
| - return StringToLowerASCII(string); |
| -} |
| -#elif defined(OS_POSIX) |
| -const std::string& LowerASCIIOnWindows(const std::string& string) { |
| - return string; |
| -} |
| -#endif |
| - |
| - |
| #if defined(OS_WIN) |
| // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
| string16 QuoteForCommandLineToArgvW(const string16& arg, |
| @@ -155,6 +143,17 @@ string16 QuoteForCommandLineToArgvW(const string16& arg, |
| } |
| #endif |
| +// Reconstruct |switches_by_stringpiece| to be a mirror of |switches|. |
| +// |switches_by_stringpiece| only contains pointers to objects owned by |
| +// |switches|. |
| +void ResetStringPieces( |
|
brettw
2015/04/24 19:50:49
Why not make this a private member function? You c
jackhou1
2015/05/11 13:51:42
Done.
|
| + const CommandLine::SwitchMap& switches, |
| + CommandLine::StringPieceSwitchMap* switches_by_stringpiece) { |
| + switches_by_stringpiece->clear(); |
| + for (auto it = switches.begin(); it != switches.end(); ++it) |
|
brettw
2015/04/24 19:50:50
auto -> const auto&
Always use const refs when app
jackhou1
2015/05/11 13:51:42
Done.
|
| + (*switches_by_stringpiece)[it->first] = &(it->second); |
| +} |
| + |
| } // namespace |
| CommandLine::CommandLine(NoProgram no_program) |
| @@ -180,6 +179,21 @@ CommandLine::CommandLine(const StringVector& argv) |
| InitFromArgv(argv); |
| } |
| +CommandLine::CommandLine(const CommandLine& other) |
| + : argv_(other.argv_), |
| + switches_(other.switches_), |
| + begin_args_(other.begin_args_) { |
| + ResetStringPieces(switches_, &switches_by_stringpiece_); |
| +} |
| + |
| +CommandLine& CommandLine::operator=(const CommandLine& other) { |
| + argv_ = other.argv_; |
| + switches_ = other.switches_; |
| + begin_args_ = other.begin_args_; |
| + ResetStringPieces(switches_, &switches_by_stringpiece_); |
| + return *this; |
| +} |
| + |
| CommandLine::~CommandLine() { |
| } |
| @@ -249,6 +263,7 @@ void CommandLine::InitFromArgv(int argc, |
| void CommandLine::InitFromArgv(const StringVector& argv) { |
| argv_ = StringVector(1); |
| switches_.clear(); |
| + switches_by_stringpiece_.clear(); |
| begin_args_ = 1; |
| SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| AppendSwitchesAndArguments(this, argv); |
| @@ -262,17 +277,18 @@ void CommandLine::SetProgram(const FilePath& program) { |
| TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
| } |
| -bool CommandLine::HasSwitch(const std::string& switch_string) const { |
| - DCHECK_EQ(StringToLowerASCII(switch_string), switch_string); |
| - return switches_.find(switch_string) != switches_.end(); |
| +bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const { |
| + DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
| + return switches_by_stringpiece_.find(switch_string) != |
| + switches_by_stringpiece_.end(); |
| } |
| -bool CommandLine::HasSwitch(const char string_constant[]) const { |
| - return HasSwitch(std::string(string_constant)); |
| +bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| + return HasSwitch(base::StringPiece(switch_constant)); |
| } |
| std::string CommandLine::GetSwitchValueASCII( |
| - const std::string& switch_string) const { |
| + const base::StringPiece& switch_string) const { |
| StringType value = GetSwitchValueNative(switch_string); |
| if (!IsStringASCII(value)) { |
| DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| @@ -286,15 +302,16 @@ std::string CommandLine::GetSwitchValueASCII( |
| } |
| FilePath CommandLine::GetSwitchValuePath( |
| - const std::string& switch_string) const { |
| + const base::StringPiece& switch_string) const { |
| return FilePath(GetSwitchValueNative(switch_string)); |
| } |
| CommandLine::StringType CommandLine::GetSwitchValueNative( |
| - const std::string& switch_string) const { |
| - SwitchMap::const_iterator result = |
| - switches_.find(LowerASCIIOnWindows(switch_string)); |
| - return result == switches_.end() ? StringType() : result->second; |
| + const base::StringPiece& switch_string) const { |
| + DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
|
brettw
2015/04/24 19:50:49
Shouldn't this function require that the switches
jackhou1
2015/05/11 13:51:42
It has the same DCHECK as HasSwitch. AppendSwitch*
|
| + auto result = switches_by_stringpiece_.find(switch_string); |
| + return result == switches_by_stringpiece_.end() ? StringType() |
| + : *(result->second); |
| } |
| void CommandLine::AppendSwitch(const std::string& switch_string) { |
| @@ -308,14 +325,19 @@ void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| const CommandLine::StringType& value) { |
| - std::string switch_key(LowerASCIIOnWindows(switch_string)); |
| #if defined(OS_WIN) |
| + const std::string switch_key = StringToLowerASCII(switch_string); |
|
brettw
2015/04/24 19:50:50
Ditto for lower-case.
jackhou1
2015/05/11 13:51:42
With AppendSwitch* the string may have come from a
|
| StringType combined_switch_string(ASCIIToUTF16(switch_key)); |
| #elif defined(OS_POSIX) |
| - StringType combined_switch_string(switch_string); |
| + const std::string& switch_key = switch_string; |
| + StringType combined_switch_string(switch_key); |
| #endif |
| size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| - switches_[switch_key.substr(prefix_length)] = value; |
| + auto insertion = |
| + switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| + if (!insertion.second) |
| + insertion.first->second = value; |
| + switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); |
| // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| if (prefix_length == 0) |
| combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |