Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d73009

Browse filesBrowse files
anonrigruyadorno
authored andcommitted
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 e1339d5 commit 0d73009
Copy full SHA for 0d73009

File tree

Expand file treeCollapse file tree

5 files changed

+43
-29
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+43
-29
lines changed
Open diff view settings
Collapse file

‎src/node_options.cc‎

Copy file name to clipboardExpand all lines: src/node_options.cc
+9-7Lines changed: 9 additions & 7 deletions
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 "
Collapse file

‎src/node_v8_platform-inl.h‎

Copy file name to clipboardExpand all lines: src/node_v8_platform-inl.h
+13-4Lines changed: 13 additions & 4 deletions
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)),
Collapse file

‎src/permission/fs_permission.cc‎

Copy file name to clipboardExpand all lines: src/permission/fs_permission.cc
+5-3Lines changed: 5 additions & 3 deletions
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

Collapse file

‎src/util.cc‎

Copy file name to clipboardExpand all lines: src/util.cc
+14-12Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,21 @@ std::string GetHumanReadableProcessName() {
165165
return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid());
166166
}
167167

168-
std::vector<std::string> SplitString(const std::string& in,
169-
char delim,
170-
bool skipEmpty) {
171-
std::vector<std::string> out;
172-
if (in.empty())
173-
return out;
174-
std::istringstream in_stream(in);
175-
while (in_stream.good()) {
176-
std::string item;
177-
std::getline(in_stream, item, delim);
178-
if (item.empty() && skipEmpty) continue;
179-
out.emplace_back(std::move(item));
168+
std::vector<std::string_view> SplitString(const std::string_view in,
169+
const std::string_view delim) {
170+
std::vector<std::string_view> out;
171+
172+
for (auto first = in.data(), second = in.data(), last = first + in.size();
173+
second != last && first != last;
174+
first = second + 1) {
175+
second =
176+
std::find_first_of(first, last, std::cbegin(delim), std::cend(delim));
177+
178+
if (first != second) {
179+
out.emplace_back(first, second - first);
180+
}
180181
}
182+
181183
return out;
182184
}
183185

Collapse file

‎src/util.h‎

Copy file name to clipboardExpand all lines: src/util.h
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,8 @@ struct FunctionDeleter {
676676
template <typename T, void (*function)(T*)>
677677
using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
678678

679-
std::vector<std::string> SplitString(const std::string& in,
680-
char delim,
681-
bool skipEmpty = true);
679+
std::vector<std::string_view> SplitString(const std::string_view in,
680+
const std::string_view delim);
682681

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.