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

Side by Side Diff: base/command_line.h

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, 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') | no next file with comments »
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.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 bool HasSwitch(const char switch_constant[]) const; 149 bool HasSwitch(const char switch_constant[]) const;
150 150
151 // Returns the value associated with the given switch. If the switch has no 151 // 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. 152 // value or isn't present, this method returns the empty string.
153 std::string GetSwitchValueASCII(const std::string& switch_string) const; 153 std::string GetSwitchValueASCII(const std::string& switch_string) const;
154 FilePath GetSwitchValuePath(const std::string& switch_string) const; 154 FilePath GetSwitchValuePath(const std::string& switch_string) const;
155 StringType GetSwitchValueNative(const std::string& switch_string) const; 155 StringType GetSwitchValueNative(const std::string& switch_string) const;
156 156
157 // Get a copy of all switches, along with their values. 157 // Get a copy of all switches, along with their values.
158 const SwitchMap& GetSwitches() const { return switches_; } 158 const SwitchMap& GetSwitches() const { return switches_; }
159 const std::vector<std::string>& GetSwitchVector() const {
160 return switch_vector_;
161 }
159 162
160 // Append a switch [with optional value] to the command line. 163 // Append a switch [with optional value] to the command line.
161 // Note: Switches will precede arguments regardless of appending order. 164 // Note: Switches will precede arguments regardless of appending order.
162 void AppendSwitch(const std::string& switch_string); 165 void AppendSwitch(const std::string& switch_string);
163 void AppendSwitchPath(const std::string& switch_string, 166 void AppendSwitchPath(const std::string& switch_string,
164 const FilePath& path); 167 const FilePath& path);
165 void AppendSwitchNative(const std::string& switch_string, 168 void AppendSwitchNative(const std::string& switch_string,
166 const StringType& value); 169 const StringType& value);
167 void AppendSwitchASCII(const std::string& switch_string, 170 void AppendSwitchASCII(const std::string& switch_string,
168 const std::string& value); 171 const std::string& value);
(...skipping 22 matching lines...) Expand all
191 // Insert a command before the current command. 194 // Insert a command before the current command.
192 // Common for debuggers, like "valgrind" or "gdb --args". 195 // Common for debuggers, like "valgrind" or "gdb --args".
193 void PrependWrapper(const StringType& wrapper); 196 void PrependWrapper(const StringType& wrapper);
194 197
195 #if defined(OS_WIN) 198 #if defined(OS_WIN)
196 // Initialize by parsing the given command line string. 199 // Initialize by parsing the given command line string.
197 // The program name is assumed to be the first item in the string. 200 // The program name is assumed to be the first item in the string.
198 void ParseFromString(const base::string16& command_line); 201 void ParseFromString(const base::string16& command_line);
199 #endif 202 #endif
200 203
204 // Optimizes lookups into this CommandLine, but it can no longer be modified.
205 void MakeReadOnly();
206
201 private: 207 private:
202 // Disallow default constructor; a program name must be explicitly specified. 208 // Disallow default constructor; a program name must be explicitly specified.
203 CommandLine(); 209 CommandLine();
204 // Allow the copy constructor. A common pattern is to copy of the current 210 // Allow the copy constructor. A common pattern is to copy of the current
205 // process's command line and then add some flags to it. For example: 211 // process's command line and then add some flags to it. For example:
206 // CommandLine cl(*CommandLine::ForCurrentProcess()); 212 // CommandLine cl(*CommandLine::ForCurrentProcess());
207 // cl.AppendSwitch(...); 213 // cl.AppendSwitch(...);
208 214
209 // Internal version of GetCommandLineString. If |quote_placeholders| is true, 215 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
210 // also quotes parts with '%' in them. 216 // also quotes parts with '%' in them.
211 StringType GetCommandLineStringInternal(bool quote_placeholders) const; 217 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
212 218
213 // Internal version of GetArgumentsString. If |quote_placeholders| is true, 219 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
214 // also quotes parts with '%' in them. 220 // also quotes parts with '%' in them.
215 StringType GetArgumentsStringInternal(bool quote_placeholders) const; 221 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
216 222
223 // Search for the switch in |switch_vector_|.
224 bool HasSwitchVector(const char string_constant[]) const;
225
226 // Search for the switch assuming the input is lowercase.
227 bool HasSwitchLowerCase(const char string_constant[]) const;
228
217 // The singleton CommandLine representing the current process's command line. 229 // The singleton CommandLine representing the current process's command line.
218 static CommandLine* current_process_commandline_; 230 static CommandLine* current_process_commandline_;
219 231
220 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 232 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
221 StringVector argv_; 233 StringVector argv_;
222 234
223 // Parsed-out switch keys and values. 235 // Parsed-out switch keys and values.
224 SwitchMap switches_; 236 SwitchMap switches_;
225 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_;
240
241 // A vector of switches. This allows looking up a const char* switch without
242 // allocating a string. It is initialized when this CommandLine is made
243 // read-only.
244 std::vector<std::string> switch_vector_;
245
246 // This is un-set when this CommandLine is made read-only.
247 bool writeable_;
228 }; 248 };
229 249
230 } // namespace base 250 } // namespace base
231 251
232 #endif // BASE_COMMAND_LINE_H_ 252 #endif // BASE_COMMAND_LINE_H_
OLDNEW
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698