From b202ad3e024d14e514100b927bc9f371f4467b7b Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Tue, 24 May 2016 16:49:54 +0200 Subject: [PATCH] Support arbitrary newlines for search result excerpts Previously we expected only LF to mark a newline but in theory we can receive CRLF or CR separated text (from files on disk) which would cause incorrect excerpt for matches crossing line boundaries. --- Frameworks/Find/src/FFResultNode.mm | 9 +++++---- Frameworks/Find/src/scan_path.h | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Frameworks/Find/src/FFResultNode.mm b/Frameworks/Find/src/FFResultNode.mm index dda68374..552d9008 100644 --- a/Frameworks/Find/src/FFResultNode.mm +++ b/Frameworks/Find/src/FFResultNode.mm @@ -53,7 +53,7 @@ static void append (ns::attr_string_t& dst, std::string const& src, size_t from, dst.add(src.substr(begin, to-begin)); } -static NSAttributedString* AttributedStringForMatch (std::string const& text, size_t from, size_t to, size_t n) +static NSAttributedString* AttributedStringForMatch (std::string const& text, size_t from, size_t to, size_t n, std::string const& newlines) { ns::attr_string_t str; str.add(ns::style::line_break(NSLineBreakByTruncatingTail)); @@ -66,7 +66,8 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si size_t last = text.size(); for(size_t it = 0; it != last; ) { - size_t eol = std::find(text.begin() + it, text.end(), '\n') - text.begin(); + size_t eol = text.find(newlines, it); + eol = eol != std::string::npos ? eol : last; if(oak::cap(it, from, eol) == from) { @@ -103,7 +104,7 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si str.add(ns::style::unbold); } - if(++eol == to) + if((eol += newlines.size()) == to) inMatch = false; if(eol != last) @@ -270,7 +271,7 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si << text::format("%zu-%zu: Range is not valid UTF-8, please contact: http://macromates.com/support", m.first, m.last); } - _excerpt = AttributedStringForMatch(prefix + middle + suffix, prefix.size(), prefix.size() + middle.size(), m.line_number); + _excerpt = AttributedStringForMatch(prefix + middle + suffix, prefix.size(), prefix.size() + middle.size(), m.line_number, m.newlines); _excerptReplaceString = replacementString; return _excerpt; } diff --git a/Frameworks/Find/src/scan_path.h b/Frameworks/Find/src/scan_path.h index 760b9c3c..9a73ad32 100644 --- a/Frameworks/Find/src/scan_path.h +++ b/Frameworks/Find/src/scan_path.h @@ -25,6 +25,7 @@ namespace find std::string excerpt; size_t excerpt_offset = 0; size_t line_number = 0; + std::string newlines = "\n"; }; struct scan_path_t