home/src/regex_selector.cc
Maxime Coste 662ba0c904 Selection: do not use [begin, end) semantics but [first, last]
selections are now defined with inclusive iterators, which means that
Selection(cursor, cursor) is a valid selection of the charateter pointed
by cursor.

On the user interface side, that means that the cursor is now part of
the selection, selectors were adapted to this behavior (and word
selectors are now much more intuitive)
2011-09-23 09:17:19 +00:00

32 lines
854 B
C++

#include "regex_selector.hh"
#include "exception.hh"
namespace Kakoune
{
RegexSelector::RegexSelector(const std::string& exp)
: m_regex(exp) {}
Selection RegexSelector::operator()(const BufferIterator& cursor) const
{
BufferIterator line_end = cursor + 1;
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);
}
catch (boost::regex_error& err)
{
throw runtime_error("regex error");
}
return Selection(cursor, cursor);
}
}