Skip to content

Commit cb62c24

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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 50e42c9 commit cb62c24

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/node_options-inl.h

+24-24
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

+21-25
Original file line numberDiff line numberDiff line change
@@ -239,43 +239,39 @@ class OptionsParser {
239239
struct NoOp {};
240240
struct V8Option {};
241241

242-
// TODO(addaleax): A lot of the `std::string` usage here could be reduced
243-
// to simple `const char*`s if it's reasonable to expect the values to be
244-
// known at compile-time.
245-
246242
// These methods add a single option to the parser. Optionally, it can be
247243
// specified whether the option should be allowed from environment variable
248244
// sources (i.e. NODE_OPTIONS).
249-
void AddOption(const std::string& name,
250-
const std::string& help_text,
245+
void AddOption(const char* name,
246+
const char* help_text,
251247
bool Options::* field,
252248
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
253-
void AddOption(const std::string& name,
254-
const std::string& help_text,
249+
void AddOption(const char* name,
250+
const char* help_text,
255251
uint64_t Options::* field,
256252
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
257-
void AddOption(const std::string& name,
258-
const std::string& help_text,
253+
void AddOption(const char* name,
254+
const char* help_text,
259255
int64_t Options::* field,
260256
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
261-
void AddOption(const std::string& name,
262-
const std::string& help_text,
257+
void AddOption(const char* name,
258+
const char* help_text,
263259
std::string Options::* field,
264260
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
265-
void AddOption(const std::string& name,
266-
const std::string& help_text,
261+
void AddOption(const char* name,
262+
const char* help_text,
267263
std::vector<std::string> Options::* field,
268264
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
269-
void AddOption(const std::string& name,
270-
const std::string& help_text,
265+
void AddOption(const char* name,
266+
const char* help_text,
271267
HostPort Options::* field,
272268
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
273-
void AddOption(const std::string& name,
274-
const std::string& help_text,
269+
void AddOption(const char* name,
270+
const char* help_text,
275271
NoOp no_op_tag,
276272
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
277-
void AddOption(const std::string& name,
278-
const std::string& help_text,
273+
void AddOption(const char* name,
274+
const char* help_text,
279275
V8Option v8_option_tag,
280276
OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
281277

@@ -286,15 +282,15 @@ class OptionsParser {
286282
// the option is presented in that form (i.e. with a '=').
287283
// If `from` has the form "--option-a <arg>", the alias will only be expanded
288284
// if the option has a non-option argument (not starting with -) following it.
289-
void AddAlias(const std::string& from, const std::string& to);
290-
void AddAlias(const std::string& from, const std::vector<std::string>& to);
291-
void AddAlias(const std::string& from,
285+
void AddAlias(const char* from, const char* to);
286+
void AddAlias(const char* from, const std::vector<std::string>& to);
287+
void AddAlias(const char* from,
292288
const std::initializer_list<std::string>& to);
293289

294290
// Add implications from some arbitrary option to a boolean one, either
295291
// in a way that makes `from` set `to` to true or to false.
296-
void Implies(const std::string& from, const std::string& to);
297-
void ImpliesNot(const std::string& from, const std::string& to);
292+
void Implies(const char* from, const char* to);
293+
void ImpliesNot(const char* from, const char* to);
298294

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

0 commit comments

Comments
 (0)