1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 12:21:48 +02:00

nix-util: Add concatMapStrings

This commit is contained in:
Robert Hensing 2025-01-18 09:44:46 +01:00 committed by Sergei Trofimovich
parent bbdc3197a9
commit f3dbaa3f54
2 changed files with 50 additions and 0 deletions

View file

@ -55,6 +55,20 @@ extern template std::string concatStringsSep(std::string_view, const std::list<s
extern template std::string concatStringsSep(std::string_view, const std::set<std::string> &);
extern template std::string concatStringsSep(std::string_view, const std::vector<std::string> &);
/**
* Apply a function to the `iterable`'s items and concat them with `separator`.
*/
template<class C, class F>
std::string concatMapStringsSep(std::string_view separator, const C & iterable, F fn)
{
std::vector<std::string> strings;
strings.reserve(iterable.size());
for (const auto & elem : iterable) {
strings.push_back(fn(elem));
}
return concatStringsSep(separator, strings);
}
/**
* Ignore any empty strings at the start of the list, and then concatenate the
* given strings with a separator between the elements.