Skip to content

Commit ab5dbcc

Browse files
committed
src/api: use std::string_view in exceptions StringFromPath
1 parent bba1711 commit ab5dbcc

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/api/exceptions.cc

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "v8.h"
99

1010
#include <cstring>
11+
#include <string>
12+
#include <string_view>
1113

1214
namespace node {
1315

@@ -20,19 +22,38 @@ using v8::Object;
2022
using v8::String;
2123
using v8::Value;
2224

23-
static Local<String> StringFromPath(Isolate* isolate, const char* path) {
25+
static Local<String> StringFromPath(Isolate* isolate, std::string_view path) {
2426
#ifdef _WIN32
25-
if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) {
26-
return String::Concat(
27-
isolate,
28-
FIXED_ONE_BYTE_STRING(isolate, "\\\\"),
29-
String::NewFromUtf8(isolate, path + 8).ToLocalChecked());
30-
} else if (strncmp(path, "\\\\?\\", 4) == 0) {
31-
return String::NewFromUtf8(isolate, path + 4).ToLocalChecked();
27+
constexpr std::string_view kUncPrefix = "\\\\?\\UNC\\";
28+
constexpr std::string_view kLongPrefix = "\\\\?\\";
29+
30+
if (path.starts_with(kUncPrefix)) {
31+
std::string s;
32+
s.reserve(2 + (path.size() - kUncPrefix.size()));
33+
s.append("\\\\");
34+
s.append(path.substr(kUncPrefix.size()));
35+
return String::NewFromUtf8(isolate,
36+
s.data(),
37+
v8::NewStringType::kNormal,
38+
static_cast<int>(s.size()))
39+
.ToLocalChecked();
40+
}
41+
42+
if (path.starts_with(kLongPrefix)) {
43+
auto rest = path.substr(kLongPrefix.size());
44+
return String::NewFromUtf8(isolate,
45+
rest.data(),
46+
v8::NewStringType::kNormal,
47+
static_cast<int>(rest.size()))
48+
.ToLocalChecked();
3249
}
3350
#endif
3451

35-
return String::NewFromUtf8(isolate, path).ToLocalChecked();
52+
return String::NewFromUtf8(isolate,
53+
path.data(),
54+
v8::NewStringType::kNormal,
55+
static_cast<int>(path.size()))
56+
.ToLocalChecked();
3657
}
3758

3859
Local<Value> ErrnoException(Isolate* isolate,
@@ -57,7 +78,7 @@ Local<Value> ErrnoException(Isolate* isolate,
5778
Local<String> path_string;
5879
if (path != nullptr) {
5980
// FIXME(bnoordhuis) It's questionable to interpret the file path as UTF-8.
60-
path_string = StringFromPath(isolate, path);
81+
path_string = StringFromPath(isolate, std::string_view(path));
6182
}
6283

6384
if (path_string.IsEmpty() == false) {
@@ -113,7 +134,7 @@ Local<Value> UVException(Isolate* isolate,
113134
js_msg = String::Concat(isolate, js_msg, js_syscall);
114135

115136
if (path != nullptr) {
116-
js_path = StringFromPath(isolate, path);
137+
js_path = StringFromPath(isolate, std::string_view(path));
117138

118139
js_msg =
119140
String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, " '"));
@@ -123,7 +144,7 @@ Local<Value> UVException(Isolate* isolate,
123144
}
124145

125146
if (dest != nullptr) {
126-
js_dest = StringFromPath(isolate, dest);
147+
js_dest = StringFromPath(isolate, std::string_view(dest));
127148

128149
js_msg = String::Concat(
129150
isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, " -> '"));

0 commit comments

Comments
 (0)