Skip to content

Commit

Permalink
src: refactor SplitString in util
Browse files Browse the repository at this point in the history
PR-URL: #48491
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
anonrig authored and ruyadorno committed Sep 12, 2023
1 parent e1339d5 commit 0d73009
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
16 changes: 9 additions & 7 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 "
Expand Down
17 changes: 13 additions & 4 deletions src/node_v8_platform-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <memory>
#include <string_view>

#include "env-inl.h"
#include "node.h"
Expand Down Expand Up @@ -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)),
Expand Down
8 changes: 5 additions & 3 deletions src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>

namespace {
Expand Down Expand Up @@ -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;
Expand All @@ -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()));
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,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;
}

Expand Down
5 changes: 2 additions & 3 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,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,
Expand Down

0 comments on commit 0d73009

Please sign in to comment.