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

Unified Diff: base/command_line.cc

Issue 1046073002: Add read-only mode to CommandLine that optimizes HasSwitch(const char*). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@whitelisted
Patch Set: Fix tests. Created 5 years, 9 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..7dab0db479b6447119a9d1ea64c6a2e6c4174c67 100644
--- a/base/command_line.cc
+++ b/base/command_line.cc
@@ -5,6 +5,7 @@
#include "base/command_line.h"
#include <algorithm>
+#include <cstring>
#include <ostream>
#include "base/basictypes.h"
@@ -93,6 +94,14 @@ void AppendSwitchesAndArguments(CommandLine* command_line,
}
}
+bool ContainsUpperASCII(const char* string) {
+ for (; *string; ++string) {
+ if (*string >= 'A' && *string <= 'Z')
+ return true;
+ }
+ return false;
+}
+
// Lowercase switches for backwards compatiblity *on Windows*.
#if defined(OS_WIN)
std::string LowerASCIIOnWindows(const std::string& string) {
@@ -104,6 +113,10 @@ const std::string& LowerASCIIOnWindows(const std::string& string) {
}
#endif
+// Comparator usable by std::lower_bound().
+bool CompareStringAndCharPtr(const std::string& s, const char* c) {
+ return strcmp(s.c_str(), c) <= 0;
+}
#if defined(OS_WIN)
// Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*.
@@ -158,25 +171,21 @@ string16 QuoteForCommandLineToArgvW(const string16& arg,
} // namespace
CommandLine::CommandLine(NoProgram no_program)
- : argv_(1),
- begin_args_(1) {
+ : argv_(1), begin_args_(1), writeable_(true) {
}
CommandLine::CommandLine(const FilePath& program)
- : argv_(1),
- begin_args_(1) {
+ : argv_(1), begin_args_(1), writeable_(true) {
SetProgram(program);
}
CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
- : argv_(1),
- begin_args_(1) {
+ : argv_(1), begin_args_(1), writeable_(true) {
InitFromArgv(argc, argv);
}
CommandLine::CommandLine(const StringVector& argv)
- : argv_(1),
- begin_args_(1) {
+ : argv_(1), begin_args_(1), writeable_(true) {
InitFromArgv(argv);
}
@@ -207,6 +216,7 @@ bool CommandLine::Init(int argc, const char* const* argv) {
#elif defined(OS_POSIX)
current_process_commandline_->InitFromArgv(argc, argv);
#endif
+ current_process_commandline_->MakeReadOnly();
return true;
}
@@ -240,6 +250,7 @@ CommandLine CommandLine::FromString(const string16& command_line) {
void CommandLine::InitFromArgv(int argc,
const CommandLine::CharType* const* argv) {
+ DCHECK(writeable_);
StringVector new_argv;
for (int i = 0; i < argc; ++i)
new_argv.push_back(argv[i]);
@@ -247,6 +258,7 @@ void CommandLine::InitFromArgv(int argc,
}
void CommandLine::InitFromArgv(const StringVector& argv) {
+ DCHECK(writeable_);
argv_ = StringVector(1);
switches_.clear();
begin_args_ = 1;
@@ -259,15 +271,24 @@ FilePath CommandLine::GetProgram() const {
}
void CommandLine::SetProgram(const FilePath& program) {
+ DCHECK(writeable_);
TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
}
bool CommandLine::HasSwitch(const std::string& switch_string) const {
- return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end();
+#if defined(OS_WIN)
+ if (ContainsUpperASCII(switch_string.c_str()))
+ return switches_.find(LowerASCIIOnWindows(switch_string)) !=
+ switches_.end();
+#endif
+
+ return switches_.find(switch_string) != switches_.end();
}
bool CommandLine::HasSwitch(const char string_constant[]) const {
- return HasSwitch(std::string(string_constant));
+ DCHECK(!ContainsUpperASCII(string_constant));
+ return writeable_ ? HasSwitchLowerCase(string_constant)
+ : HasSwitchVector(string_constant);
}
std::string CommandLine::GetSwitchValueASCII(
@@ -291,8 +312,15 @@ FilePath CommandLine::GetSwitchValuePath(
CommandLine::StringType CommandLine::GetSwitchValueNative(
const std::string& switch_string) const {
- SwitchMap::const_iterator result =
- switches_.find(LowerASCIIOnWindows(switch_string));
+#if defined(OS_WIN)
+ if (ContainsUpperASCII(switch_string.c_str())) {
+ SwitchMap::const_iterator result =
+ switches_.find(LowerASCIIOnWindows(switch_string));
+ return result == switches_.end() ? StringType() : result->second;
+ }
+#endif
+
+ SwitchMap::const_iterator result = switches_.find(switch_string);
return result == switches_.end() ? StringType() : result->second;
}
@@ -307,6 +335,7 @@ void CommandLine::AppendSwitchPath(const std::string& switch_string,
void CommandLine::AppendSwitchNative(const std::string& switch_string,
const CommandLine::StringType& value) {
+ DCHECK(writeable_);
std::string switch_key(LowerASCIIOnWindows(switch_string));
#if defined(OS_WIN)
StringType combined_switch_string(ASCIIToUTF16(switch_key));
@@ -326,6 +355,7 @@ void CommandLine::AppendSwitchNative(const std::string& switch_string,
void CommandLine::AppendSwitchASCII(const std::string& switch_string,
const std::string& value_string) {
+ DCHECK(writeable_);
#if defined(OS_WIN)
AppendSwitchNative(switch_string, ASCIIToUTF16(value_string));
#elif defined(OS_POSIX)
@@ -336,6 +366,7 @@ void CommandLine::AppendSwitchASCII(const std::string& switch_string,
void CommandLine::CopySwitchesFrom(const CommandLine& source,
const char* const switches[],
size_t count) {
+ DCHECK(writeable_);
for (size_t i = 0; i < count; ++i) {
if (source.HasSwitch(switches[i]))
AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i]));
@@ -367,17 +398,20 @@ void CommandLine::AppendArgPath(const FilePath& path) {
}
void CommandLine::AppendArgNative(const CommandLine::StringType& value) {
+ DCHECK(writeable_);
argv_.push_back(value);
}
void CommandLine::AppendArguments(const CommandLine& other,
bool include_program) {
+ DCHECK(writeable_);
if (include_program)
SetProgram(other.GetProgram());
AppendSwitchesAndArguments(this, other.argv());
}
void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
+ DCHECK(writeable_);
if (wrapper.empty())
return;
// The wrapper may have embedded arguments (like "gdb --args"). In this case,
@@ -391,6 +425,7 @@ void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
#if defined(OS_WIN)
void CommandLine::ParseFromString(const string16& command_line) {
+ DCHECK(writeable_);
string16 command_line_string;
TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
if (command_line_string.empty())
@@ -407,6 +442,12 @@ void CommandLine::ParseFromString(const string16& command_line) {
}
#endif
+void CommandLine::MakeReadOnly() {
+ for (const auto& i : switches_)
+ switch_vector_.push_back(i.first);
+ std::sort(switch_vector_.begin(), switch_vector_.end());
+}
+
CommandLine::StringType CommandLine::GetCommandLineStringInternal(
bool quote_placeholders) const {
StringType string(argv_[0]);
@@ -452,4 +493,15 @@ CommandLine::StringType CommandLine::GetArgumentsStringInternal(
return params;
}
+bool CommandLine::HasSwitchVector(const char string_constant[]) const {
+ DCHECK(!writeable_);
+ auto lb = std::lower_bound(switch_vector_.begin(), switch_vector_.end(),
+ string_constant, CompareStringAndCharPtr);
+ return lb != switch_vector_.end() && !strcmp(lb->c_str(), string_constant);
+}
+
+bool CommandLine::HasSwitchLowerCase(const char string_constant[]) const {
+ return switches_.find(std::string(string_constant)) != switches_.end();
+}
+
} // namespace base
« 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