remove BufferIterator::is_{begin,end}

This commit is contained in:
Maxime Coste 2013-06-04 18:53:56 +02:00
parent 51acd456cc
commit 9fb4d42408
5 changed files with 68 additions and 87 deletions

View File

@ -62,8 +62,6 @@ public:
BufferIterator& operator=(const BufferCoord& coord); BufferIterator& operator=(const BufferCoord& coord);
operator const BufferCoord&() const { return m_coord; } operator const BufferCoord&() const { return m_coord; }
bool is_begin() const;
bool is_end() const;
bool is_valid() const; bool is_valid() const;
const Buffer& buffer() const; const Buffer& buffer() const;

View File

@ -127,17 +127,5 @@ inline BufferIterator& BufferIterator::operator=(const BufferCoord& coord)
return *this; return *this;
} }
inline bool BufferIterator::is_begin() const
{
kak_assert(m_buffer);
return m_coord.line == 0 and m_coord.column == 0;
}
inline bool BufferIterator::is_end() const
{
kak_assert(m_buffer);
return m_buffer->is_end(m_coord);
}
} }
#endif // buffer_iterator_inl_h_INCLUDED #endif // buffer_iterator_inl_h_INCLUDED

View File

@ -13,7 +13,7 @@ void preserve_indent(Buffer& buffer, Selection& selection, String& content)
BufferCoord line_begin{selection.last().line, 0}; BufferCoord line_begin{selection.last().line, 0};
auto first_non_white = buffer.iterator_at(line_begin); auto first_non_white = buffer.iterator_at(line_begin);
while ((*first_non_white == '\t' or *first_non_white == ' ') and while ((*first_non_white == '\t' or *first_non_white == ' ') and
not first_non_white.is_end()) first_non_white != buffer.end())
++first_non_white; ++first_non_white;
content += buffer.string(line_begin, first_non_white); content += buffer.string(line_begin, first_non_white);
@ -23,11 +23,11 @@ void preserve_indent(Buffer& buffer, Selection& selection, String& content)
void cleanup_whitespaces(Buffer& buffer, Selection& selection, String& content) void cleanup_whitespaces(Buffer& buffer, Selection& selection, String& content)
{ {
const auto position = buffer.iterator_at(selection.last()); const auto position = buffer.iterator_at(selection.last());
if (content[0] == '\n' and not position.is_begin()) if (content[0] == '\n' and position != buffer.begin())
{ {
auto whitespace_start = position-1; auto whitespace_start = position-1;
while ((*whitespace_start == ' ' or *whitespace_start == '\t') and while ((*whitespace_start == ' ' or *whitespace_start == '\t') and
not whitespace_start .is_begin()) whitespace_start != buffer.begin())
--whitespace_start; --whitespace_start;
++whitespace_start; ++whitespace_start;
if (whitespace_start != position) if (whitespace_start != position)

View File

@ -501,12 +501,12 @@ static BufferCompletion complete_word(const Buffer& buffer,
bool other_buffers) bool other_buffers)
{ {
auto pos = buffer.iterator_at(cursor_pos); auto pos = buffer.iterator_at(cursor_pos);
if (pos.is_begin() or not is_word(*utf8::previous(pos))) if (pos == buffer.begin() or not is_word(*utf8::previous(pos)))
return {}; return {};
auto end = buffer.iterator_at(cursor_pos); auto end = buffer.iterator_at(cursor_pos);
auto begin = end-1; auto begin = end-1;
while (not begin.is_begin() and is_word(*begin)) while (begin != buffer.begin() and is_word(*begin))
--begin; --begin;
if (not is_word(*begin)) if (not is_word(*begin))
++begin; ++begin;

View File

@ -53,23 +53,17 @@ CharCategories categorize(Codepoint c)
: CharCategories::Punctuation; : CharCategories::Punctuation;
} }
bool is_begin(const BufferIterator& it) { return it.is_begin(); } template<typename Iterator, typename EndIterator, typename T>
bool is_end(const BufferIterator& it) { return it.is_end(); } void skip_while(Iterator& it, const EndIterator& end, T condition)
bool is_begin(const Utf8Iterator& it) { return it.underlying_iterator().is_begin(); }
bool is_end(const Utf8Iterator& it) { return it.underlying_iterator().is_end(); }
template<typename Iterator, typename T>
void skip_while(Iterator& it, T condition)
{ {
while (not is_end(it) and condition(*it)) while (it != end and condition(*it))
++it; ++it;
} }
template<typename Iterator, typename T> template<typename Iterator, typename BeginIterator, typename T>
void skip_while_reverse(Iterator& it, T condition) void skip_while_reverse(Iterator& it, const BeginIterator& begin, T condition)
{ {
while (not is_begin(it) and condition(*it)) while (it != begin and condition(*it))
--it; --it;
} }
@ -86,23 +80,23 @@ template<bool punctuation_is_word>
Selection select_to_next_word(const Buffer& buffer, const Selection& selection) Selection select_to_next_word(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (is_end(begin+1)) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<punctuation_is_word>(*begin) !=
categorize<punctuation_is_word>(*(begin+1))) categorize<punctuation_is_word>(*(begin+1)))
++begin; ++begin;
skip_while(begin, is_eol); skip_while(begin, buffer.end(), is_eol);
if (is_end(begin)) if (begin == buffer.end())
return selection; return selection;
Utf8Iterator end = begin+1; Utf8Iterator end = begin+1;
if (not punctuation_is_word and is_punctuation(*begin)) if (not punctuation_is_word and is_punctuation(*begin))
skip_while(end, is_punctuation); skip_while(end, buffer.end(), is_punctuation);
else if (is_word<punctuation_is_word>(*begin)) else if (is_word<punctuation_is_word>(*begin))
skip_while(end, is_word<punctuation_is_word>); skip_while(end, buffer.end(), is_word<punctuation_is_word>);
skip_while(end, is_blank); skip_while(end, buffer.end(), is_blank);
return utf8_range(begin, end-1); return utf8_range(begin, end-1);
} }
@ -113,22 +107,22 @@ template<bool punctuation_is_word>
Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection) Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (is_end(begin+1)) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<punctuation_is_word>(*begin) !=
categorize<punctuation_is_word>(*(begin+1))) categorize<punctuation_is_word>(*(begin+1)))
++begin; ++begin;
skip_while(begin, is_eol); skip_while(begin, buffer.end(), is_eol);
if (is_end(begin)) if (begin == buffer.end())
return selection; return selection;
Utf8Iterator end = begin; Utf8Iterator end = begin;
skip_while(end, is_blank); skip_while(end, buffer.end(), is_blank);
if (not punctuation_is_word and is_punctuation(*end)) if (not punctuation_is_word and is_punctuation(*end))
skip_while(end, is_punctuation); skip_while(end, buffer.end(), is_punctuation);
else if (is_word<punctuation_is_word>(*end)) else if (is_word<punctuation_is_word>(*end))
skip_while(end, is_word<punctuation_is_word>); skip_while(end, buffer.end(), is_word<punctuation_is_word>);
return utf8_range(begin, end-1); return utf8_range(begin, end-1);
} }
@ -139,25 +133,25 @@ template<bool punctuation_is_word>
Selection select_to_previous_word(const Buffer& buffer, const Selection& selection) Selection select_to_previous_word(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (is_end(begin+1)) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<punctuation_is_word>(*begin) !=
categorize<punctuation_is_word>(*(begin-1))) categorize<punctuation_is_word>(*(begin-1)))
--begin; --begin;
skip_while_reverse(begin, is_eol); skip_while_reverse(begin, buffer.begin(), is_eol);
Utf8Iterator end = begin; Utf8Iterator end = begin;
skip_while_reverse(end, is_blank); skip_while_reverse(end, buffer.begin(), is_blank);
bool with_end = false; bool with_end = false;
if (not punctuation_is_word and is_punctuation(*end)) if (not punctuation_is_word and is_punctuation(*end))
{ {
skip_while_reverse(end, is_punctuation); skip_while_reverse(end, buffer.begin(), is_punctuation);
with_end = is_punctuation(*end); with_end = is_punctuation(*end);
} }
else if (is_word<punctuation_is_word>(*end)) else if (is_word<punctuation_is_word>(*end))
{ {
skip_while_reverse(end, is_word<punctuation_is_word>); skip_while_reverse(end, buffer.begin(), is_word<punctuation_is_word>);
with_end = is_word<punctuation_is_word>(*end); with_end = is_word<punctuation_is_word>(*end);
} }
@ -169,14 +163,14 @@ template Selection select_to_previous_word<true>(const Buffer&, const Selection&
Selection select_line(const Buffer& buffer, const Selection& selection) Selection select_line(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator first = buffer.iterator_at(selection.last()); Utf8Iterator first = buffer.iterator_at(selection.last());
if (*first == '\n' and not is_end(first + 1)) if (*first == '\n' and first + 1 != buffer.end())
++first; ++first;
while (not is_begin(first) and *(first - 1) != '\n') while (first != buffer.begin() and *(first - 1) != '\n')
--first; --first;
Utf8Iterator last = first; Utf8Iterator last = first;
while (not is_end(last + 1) and *last != '\n') while (last + 1 != buffer.end() and *last != '\n')
++last; ++last;
return utf8_range(first, last); return utf8_range(first, last);
} }
@ -203,13 +197,12 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
int level = 0; int level = 0;
const Codepoint opening = *match; const Codepoint opening = *match;
const Codepoint closing = *(match+1); const Codepoint closing = *(match+1);
while (not is_end(it)) while (it != buffer.end())
{ {
if (*it == opening) if (*it == opening)
++level; ++level;
else if (*it == closing and --level == 0) else if (*it == closing and --level == 0)
return utf8_range(begin, it); return utf8_range(begin, it);
++it; ++it;
} }
} }
@ -224,7 +217,7 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
++level; ++level;
else if (*it == opening and --level == 0) else if (*it == opening and --level == 0)
return utf8_range(begin, it); return utf8_range(begin, it);
if (is_begin(it)) if (it == buffer.begin())
break; break;
--it; --it;
} }
@ -234,18 +227,20 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
// c++14 will add std::optional, so we use boost::optional until then // c++14 will add std::optional, so we use boost::optional until then
using boost::optional; using boost::optional;
static optional<Range> find_surrounding(const BufferIterator& pos, static optional<Range> find_surrounding(const Buffer& buffer,
const BufferCoord& coord,
const CodepointPair& matching, const CodepointPair& matching,
ObjectFlags flags) ObjectFlags flags)
{ {
const bool to_begin = flags & ObjectFlags::ToBegin; const bool to_begin = flags & ObjectFlags::ToBegin;
const bool to_end = flags & ObjectFlags::ToEnd; const bool to_end = flags & ObjectFlags::ToEnd;
const bool nestable = matching.first != matching.second; const bool nestable = matching.first != matching.second;
auto pos = buffer.iterator_at(coord);
Utf8Iterator first = pos; Utf8Iterator first = pos;
if (to_begin) if (to_begin)
{ {
int level = 0; int level = 0;
while (not is_begin(first)) while (first != buffer.begin())
{ {
if (nestable and first != pos and *first == matching.second) if (nestable and first != pos and *first == matching.second)
++level; ++level;
@ -267,7 +262,7 @@ static optional<Range> find_surrounding(const BufferIterator& pos,
{ {
int level = 0; int level = 0;
last = first + 1; last = first + 1;
while (not is_end(last)) while (last != buffer.end())
{ {
if (nestable and *last == matching.first) if (nestable and *last == matching.first)
++level; ++level;
@ -298,7 +293,7 @@ Selection select_surrounding(const Buffer& buffer, const Selection& selection,
const CodepointPair& matching, const CodepointPair& matching,
ObjectFlags flags) ObjectFlags flags)
{ {
auto res = find_surrounding(buffer.iterator_at(selection.last()), matching, flags); auto res = find_surrounding(buffer, selection.last(), matching, flags);
if (not res) if (not res)
return selection; return selection;
@ -306,7 +301,7 @@ Selection select_surrounding(const Buffer& buffer, const Selection& selection,
matching.first != matching.second and not buffer.is_end(res->last()) and matching.first != matching.second and not buffer.is_end(res->last()) and
(*res == selection or Range{res->last(), res->first()} == selection)) (*res == selection or Range{res->last(), res->first()} == selection))
{ {
res = find_surrounding(buffer.iterator_at(res->last()) + 1, matching, flags); res = find_surrounding(buffer, buffer.next(res->last()), matching, flags);
return res ? Selection{*res} : selection; return res ? Selection{*res} : selection;
} }
return *res; return *res;
@ -320,8 +315,8 @@ Selection select_to(const Buffer& buffer, const Selection& selection,
do do
{ {
++end; ++end;
skip_while(end, [c](Codepoint cur) { return cur != c; }); skip_while(end, buffer.end(), [c](Codepoint cur) { return cur != c; });
if (is_end(end)) if (end == buffer.end())
return selection; return selection;
} }
while (--count > 0); while (--count > 0);
@ -337,8 +332,8 @@ Selection select_to_reverse(const Buffer& buffer, const Selection& selection,
do do
{ {
--end; --end;
skip_while_reverse(end, [c](Codepoint cur) { return cur != c; }); skip_while_reverse(end, buffer.begin(), [c](Codepoint cur) { return cur != c; });
if (is_begin(end)) if (end == buffer.begin())
return selection; return selection;
} }
while (--count > 0); while (--count > 0);
@ -350,7 +345,7 @@ Selection select_to_eol(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
Utf8Iterator end = begin + 1; Utf8Iterator end = begin + 1;
skip_while(end, [](Codepoint cur) { return not is_eol(cur); }); skip_while(end, buffer.end(), [](Codepoint cur) { return not is_eol(cur); });
return utf8_range(begin, end-1); return utf8_range(begin, end-1);
} }
@ -358,8 +353,8 @@ Selection select_to_eol_reverse(const Buffer& buffer, const Selection& selection
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
Utf8Iterator end = begin - 1; Utf8Iterator end = begin - 1;
skip_while_reverse(end, [](Codepoint cur) { return not is_eol(cur); }); skip_while_reverse(end, buffer.begin(), [](Codepoint cur) { return not is_eol(cur); });
return utf8_range(begin, is_begin(end) ? end : end+1); return utf8_range(begin, end == buffer.begin() ? end : end+1);
} }
template<bool punctuation_is_word> template<bool punctuation_is_word>
@ -371,15 +366,15 @@ Selection select_whole_word(const Buffer& buffer, const Selection& selection, Ob
{ {
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
skip_while_reverse(first, is_word<punctuation_is_word>); skip_while_reverse(first, buffer.begin(), is_word<punctuation_is_word>);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<punctuation_is_word>(*first))
++first; ++first;
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
skip_while(last, is_word<punctuation_is_word>); skip_while(last, buffer.end(), is_word<punctuation_is_word>);
if (not (flags & ObjectFlags::Inner)) if (not (flags & ObjectFlags::Inner))
skip_while(last, is_blank); skip_while(last, buffer.end(), is_blank);
--last; --last;
} }
} }
@ -387,16 +382,16 @@ Selection select_whole_word(const Buffer& buffer, const Selection& selection, Ob
{ {
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
skip_while_reverse(first, is_blank); skip_while_reverse(first, buffer.begin(), is_blank);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<punctuation_is_word>(*first))
return selection; return selection;
skip_while_reverse(first, is_word<punctuation_is_word>); skip_while_reverse(first, buffer.begin(), is_word<punctuation_is_word>);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<punctuation_is_word>(*first))
++first; ++first;
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
skip_while(last, is_blank); skip_while(last, buffer.end(), is_blank);
--last; --last;
} }
} }
@ -414,7 +409,7 @@ Selection select_whole_sentence(const Buffer& buffer, const Selection& selection
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
bool saw_non_blank = false; bool saw_non_blank = false;
while (not is_begin(first)) while (first != buffer.begin())
{ {
char cur = *first; char cur = *first;
char prev = *(first-1); char prev = *(first-1);
@ -434,22 +429,22 @@ Selection select_whole_sentence(const Buffer& buffer, const Selection& selection
} }
--first; --first;
} }
skip_while(first, is_blank); skip_while(first, buffer.end(), is_blank);
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
while (not is_end(last)) while (last != buffer.end())
{ {
char cur = *last; char cur = *last;
if (cur == '.' or cur == ';' or cur == '!' or cur == '?' or if (cur == '.' or cur == ';' or cur == '!' or cur == '?' or
(is_eol(cur) and (is_end(last+1) or is_eol(*last+1)))) (is_eol(cur) and (last+1 == buffer.end() or is_eol(*(last+1)))))
break; break;
++last; ++last;
} }
if (not (flags & ObjectFlags::Inner) and not is_end(last)) if (not (flags & ObjectFlags::Inner) and last != buffer.end())
{ {
++last; ++last;
skip_while(last, is_blank); skip_while(last, buffer.end(), is_blank);
--last; --last;
} }
} }
@ -462,12 +457,12 @@ Selection select_whole_paragraph(const Buffer& buffer, const Selection& selectio
BufferIterator first = buffer.iterator_at(selection.last()); BufferIterator first = buffer.iterator_at(selection.last());
BufferIterator last = first; BufferIterator last = first;
if (flags & ObjectFlags::ToBegin and not is_begin(first)) if (flags & ObjectFlags::ToBegin and first != buffer.begin())
{ {
skip_while_reverse(first, is_eol); skip_while_reverse(first, buffer.begin(), is_eol);
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
last = first; last = first;
while (not is_begin(first)) while (first != buffer.begin())
{ {
char cur = *first; char cur = *first;
char prev = *(first-1); char prev = *(first-1);
@ -481,14 +476,14 @@ Selection select_whole_paragraph(const Buffer& buffer, const Selection& selectio
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
while (not is_end(last)) while (last != buffer.end())
{ {
char cur = *last; char cur = *last;
char prev = *(last-1); char prev = *(last-1);
if (is_eol(cur) and is_eol(prev)) if (is_eol(cur) and is_eol(prev))
{ {
if (not (flags & ObjectFlags::Inner)) if (not (flags & ObjectFlags::Inner))
skip_while(last, is_eol); skip_while(last, buffer.end(), is_eol);
break; break;
} }
++last; ++last;
@ -508,12 +503,12 @@ Selection select_whole_lines(const Buffer& buffer, const Selection& selection)
BufferIterator& to_line_end = first <= last ? last : first; BufferIterator& to_line_end = first <= last ? last : first;
--to_line_start; --to_line_start;
skip_while_reverse(to_line_start, [](char cur) { return not is_eol(cur); }); skip_while_reverse(to_line_start, buffer.begin(), [](char cur) { return not is_eol(cur); });
if (is_eol(*to_line_start)) if (is_eol(*to_line_start))
++to_line_start; ++to_line_start;
skip_while(to_line_end, [](char cur) { return not is_eol(cur); }); skip_while(to_line_end, buffer.end(), [](char cur) { return not is_eol(cur); });
if (is_end(to_line_end)) if (to_line_end == buffer.end())
--to_line_end; --to_line_end;
return Selection(first, last); return Selection(first, last);
@ -527,7 +522,7 @@ Selection trim_partial_lines(const Buffer& buffer, const Selection& selection)
BufferIterator& to_line_start = first <= last ? first : last; BufferIterator& to_line_start = first <= last ? first : last;
BufferIterator& to_line_end = first <= last ? last : first; BufferIterator& to_line_end = first <= last ? last : first;
while (not is_begin(to_line_start) and *(to_line_start-1) != '\n') while (to_line_start != buffer.begin() and *(to_line_start-1) != '\n')
++to_line_start; ++to_line_start;
while (*(to_line_end+1) != '\n' and to_line_end != to_line_start) while (*(to_line_end+1) != '\n' and to_line_end != to_line_start)
--to_line_end; --to_line_end;