diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-06-24 15:51:57 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-06-24 15:51:57 +0100 |
commit | 43adbcff65fa3efdee05f569b0bb2eb94b60d09b (patch) | |
tree | b2493f5cfc6cd77af40330949547e9b44c325e08 /src/libutil | |
parent | 8c1c1ddd88a2ea84ca937fce536553cc4b8cd3da (diff) | |
download | rspamd-43adbcff65fa3efdee05f569b0bb2eb94b60d09b.tar.gz rspamd-43adbcff65fa3efdee05f569b0bb2eb94b60d09b.zip |
[Minor] Add utility to split strings on some character
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/cxx/util.hxx | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 6faba9277..b852b38d5 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -94,6 +94,24 @@ inline auto string_foreach_line(const S &input, const F &functor) } } +template<class S, typename std::enable_if_t<std::is_constructible_v<std::string_view, S>, bool> = true> +inline auto string_split_on(const S &input, std::string_view::value_type chr) -> std::pair<std::string_view, std::string_view> +{ + auto pos = std::find(std::begin(input), std::end(input), chr); + + if (pos != input.end()) { + auto first = std::string_view{std::begin(input), pos}; + while (*pos == chr && pos != input.end()) { + ++pos; + } + auto last = std::string_view{pos, std::end(input)}; + + return {first, last}; + } + + return {std::string_view{input}, std::string_view{}}; +} + /** * Enumerate for range loop */ |