fmt: add more string_view versions of string functions

This commit is contained in:
Megamouse 2025-12-08 03:07:49 +01:00
parent c840c98e9e
commit 27f39d2ac0
36 changed files with 413 additions and 90 deletions

View file

@ -139,57 +139,90 @@ namespace fmt
// Splits the string into a vector of strings using the separators. The vector may contain empty strings unless is_skip_empty is true.
std::vector<std::string> split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
// Splits the string_view into a vector of string_views using the separators. The vector may contain empty string_views unless is_skip_empty is true.
std::vector<std::string_view> split_sv(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
// Removes all preceding and trailing characters specified by 'values' from 'source'.
std::string trim(const std::string& source, std::string_view values = " \t");
// Removes all preceding and trailing characters specified by 'values' from 'source' and returns the result.
std::string_view trim_sv(std::string_view source, std::string_view values = " \t");
// Removes all preceding characters specified by 'values' from 'source'.
std::string trim_front(const std::string& source, std::string_view values = " \t");
// Removes all preceding characters specified by 'values' from 'source' and returns the result.
std::string_view trim_front_sv(std::string_view source, std::string_view values = " \t");
// Removes all trailing characters specified by 'values' from 'source'.
void trim_back(std::string& source, std::string_view values = " \t");
// Removes all trailing characters specified by 'values' from 'source' and returns the result.
std::string_view trim_back_sv(std::string_view source, std::string_view values = " \t");
template <typename T>
std::string merge(const T& source, const std::string& separator)
std::string merge(const T& source, std::string_view separator)
{
if (source.empty())
{
return {};
}
usz total = (source.size() - 1) * separator.size();
for (const auto& s : source)
{
total += s.size();
}
std::string result;
result.reserve(total);
auto it = source.begin();
auto end = source.end();
for (--end; it != end; ++it)
{
result += std::string{*it} + separator;
result.append(*it);
if (!separator.empty())
result.append(separator);
}
return result + std::string{source.back()};
return result.append(source.back());
}
template <typename T>
std::string merge(std::initializer_list<T> sources, const std::string& separator)
std::string merge(std::initializer_list<T> sources, std::string_view separator)
{
if (!sources.size())
{
return {};
}
usz total = (sources.size() - 1) * separator.size();
for (const auto& s : sources)
{
if (s.empty()) continue;
total += s.size() + (s.size() - 1) * separator.size();
}
std::string result;
result.reserve(total);
bool first = true;
for (const auto& v : sources)
{
if (first)
{
result = fmt::merge(v, separator);
first = false;
first = false;
}
else
else if (!separator.empty())
{
result += separator + fmt::merge(v, separator);
result.append(separator);
}
result.append(fmt::merge(v, separator));
}
return result;