Skip to content

Commit 8e6175e

Browse files
addaleaxBridgeAR
authored andcommitted
src: use generic helper for splitting strings
PR-URL: #25363 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent 243f902 commit 8e6175e

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

src/node.cc

+9-19
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,12 @@ static struct {
282282
if (per_process::cli_options->trace_event_categories.empty()) {
283283
tracing_file_writer_ = tracing_agent_->DefaultHandle();
284284
} else {
285+
std::vector<std::string> categories =
286+
SplitString(per_process::cli_options->trace_event_categories, ',');
287+
285288
tracing_file_writer_ = tracing_agent_->AddClient(
286-
ParseCommaSeparatedSet(
287-
per_process::cli_options->trace_event_categories),
289+
std::set<std::string>(std::make_move_iterator(categories.begin()),
290+
std::make_move_iterator(categories.end())),
288291
std::unique_ptr<tracing::AsyncTraceWriter>(
289292
new tracing::NodeTraceWriter(
290293
per_process::cli_options->trace_event_file_pattern)),
@@ -1424,23 +1427,10 @@ void Init(std::vector<std::string>* argv,
14241427
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
14251428
std::string node_options;
14261429
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
1427-
std::vector<std::string> env_argv;
1428-
// [0] is expected to be the program name, fill it in from the real argv.
1429-
env_argv.push_back(argv->at(0));
1430-
1431-
// Split NODE_OPTIONS at each ' ' character.
1432-
std::string::size_type index = std::string::npos;
1433-
do {
1434-
std::string::size_type prev_index = index;
1435-
index = node_options.find(' ', index + 1);
1436-
if (index - prev_index == 1) continue;
1437-
1438-
const std::string option = node_options.substr(
1439-
prev_index + 1, index - prev_index - 1);
1440-
if (!option.empty())
1441-
env_argv.emplace_back(std::move(option));
1442-
} while (index != std::string::npos);
1443-
1430+
// [0] is expected to be the program name, fill it in from the real argv
1431+
// and use 'x' as a placeholder while parsing.
1432+
std::vector<std::string> env_argv = SplitString("x " + node_options, ' ');
1433+
env_argv[0] = argv->at(0);
14441434

14451435
ProcessArgv(&env_argv, nullptr, true);
14461436
}

src/util.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ void GetHumanReadableProcessName(char (*name)[1024]) {
122122
snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
123123
}
124124

125-
std::set<std::string> ParseCommaSeparatedSet(const std::string& in) {
126-
std::set<std::string> out;
125+
std::vector<std::string> SplitString(const std::string& in, char delim) {
126+
std::vector<std::string> out;
127127
if (in.empty())
128128
return out;
129129
std::istringstream in_stream(in);
130130
while (in_stream.good()) {
131131
std::string item;
132-
getline(in_stream, item, ',');
133-
out.emplace(std::move(item));
132+
std::getline(in_stream, item, delim);
133+
out.emplace_back(std::move(item));
134134
}
135135
return out;
136136
}

src/util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ struct FunctionDeleter {
523523
template <typename T, void (*function)(T*)>
524524
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
525525

526-
std::set<std::string> ParseCommaSeparatedSet(const std::string& in);
526+
std::vector<std::string> SplitString(const std::string& in, char delim);
527527

528528
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
529529
const std::string& str,

0 commit comments

Comments
 (0)