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

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: Address comments. 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
« no previous file with comments | « base/command_line.h ('k') | base/command_line_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/command_line.cc
diff --git a/base/command_line.cc b/base/command_line.cc
index 3e143cc5d449f358936726f33c8d3fda95064f03..9b7fa2f9e6800381a05d454d0b81a1056f29b2c3 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,18 @@ 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(
+ const CommandLine::SwitchMap& switches,
+ std::map<base::StringPiece, const CommandLine::StringType*>*
tapted 2015/04/17 01:14:33 nit: this should have a typedef (I know there's di
jackhou1 2015/04/22 04:09:42 Done.
+ 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 +180,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 +264,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 +278,18 @@ 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 base::StringPiece& switch_string) const {
+ DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string);
tapted 2015/04/17 01:14:33 rebase?
jackhou1 2015/04/22 04:09:42 Done.
+ 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.";
@@ -285,15 +303,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);
+ 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 +326,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);
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;
« no previous file with comments | « 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