Skip to content

Commit c3386e6

Browse files
ZYSzysBridgeAR
authored andcommitted
src: reduce to simple const char* in OptionsParser
> A lot of the `std::string` usage here could be reduced to simple `const char*`s if it's reasonable to expect the values to be known at compile-time. So this commit uses `const char*` to replace most of `std::string` in `OptionsParser`. PR-URL: #26297 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 3826d69 commit c3386e6

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/node_options-inl.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ EnvironmentOptions* PerIsolateOptions::get_per_env_options() {
2828
namespace options_parser {
2929

3030
template <typename Options>
31-
void OptionsParser<Options>::AddOption(const std::string& name,
32-
const std::string& help_text,
31+
void OptionsParser<Options>::AddOption(const char* name,
32+
const char* help_text,
3333
bool Options::* field,
3434
OptionEnvvarSettings env_setting) {
3535
options_.emplace(name,
@@ -40,8 +40,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
4040
}
4141

4242
template <typename Options>
43-
void OptionsParser<Options>::AddOption(const std::string& name,
44-
const std::string& help_text,
43+
void OptionsParser<Options>::AddOption(const char* name,
44+
const char* help_text,
4545
uint64_t Options::* field,
4646
OptionEnvvarSettings env_setting) {
4747
options_.emplace(
@@ -53,8 +53,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
5353
}
5454

5555
template <typename Options>
56-
void OptionsParser<Options>::AddOption(const std::string& name,
57-
const std::string& help_text,
56+
void OptionsParser<Options>::AddOption(const char* name,
57+
const char* help_text,
5858
int64_t Options::* field,
5959
OptionEnvvarSettings env_setting) {
6060
options_.emplace(
@@ -66,8 +66,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
6666
}
6767

6868
template <typename Options>
69-
void OptionsParser<Options>::AddOption(const std::string& name,
70-
const std::string& help_text,
69+
void OptionsParser<Options>::AddOption(const char* name,
70+
const char* help_text,
7171
std::string Options::* field,
7272
OptionEnvvarSettings env_setting) {
7373
options_.emplace(
@@ -80,8 +80,8 @@ void OptionsParser<Options>::AddOption(const std::string& name,
8080

8181
template <typename Options>
8282
void OptionsParser<Options>::AddOption(
83-
const std::string& name,
84-
const std::string& help_text,
83+
const char* name,
84+
const char* help_text,
8585
std::vector<std::string> Options::* field,
8686
OptionEnvvarSettings env_setting) {
8787
options_.emplace(name, OptionInfo {
@@ -93,8 +93,8 @@ void OptionsParser<Options>::AddOption(
9393
}
9494

9595
template <typename Options>
96-
void OptionsParser<Options>::AddOption(const std::string& name,
97-
const std::string& help_text,
96+
void OptionsParser<Options>::AddOption(const char* name,
97+
const char* help_text,
9898
HostPort Options::* field,
9999
OptionEnvvarSettings env_setting) {
100100
options_.emplace(
@@ -106,44 +106,44 @@ void OptionsParser<Options>::AddOption(const std::string& name,
106106
}
107107

108108
template <typename Options>
109-
void OptionsParser<Options>::AddOption(const std::string& name,
110-
const std::string& help_text,
109+
void OptionsParser<Options>::AddOption(const char* name,
110+
const char* help_text,
111111
NoOp no_op_tag,
112112
OptionEnvvarSettings env_setting) {
113113
options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text});
114114
}
115115

116116
template <typename Options>
117-
void OptionsParser<Options>::AddOption(const std::string& name,
118-
const std::string& help_text,
117+
void OptionsParser<Options>::AddOption(const char* name,
118+
const char* help_text,
119119
V8Option v8_option_tag,
120120
OptionEnvvarSettings env_setting) {
121121
options_.emplace(name,
122122
OptionInfo{kV8Option, nullptr, env_setting, help_text});
123123
}
124124

125125
template <typename Options>
126-
void OptionsParser<Options>::AddAlias(const std::string& from,
127-
const std::string& to) {
126+
void OptionsParser<Options>::AddAlias(const char* from,
127+
const char* to) {
128128
aliases_[from] = { to };
129129
}
130130

131131
template <typename Options>
132-
void OptionsParser<Options>::AddAlias(const std::string& from,
132+
void OptionsParser<Options>::AddAlias(const char* from,
133133
const std::vector<std::string>& to) {
134134
aliases_[from] = to;
135135
}
136136

137137
template <typename Options>
138138
void OptionsParser<Options>::AddAlias(
139-
const std::string& from,
139+
const char* from,
140140
const std::initializer_list<std::string>& to) {
141141
AddAlias(from, std::vector<std::string>(to));
142142
}
143143

144144
template <typename Options>
145-
void OptionsParser<Options>::Implies(const std::string& from,
146-
const std::string& to) {
145+
void OptionsParser<Options>::Implies(const char* from,
146+
const char* to) {
147147
auto it = options_.find(to);
148148
CHECK_NE(it, options_.end());
149149
CHECK_EQ(it->second.type, kBoolean);
@@ -153,8 +153,8 @@ void OptionsParser<Options>::Implies(const std::string& from,
153153
}
154154

155155
template <typename Options>
156-
void OptionsParser<Options>::ImpliesNot(const std::string& from,
157-
const std::string& to) {
156+
void OptionsParser<Options>::ImpliesNot(const char* from,
157+
const char* to) {
158158
auto it = options_.find(to);
159159
CHECK_NE(it, options_.end());
160160
CHECK_EQ(it->second.type, kBoolean);

src/node_options.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,43 +229,39 @@ class OptionsParser {
229229
struct NoOp {};
230230
struct V8Option {};
231231

232-
// TODO(addaleax): A lot of the `std::string` usage here could be reduced
233-
// to simple `const char*`s if it's reasonable to expect the values to be
234-
// known at compile-time.
235-
236232
// These methods add a single option to the parser. Optionally, it can be
237233
// specified whether the option should be allowed from environment variable
238234
// sources (i.e. NODE_OPTIONS).
239-
void AddOption(const std::string& name,
240-
const std::string& help_text,
235+
void AddOption(const char* name,
236+
const char* help_text,
241237
bool Options::* field,
242238
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
243-
void AddOption(const std::string& name,
244-
const std::string& help_text,
239+
void AddOption(const char* name,
240+
const char* help_text,
245241
uint64_t Options::* field,
246242
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
247-
void AddOption(const std::string& name,
248-
const std::string& help_text,
243+
void AddOption(const char* name,
244+
const char* help_text,
249245
int64_t Options::* field,
250246
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
251-
void AddOption(const std::string& name,
252-
const std::string& help_text,
247+
void AddOption(const char* name,
248+
const char* help_text,
253249
std::string Options::* field,
254250
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
255-
void AddOption(const std::string& name,
256-
const std::string& help_text,
251+
void AddOption(const char* name,
252+
const char* help_text,
257253
std::vector<std::string> Options::* field,
258254
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
259-
void AddOption(const std::string& name,
260-
const std::string& help_text,
255+
void AddOption(const char* name,
256+
const char* help_text,
261257
HostPort Options::* field,
262258
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
263-
void AddOption(const std::string& name,
264-
const std::string& help_text,
259+
void AddOption(const char* name,
260+
const char* help_text,
265261
NoOp no_op_tag,
266262
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
267-
void AddOption(const std::string& name,
268-
const std::string& help_text,
263+
void AddOption(const char* name,
264+
const char* help_text,
269265
V8Option v8_option_tag,
270266
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
271267

@@ -276,15 +272,15 @@ class OptionsParser {
276272
// the option is presented in that form (i.e. with a '=').
277273
// If `from` has the form "--option-a <arg>", the alias will only be expanded
278274
// if the option has a non-option argument (not starting with -) following it.
279-
void AddAlias(const std::string& from, const std::string& to);
280-
void AddAlias(const std::string& from, const std::vector<std::string>& to);
281-
void AddAlias(const std::string& from,
275+
void AddAlias(const char* from, const char* to);
276+
void AddAlias(const char* from, const std::vector<std::string>& to);
277+
void AddAlias(const char* from,
282278
const std::initializer_list<std::string>& to);
283279

284280
// Add implications from some arbitrary option to a boolean one, either
285281
// in a way that makes `from` set `to` to true or to false.
286-
void Implies(const std::string& from, const std::string& to);
287-
void ImpliesNot(const std::string& from, const std::string& to);
282+
void Implies(const char* from, const char* to);
283+
void ImpliesNot(const char* from, const char* to);
288284

289285
// Insert options from another options parser into this one, along with
290286
// a method that yields the target options type from this parser's options

0 commit comments

Comments
 (0)