Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3101)

Unified Diff: base/command_line.cc

Issue 1063933002: Take a StringPiece when looking up CommandLine switches. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cmd
Patch Set: Override copy and assign. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/command_line.cc
diff --git a/base/command_line.cc b/base/command_line.cc
index 3e143cc5d449f358936726f33c8d3fda95064f03..5b6107b71313255b7718577865b570404fb29f7e 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,15 @@ string16 QuoteForCommandLineToArgvW(const string16& arg,
}
#endif
+void ResetStringPieces(
tapted 2015/04/15 00:07:53 needs a comment
jackhou1 2015/04/17 00:56:10 Done.
+ const CommandLine::SwitchMap& switches,
+ std::map<base::StringPiece, const CommandLine::StringType*>*
+ switches_by_stringpiece) {
+ switches_by_stringpiece->clear();
+ for (auto it = switches.begin(); it != switches.end(); ++it)
+ (*switches_by_stringpiece)[it->first] = &(it->second);
+}
+
} // namespace
CommandLine::CommandLine(NoProgram no_program)
@@ -180,6 +177,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 +261,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,16 +275,16 @@ void CommandLine::SetProgram(const FilePath& program) {
TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
}
-bool CommandLine::HasSwitch(const std::string& switch_string) const {
- return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
-}
-
-bool CommandLine::HasSwitch(const char string_constant[]) const {
- return HasSwitch(std::string(string_constant));
+bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const {
+ DCHECK_EQ(StringToLowerASCII(
+ std::string(switch_string.data(), switch_string.size())),
tapted 2015/04/15 00:07:53 switch_string.as_string() more below
jackhou1 2015/04/17 00:56:10 Done.
+ switch_string);
+ return switches_by_stringpiece_.find(switch_string) !=
+ switches_by_stringpiece_.end();
}
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.";
@@ -285,15 +298,18 @@ 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(
+ std::string(switch_string.data(), switch_string.size())),
+ switch_string);
+ 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) {
@@ -307,14 +323,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)
+ std::string switch_key = StringToLowerASCII(switch_string);
tapted 2015/04/15 00:07:53 const? (const reference would work too, but it re
jackhou1 2015/04/17 00:56:10 Done.
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 result =
tapted 2015/04/15 00:07:53 nit: result -> inserted/insertion
jackhou1 2015/04/17 00:56:10 Done.
+ switches_.insert(make_pair(switch_key.substr(prefix_length), value));
+ if (!result.second)
+ result.first->second = value;
+ switches_by_stringpiece_[result.first->first] = &(result.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;
« base/command_line.h ('K') | « base/command_line.h ('k') | base/command_line_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698