utils-file: don't use regexp in join_paths

It's slow.
This commit is contained in:
Dirk-Jan C. Binnema 2023-08-06 12:58:02 +03:00
parent fa2ceec4ae
commit 4ecf386cda
1 changed files with 27 additions and 5 deletions

View File

@ -157,16 +157,38 @@ std::string runtime_path(RuntimePath path, const std::string& muhome="");
* @return the path
*/
static inline std::string join_paths() { return {}; }
template<typename S, typename...Args>
std::string join_paths(S&& s, Args...args) {
template<typename S> std::string join_paths_(S&& s) { return std::string{s}; }
template<typename S, typename...Args>
std::string join_paths_(S&& s, Args...args) {
static std::string sepa{"/"};
auto&& str{std::string{std::forward<S>(s)}};
if (auto&& rest{join_paths(std::forward<Args>(args)...)}; !rest.empty())
if (auto&& rest{join_paths_(std::forward<Args>(args)...)}; !rest.empty())
str += (sepa + rest);
return str;
}
static auto rx = Regex::make("//*").value();
return rx.replace(str, sepa);
template<typename S, typename...Args>
std::string join_paths(S&& s, Args...args) {
constexpr auto sepa = '/';
auto path = join_paths_(std::forward<S>(s), std::forward<Args>(args)...);
auto c{0U};
while (c < path.size()) {
if (path[c] != sepa) {
++c;
continue;
}
while (path[++c] == '/') {
path.erase(c, 1);
--c;
}
}
return path;
}