Skip to content

Commit d66f469

Browse files
addaleaxMylesBorins
authored andcommitted
src: minor cleanups to node_url.cc
- Remove pointless pointers - Make `WriteHost` take a const argument so that it’s functionality is clear from the signature - Make `FindLongestZeroSequence` templated to accommodate the constness in `WriteHost` and because using `uint16_t` is an articifial, unnecessary restriction - Remove string copying when no copies are needed - Make `PercentDecode` just return its return value - Make `ParseHost` (string-only version) take its constant argument as a constant reference Backport-PR-URL: #18324 PR-URL: #17470 Reviewed-By: Timothy Gu <[email protected]>
1 parent 1b68986 commit d66f469

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/node_url.cc

+29-32
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,11 @@ static inline unsigned hex2bin(const T ch) {
506506
return static_cast<unsigned>(-1);
507507
}
508508

509-
static inline void PercentDecode(const char* input,
510-
size_t len,
511-
std::string* dest) {
509+
inline std::string PercentDecode(const char* input, size_t len) {
510+
std::string dest;
512511
if (len == 0)
513-
return;
514-
dest->reserve(len);
512+
return dest;
513+
dest.reserve(len);
515514
const char* pointer = input;
516515
const char* end = input + len;
517516

@@ -522,17 +521,18 @@ static inline void PercentDecode(const char* input,
522521
(ch == '%' &&
523522
(!IsASCIIHexDigit(pointer[1]) ||
524523
!IsASCIIHexDigit(pointer[2])))) {
525-
*dest += ch;
524+
dest += ch;
526525
pointer++;
527526
continue;
528527
} else {
529528
unsigned a = hex2bin(pointer[1]);
530529
unsigned b = hex2bin(pointer[2]);
531530
char c = static_cast<char>(a * 16 + b);
532-
*dest += c;
531+
dest += c;
533532
pointer += 3;
534533
}
535534
}
535+
return dest;
536536
}
537537

538538
#define SPECIALS(XX) \
@@ -860,7 +860,7 @@ static url_host_type ParseHost(url_host* host,
860860
return ParseOpaqueHost(host, input, length);
861861

862862
// First, we have to percent decode
863-
PercentDecode(input, length, &decoded);
863+
decoded = PercentDecode(input, length);
864864

865865
// Then we have to punycode toASCII
866866
if (!ToASCII(decoded, &decoded))
@@ -894,13 +894,13 @@ static url_host_type ParseHost(url_host* host,
894894

895895
// Locates the longest sequence of 0 segments in an IPv6 address
896896
// in order to use the :: compression when serializing
897-
static inline uint16_t* FindLongestZeroSequence(uint16_t* values,
898-
size_t len) {
899-
uint16_t* start = values;
900-
uint16_t* end = start + len;
901-
uint16_t* result = nullptr;
897+
template<typename T>
898+
static inline T* FindLongestZeroSequence(T* values, size_t len) {
899+
T* start = values;
900+
T* end = start + len;
901+
T* result = nullptr;
902902

903-
uint16_t* current = nullptr;
903+
T* current = nullptr;
904904
unsigned counter = 0, longest = 1;
905905

906906
while (start < end) {
@@ -923,7 +923,7 @@ static inline uint16_t* FindLongestZeroSequence(uint16_t* values,
923923
return result;
924924
}
925925

926-
static url_host_type WriteHost(url_host* host, std::string* dest) {
926+
static url_host_type WriteHost(const url_host* host, std::string* dest) {
927927
dest->clear();
928928
switch (host->type) {
929929
case HOST_TYPE_DOMAIN:
@@ -934,8 +934,7 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
934934
uint32_t value = host->value.ipv4;
935935
for (int n = 0; n < 4; n++) {
936936
char buf[4];
937-
char* buffer = buf;
938-
snprintf(buffer, sizeof(buf), "%d", value % 256);
937+
snprintf(buf, sizeof(buf), "%d", value % 256);
939938
dest->insert(0, buf);
940939
if (n < 3)
941940
dest->insert(0, 1, '.');
@@ -946,12 +945,12 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
946945
case HOST_TYPE_IPV6: {
947946
dest->reserve(41);
948947
*dest+= '[';
949-
uint16_t* start = &host->value.ipv6[0];
950-
uint16_t* compress_pointer =
948+
const uint16_t* start = &host->value.ipv6[0];
949+
const uint16_t* compress_pointer =
951950
FindLongestZeroSequence(start, 8);
952951
bool ignore0 = false;
953952
for (int n = 0; n <= 7; n++) {
954-
uint16_t* piece = &host->value.ipv6[n];
953+
const uint16_t* piece = &host->value.ipv6[n];
955954
if (ignore0 && *piece == 0)
956955
continue;
957956
else if (ignore0)
@@ -962,8 +961,7 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
962961
continue;
963962
}
964963
char buf[5];
965-
char* buffer = buf;
966-
snprintf(buffer, sizeof(buf), "%x", *piece);
964+
snprintf(buf, sizeof(buf), "%x", *piece);
967965
*dest += buf;
968966
if (n < 7)
969967
*dest += ':';
@@ -980,16 +978,16 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
980978
return host->type;
981979
}
982980

983-
static bool ParseHost(std::string* input,
981+
static bool ParseHost(const std::string& input,
984982
std::string* output,
985983
bool is_special,
986984
bool unicode = false) {
987-
if (input->length() == 0) {
985+
if (input.length() == 0) {
988986
output->clear();
989987
return true;
990988
}
991989
url_host host{{""}, HOST_TYPE_DOMAIN};
992-
ParseHost(&host, input->c_str(), input->length(), is_special, unicode);
990+
ParseHost(&host, input.c_str(), input.length(), is_special, unicode);
993991
if (host.type == HOST_TYPE_FAILED)
994992
return false;
995993
WriteHost(&host, output);
@@ -1092,7 +1090,7 @@ static inline void HarvestContext(Environment* env,
10921090
}
10931091

10941092
// Single dot segment can be ".", "%2e", or "%2E"
1095-
static inline bool IsSingleDotSegment(std::string str) {
1093+
static inline bool IsSingleDotSegment(const std::string& str) {
10961094
switch (str.size()) {
10971095
case 1:
10981096
return str == ".";
@@ -1108,7 +1106,7 @@ static inline bool IsSingleDotSegment(std::string str) {
11081106
// Double dot segment can be:
11091107
// "..", ".%2e", ".%2E", "%2e.", "%2E.",
11101108
// "%2e%2e", "%2E%2E", "%2e%2E", or "%2E%2e"
1111-
static inline bool IsDoubleDotSegment(std::string str) {
1109+
static inline bool IsDoubleDotSegment(const std::string& str) {
11121110
switch (str.size()) {
11131111
case 2:
11141112
return str == "..";
@@ -1542,7 +1540,7 @@ void URL::Parse(const char* input,
15421540
return;
15431541
}
15441542
url->flags |= URL_FLAGS_HAS_HOST;
1545-
if (!ParseHost(&buffer, &url->host, special)) {
1543+
if (!ParseHost(buffer, &url->host, special)) {
15461544
url->flags |= URL_FLAGS_FAILED;
15471545
return;
15481546
}
@@ -1569,7 +1567,7 @@ void URL::Parse(const char* input,
15691567
return;
15701568
}
15711569
url->flags |= URL_FLAGS_HAS_HOST;
1572-
if (!ParseHost(&buffer, &url->host, special)) {
1570+
if (!ParseHost(buffer, &url->host, special)) {
15731571
url->flags |= URL_FLAGS_FAILED;
15741572
return;
15751573
}
@@ -1741,7 +1739,7 @@ void URL::Parse(const char* input,
17411739
state = kPathStart;
17421740
} else {
17431741
std::string host;
1744-
if (!ParseHost(&buffer, &host, special)) {
1742+
if (!ParseHost(buffer, &host, special)) {
17451743
url->flags |= URL_FLAGS_FAILED;
17461744
return;
17471745
}
@@ -2104,8 +2102,7 @@ std::string URL::ToFilePath() const {
21042102
#endif
21052103
std::string decoded_path;
21062104
for (const std::string& part : context_.path) {
2107-
std::string decoded;
2108-
PercentDecode(part.c_str(), part.length(), &decoded);
2105+
std::string decoded = PercentDecode(part.c_str(), part.length());
21092106
for (char& ch : decoded) {
21102107
if (is_slash(ch)) {
21112108
return "";

0 commit comments

Comments
 (0)