src: refactor SplitString in util

PR-URL: https://github.com/nodejs/node/pull/48491
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Yagiz Nizipli 2023-06-22 18:23:28 -04:00 committed by GitHub
parent 1f4b0c056c
commit 640a791831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 29 deletions

View File

@ -11,10 +11,11 @@
#endif
#include <errno.h>
#include <sstream>
#include <limits>
#include <algorithm>
#include <cstdlib> // strtoul, errno
#include <limits>
#include <sstream>
#include <string_view>
using v8::Boolean;
using v8::Context;
@ -50,14 +51,15 @@ void DebugOptions::CheckOptions(std::vector<std::string>* errors,
"`node --inspect-brk` instead.");
}
std::vector<std::string> destinations =
SplitString(inspect_publish_uid_string, ',');
using std::string_view_literals::operator""sv;
const std::vector<std::string_view> destinations =
SplitString(inspect_publish_uid_string, ","sv);
inspect_publish_uid.console = false;
inspect_publish_uid.http = false;
for (const std::string& destination : destinations) {
if (destination == "stderr") {
for (const std::string_view destination : destinations) {
if (destination == "stderr"sv) {
inspect_publish_uid.console = true;
} else if (destination == "http") {
} else if (destination == "http"sv) {
inspect_publish_uid.http = true;
} else {
errors->push_back("--inspect-publish-uid destination can be "

View File

@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <memory>
#include <string_view>
#include "env-inl.h"
#include "node.h"
@ -126,15 +127,23 @@ struct V8Platform {
}
inline void StartTracingAgent() {
constexpr auto convert_to_set =
[](std::vector<std::string_view> categories) -> std::set<std::string> {
std::set<std::string> out;
for (const auto s : categories) {
out.emplace(s);
}
return out;
};
// Attach a new NodeTraceWriter only if this function hasn't been called
// before.
if (tracing_file_writer_.IsDefaultHandle()) {
std::vector<std::string> categories =
SplitString(per_process::cli_options->trace_event_categories, ',');
using std::string_view_literals::operator""sv;
const std::vector<std::string_view> categories =
SplitString(per_process::cli_options->trace_event_categories, ","sv);
tracing_file_writer_ = tracing_agent_->AddClient(
std::set<std::string>(std::make_move_iterator(categories.begin()),
std::make_move_iterator(categories.end())),
convert_to_set(categories),
std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)),

View File

@ -9,6 +9,7 @@
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
namespace {
@ -74,8 +75,9 @@ namespace permission {
// allow = '*'
// allow = '/tmp/,/home/example.js'
void FSPermission::Apply(const std::string& allow, PermissionScope scope) {
for (const auto& res : SplitString(allow, ',')) {
if (res == "*") {
using std::string_view_literals::operator""sv;
for (const std::string_view res : SplitString(allow, ","sv)) {
if (res == "*"sv) {
if (scope == PermissionScope::kFileSystemRead) {
deny_all_in_ = false;
allow_all_in_ = true;
@ -85,7 +87,7 @@ void FSPermission::Apply(const std::string& allow, PermissionScope scope) {
}
return;
}
GrantAccess(scope, res);
GrantAccess(scope, std::string(res.data(), res.size()));
}
}

View File

@ -169,19 +169,21 @@ std::string GetHumanReadableProcessName() {
return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid());
}
std::vector<std::string> SplitString(const std::string& in,
char delim,
bool skipEmpty) {
std::vector<std::string> out;
if (in.empty())
return out;
std::istringstream in_stream(in);
while (in_stream.good()) {
std::string item;
std::getline(in_stream, item, delim);
if (item.empty() && skipEmpty) continue;
out.emplace_back(std::move(item));
std::vector<std::string_view> SplitString(const std::string_view in,
const std::string_view delim) {
std::vector<std::string_view> out;
for (auto first = in.data(), second = in.data(), last = first + in.size();
second != last && first != last;
first = second + 1) {
second =
std::find_first_of(first, last, std::cbegin(delim), std::cend(delim));
if (first != second) {
out.emplace_back(first, second - first);
}
}
return out;
}

View File

@ -685,9 +685,8 @@ struct FunctionDeleter {
template <typename T, void (*function)(T*)>
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
std::vector<std::string> SplitString(const std::string& in,
char delim,
bool skipEmpty = true);
std::vector<std::string_view> SplitString(const std::string_view in,
const std::string_view delim);
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,