Extend string helper functions
Moved mxs::strtok into mxb namespace and added mxb::join for joining objects into strings.
This commit is contained in:
@ -30,6 +30,7 @@
|
|||||||
#include <maxscale/buffer.hh>
|
#include <maxscale/buffer.hh>
|
||||||
#include <maxscale/utils.h>
|
#include <maxscale/utils.h>
|
||||||
#include <maxscale/jansson.hh>
|
#include <maxscale/jansson.hh>
|
||||||
|
#include <maxbase/string.hh>
|
||||||
|
|
||||||
namespace maxscale
|
namespace maxscale
|
||||||
{
|
{
|
||||||
@ -44,17 +45,7 @@ namespace maxscale
|
|||||||
*/
|
*/
|
||||||
inline std::vector<std::string> strtok(std::string str, const char* delim)
|
inline std::vector<std::string> strtok(std::string str, const char* delim)
|
||||||
{
|
{
|
||||||
std::vector<std::string> rval;
|
return mxb::strtok(str, delim);
|
||||||
char* save_ptr;
|
|
||||||
char* tok = strtok_r(&str[0], delim, &save_ptr);
|
|
||||||
|
|
||||||
while (tok)
|
|
||||||
{
|
|
||||||
rval.emplace_back(tok);
|
|
||||||
tok = strtok_r(NULL, delim, &save_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -15,6 +15,9 @@
|
|||||||
#include <maxbase/ccdefs.hh>
|
#include <maxbase/ccdefs.hh>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thread-safe (but not re-entrant) strerror.
|
* Thread-safe (but not re-entrant) strerror.
|
||||||
@ -143,4 +146,54 @@ inline std::string trimmed_copy(const std::string& original)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tokenize a string
|
||||||
|
*
|
||||||
|
* @param str String to tokenize
|
||||||
|
* @param delim List of delimiters (see strtok(3))
|
||||||
|
*
|
||||||
|
* @return List of tokenized strings
|
||||||
|
*/
|
||||||
|
inline std::vector<std::string> strtok(std::string str, const char* delim)
|
||||||
|
{
|
||||||
|
std::vector<std::string> rval;
|
||||||
|
char* save_ptr;
|
||||||
|
char* tok = strtok_r(&str[0], delim, &save_ptr);
|
||||||
|
|
||||||
|
while (tok)
|
||||||
|
{
|
||||||
|
rval.emplace_back(tok);
|
||||||
|
tok = strtok_r(NULL, delim, &save_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join objects into a string delimited by separators
|
||||||
|
*
|
||||||
|
* @param container Container that provides iterators, stored value must support writing to ostream with
|
||||||
|
* operator<<
|
||||||
|
* @param separator Value used as the separator
|
||||||
|
*
|
||||||
|
* @return String created by joining all values and delimiting them with `separator` (no trailing delimiter)
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
std::string join(const T& container, const std::string& separator = ",")
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
auto it = begin(container);
|
||||||
|
|
||||||
|
if (it != end(container))
|
||||||
|
{
|
||||||
|
ss << *it++;
|
||||||
|
|
||||||
|
while (it != end(container))
|
||||||
|
{
|
||||||
|
ss << separator << *it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user