RegexSelector: support captures

This commit is contained in:
Maxime Coste 2011-11-16 20:05:29 +00:00
parent 18913cfbff
commit bef193ae54

View File

@ -9,28 +9,48 @@ RegexSelector::RegexSelector(const std::string& exp)
Selection RegexSelector::operator()(const BufferIterator& cursor) const Selection RegexSelector::operator()(const BufferIterator& cursor) const
{ {
BufferIterator begin = cursor;
BufferIterator end = cursor;
Selection::CaptureList captures;
try try
{ {
boost::match_results<BufferIterator> matches; boost::match_results<BufferIterator> matches;
if (boost::regex_search(cursor, cursor.buffer().end(), matches, m_regex, boost::match_nosubs)) if (boost::regex_search(cursor, cursor.buffer().end(), matches,
return Selection(matches.begin()->first, matches.begin()->second-1); m_regex))
else if (boost::regex_search(cursor.buffer().begin(), cursor, matches, m_regex, boost::match_nosubs)) {
return Selection(matches.begin()->first, matches.begin()->second-1); begin = matches[0].first;
end = matches[0].second;
std::copy(matches.begin(), matches.end(),
std::back_inserter(captures));
}
else if (boost::regex_search(cursor.buffer().begin(), cursor, matches,
m_regex))
{
begin = matches[0].first;
end = matches[0].second;
std::copy(matches.begin(), matches.end(),
std::back_inserter(captures));
}
} }
catch (boost::regex_error& err) catch (boost::regex_error& err)
{ {
throw runtime_error("regex error"); throw runtime_error("regex error");
} }
return Selection(cursor, cursor);
if (begin == end)
++end;
return Selection(begin, end - 1, std::move(captures));
} }
SelectionList RegexSelector::operator()(const Selection& selection) const SelectionList RegexSelector::operator()(const Selection& selection) const
{ {
boost::regex_iterator<BufferIterator> re_it(selection.begin(), boost::regex_iterator<BufferIterator> re_it(selection.begin(),
selection.end(), selection.end(),
m_regex, boost::match_nosubs); m_regex);
boost::regex_iterator<BufferIterator> re_end; boost::regex_iterator<BufferIterator> re_end;
SelectionList result; SelectionList result;
@ -38,9 +58,12 @@ SelectionList RegexSelector::operator()(const Selection& selection) const
{ {
BufferIterator begin = (*re_it)[0].first; BufferIterator begin = (*re_it)[0].first;
BufferIterator end = (*re_it)[0].second; BufferIterator end = (*re_it)[0].second;
assert(begin != end);
result.push_back(Selection(begin, end-1)); if (begin == end)
++end;
Selection::CaptureList captures(re_it->begin(), re_it->end());
result.push_back(Selection(begin, end-1, std::move(captures)));
} }
return result; return result;
} }