From 3f4deaae48fe9878cab682d3e7214eb3c2069f5c Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 19 Oct 2023 10:05:40 -0400 Subject: [PATCH 1/6] src: use find instead of char-by-char in FromFilePath() --- src/node_url.cc | 12 ++++++++---- src/node_url.h | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index 89fcfec20f5685..12b27ee74a98ce 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -417,12 +417,16 @@ void BindingData::RegisterExternalReferences( } } -std::string FromFilePath(const std::string_view file_path) { +std::string FromFilePath(std::string_view file_path) { std::string escaped_file_path; - for (size_t i = 0; i < file_path.length(); ++i) { - escaped_file_path += file_path[i]; - if (file_path[i] == '%') escaped_file_path += "25"; + size_t pos = 0; + while ((pos = file_path.find('%', pos)) != std::string_view::npos) { + escaped_file_path += file_path.substr(0, pos + 1); + escaped_file_path += "25"; + file_path = file_path.substr(pos + 1); + pos = 0; } + escaped_file_path += file_path; return ada::href_from_file(escaped_file_path); } diff --git a/src/node_url.h b/src/node_url.h index f3aa136a5b538d..ca59a375d1e2af 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -81,7 +81,7 @@ class BindingData : public SnapshotableObject { std::optional base); }; -std::string FromFilePath(const std::string_view file_path); +std::string FromFilePath(std::string_view file_path); } // namespace url From 95e9f4c5f328b3c55d5ddc9b74dc5f77fb51a5b4 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 19 Oct 2023 10:47:55 -0400 Subject: [PATCH 2/6] fix typo --- src/node_url.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index 12b27ee74a98ce..7bf07536d591e9 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -418,14 +418,19 @@ void BindingData::RegisterExternalReferences( } std::string FromFilePath(std::string_view file_path) { + // avoid unnecessary allocations + size_t pos = file_path.empty() ? std::string_view::npos : file_path.find('%'); + if (pos == std::string_view::npos) { + return ada::href_from_file(file_path); + } + // escape '%' characters to a temporary string std::string escaped_file_path; - size_t pos = 0; - while ((pos = file_path.find('%', pos)) != std::string_view::npos) { + do { escaped_file_path += file_path.substr(0, pos + 1); escaped_file_path += "25"; file_path = file_path.substr(pos + 1); pos = 0; - } + } while((pos = file_path.find('%', pos)) != std::string_view::npos); escaped_file_path += file_path; return ada::href_from_file(escaped_file_path); } From a61db5d18144802229e75c4e9e56ffa78809f9d4 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 19 Oct 2023 10:51:20 -0400 Subject: [PATCH 3/6] adding space --- src/node_url.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index 7bf07536d591e9..4f7b31969b382f 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -430,7 +430,7 @@ std::string FromFilePath(std::string_view file_path) { escaped_file_path += "25"; file_path = file_path.substr(pos + 1); pos = 0; - } while((pos = file_path.find('%', pos)) != std::string_view::npos); + } while ((pos = file_path.find('%', pos)) != std::string_view::npos); escaped_file_path += file_path; return ada::href_from_file(escaped_file_path); } From 8c6a185dc42b43daeb2b55e1af0e8b0165da628f Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 19 Oct 2023 18:33:34 -0400 Subject: [PATCH 4/6] Update src/node_url.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tobias Nießen --- src/node_url.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index 4f7b31969b382f..b2eaca861f0d41 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -423,7 +423,7 @@ std::string FromFilePath(std::string_view file_path) { if (pos == std::string_view::npos) { return ada::href_from_file(file_path); } - // escape '%' characters to a temporary string + // Escape '%' characters to a temporary string. std::string escaped_file_path; do { escaped_file_path += file_path.substr(0, pos + 1); From 11d038f2243590da50a886ed4b43a781e919e0de Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Thu, 19 Oct 2023 18:33:39 -0400 Subject: [PATCH 5/6] Update src/node_url.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tobias Nießen --- src/node_url.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index b2eaca861f0d41..ddcea188d5e068 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -418,7 +418,7 @@ void BindingData::RegisterExternalReferences( } std::string FromFilePath(std::string_view file_path) { - // avoid unnecessary allocations + // Avoid unnecessary allocations. size_t pos = file_path.empty() ? std::string_view::npos : file_path.find('%'); if (pos == std::string_view::npos) { return ada::href_from_file(file_path); From ba7f31d180b5730deab0898d1239fa24d098c0d1 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 20 Oct 2023 22:15:13 -0400 Subject: [PATCH 6/6] simplifying by not setting pos to zero. --- src/node_url.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index ddcea188d5e068..ccc148386e5a67 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -429,8 +429,8 @@ std::string FromFilePath(std::string_view file_path) { escaped_file_path += file_path.substr(0, pos + 1); escaped_file_path += "25"; file_path = file_path.substr(pos + 1); - pos = 0; - } while ((pos = file_path.find('%', pos)) != std::string_view::npos); + pos = file_path.empty() ? std::string_view::npos : file_path.find('%'); + } while (pos != std::string_view::npos); escaped_file_path += file_path; return ada::href_from_file(escaped_file_path); }