]> sigrok.org Git - pulseview.git/blobdiff - pv/util.cpp
util: Introduce string tokenize helper routine
[pulseview.git] / pv / util.cpp
index 8f2d7d2544cd8e30301a28c56f6537269dbe759a..a452aac4b6eaef7e51f85b9cd6d0b0f37f7b5e19 100644 (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