Skip to content

Commit

Permalink
src: use const reference instead of pointer in URL::SerializeURL
Browse files Browse the repository at this point in the history
Just some general cleanup to make things C++-y instead of C-y.

PR-URL: nodejs#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]>
  • Loading branch information
addaleax authored and bengl committed Feb 21, 2022
1 parent 470f870 commit 7395de0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
66 changes: 33 additions & 33 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1544,59 +1544,59 @@ void URL::Parse(const char* input,
} // NOLINT(readability/fn_size)

// https://url.spec.whatwg.org/#url-serializing
std::string URL::SerializeURL(const struct url_data* url,
std::string URL::SerializeURL(const url_data& url,
bool exclude = false) {
std::string output;
output.reserve(
10 +
url->scheme.size() +
url->username.size() +
url->password.size() +
url->host.size() +
url->query.size() +
url->fragment.size() +
url->href.size() +
10 + // We generally insert < 10 separator characters between URL parts
url.scheme.size() +
url.username.size() +
url.password.size() +
url.host.size() +
url.query.size() +
url.fragment.size() +
url.href.size() +
std::accumulate(
url->path.begin(),
url->path.end(),
url.path.begin(),
url.path.end(),
0,
[](size_t sum, const auto& str) { return sum + str.size(); }));

output += url->scheme;
if (url->flags & URL_FLAGS_HAS_HOST) {
output += url.scheme;
if (url.flags & URL_FLAGS_HAS_HOST) {
output += "//";
if (url->flags & URL_FLAGS_HAS_USERNAME ||
url->flags & URL_FLAGS_HAS_PASSWORD) {
if (url->flags & URL_FLAGS_HAS_USERNAME) {
output += url->username;
if (url.flags & URL_FLAGS_HAS_USERNAME ||
url.flags & URL_FLAGS_HAS_PASSWORD) {
if (url.flags & URL_FLAGS_HAS_USERNAME) {
output += url.username;
}
if (url->flags & URL_FLAGS_HAS_PASSWORD) {
output += ":" + url->password;
if (url.flags & URL_FLAGS_HAS_PASSWORD) {
output += ":" + url.password;
}
output += "@";
}
output += url->host;
if (url->port != -1) {
output += ":" + std::to_string(url->port);
output += url.host;
if (url.port != -1) {
output += ":" + std::to_string(url.port);
}
}
if (url->flags & URL_FLAGS_CANNOT_BE_BASE) {
output += url->path[0];
if (url.flags & URL_FLAGS_CANNOT_BE_BASE) {
output += url.path[0];
} else {
if (!(url->flags & URL_FLAGS_HAS_HOST) &&
url->path.size() > 1 &&
url->path[0].empty()) {
if (!(url.flags & URL_FLAGS_HAS_HOST) &&
url.path.size() > 1 &&
url.path[0].empty()) {
output += "/.";
}
for (size_t i = 1; i < url->path.size(); i++) {
output += "/" + url->path[i];
for (size_t i = 1; i < url.path.size(); i++) {
output += "/" + url.path[i];
}
}
if (url->flags & URL_FLAGS_HAS_QUERY) {
output += "?" + url->query;
if (url.flags & URL_FLAGS_HAS_QUERY) {
output += "?" + url.query;
}
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
output += "#" + url->fragment;
if (!exclude && (url.flags & URL_FLAGS_HAS_FRAGMENT)) {
output += "#" + url.fragment;
}
output.shrink_to_fit();
return output;
Expand Down
6 changes: 3 additions & 3 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class URL {
const struct url_data* base,
bool has_base);

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

URL(const char* input, const size_t len) {
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
Expand Down Expand Up @@ -174,7 +174,7 @@ class URL {
}

std::string href() const {
return SerializeURL(&context_, false);
return SerializeURL(context_, false);
}

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

private:
struct url_data context_;
url_data context_;
};

} // namespace url
Expand Down

0 comments on commit 7395de0

Please sign in to comment.