diff options
| author | Maxime Coste <mawww@kakoune.org> | 2019-11-23 21:50:58 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2019-11-23 21:50:58 +1100 |
| commit | 19e1be8e0d1946bbff02412ec703be33c7f4fede (patch) | |
| tree | 1c65a86fd78edae7cfdd2e5a8983055656d93df4 /src/string_utils.hh | |
| parent | 936bd923eae554d83dc77ab788d84b6d83c108f9 (diff) | |
Make wrap_lines a lazy range view
Avoid the need to allocate a vector by using the ranges
framework.
Diffstat (limited to 'src/string_utils.hh')
| -rw-r--r-- | src/string_utils.hh | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/string_utils.hh b/src/string_utils.hh index 49f4baeb..6f78b674 100644 --- a/src/string_utils.hh +++ b/src/string_utils.hh @@ -4,6 +4,7 @@ #include "string.hh" #include "enum.hh" #include "vector.hh" +#include "ranges.hh" #include "optional.hh" namespace Kakoune @@ -61,7 +62,39 @@ bool subsequence_match(StringView str, StringView subseq); String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col = 0); -Vector<StringView> wrap_lines(StringView text, ColumnCount max_width); +struct WrapView +{ + struct Iterator : std::iterator<std::forward_iterator_tag, StringView> + { + Iterator(StringView text, ColumnCount max_width); + + Iterator& operator++(); + Iterator operator++(int) { auto copy = *this; ++(*this); return copy; } + + bool operator==(Iterator other) const { return m_remaining == other.m_remaining and m_current == other.m_current; } + bool operator!=(Iterator other) const { return not (*this == other); } + + StringView operator*() { return m_current; } + + private: + StringView m_current; + StringView m_remaining; + ColumnCount m_max_width; + }; + + Iterator begin() const { return {text, max_width}; } + Iterator end() const { return {{}, 1}; } + + StringView text; + ColumnCount max_width; +}; + +inline auto wrap_at(ColumnCount max_width) +{ + return make_view_factory([=](StringView text) { + return WrapView{text, max_width}; + }); +} int str_to_int(StringView str); // throws on error Optional<int> str_to_int_ifp(StringView str); |
