From 59f6c503cb334fe5f480334691c9c6b0a7a12e3d Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Thu, 27 Jun 2019 11:11:17 +0200 Subject: [PATCH] shared --- src/utils/smallut.cpp | 27 +++++++++++++++++++++++++++ src/utils/smallut.h | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/src/utils/smallut.cpp b/src/utils/smallut.cpp index 0200dcbd..282eed63 100644 --- a/src/utils/smallut.cpp +++ b/src/utils/smallut.cpp @@ -17,6 +17,8 @@ */ #include #include +#include + #ifdef _WIN32 // needed for localtime_r under mingw? #define _POSIX_THREAD_SAFE_FUNCTIONS @@ -440,6 +442,31 @@ void stringToTokens(const string& str, vector& tokens, } } +void stringSplitString(const string& str, vector& 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()) { diff --git a/src/utils/smallut.h b/src/utils/smallut.h index 0946398c..d1b2b760 100644 --- a/src/utils/smallut.h +++ b/src/utils/smallut.h @@ -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& tokens, + const std::string& sep); + /** Convert string to boolean */ extern bool stringToBool(const std::string& s);