Skip to content

Commit b9c78c4

Browse files
author
Jake Champion
committed
refactor(backend): make use of std::string_view to avoid creating copies of new strings where possible
1 parent ec072c1 commit b9c78c4

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

c-dependencies/js-compute-runtime/builtins/backend.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#include <charconv>
12
#include <cctype>
23
#include <iostream>
34
#include <optional>
5+
#include <string>
6+
#include <string_view>
47

58
// TODO: remove these once the warnings are fixed
69
#pragma clang diagnostic push
@@ -23,7 +26,7 @@ bool isNotAlphaNumericDotOrDash(char character) {
2326
return !std::isalnum(character) && !isDash(character) && !isDot(character);
2427
}
2528

26-
bool isValidHost(std::string host) {
29+
bool isValidHost(std::string_view host) {
2730
// ValidHostRegex =
2831
// "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(:[0-9]+)$";
2932
auto firstCharacter = host.front();
@@ -33,7 +36,7 @@ bool isValidHost(std::string host) {
3336
}
3437
// split the hostname from the port
3538
int pos = host.find_first_of(':');
36-
std::string hostname = host.substr(0, pos);
39+
std::string_view hostname = host.substr(0, pos);
3740

3841
auto lastCharacter = hostname.back();
3942
// check last character is in the regex [a-zA-Z0-9]
@@ -67,11 +70,15 @@ bool isValidHost(std::string host) {
6770
}
6871
// if there is a port - confirm it is all digits and is between 0 and 65536
6972
if (pos != std::string::npos) {
70-
std::string port = host.substr(pos + 1);
73+
std::string_view port = host.substr(pos + 1);
7174
if (!std::all_of(port.begin(), port.end(), ::isdigit)) {
7275
return false;
7376
}
74-
auto value = std::stoi(port);
77+
int value;
78+
const std::from_chars_result result = std::from_chars(port.data(), port.data() + port.size(), value);
79+
if (result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) {
80+
return false;
81+
}
7582
if (value == 0 || value >= 65536) {
7683
return false;
7784
}
@@ -302,7 +309,7 @@ bool Backend::set_target(JSContext *cx, JSObject *backend, JS::HandleValue targe
302309
return false;
303310
}
304311

305-
std::string targetString((char *)targetStringSlice.data, targetStringSlice.len);
312+
std::string_view targetString((char *)targetStringSlice.data, targetStringSlice.len);
306313
auto length = targetString.length();
307314
if (length == 0) {
308315
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BACKEND_TARGET_EMPTY);
@@ -315,7 +322,7 @@ bool Backend::set_target(JSContext *cx, JSObject *backend, JS::HandleValue targe
315322
return false;
316323
}
317324

318-
auto targetStr = JS_NewStringCopyN(cx, targetString.c_str(), targetString.length());
325+
auto targetStr = JS_NewStringCopyN(cx, targetString.data(), targetString.length());
319326
if (!targetStr) {
320327
return false;
321328
}
@@ -358,7 +365,7 @@ JSObject *Backend::create(JSContext *cx, JS::HandleObject request) {
358365
return nullptr;
359366
}
360367
const jsurl::SpecString origin_specstring = jsurl::origin(url);
361-
std::string origin((char *)origin_specstring.data, origin_specstring.len);
368+
std::string_view origin((char *)origin_specstring.data, origin_specstring.len);
362369

363370
auto use_ssl = origin.rfind("https://", 0) == 0;
364371
JS::SetReservedSlot(backend, Backend::Slots::UseSsl, JS::BooleanValue(use_ssl));

0 commit comments

Comments
 (0)