Refactor regex based selection code

This commit is contained in:
Maxime Coste 2017-03-03 23:43:54 +00:00
parent 31edff85c3
commit ed3c48057d

View File

@ -808,24 +808,30 @@ void select_buffer(SelectionList& selections)
selections = SelectionList{ buffer, target_eol({{0,0}, buffer.back_coord()}) }; selections = SelectionList{ buffer, target_eol({{0,0}, buffer.back_coord()}) };
} }
template<Direction direction> static RegexConstant::match_flag_type
static bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, match_flags(const Buffer& buf, BufferIterator begin, BufferIterator end)
{
return match_flags(is_bol(begin.coord()), is_eol(buf, end.coord()),
is_bow(buf, begin.coord()), is_eow(buf, end.coord()));
}
static bool find_next(const Buffer& buffer, BufferIterator pos,
MatchResults<BufferIterator>& matches, MatchResults<BufferIterator>& matches,
const Regex& ex, bool& wrapped) const Regex& ex, bool& wrapped)
{ {
wrapped = false;
if (direction == Forward)
{
if (pos != buffer.end() and if (pos != buffer.end() and
regex_search(pos, buffer.end(), matches, ex, regex_search(pos, buffer.end(), matches, ex,
match_flags(is_bol(pos.coord()), true, match_flags(buffer, pos, buffer.end())))
is_bow(buffer, pos.coord()), true)))
return true; return true;
wrapped = true; wrapped = true;
return regex_search(buffer.begin(), buffer.end(), matches, ex); return regex_search(buffer.begin(), buffer.end(), matches, ex,
} match_flags(buffer, buffer.begin(), buffer.end()));
else }
{
static bool find_prev(const Buffer& buffer, BufferIterator pos,
MatchResults<BufferIterator>& matches,
const Regex& ex, bool& wrapped)
{
auto find_last_match = [&](const BufferIterator& pos) { auto find_last_match = [&](const BufferIterator& pos) {
MatchResults<BufferIterator> m; MatchResults<BufferIterator> m;
const bool is_pos_eol = is_eol(buffer, pos.coord()); const bool is_pos_eol = is_eol(buffer, pos.coord());
@ -842,39 +848,30 @@ static bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos,
} }
return not matches.empty(); return not matches.empty();
}; };
if (find_last_match(pos)) if (find_last_match(pos))
return true; return true;
wrapped = true; wrapped = true;
return find_last_match(buffer.end()); return find_last_match(buffer.end());
}
} }
template bool find_match_in_buffer<Forward>(const Buffer&, const BufferIterator,
MatchResults<BufferIterator>&, const Regex&, bool&);
template bool find_match_in_buffer<Backward>(const Buffer&, const BufferIterator,
MatchResults<BufferIterator>&, const Regex&, bool&);
template<Direction direction> template<Direction direction>
Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex, bool& wrapped) Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex, bool& wrapped)
{ {
auto begin = buffer.iterator_at(direction == Backward ? sel.min() : sel.max());
auto end = begin;
CaptureList captures;
MatchResults<BufferIterator> matches; MatchResults<BufferIterator> matches;
bool found = false; auto pos = buffer.iterator_at(direction == Backward ? sel.min() : sel.max());
auto pos = direction == Forward ? utf8::next(begin, buffer.end()) : begin; wrapped = false;
if ((found = find_match_in_buffer<direction>(buffer, pos, matches, regex, wrapped))) const bool found = (direction == Forward) ?
{ find_next(buffer, utf8::next(pos, buffer.end()), matches, regex, wrapped)
begin = matches[0].first; : find_prev(buffer, pos, matches, regex, wrapped);
end = matches[0].second;
for (const auto& match : matches) if (not found or matches[0].first == buffer.end())
captures.push_back(buffer.string(match.first.coord(),
match.second.coord()));
}
if (not found or begin == buffer.end())
throw runtime_error(format("'{}': no matches found", regex.str())); throw runtime_error(format("'{}': no matches found", regex.str()));
CaptureList captures;
for (const auto& match : matches)
captures.push_back(buffer.string(match.first.coord(), match.second.coord()));
auto begin = matches[0].first, end = matches[0].second;
end = (begin == end) ? end : utf8::previous(end, begin); end = (begin == end) ? end : utf8::previous(end, begin);
if (direction == Backward) if (direction == Backward)
std::swap(begin, end); std::swap(begin, end);
@ -898,11 +895,8 @@ void select_all_matches(SelectionList& selections, const Regex& regex, int captu
{ {
auto sel_beg = buffer.iterator_at(sel.min()); auto sel_beg = buffer.iterator_at(sel.min());
auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end()); auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end());
const auto flags = match_flags(is_bol(sel_beg.coord()), RegexIt re_it(sel_beg, sel_end, regex,
is_eol(buffer, sel_end.coord()), match_flags(buffer, sel_beg, sel_end));
is_bow(buffer, sel_beg.coord()),
is_eow(buffer, sel_end.coord()));
RegexIt re_it(sel_beg, sel_end, regex, flags);
RegexIt re_end; RegexIt re_end;
for (; re_it != re_end; ++re_it) for (; re_it != re_end; ++re_it)
@ -945,12 +939,9 @@ void split_selections(SelectionList& selections, const Regex& regex, int capture
{ {
auto begin = buffer.iterator_at(sel.min()); auto begin = buffer.iterator_at(sel.min());
auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end()); auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end());
const auto flags = match_flags(is_bol(begin.coord()),
is_eol(buffer, sel_end.coord()),
is_bow(buffer, begin.coord()),
is_eow(buffer, sel_end.coord()));
RegexIt re_it(begin, sel_end, regex, flags); RegexIt re_it(begin, sel_end, regex,
match_flags(buffer, begin, sel_end));
RegexIt re_end; RegexIt re_end;
for (; re_it != re_end; ++re_it) for (; re_it != re_end; ++re_it)