Tweak useage of skip_while and move the functions in utils.hh

This commit is contained in:
Maxime Coste 2016-03-06 00:07:06 +00:00
parent 72b6340b35
commit fb2fb3811f
2 changed files with 23 additions and 26 deletions

View File

@ -24,20 +24,6 @@ inline Selection target_eol(Selection sel)
return sel; return sel;
} }
template<typename Iterator, typename EndIterator, typename T>
void skip_while(Iterator& it, const EndIterator& end, T condition)
{
while (it != end and condition(*it))
++it;
}
template<typename Iterator, typename BeginIterator, typename T>
void skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition)
{
while (it != begin and condition(*it))
--it;
}
using Utf8Iterator = utf8::iterator<BufferIterator, utf8::InvalidPolicy::Pass>; using Utf8Iterator = utf8::iterator<BufferIterator, utf8::InvalidPolicy::Pass>;
inline Selection utf8_range(const BufferIterator& first, const BufferIterator& last) 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<word_type>(*begin) != categorize<word_type>(*(begin+1))) if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
++begin; ++begin;
skip_while(begin, buffer.end(), [](Codepoint c){ return is_eol(c); }); if (not skip_while(begin, buffer.end(),
if (begin == buffer.end()) [](Codepoint c) { return is_eol(c); }))
return selection; return selection;
Utf8Iterator end = begin+1; Utf8Iterator end = begin+1;
@ -83,8 +69,8 @@ Selection select_to_next_word_end(const Buffer& buffer, const Selection& selecti
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1))) if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
++begin; ++begin;
skip_while(begin, buffer.end(), [](Codepoint c){ return is_eol(c); }); if (not skip_while(begin, buffer.end(),
if (begin == buffer.end()) [](Codepoint c) { return is_eol(c); }))
return selection; return selection;
Utf8Iterator end = begin; Utf8Iterator end = begin;
skip_while(end, buffer.end(), is_horizontal_blank); 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; bool with_end = false;
if (word_type == Word and is_punctuation(*end)) if (word_type == Word and is_punctuation(*end))
{ with_end = skip_while_reverse(end, buffer.begin(), is_punctuation);
skip_while_reverse(end, buffer.begin(), is_punctuation);
with_end = is_punctuation(*end);
}
else if (is_word<word_type>(*end)) else if (is_word<word_type>(*end))
{ with_end = skip_while_reverse(end, buffer.begin(), is_word<word_type>);
skip_while_reverse(end, buffer.begin(), is_word<word_type>);
with_end = is_word<word_type>(*end);
}
return utf8_range(begin, with_end ? end : end+1); return utf8_range(begin, with_end ? end : end+1);
} }

View File

@ -99,6 +99,22 @@ const T& clamp(const T& val, const T& min, const T& max)
return (val < min ? min : (val > max ? max : val)); return (val < min ? min : (val > max ? max : val));
} }
template<typename Iterator, typename EndIterator, typename T>
bool skip_while(Iterator& it, const EndIterator& end, T condition)
{
while (it != end and condition(*it))
++it;
return it != end;
}
template<typename Iterator, typename BeginIterator, typename T>
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 #endif // utils_hh_INCLUDED