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:
parent
6d663c79ac
commit
7f67fe6f14
@ -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>
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user