src: use find instead of char-by-char in FromFilePath()

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Daniel Lemire 2023-10-24 14:09:08 -04:00 committed by GitHub
parent c6d650f179
commit c89bae161b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -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);
}

View File

@ -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