From edfb8b6039907869ce0cbfdb5bcbae258eef9480 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 6 Jan 2019 02:48:05 +0100 Subject: [PATCH 1/2] src: use generic helper for splitting strings --- src/node.cc | 28 +++++++++------------------- src/util.cc | 8 ++++---- src/util.h | 2 +- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/node.cc b/src/node.cc index 01c9c072da7466..9a6c62c373a88c 100644 --- a/src/node.cc +++ b/src/node.cc @@ -282,9 +282,12 @@ static struct { if (per_process::cli_options->trace_event_categories.empty()) { tracing_file_writer_ = tracing_agent_->DefaultHandle(); } else { + std::vector categories = + SplitString(per_process::cli_options->trace_event_categories); + tracing_file_writer_ = tracing_agent_->AddClient( - ParseCommaSeparatedSet( - per_process::cli_options->trace_event_categories), + std::set(std::make_move_iterator(categories.begin()), + std::make_move_iterator(categories.end())), std::unique_ptr( new tracing::NodeTraceWriter( per_process::cli_options->trace_event_file_pattern)), @@ -1420,23 +1423,10 @@ void Init(std::vector* argv, #if !defined(NODE_WITHOUT_NODE_OPTIONS) std::string node_options; if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { - std::vector env_argv; - // [0] is expected to be the program name, fill it in from the real argv. - env_argv.push_back(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); - + // [0] is expected to be the program name, fill it in from the real argv + // and use 'x' as a placeholder while parsing. + std::vector env_argv = SplitString("x " + node_options, ' '); + env_argv[0] = argv->at(0); ProcessArgv(&env_argv, nullptr, true); } diff --git a/src/util.cc b/src/util.cc index e88b39f47b8ddc..9f32814a8599ec 100644 --- a/src/util.cc +++ b/src/util.cc @@ -122,15 +122,15 @@ void GetHumanReadableProcessName(char (*name)[1024]) { snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid()); } -std::set ParseCommaSeparatedSet(const std::string& in) { - std::set out; +std::vector SplitString(const std::string& in, char delim) { + std::vector out; if (in.empty()) return out; std::istringstream in_stream(in); while (in_stream.good()) { std::string item; - getline(in_stream, item, ','); - out.emplace(std::move(item)); + std::getline(in_stream, item, delim); + out.emplace_back(std::move(item)); } return out; } diff --git a/src/util.h b/src/util.h index 1205f2d06b142d..3aa7f56dd10fbe 100644 --- a/src/util.h +++ b/src/util.h @@ -523,7 +523,7 @@ struct FunctionDeleter { template using DeleteFnPtr = typename FunctionDeleter::Pointer; -std::set ParseCommaSeparatedSet(const std::string& in); +std::vector SplitString(const std::string& in, char delim = ','); inline v8::MaybeLocal ToV8Value(v8::Local context, const std::string& str, From 3e744feab088fbe5d170a4283a901cbc745af276 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 6 Jan 2019 22:35:43 +0100 Subject: [PATCH 2/2] fixup! src: use generic helper for splitting strings --- src/node.cc | 2 +- src/util.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node.cc b/src/node.cc index 9a6c62c373a88c..24b0167b891272 100644 --- a/src/node.cc +++ b/src/node.cc @@ -283,7 +283,7 @@ static struct { tracing_file_writer_ = tracing_agent_->DefaultHandle(); } else { std::vector categories = - SplitString(per_process::cli_options->trace_event_categories); + SplitString(per_process::cli_options->trace_event_categories, ','); tracing_file_writer_ = tracing_agent_->AddClient( std::set(std::make_move_iterator(categories.begin()), diff --git a/src/util.h b/src/util.h index 3aa7f56dd10fbe..2fa16cadeac462 100644 --- a/src/util.h +++ b/src/util.h @@ -523,7 +523,7 @@ struct FunctionDeleter { template using DeleteFnPtr = typename FunctionDeleter::Pointer; -std::vector SplitString(const std::string& in, char delim = ','); +std::vector SplitString(const std::string& in, char delim); inline v8::MaybeLocal ToV8Value(v8::Local context, const std::string& str,