summaryrefslogtreecommitdiff
path: root/src/str.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-07 09:12:22 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-07 09:13:15 +0200
commitc87f9627efc8b612eb9b000acfcc6731cad15765 (patch)
tree34eb4b9e70a51c2f3db3a97c2aef31ba0b139ec9 /src/str.cc
Initial commit
Diffstat (limited to 'src/str.cc')
-rw-r--r--src/str.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/str.cc b/src/str.cc
new file mode 100644
index 0000000..44db3a6
--- /dev/null
+++ b/src/str.cc
@@ -0,0 +1,53 @@
+#include "str.hh"
+
+#include <cstddef>
+#include <string_view>
+#include <vector>
+
+namespace str {
+
+namespace {
+
+[[nodiscard]]
+inline bool is_space(char c) {
+ return c == ' ' || c == '\t' || c == '\r' || c == '\n';
+}
+
+} // namespace
+
+void split(std::string_view str, std::vector<std::string_view>& out,
+ char 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 + 1;
+ }
+}
+
+std::vector<std::string_view> split(std::string_view str, char 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();
+ while (s < e && is_space(str[s]))
+ ++s;
+ while (e > s && is_space(str[e - 1]))
+ --e;
+ return str.substr(s, e - s);
+}
+
+} // namespace str