From d91a95641926a0d34c30d3eb3c17faaa2e6529ab Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 29 Jan 2022 22:22:09 +0100 Subject: [PATCH] src: use const reference instead of pointer in URL::SerializeURL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Darshan Sen Reviewed-By: James M Snell Reviewed-By: Tobias Nießen Reviewed-By: Daijiro Wachi --- src/node_url.cc | 66 ++++++++++++++++++++++++------------------------- src/node_url.h | 6 ++--- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index f9bf1cc6df297b..6e47acf7ae3c3f 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -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; diff --git a/src/node_url.h b/src/node_url.h index 0de05e96e100fa..d7b9a1c368cdae 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -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