From c83e3eba10930373bdade606d6d54fb5615cbe74 Mon Sep 17 00:00:00 2001
From: Timo Koch <timo.koch@iws.uni-stuttgart.de>
Date: Thu, 3 Dec 2020 15:18:18 +0100
Subject: [PATCH] [common][string] Implement split function for splitting
 strings

---
 dumux/common/stringutilities.hh | 35 +++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/dumux/common/stringutilities.hh b/dumux/common/stringutilities.hh
index 21f05661f8..60cd68f990 100644
--- a/dumux/common/stringutilities.hh
+++ b/dumux/common/stringutilities.hh
@@ -60,6 +60,41 @@ std::vector<std::string_view> tokenize(std::string_view str, std::string_view de
     return tokens;
 }
 
+/*
+ * \brief Split a string at a given seperator string
+ * \ingroup Common
+ * \param str a string to be split
+ * \param delim a set of delimiter characters separating the tokens
+ * \param removeEmpty if empty string between two separator should be removed from the result
+ * \note this works without copying the original string, make sure that string
+ *       does not go out of scope!
+ *
+ * Examples:
+ *  - split("| Hello | world |", "|") -> {"", " Hello ", " world ", ""}
+ *  - split("| Hello | world |", "|", true) -> {" Hello ", " world "}
+ *  - split("blafoobla", "foo") -> {"bla", "bla"}
+ *  - split("blafoobla", "bla") -> {"", "foo", ""}
+ *  - split("blafoobla", "bla", true) -> {"foo"}
+ */
+std::vector<std::string_view> split(std::string_view str, std::string_view delim, bool removeEmpty = false)
+{
+    const auto token = [&](std::size_t pos){
+        const auto end = str.find(delim, pos);
+        return std::make_pair(pos, end);
+    };
+
+    std::vector<std::string_view> split;
+    for (auto [start, end] = token(0); start != std::string::npos; std::tie(start, end) = token(start))
+    {
+        if (auto&& sub = str.substr(start, end-start); !removeEmpty || !sub.empty())
+            split.emplace_back(std::move(sub));
+
+        // skip to after the delimiter
+        start = (end != std::string::npos) ? end + delim.size() : end;
+    }
+    return split;
+}
+
 } // end namespace Dumux
 
 #endif
-- 
GitLab