src: use generic helper for splitting strings

PR-URL: https://github.com/nodejs/node/pull/25363
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Anna Henningsen 2019-01-06 02:48:05 +01:00
parent 0c8dedd103
commit 8ec3c350f5
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
3 changed files with 14 additions and 24 deletions

View File

@ -282,9 +282,12 @@ static struct {
if (per_process::cli_options->trace_event_categories.empty()) { if (per_process::cli_options->trace_event_categories.empty()) {
tracing_file_writer_ = tracing_agent_->DefaultHandle(); tracing_file_writer_ = tracing_agent_->DefaultHandle();
} else { } else {
std::vector<std::string> categories =
SplitString(per_process::cli_options->trace_event_categories, ',');
tracing_file_writer_ = tracing_agent_->AddClient( tracing_file_writer_ = tracing_agent_->AddClient(
ParseCommaSeparatedSet( std::set<std::string>(std::make_move_iterator(categories.begin()),
per_process::cli_options->trace_event_categories), std::make_move_iterator(categories.end())),
std::unique_ptr<tracing::AsyncTraceWriter>( std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter( new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)), per_process::cli_options->trace_event_file_pattern)),
@ -1420,23 +1423,10 @@ void Init(std::vector<std::string>* argv,
#if !defined(NODE_WITHOUT_NODE_OPTIONS) #if !defined(NODE_WITHOUT_NODE_OPTIONS)
std::string node_options; std::string node_options;
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
std::vector<std::string> env_argv; // [0] is expected to be the program name, fill it in from the real argv
// [0] is expected to be the program name, fill it in from the real argv. // and use 'x' as a placeholder while parsing.
env_argv.push_back(argv->at(0)); std::vector<std::string> env_argv = SplitString("x " + node_options, ' ');
env_argv[0] = argv->at(0);
// Split NODE_OPTIONS at each ' ' character.
std::string::size_type index = std::string::npos;
do {
std::string::size_type prev_index = index;
index = node_options.find(' ', index + 1);
if (index - prev_index == 1) continue;
const std::string option = node_options.substr(
prev_index + 1, index - prev_index - 1);
if (!option.empty())
env_argv.emplace_back(std::move(option));
} while (index != std::string::npos);
ProcessArgv(&env_argv, nullptr, true); ProcessArgv(&env_argv, nullptr, true);
} }

View File

@ -122,15 +122,15 @@ void GetHumanReadableProcessName(char (*name)[1024]) {
snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid()); snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
} }
std::set<std::string> ParseCommaSeparatedSet(const std::string& in) { std::vector<std::string> SplitString(const std::string& in, char delim) {
std::set<std::string> out; std::vector<std::string> out;
if (in.empty()) if (in.empty())
return out; return out;
std::istringstream in_stream(in); std::istringstream in_stream(in);
while (in_stream.good()) { while (in_stream.good()) {
std::string item; std::string item;
getline(in_stream, item, ','); std::getline(in_stream, item, delim);
out.emplace(std::move(item)); out.emplace_back(std::move(item));
} }
return out; return out;
} }

View File

@ -523,7 +523,7 @@ struct FunctionDeleter {
template <typename T, void (*function)(T*)> template <typename T, void (*function)(T*)>
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
std::set<std::string> ParseCommaSeparatedSet(const std::string& in); std::vector<std::string> SplitString(const std::string& in, char delim);
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::string& str, const std::string& str,