From fb2fb3811fb28ffa1dfa69f471260ea2e47996f7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 6 Mar 2016 00:07:06 +0000 Subject: [PATCH] Tweak useage of skip_while and move the functions in utils.hh --- src/selectors.hh | 33 +++++++-------------------------- src/utils.hh | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/selectors.hh b/src/selectors.hh index 12ec7491..fd896068 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -24,20 +24,6 @@ inline Selection target_eol(Selection sel) return sel; } -template -void skip_while(Iterator& it, const EndIterator& end, T condition) -{ - while (it != end and condition(*it)) - ++it; -} - -template -void skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition) -{ - while (it != begin and condition(*it)) - --it; -} - using Utf8Iterator = utf8::iterator; inline Selection utf8_range(const BufferIterator& first, const BufferIterator& last) @@ -59,8 +45,8 @@ Selection select_to_next_word(const Buffer& buffer, const Selection& selection) if (categorize(*begin) != categorize(*(begin+1))) ++begin; - skip_while(begin, buffer.end(), [](Codepoint c){ return is_eol(c); }); - if (begin == buffer.end()) + if (not skip_while(begin, buffer.end(), + [](Codepoint c) { return is_eol(c); })) return selection; Utf8Iterator end = begin+1; @@ -83,8 +69,8 @@ Selection select_to_next_word_end(const Buffer& buffer, const Selection& selecti if (categorize(*begin) != categorize(*(begin+1))) ++begin; - skip_while(begin, buffer.end(), [](Codepoint c){ return is_eol(c); }); - if (begin == buffer.end()) + if (not skip_while(begin, buffer.end(), + [](Codepoint c) { return is_eol(c); })) return selection; Utf8Iterator end = begin; skip_while(end, buffer.end(), is_horizontal_blank); @@ -112,15 +98,10 @@ Selection select_to_previous_word(const Buffer& buffer, const Selection& selecti bool with_end = false; if (word_type == Word and is_punctuation(*end)) - { - skip_while_reverse(end, buffer.begin(), is_punctuation); - with_end = is_punctuation(*end); - } + with_end = skip_while_reverse(end, buffer.begin(), is_punctuation); + else if (is_word(*end)) - { - skip_while_reverse(end, buffer.begin(), is_word); - with_end = is_word(*end); - } + with_end = skip_while_reverse(end, buffer.begin(), is_word); return utf8_range(begin, with_end ? end : end+1); } diff --git a/src/utils.hh b/src/utils.hh index 68acc296..516d530c 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -99,6 +99,22 @@ const T& clamp(const T& val, const T& min, const T& max) return (val < min ? min : (val > max ? max : val)); } +template +bool skip_while(Iterator& it, const EndIterator& end, T condition) +{ + while (it != end and condition(*it)) + ++it; + return it != end; +} + +template +bool skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition) +{ + while (it != begin and condition(*it)) + --it; + return condition(*it); +} + } #endif // utils_hh_INCLUDED