From: Vsevolod Stakhov Date: Sat, 24 Jun 2023 13:55:55 +0000 (+0100) Subject: [Minor] Add utility to iterate over a list of newline separated strings X-Git-Tag: 3.6~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8c1c1ddd88a2ea84ca937fce536553cc4b8cd3da;p=rspamd.git [Minor] Add utility to iterate over a list of newline separated strings --- diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 88a022954..6faba9277 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -65,6 +65,35 @@ inline constexpr auto make_string_view_from_it(_It begin, _It end) }; } +/** + * Iterate over lines in a string, newline characters are dropped + * @tparam S + * @tparam F + * @param input + * @param functor + * @return + */ +template && + std::is_constructible_v, bool> = true> +inline auto string_foreach_line(const S &input, const F &functor) +{ + auto it = input.begin(); + auto end = input.end(); + + while (it != end) { + auto next = std::find(it, end, '\n'); + while (next >= it && (*next == '\n' || *next == '\r')) { + --next; + } + functor(make_string_view_from_it(it, next)); + it = next; + + if (it != end) { + ++it; + } + } +} + /** * Enumerate for range loop */