diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2011-11-16 20:05:29 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2011-11-16 20:05:29 +0000 |
| commit | bef193ae54796b7efd6ceed1b2b3e4b2906294f9 (patch) | |
| tree | 66caa39e5cd500dc9be3821aa4f10109babc73eb /src | |
| parent | 18913cfbff30796118f2e08f53df8c5136b8007d (diff) | |
RegexSelector: support captures
Diffstat (limited to 'src')
| -rw-r--r-- | src/regex_selector.cc | 39 |
1 files 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<BufferIterator> 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<BufferIterator> re_it(selection.begin(), selection.end(), - m_regex, boost::match_nosubs); + m_regex); boost::regex_iterator<BufferIterator> 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; } |
