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

Side by Side Diff: base/command_line.h

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 unified diff | Download patch
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | base/command_line.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This class works with command lines: building and parsing. 5 // This class works with command lines: building and parsing.
6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7 // Switches will precede all other arguments without switch prefixes. 7 // Switches will precede all other arguments without switch prefixes.
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9 // An argument of "--" will terminate switch parsing during initialization, 9 // An argument of "--" will terminate switch parsing during initialization,
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
11 11
12 // There is a singleton read-only CommandLine that represents the command line 12 // There is a singleton read-only CommandLine that represents the command line
13 // that the current process was started with. It must be initialized in main(). 13 // that the current process was started with. It must be initialized in main().
14 14
15 #ifndef BASE_COMMAND_LINE_H_ 15 #ifndef BASE_COMMAND_LINE_H_
16 #define BASE_COMMAND_LINE_H_ 16 #define BASE_COMMAND_LINE_H_
17 17
18 #include <stddef.h> 18 #include <stddef.h>
19 #include <map> 19 #include <map>
20 #include <string> 20 #include <string>
21 #include <vector> 21 #include <vector>
22 22
23 #include "base/base_export.h" 23 #include "base/base_export.h"
24 #include "base/strings/string16.h" 24 #include "base/strings/string16.h"
25 #include "base/strings/string_piece.h"
25 #include "build/build_config.h" 26 #include "build/build_config.h"
26 27
27 namespace base { 28 namespace base {
28 29
29 class FilePath; 30 class FilePath;
30 31
31 class BASE_EXPORT CommandLine { 32 class BASE_EXPORT CommandLine {
32 public: 33 public:
33 #if defined(OS_WIN) 34 #if defined(OS_WIN)
34 // The native command line string type. 35 // The native command line string type.
(...skipping 10 matching lines...) Expand all
45 enum NoProgram { NO_PROGRAM }; 46 enum NoProgram { NO_PROGRAM };
46 explicit CommandLine(NoProgram no_program); 47 explicit CommandLine(NoProgram no_program);
47 48
48 // Construct a new command line with |program| as argv[0]. 49 // Construct a new command line with |program| as argv[0].
49 explicit CommandLine(const FilePath& program); 50 explicit CommandLine(const FilePath& program);
50 51
51 // Construct a new command line from an argument list. 52 // Construct a new command line from an argument list.
52 CommandLine(int argc, const CharType* const* argv); 53 CommandLine(int argc, const CharType* const* argv);
53 explicit CommandLine(const StringVector& argv); 54 explicit CommandLine(const StringVector& argv);
54 55
56 // Override copy and assign to ensure |switches_by_stringpiece_| is valid.
57 CommandLine(const CommandLine& other);
58 CommandLine& operator=(const CommandLine& other);
59
55 ~CommandLine(); 60 ~CommandLine();
56 61
57 #if defined(OS_WIN) 62 #if defined(OS_WIN)
58 // By default this class will treat command-line arguments beginning with 63 // By default this class will treat command-line arguments beginning with
59 // slashes as switches on Windows, but not other platforms. 64 // slashes as switches on Windows, but not other platforms.
60 // 65 //
61 // If this behavior is inappropriate for your application, you can call this 66 // If this behavior is inappropriate for your application, you can call this
62 // function BEFORE initializing the current process' global command line 67 // function BEFORE initializing the current process' global command line
63 // object and the behavior will be the same as Posix systems (only hyphens 68 // object and the behavior will be the same as Posix systems (only hyphens
64 // begin switches, everything else will be an arg). 69 // begin switches, everything else will be an arg).
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 #endif 140 #endif
136 141
137 // Returns the original command line string as a vector of strings. 142 // Returns the original command line string as a vector of strings.
138 const StringVector& argv() const { return argv_; } 143 const StringVector& argv() const { return argv_; }
139 144
140 // Get and Set the program part of the command line string (the first item). 145 // Get and Set the program part of the command line string (the first item).
141 FilePath GetProgram() const; 146 FilePath GetProgram() const;
142 void SetProgram(const FilePath& program); 147 void SetProgram(const FilePath& program);
143 148
144 // Returns true if this command line contains the given switch. 149 // Returns true if this command line contains the given switch.
145 // (Switch names are case-insensitive). 150 // Switch names must be lowercase.
146 // The second override provides an optimized version to avoid inlining the 151 // The second override provides an optimized version to avoid inlining the
147 // codegen for the string allocation. 152 // codegen to find the length of the constant and construct a StringPiece.
148 bool HasSwitch(const std::string& switch_string) const; 153 bool HasSwitch(const base::StringPiece& switch_string) const;
149 bool HasSwitch(const char switch_constant[]) const; 154 bool HasSwitch(const char switch_constant[]) const;
150 155
151 // Returns the value associated with the given switch. If the switch has no 156 // Returns the value associated with the given switch. If the switch has no
152 // value or isn't present, this method returns the empty string. 157 // value or isn't present, this method returns the empty string.
153 std::string GetSwitchValueASCII(const std::string& switch_string) const; 158 // Switch names must be lowercase.
154 FilePath GetSwitchValuePath(const std::string& switch_string) const; 159 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const;
155 StringType GetSwitchValueNative(const std::string& switch_string) const; 160 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const;
161 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const;
156 162
157 // Get a copy of all switches, along with their values. 163 // Get a reference to the internal switch map.
158 const SwitchMap& GetSwitches() const { return switches_; } 164 const SwitchMap& GetSwitches() const { return switches_; }
159 165
160 // Append a switch [with optional value] to the command line. 166 // Append a switch [with optional value] to the command line.
161 // Note: Switches will precede arguments regardless of appending order. 167 // Note: Switches will precede arguments regardless of appending order.
162 void AppendSwitch(const std::string& switch_string); 168 void AppendSwitch(const std::string& switch_string);
163 void AppendSwitchPath(const std::string& switch_string, 169 void AppendSwitchPath(const std::string& switch_string,
164 const FilePath& path); 170 const FilePath& path);
165 void AppendSwitchNative(const std::string& switch_string, 171 void AppendSwitchNative(const std::string& switch_string,
166 const StringType& value); 172 const StringType& value);
167 void AppendSwitchASCII(const std::string& switch_string, 173 void AppendSwitchASCII(const std::string& switch_string,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 222
217 // The singleton CommandLine representing the current process's command line. 223 // The singleton CommandLine representing the current process's command line.
218 static CommandLine* current_process_commandline_; 224 static CommandLine* current_process_commandline_;
219 225
220 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 226 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
221 StringVector argv_; 227 StringVector argv_;
222 228
223 // Parsed-out switch keys and values. 229 // Parsed-out switch keys and values.
224 SwitchMap switches_; 230 SwitchMap switches_;
225 231
232 // A mirror of |switches_| with only references to the actual strings.
233 // The StringPiece internally holds a pointer to a key in |switches_| while
234 // the mapped_type points to a value in |switches_|.
235 // Used for allocation-free lookups.
236 std::map<base::StringPiece, const StringType*> switches_by_stringpiece_;
237
226 // The index after the program and switches, any arguments start here. 238 // The index after the program and switches, any arguments start here.
227 size_t begin_args_; 239 size_t begin_args_;
228 }; 240 };
229 241
230 } // namespace base 242 } // namespace base
231 243
232 #endif // BASE_COMMAND_LINE_H_ 244 #endif // BASE_COMMAND_LINE_H_
OLDNEW
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | base/command_line.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698