Skip to content

Commit 7395de0

Browse files
addaleaxbengl
authored andcommitted
src: use const reference instead of pointer in URL::SerializeURL
Just some general cleanup to make things C++-y instead of C-y. PR-URL: #41759 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
1 parent 470f870 commit 7395de0

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/node_url.cc

+33-33
Original file line numberDiff line numberDiff line change
@@ -1544,59 +1544,59 @@ void URL::Parse(const char* input,
15441544
} // NOLINT(readability/fn_size)
15451545

15461546
// https://url.spec.whatwg.org/#url-serializing
1547-
std::string URL::SerializeURL(const struct url_data* url,
1547+
std::string URL::SerializeURL(const url_data& url,
15481548
bool exclude = false) {
15491549
std::string output;
15501550
output.reserve(
1551-
10 +
1552-
url->scheme.size() +
1553-
url->username.size() +
1554-
url->password.size() +
1555-
url->host.size() +
1556-
url->query.size() +
1557-
url->fragment.size() +
1558-
url->href.size() +
1551+
10 + // We generally insert < 10 separator characters between URL parts
1552+
url.scheme.size() +
1553+
url.username.size() +
1554+
url.password.size() +
1555+
url.host.size() +
1556+
url.query.size() +
1557+
url.fragment.size() +
1558+
url.href.size() +
15591559
std::accumulate(
1560-
url->path.begin(),
1561-
url->path.end(),
1560+
url.path.begin(),
1561+
url.path.end(),
15621562
0,
15631563
[](size_t sum, const auto& str) { return sum + str.size(); }));
15641564

1565-
output += url->scheme;
1566-
if (url->flags & URL_FLAGS_HAS_HOST) {
1565+
output += url.scheme;
1566+
if (url.flags & URL_FLAGS_HAS_HOST) {
15671567
output += "//";
1568-
if (url->flags & URL_FLAGS_HAS_USERNAME ||
1569-
url->flags & URL_FLAGS_HAS_PASSWORD) {
1570-
if (url->flags & URL_FLAGS_HAS_USERNAME) {
1571-
output += url->username;
1568+
if (url.flags & URL_FLAGS_HAS_USERNAME ||
1569+
url.flags & URL_FLAGS_HAS_PASSWORD) {
1570+
if (url.flags & URL_FLAGS_HAS_USERNAME) {
1571+
output += url.username;
15721572
}
1573-
if (url->flags & URL_FLAGS_HAS_PASSWORD) {
1574-
output += ":" + url->password;
1573+
if (url.flags & URL_FLAGS_HAS_PASSWORD) {
1574+
output += ":" + url.password;
15751575
}
15761576
output += "@";
15771577
}
1578-
output += url->host;
1579-
if (url->port != -1) {
1580-
output += ":" + std::to_string(url->port);
1578+
output += url.host;
1579+
if (url.port != -1) {
1580+
output += ":" + std::to_string(url.port);
15811581
}
15821582
}
1583-
if (url->flags & URL_FLAGS_CANNOT_BE_BASE) {
1584-
output += url->path[0];
1583+
if (url.flags & URL_FLAGS_CANNOT_BE_BASE) {
1584+
output += url.path[0];
15851585
} else {
1586-
if (!(url->flags & URL_FLAGS_HAS_HOST) &&
1587-
url->path.size() > 1 &&
1588-
url->path[0].empty()) {
1586+
if (!(url.flags & URL_FLAGS_HAS_HOST) &&
1587+
url.path.size() > 1 &&
1588+
url.path[0].empty()) {
15891589
output += "/.";
15901590
}
1591-
for (size_t i = 1; i < url->path.size(); i++) {
1592-
output += "/" + url->path[i];
1591+
for (size_t i = 1; i < url.path.size(); i++) {
1592+
output += "/" + url.path[i];
15931593
}
15941594
}
1595-
if (url->flags & URL_FLAGS_HAS_QUERY) {
1596-
output += "?" + url->query;
1595+
if (url.flags & URL_FLAGS_HAS_QUERY) {
1596+
output += "?" + url.query;
15971597
}
1598-
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
1599-
output += "#" + url->fragment;
1598+
if (!exclude && (url.flags & URL_FLAGS_HAS_FRAGMENT)) {
1599+
output += "#" + url.fragment;
16001600
}
16011601
output.shrink_to_fit();
16021602
return output;

src/node_url.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class URL {
9494
const struct url_data* base,
9595
bool has_base);
9696

97-
static std::string SerializeURL(const struct url_data* url, bool exclude);
97+
static std::string SerializeURL(const url_data& url, bool exclude);
9898

9999
URL(const char* input, const size_t len) {
100100
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
@@ -174,7 +174,7 @@ class URL {
174174
}
175175

176176
std::string href() const {
177-
return SerializeURL(&context_, false);
177+
return SerializeURL(context_, false);
178178
}
179179

180180
// Get the path of the file: URL in a format consumable by native file system
@@ -193,7 +193,7 @@ class URL {
193193
URL() : URL("") {}
194194

195195
private:
196-
struct url_data context_;
196+
url_data context_;
197197
};
198198

199199
} // namespace url

0 commit comments

Comments
 (0)