From 8e6175e001a16939ced16a01a900d6bb9a8afc86 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 6 Jan 2019 02:48:05 +0100 Subject: [PATCH] src: use generic helper for splitting strings PR-URL: https://github.com/nodejs/node/pull/25363 Reviewed-By: James M Snell Reviewed-By: Richard Lau --- 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 6401010f4371e4..f6a0995fe95f2c 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)), @@ -1424,23 +1427,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..2fa16cadeac462 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,