Index: base/command_line.h |
diff --git a/base/command_line.h b/base/command_line.h |
index 439921ed6e50ae08a7054740b4559ee0546d3e71..bfccb5e66d54ac634563f34d6074d09409e17c51 100644 |
--- a/base/command_line.h |
+++ b/base/command_line.h |
@@ -22,6 +22,7 @@ |
#include "base/base_export.h" |
#include "base/strings/string16.h" |
+#include "base/strings/string_piece.h" |
#include "build/build_config.h" |
namespace base { |
@@ -40,6 +41,7 @@ class BASE_EXPORT CommandLine { |
typedef StringType::value_type CharType; |
typedef std::vector<StringType> StringVector; |
typedef std::map<std::string, StringType> SwitchMap; |
+ typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap; |
// A constructor for CommandLines that only carry switches and arguments. |
enum NoProgram { NO_PROGRAM }; |
@@ -52,6 +54,10 @@ class BASE_EXPORT CommandLine { |
CommandLine(int argc, const CharType* const* argv); |
explicit CommandLine(const StringVector& argv); |
+ // Override copy and assign to ensure |switches_by_stringpiece_| is valid. |
+ CommandLine(const CommandLine& other); |
+ CommandLine& operator=(const CommandLine& other); |
+ |
~CommandLine(); |
#if defined(OS_WIN) |
@@ -142,17 +148,18 @@ class BASE_EXPORT CommandLine { |
void SetProgram(const FilePath& program); |
// Returns true if this command line contains the given switch. |
- // Switch names should only be lowercase. |
+ // Switch names must be lowercase. |
// The second override provides an optimized version to avoid inlining the |
- // codegen for the string allocation. |
- bool HasSwitch(const std::string& switch_string) const; |
+ // codegen to find the length of the constant and construct a StringPiece. |
+ bool HasSwitch(const base::StringPiece& switch_string) const; |
bool HasSwitch(const char switch_constant[]) const; |
brettw
2015/04/24 19:50:50
I think this second version is no longer necessary
jackhou1
2015/05/11 13:51:42
IIUC having the second version saves an appreciabl
|
// Returns the value associated with the given switch. If the switch has no |
// value or isn't present, this method returns the empty string. |
- std::string GetSwitchValueASCII(const std::string& switch_string) const; |
- FilePath GetSwitchValuePath(const std::string& switch_string) const; |
- StringType GetSwitchValueNative(const std::string& switch_string) const; |
+ // Switch names must be lowercase. |
+ std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const; |
+ FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const; |
+ StringType GetSwitchValueNative(const base::StringPiece& switch_string) const; |
// Get a copy of all switches, along with their values. |
const SwitchMap& GetSwitches() const { return switches_; } |
@@ -223,6 +230,12 @@ class BASE_EXPORT CommandLine { |
// Parsed-out switch keys and values. |
SwitchMap switches_; |
+ // A mirror of |switches_| with only references to the actual strings. |
+ // The StringPiece internally holds a pointer to a key in |switches_| while |
+ // the mapped_type points to a value in |switches_|. |
+ // Used for allocation-free lookups. |
+ StringPieceSwitchMap switches_by_stringpiece_; |
+ |
// The index after the program and switches, any arguments start here. |
size_t begin_args_; |
}; |