Add string tokenization helper

The mxs::strtok function splits a string into a vector of strings based on
a set of delimiters. The function makes it easier to iterate over a set of
values given as a delimited string.
This commit is contained in:
Markus Mäkelä 2018-07-09 13:31:22 +03:00
parent 6d663c79ac
commit 7f67fe6f14
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19

View File

@ -24,6 +24,7 @@
#include <iterator>
#include <string>
#include <unordered_map>
#include <vector>
#include <maxscale/buffer.h>
#include <maxscale/utils.h>
@ -107,6 +108,29 @@ inline std::string trimmed_copy(const std::string& original)
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;
}
/**
* @class CloserTraits utils.hh <maxscale/utils.hh>
*