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:
parent
c6d650f179
commit
c89bae161b
@ -417,12 +417,21 @@ 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;
|
// Avoid unnecessary allocations.
|
||||||
for (size_t i = 0; i < file_path.length(); ++i) {
|
size_t pos = file_path.empty() ? std::string_view::npos : file_path.find('%');
|
||||||
escaped_file_path += file_path[i];
|
if (pos == std::string_view::npos) {
|
||||||
if (file_path[i] == '%') escaped_file_path += "25";
|
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);
|
return ada::href_from_file(escaped_file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ class BindingData : public SnapshotableObject {
|
|||||||
std::optional<std::string> base);
|
std::optional<std::string> base);
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string FromFilePath(const std::string_view file_path);
|
std::string FromFilePath(std::string_view file_path);
|
||||||
|
|
||||||
} // namespace url
|
} // namespace url
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user