From: Gerhard Sittig Date: Sun, 25 Jun 2017 11:13:01 +0000 (+0200) Subject: util: Introduce string tokenize helper routine X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=34f4a40baa0b4485ebee71d51c0a3bf70a0bad8b util: Introduce string tokenize helper routine Introduce a helper routine which splits a string into tokens that were separated by a delimiter. --- diff --git a/pv/util.cpp b/pv/util.cpp index 8f2d7d25..a452aac4 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -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 split_string(string text, string separator) +{ + vector 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 diff --git a/pv/util.hpp b/pv/util.hpp index bc71624b..274839f8 100644 --- a/pv/util.hpp +++ b/pv/util.hpp @@ -21,6 +21,8 @@ #define PULSEVIEW_UTIL_HPP #include +#include +#include #ifndef Q_MOC_RUN #include @@ -29,6 +31,9 @@ #include #include +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 split_string(string text, string separator); + } // namespace util } // namespace pv