This commit is contained in:
Jean-Francois Dockes 2019-06-27 11:11:17 +02:00
parent 3f7d270691
commit 59f6c503cb
2 changed files with 32 additions and 0 deletions

View File

@ -17,6 +17,8 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#ifdef _WIN32
// needed for localtime_r under mingw?
#define _POSIX_THREAD_SAFE_FUNCTIONS
@ -440,6 +442,31 @@ void stringToTokens(const string& str, vector<string>& tokens,
}
}
void stringSplitString(const string& str, vector<string>& tokens,
const string& sep)
{
if (str.empty() || sep.empty())
return;
string::size_type startPos = 0, pos;
while (startPos < str.size()) {
// Find next delimiter or end of string (end of token)
pos = str.find(sep, startPos);
// Add token to the vector and adjust start
if (pos == string::npos) {
tokens.push_back(str.substr(startPos));
break;
} else if (pos == startPos) {
// Initial or consecutive separators
tokens.push_back(string());
} else {
tokens.push_back(str.substr(startPos, pos - startPos));
}
startPos = pos + sep.size();
}
}
bool stringToBool(const string& s)
{
if (s.empty()) {

View File

@ -136,6 +136,11 @@ extern void stringToTokens(const std::string& s,
const std::string& delims = " \t",
bool skipinit = true);
/** Like toTokens but with multichar separator */
extern void stringSplitString(const std::string& str,
std::vector<std::string>& tokens,
const std::string& sep);
/** Convert string to boolean */
extern bool stringToBool(const std::string& s);