Skip to content

Commit

Permalink
src: use find instead of char-by-char in FromFilePath()
Browse files Browse the repository at this point in the history
PR-URL: #50288
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
lemire authored and UlisesGascon committed Dec 11, 2023
1 parent f87921d commit 9346f15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/node_url.cc
Expand Up @@ -417,12 +417,21 @@ void BindingData::RegisterExternalReferences(
}
}

std::string FromFilePath(const 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";
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;
do {
escaped_file_path += file_path.substr(0, pos + 1);
escaped_file_path += "25";
file_path = file_path.substr(pos + 1);
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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_url.h
Expand Up @@ -81,7 +81,7 @@ class BindingData : public SnapshotableObject {
std::optional<std::string> base);
};

std::string FromFilePath(const std::string_view file_path);
std::string FromFilePath(std::string_view file_path);

} // namespace url

Expand Down

0 comments on commit 9346f15

Please sign in to comment.