util: Introduce string tokenize helper routine

Introduce a helper routine which splits a string into tokens that were
separated by a delimiter.
This commit is contained in:
Gerhard Sittig 2017-06-25 13:13:01 +02:00
parent a998be27f1
commit 34f4a40baa
2 changed files with 29 additions and 0 deletions

View File

@ -224,5 +224,27 @@ QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
return s;
}
/**
* Split a string into tokens at occurances of the separator.
*
* @param[in] text the input string to split
* @param[in] separator the delimiter between tokens
*
* @return a vector of broken down tokens
*/
vector<string> split_string(string text, string separator)
{
vector<string> result;
size_t pos;
while ((pos = text.find(separator)) != std::string::npos) {
result.push_back(text.substr(0, pos));
text = text.substr(pos + separator.length());
}
result.push_back(text);
return result;
}
} // namespace util
} // namespace pv

View File

@ -21,6 +21,8 @@
#define PULSEVIEW_UTIL_HPP
#include <cmath>
#include <string>
#include <vector>
#ifndef Q_MOC_RUN
#include <boost/multiprecision/cpp_dec_float.hpp>
@ -29,6 +31,9 @@
#include <QMetaType>
#include <QString>
using std::string;
using std::vector;
namespace pv {
namespace util {
@ -112,6 +117,8 @@ QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
QString format_time_minutes(const Timestamp& t, signed precision = 0,
bool sign = true);
vector<string> split_string(string text, string separator);
} // namespace util
} // namespace pv