More generic 'join' function for string sequences

This commit is contained in:
Matthias Koefferlein 2022-08-27 22:27:59 +02:00
parent a6ad6c8866
commit 728d60a510
2 changed files with 29 additions and 19 deletions

View File

@ -28,7 +28,6 @@
#include <cctype>
#include <limits>
#include <vector>
#include <sstream>
#include <cwctype>
#include <algorithm>
@ -936,23 +935,6 @@ from_string (const std::string &s, bool &b)
}
}
std::string
join (const std::vector <std::string> &vv, const std::string &s)
{
std::ostringstream r;
bool first = true;
for (std::vector <std::string>::const_iterator i = vv.begin (); i != vv.end (); ++i) {
if (!first) {
r << s;
}
first = false;
r << *i;
}
return r.str ();
}
std::vector<std::string>
split (const std::string &t, const std::string &s)
{

View File

@ -27,6 +27,7 @@
#include "tlCommon.h"
#include <string>
#include <sstream>
#include <typeinfo>
#include <stdexcept>
#include <stdint.h>
@ -930,7 +931,34 @@ inline std::string sprintf (const std::string &fmt, const tl::Variant &a1, const
TL_PUBLIC std::string trim (const std::string &s);
TL_PUBLIC std::vector<std::string> split (const std::string &s, const std::string &sep);
TL_PUBLIC std::string join (const std::vector<std::string> &strings, const std::string &sep);
/**
* @brief Joins a generic iterated list into a single string using the given separator
*/
template <class Iter>
TL_PUBLIC_TEMPLATE std::string join (Iter from, Iter to, const std::string &sep)
{
std::ostringstream r;
bool first = true;
for (Iter i = from; i != to; ++i) {
if (!first) {
r << sep;
}
first = false;
r << tl::to_string (*i);
}
return r.str ();
}
/**
* @brief Joins a vector of strings into a single string using the given separator
*/
inline std::string join (const std::vector<std::string> &strings, const std::string &sep)
{
return join (strings.begin (), strings.end (), sep);
}
/**
* @brief Returns the lower-case character for a wchar_t