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

Unified Diff: base/command_line.cc

Issue 1046363002: Enforce lowercase switches when calling CommandLine::HasSwitch(const char*). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove EXPECT_DEBUG_DEATH 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 | « no previous file | base/command_line_unittest.cc » ('j') | base/command_line_unittest.cc » ('J')
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..136a8b34ffb744e24416bb72953c79f83345d06b 100644
--- a/base/command_line.cc
+++ b/base/command_line.cc
@@ -93,6 +93,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) {
@@ -263,11 +271,18 @@ void CommandLine::SetProgram(const FilePath& program) {
}
bool CommandLine::HasSwitch(const std::string& switch_string) const {
tapted 2015/04/02 01:46:14 How many times is the std::string version called n
jackhou1 2015/04/02 04:51:51 Agreed. 1% of calls.
- 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));
tapted 2015/04/02 01:46:14 I think we can avoid the extra function with somet
jackhou1 2015/04/02 04:51:51 Done.
+ return switches_.find(std::string(string_constant)) != switches_.end();
tapted 2015/04/02 01:46:15 is the std::string(..) still needed?
jackhou1 2015/04/02 04:51:51 Nope, but the one in the DEBUG_EQ is.
}
std::string CommandLine::GetSwitchValueASCII(
@@ -291,8 +306,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)
tapted 2015/04/02 01:46:14 I think we should defer this until there's a char[
jackhou1 2015/04/02 04:51:51 This method is about 9% of lookups during startup,
+ 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;
}
« no previous file with comments | « no previous file | base/command_line_unittest.cc » ('j') | base/command_line_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698