From bef193ae54796b7efd6ceed1b2b3e4b2906294f9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 16 Nov 2011 20:05:29 +0000 Subject: [PATCH] RegexSelector: support captures --- src/regex_selector.cc | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/regex_selector.cc b/src/regex_selector.cc index 821f79f3..0bfce956 100644 --- a/src/regex_selector.cc +++ b/src/regex_selector.cc @@ -9,28 +9,48 @@ RegexSelector::RegexSelector(const std::string& exp) Selection RegexSelector::operator()(const BufferIterator& cursor) const { + BufferIterator begin = cursor; + BufferIterator end = cursor; + Selection::CaptureList captures; + try { boost::match_results matches; - if (boost::regex_search(cursor, cursor.buffer().end(), matches, m_regex, boost::match_nosubs)) - return Selection(matches.begin()->first, matches.begin()->second-1); - else if (boost::regex_search(cursor.buffer().begin(), cursor, matches, m_regex, boost::match_nosubs)) - return Selection(matches.begin()->first, matches.begin()->second-1); + if (boost::regex_search(cursor, cursor.buffer().end(), matches, + m_regex)) + { + 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) { 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 { boost::regex_iterator re_it(selection.begin(), selection.end(), - m_regex, boost::match_nosubs); + m_regex); boost::regex_iterator re_end; SelectionList result; @@ -38,9 +58,12 @@ SelectionList RegexSelector::operator()(const Selection& selection) const { BufferIterator begin = (*re_it)[0].first; 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; }