Skip to content

Commit 640a791

Browse files
authored
src: refactor SplitString in util
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>
1 parent 1f4b0c0 commit 640a791

File tree

5 files changed

+43
-29
lines changed

5 files changed

+43
-29
lines changed

src/node_options.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
#endif
1212

1313
#include <errno.h>
14-
#include <sstream>
15-
#include <limits>
1614
#include <algorithm>
1715
#include <cstdlib> // strtoul, errno
16+
#include <limits>
17+
#include <sstream>
18+
#include <string_view>
1819

1920
using v8::Boolean;
2021
using v8::Context;
@@ -50,14 +51,15 @@ void DebugOptions::CheckOptions(std::vector<std::string>* errors,
5051
"`node --inspect-brk` instead.");
5152
}
5253

53-
std::vector<std::string> destinations =
54-
SplitString(inspect_publish_uid_string, ',');
54+
using std::string_view_literals::operator""sv;
55+
const std::vector<std::string_view> destinations =
56+
SplitString(inspect_publish_uid_string, ","sv);
5557
inspect_publish_uid.console = false;
5658
inspect_publish_uid.http = false;
57-
for (const std::string& destination : destinations) {
58-
if (destination == "stderr") {
59+
for (const std::string_view destination : destinations) {
60+
if (destination == "stderr"sv) {
5961
inspect_publish_uid.console = true;
60-
} else if (destination == "http") {
62+
} else if (destination == "http"sv) {
6163
inspect_publish_uid.http = true;
6264
} else {
6365
errors->push_back("--inspect-publish-uid destination can be "

src/node_v8_platform-inl.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include <memory>
7+
#include <string_view>
78

89
#include "env-inl.h"
910
#include "node.h"
@@ -126,15 +127,23 @@ struct V8Platform {
126127
}
127128

128129
inline void StartTracingAgent() {
130+
constexpr auto convert_to_set =
131+
[](std::vector<std::string_view> categories) -> std::set<std::string> {
132+
std::set<std::string> out;
133+
for (const auto s : categories) {
134+
out.emplace(s);
135+
}
136+
return out;
137+
};
129138
// Attach a new NodeTraceWriter only if this function hasn't been called
130139
// before.
131140
if (tracing_file_writer_.IsDefaultHandle()) {
132-
std::vector<std::string> categories =
133-
SplitString(per_process::cli_options->trace_event_categories, ',');
141+
using std::string_view_literals::operator""sv;
142+
const std::vector<std::string_view> categories =
143+
SplitString(per_process::cli_options->trace_event_categories, ","sv);
134144

135145
tracing_file_writer_ = tracing_agent_->AddClient(
136-
std::set<std::string>(std::make_move_iterator(categories.begin()),
137-
std::make_move_iterator(categories.end())),
146+
convert_to_set(categories),
138147
std::unique_ptr<tracing::AsyncTraceWriter>(
139148
new tracing::NodeTraceWriter(
140149
per_process::cli_options->trace_event_file_pattern)),

src/permission/fs_permission.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <algorithm>
1010
#include <filesystem>
1111
#include <string>
12+
#include <string_view>
1213
#include <vector>
1314

1415
namespace {
@@ -74,8 +75,9 @@ namespace permission {
7475
// allow = '*'
7576
// allow = '/tmp/,/home/example.js'
7677
void FSPermission::Apply(const std::string& allow, PermissionScope scope) {
77-
for (const auto& res : SplitString(allow, ',')) {
78-
if (res == "*") {
78+
using std::string_view_literals::operator""sv;
79+
for (const std::string_view res : SplitString(allow, ","sv)) {
80+
if (res == "*"sv) {
7981
if (scope == PermissionScope::kFileSystemRead) {
8082
deny_all_in_ = false;
8183
allow_all_in_ = true;
@@ -85,7 +87,7 @@ void FSPermission::Apply(const std::string& allow, PermissionScope scope) {
8587
}
8688
return;
8789
}
88-
GrantAccess(scope, res);
90+
GrantAccess(scope, std::string(res.data(), res.size()));
8991
}
9092
}
9193

src/util.cc

+14-12
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,21 @@ std::string GetHumanReadableProcessName() {
169169
return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid());
170170
}
171171

172-
std::vector<std::string> SplitString(const std::string& in,
173-
char delim,
174-
bool skipEmpty) {
175-
std::vector<std::string> out;
176-
if (in.empty())
177-
return out;
178-
std::istringstream in_stream(in);
179-
while (in_stream.good()) {
180-
std::string item;
181-
std::getline(in_stream, item, delim);
182-
if (item.empty() && skipEmpty) continue;
183-
out.emplace_back(std::move(item));
172+
std::vector<std::string_view> SplitString(const std::string_view in,
173+
const std::string_view delim) {
174+
std::vector<std::string_view> out;
175+
176+
for (auto first = in.data(), second = in.data(), last = first + in.size();
177+
second != last && first != last;
178+
first = second + 1) {
179+
second =
180+
std::find_first_of(first, last, std::cbegin(delim), std::cend(delim));
181+
182+
if (first != second) {
183+
out.emplace_back(first, second - first);
184+
}
184185
}
186+
185187
return out;
186188
}
187189

src/util.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,8 @@ struct FunctionDeleter {
685685
template <typename T, void (*function)(T*)>
686686
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
687687

688-
std::vector<std::string> SplitString(const std::string& in,
689-
char delim,
690-
bool skipEmpty = true);
688+
std::vector<std::string_view> SplitString(const std::string_view in,
689+
const std::string_view delim);
691690

692691
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
693692
std::string_view str,

0 commit comments

Comments
 (0)