From 47df1374fe4a8ed5575ae607c654bbf68988ebb4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 17 Feb 2016 23:40:14 +0000 Subject: [PATCH] Refactor use selection as search pattern implementation --- src/buffer_utils.hh | 9 +++++++++ src/normal.cc | 19 +++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/buffer_utils.hh b/src/buffer_utils.hh index 8e7fe5dc..543e2357 100644 --- a/src/buffer_utils.hh +++ b/src/buffer_utils.hh @@ -37,6 +37,15 @@ inline bool is_eol(const Buffer& buffer, ByteCoord coord) return buffer.is_end(coord) or buffer[coord.line].length() == coord.column+1; } +inline bool is_bow(const Buffer& buffer, ByteCoord coord) +{ + auto it = utf8::iterator(buffer.iterator_at(coord), buffer); + if (coord == ByteCoord{0,0}) + return is_word(*it); + + return not is_word(*(it-1)) and is_word(*it); +} + inline bool is_eow(const Buffer& buffer, ByteCoord coord) { if (buffer.is_end(coord) or coord == ByteCoord{0,0}) diff --git a/src/normal.cc b/src/normal.cc index 78877f91..0702ca62 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -670,22 +670,13 @@ void use_selection_as_search_pattern(Context& context, NormalParams params) Vector patterns; auto& sels = context.selections(); const auto& buffer = context.buffer(); - using Utf8It = utf8::iterator; for (auto& sel : sels) { - Utf8It begin{buffer.iterator_at(sel.min()), buffer}; - Utf8It end{buffer.iterator_at(sel.max()), buffer}; - ++end; - - auto content = "\\Q" + buffer.string(begin.base().coord(), end.base().coord()) + "\\E"; - if (smart) - { - if (is_word(*begin) and (begin == buffer.begin() or not is_word(*(begin-1)))) - content = "\\b" + content; - if (is_word(*(end-1)) and (end == buffer.end() or not is_word(*end))) - content = content + "\\b"; - } - patterns.push_back(std::move(content)); + const auto beg = sel.min(), end = buffer.char_next(sel.max()); + patterns.push_back(format("{}\\Q{}\\E{}", + smart and is_bow(buffer, beg) ? "\\b" : "", + buffer.string(beg, end), + smart and is_eow(buffer, end) ? "\\b" : "")); } const char reg = to_lower(params.reg ? params.reg : '/');