diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-02-02 22:48:54 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-02-02 22:54:05 +0000 |
| commit | 18eec7e511de7b13ccb2a6e561bd2613c68e9460 (patch) | |
| tree | 2fd5cca3863d6693670efa48c9ff754b187f2266 /src | |
| parent | 340fc63f8472934060525325081fe34bd1f0752f (diff) | |
Regex selectors more tolerant to matches not ending on char boundaries
Diffstat (limited to 'src')
| -rw-r--r-- | src/selectors.cc | 7 | ||||
| -rw-r--r-- | src/selectors.hh | 10 |
2 files changed, 12 insertions, 5 deletions
diff --git a/src/selectors.cc b/src/selectors.cc index f5fdb088..781ea0e0 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -510,8 +510,8 @@ void select_all_matches(SelectionList& selections, const Regex& regex) for (; re_it != re_end; ++re_it) { - auto& begin = (*re_it)[0].first; - auto& end = (*re_it)[0].second; + auto begin = ensure_char_start(buffer, (*re_it)[0].first); + auto end = ensure_char_start(buffer, (*re_it)[0].second); if (begin == sel_end) continue; @@ -549,8 +549,9 @@ void split_selections(SelectionList& selections, const Regex& regex) if (end == buf_end) continue; + end = ensure_char_start(buffer, end); result.push_back(keep_direction({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }, sel)); - begin = (*re_it)[0].second; + begin = ensure_char_start(buffer, (*re_it)[0].second); } if (begin.coord() <= sel.max()) result.push_back(keep_direction({ begin.coord(), sel.max() }, sel)); diff --git a/src/selectors.hh b/src/selectors.hh index 3dc4e5ba..09ba45e1 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -256,6 +256,12 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, find_last_match(buffer.begin(), buffer.end(), matches, ex)); } +inline BufferIterator ensure_char_start(const Buffer& buffer, const BufferIterator& it) +{ + return it != buffer.end() ? + utf8::character_start(it, buffer.iterator_at(it.coord().line)) : it; +} + template<Direction direction> Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex) { @@ -269,8 +275,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege : utf8::previous(begin, buffer.begin()); if ((found = find_match_in_buffer<direction>(buffer, pos, matches, regex))) { - begin = matches[0].first; - end = matches[0].second; + begin = ensure_char_start(buffer, matches[0].first); + end = ensure_char_start(buffer, matches[0].second); for (auto& match : matches) captures.emplace_back(match.first, match.second); } |
