diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-10-17 09:46:56 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-10-17 09:46:56 +0800 |
commit | 9e4419826b3e23c63567591701a2834a837da98e (patch) | |
tree | 763838a83850f5cb07cff43915aa6be3a689bef0 /src/base/StringUtil.cpp | |
parent | a6b5b8b879a9a587ec0ad605722d5d6428d5e68c (diff) | |
download | cru-9e4419826b3e23c63567591701a2834a837da98e.tar.gz cru-9e4419826b3e23c63567591701a2834a837da98e.tar.bz2 cru-9e4419826b3e23c63567591701a2834a837da98e.zip |
Toml remove String.
Diffstat (limited to 'src/base/StringUtil.cpp')
-rw-r--r-- | src/base/StringUtil.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/base/StringUtil.cpp b/src/base/StringUtil.cpp index bbd201cd..42aea114 100644 --- a/src/base/StringUtil.cpp +++ b/src/base/StringUtil.cpp @@ -5,6 +5,7 @@ #include <algorithm> #include <cctype> #include <compare> +#include <string_view> namespace cru { namespace string { @@ -40,6 +41,43 @@ std::string Trim(std::string_view str) { [](char c) { return !std::isspace(c); }); return std::string(iter1, str.cend() - (iter2 - str.crbegin())); } + +bool IsSpace(std::string_view str) { + return std::ranges::all_of(str, [](char c) { return std::isspace(c); }); +} + +std::vector<std::string> Split(std::string_view str, std::string_view sep, + SplitOption options) { + using size_type = std::string_view::size_type; + + if (sep.empty()) throw std::invalid_argument("Sep can't be empty."); + if (str.empty()) return {}; + + size_type current_pos = 0; + std::vector<std::string> result; + + while (current_pos != std::string_view::npos) { + if (current_pos == str.size()) { + if (!options.Has(SplitOptions::RemoveEmpty)) { + result.push_back({}); + } + break; + } + + auto next_pos = str.find(sep, current_pos); + auto sub = str.substr(current_pos, next_pos == std::string_view::npos + ? std::string_view::npos + : next_pos - current_pos); + if (!(options.Has(SplitOptions::RemoveEmpty) && sub.empty() || + options.Has(SplitOptions::RemoveSpace) && IsSpace(sub))) { + result.push_back(std::string(sub)); + } + current_pos = next_pos == std::string_view::npos ? std::string_view::npos + : next_pos + sep.size(); + } + + return result; +} } // namespace string using details::ExtractBits; |