Allow to skip empty strings in fmt::merge

This commit is contained in:
Megamouse
2026-07-30 02:35:53 +02:00
parent bad72a3844
commit a7fe472583
3 changed files with 191 additions and 5 deletions
+25 -3
View File
@@ -161,7 +161,7 @@ namespace fmt
std::string_view trim_back_sv(std::string_view source, std::string_view values = " \t");
template <typename T>
std::string merge(const T& source, std::string_view separator)
std::string merge(const T& source, std::string_view separator, bool is_skip_empty = false)
{
if (source.empty())
{
@@ -180,6 +180,23 @@ namespace fmt
auto it = source.begin();
auto end = source.end();
if (is_skip_empty)
{
for (; it != end; ++it)
{
if (it->empty()) continue;
if (!result.empty() && !separator.empty())
{
result.append(separator);
}
result.append(*it);
}
return result;
}
for (--end; it != end; ++it)
{
result.append(*it);
@@ -192,7 +209,7 @@ namespace fmt
}
template <typename T>
std::string merge(std::initializer_list<T> sources, std::string_view separator)
std::string merge(std::initializer_list<T> sources, std::string_view separator, bool is_skip_empty = false)
{
if (!sources.size())
{
@@ -213,6 +230,11 @@ namespace fmt
for (const auto& v : sources)
{
if (v.empty()) continue;
std::string sub = fmt::merge(v, separator, is_skip_empty);
if (sub.empty()) continue;
if (first)
{
first = false;
@@ -222,7 +244,7 @@ namespace fmt
result.append(separator);
}
result.append(fmt::merge(v, separator));
result.append(std::move(sub));
}
return result;