GeezerVison is showing github.com — src: refactor `SplitString` in util · nodejs/node@c4b8ede
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | #endif | 12 | 12 |
| 13 | 13 | #include <errno.h> | 14 | | -#include <sstream> | 15 | | -#include <limits> | 16 | 14 | #include <algorithm> | 17 | 15 | #include <cstdlib> // strtoul, errno | | 16 | +#include <limits> | | 17 | +#include <sstream> | | 18 | +#include <string_view> | 18 | 19 |
| 19 | 20 | using v8::Boolean; | 20 | 21 | using v8::Context; | @@ -50,14 +51,15 @@ void DebugOptions::CheckOptions(std::vector<std::string>* errors, | 50 | 51 | "`node --inspect-brk` instead."); | 51 | 52 | } | 52 | 53 |
| 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); | 55 | 57 | inspect_publish_uid.console = false; | 56 | 58 | 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) { | 59 | 61 | inspect_publish_uid.console = true; | 60 | | - } else if (destination == "http") { | | 62 | + } else if (destination == "http"sv) { | 61 | 63 | inspect_publish_uid.http = true; | 62 | 64 | } else { | 63 | 65 | errors->push_back("--inspect-publish-uid destination can be " | |
Collapse file Copy file name to clipboardExpand all lines: src/node_v8_platform-inl.h +13-4Lines changed: 13 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | 5 | 5 |
| 6 | 6 | #include <memory> | | 7 | +#include <string_view> | 7 | 8 |
| 8 | 9 | #include "env-inl.h" | 9 | 10 | #include "node.h" | @@ -126,15 +127,23 @@ struct V8Platform { | 126 | 127 | } | 127 | 128 |
| 128 | 129 | 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 | + }; | 129 | 138 | // Attach a new NodeTraceWriter only if this function hasn't been called | 130 | 139 | // before. | 131 | 140 | 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); | 134 | 144 |
| 135 | 145 | 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), | 138 | 147 | std::unique_ptr<tracing::AsyncTraceWriter>( | 139 | 148 | new tracing::NodeTraceWriter( | 140 | 149 | per_process::cli_options->trace_event_file_pattern)), | |
Collapse file Copy file name to clipboardExpand all lines: src/permission/fs_permission.cc +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | #include <algorithm> | 10 | 10 | #include <filesystem> | 11 | 11 | #include <string> | | 12 | +#include <string_view> | 12 | 13 | #include <vector> | 13 | 14 |
| 14 | 15 | namespace { | @@ -74,8 +75,9 @@ namespace permission { | 74 | 75 | // allow = '*' | 75 | 76 | // allow = '/tmp/,/home/example.js' | 76 | 77 | 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) { | 79 | 81 | if (scope == PermissionScope::kFileSystemRead) { | 80 | 82 | deny_all_in_ = false; | 81 | 83 | allow_all_in_ = true; | @@ -85,7 +87,7 @@ void FSPermission::Apply(const std::string& allow, PermissionScope scope) { | 85 | 87 | } | 86 | 88 | return; | 87 | 89 | } | 88 | | - GrantAccess(scope, res); | | 90 | + GrantAccess(scope, std::string(res.data(), res.size())); | 89 | 91 | } | 90 | 92 | } | 91 | 93 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/util.cc +14-12Lines changed: 14 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,19 +169,21 @@ std::string GetHumanReadableProcessName() { | 169 | 169 | return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid()); | 170 | 170 | } | 171 | 171 |
| 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 | + } | 184 | 185 | } | | 186 | + | 185 | 187 | return out; | 186 | 188 | } | 187 | 189 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/util.h +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -685,9 +685,8 @@ struct FunctionDeleter { | 685 | 685 | template <typename T, void (*function)(T*)> | 686 | 686 | using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; | 687 | 687 |
| 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); | 691 | 690 |
| 692 | 691 | inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | 693 | 692 | std::string_view str, | |
|