From c75143df94cc597097c0ed7eb818efe2fd047be8 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 24 Nov 2025 13:40:13 +0100 Subject: [PATCH] port over string split util --- solanaceae/util/string_view_split.hpp | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 solanaceae/util/string_view_split.hpp diff --git a/solanaceae/util/string_view_split.hpp b/solanaceae/util/string_view_split.hpp new file mode 100644 index 0000000..0df9e3b --- /dev/null +++ b/solanaceae/util/string_view_split.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include + +namespace MM::std_utils { + +inline std::string_view trim_prefix(std::string_view sv) { + while (!sv.empty() && std::isspace(sv.front())) { + sv.remove_prefix(1); + } + + return sv; +} + +inline std::string_view trim_suffix(std::string_view sv) { + while (!sv.empty() && std::isspace(sv.back())) { + sv.remove_suffix(1); + } + + return sv; +} + +inline std::string_view trim(std::string_view sv) { + return trim_suffix(trim_prefix(sv)); +} + +// src : https://marcoarena.wordpress.com/2017/01/03/string_view-odi-et-amo/ +inline std::vector split(std::string_view str, const char* delims) { + std::vector ret; + + std::string_view::size_type start = 0; + auto pos = str.find_first_of(delims, start); + while (pos != std::string_view::npos) { + if (pos != start) { + ret.push_back(str.substr(start, pos - start)); + } + start = pos + 1; + pos = str.find_first_of(delims, start); + } + + if (start < str.length()) { + ret.push_back(str.substr(start, str.length() - start)); + } + + return ret; +} + +} // MM::std_utils +