From: Vsevolod Stakhov Date: Sat, 24 Jun 2023 14:51:57 +0000 (+0100) Subject: [Minor] Add utility to split strings on some character X-Git-Tag: 3.6~56 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=43adbcff65fa3efdee05f569b0bb2eb94b60d09b;p=thirdparty%2Frspamd.git [Minor] Add utility to split strings on some character --- diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 6faba92778..b852b38d58 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, bool> = true> +inline auto string_split_on(const S &input, std::string_view::value_type chr) -> std::pair +{ + 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 */