mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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: https://github.com/nodejs/node/pull/41759 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
committed by
Node.js GitHub Bot
parent
889534ec35
commit
33ffaa49ba
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
@@ -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
|
||||
@@ -193,7 +193,7 @@ class URL {
|
||||
URL() : URL("") {}
|
||||
|
||||
private:
|
||||
struct url_data context_;
|
||||
url_data context_;
|
||||
};
|
||||
|
||||
} // namespace url
|
||||
|
||||
Reference in New Issue
Block a user