summaryrefslogtreecommitdiff
path: root/src/str.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/str.cc')
-rw-r--r--src/str.cc26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/str.cc b/src/str.cc
index 44db3a6..e5b70ed 100644
--- a/src/str.cc
+++ b/src/str.cc
@@ -40,6 +40,32 @@ std::vector<std::string_view> split(std::string_view str, char separator,
return vec;
}
+void split(std::string_view str, std::vector<std::string_view>& out,
+ std::string_view separator, bool keep_empty) {
+ out.clear();
+
+ size_t offset = 0;
+ while (true) {
+ auto next = str.find(separator, offset);
+ if (next == std::string_view::npos) {
+ if (keep_empty || offset < str.size())
+ out.push_back(str.substr(offset));
+ break;
+ }
+ if (keep_empty || offset < next)
+ out.push_back(str.substr(offset, next - offset));
+ offset = next + separator.size();
+ }
+}
+
+std::vector<std::string_view> split(std::string_view str,
+ std::string_view separator,
+ bool keep_empty) {
+ std::vector<std::string_view> vec;
+ split(str, vec, separator, keep_empty);
+ return vec;
+}
+
std::string_view trim(std::string_view str) {
size_t s = 0;
size_t e = str.size();