Skip to content

Commit

Permalink
src: use generic helper for splitting strings
Browse files Browse the repository at this point in the history
PR-URL: #25363
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
addaleax committed Jan 15, 2019
1 parent ab7284a commit d6b4cb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
28 changes: 9 additions & 19 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,12 @@ static struct {
if (per_process::cli_options->trace_event_categories.empty()) {
tracing_file_writer_ = tracing_agent_->DefaultHandle();
} else {
std::vector<std::string> 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::string>(std::make_move_iterator(categories.begin()),
std::make_move_iterator(categories.end())),
std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)),
Expand Down Expand Up @@ -1424,23 +1427,10 @@ void Init(std::vector<std::string>* argv,
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
std::string 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.
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<std::string> env_argv = SplitString("x " + node_options, ' ');
env_argv[0] = argv->at(0);

ProcessArgv(&env_argv, nullptr, true);
}
Expand Down
8 changes: 4 additions & 4 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ void GetHumanReadableProcessName(char (*name)[1024]) {
snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
}

std::set<std::string> ParseCommaSeparatedSet(const std::string& in) {
std::set<std::string> out;
std::vector<std::string> SplitString(const std::string& in, char delim) {
std::vector<std::string> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ struct FunctionDeleter {
template <typename T, void (*function)(T*)>
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,
const std::string& str,
Expand Down

0 comments on commit d6b4cb3

Please sign in to comment.