From aa68dcefa0aa4818b2d01d69e33db4906017ebfc Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Sun, 4 Nov 2018 14:54:06 +0100 Subject: [PATCH] Findadv: 'Optimized' detection of matched string This is clearly a hack, because I don't understand why the previous code did not work. --- src/lyxfind.cpp | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index e86d296d71..4bc5295f25 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -798,7 +798,7 @@ static docstring buffer_to_latex(Buffer & buffer) otexstream os(ods); runparams.nice = true; runparams.flavor = OutputParams::LATEX; - runparams.linelen = 80; //lyxrc.plaintext_linelen; + runparams.linelen = 100000; //lyxrc.plaintext_linelen; // No side effect of file copying and image conversion runparams.dryrun = true; runparams.for_search = true; @@ -1718,9 +1718,9 @@ void LatexInfo::removeHead(KeyInfo &actual, int count) int LatexInfo::dispatch(ostringstream &os, int previousStart, KeyInfo &actual) { - int nextKeyIdx; + int nextKeyIdx = 0; switch (actual.keytype) - { + { case KeyInfo::isChar: { nextKeyIdx = getNextKey(); break; @@ -1847,7 +1847,7 @@ int LatexInfo::dispatch(ostringstream &os, int previousStart, KeyInfo &actual) nextKeyIdx = getNextKey(); break; } - } + } return(nextKeyIdx); } @@ -2458,10 +2458,33 @@ int findAdvFinalize(DocIterator & cur, MatchStringAdv const & match) if (old_len < 0) old_len = 0; int new_len; // Greedy behaviour while matching regexps - while ((new_len = match(cur, len + 1)) > old_len) { - ++len; - old_len = new_len; - LYXERR(Debug::FIND, "verifying match with len = " << len); + bool examining = true; + int lastvalidlen = len; + while (examining) { + examining = false; + // Kornel: The loop is needed, since it looks like + // incrementing 'cur.pos()' does not always lead to the following + // char which we could then match. + for (int count = 1; count < 5; ++count) { + if (cur.pos() + len + count > cur.lastpos()) { + break; + } + new_len = match(cur, len + count); + if (new_len > old_len) { + len += count; + old_len = new_len; + examining = true; + break; + } + else if (new_len == old_len) + lastvalidlen = len+count; + } + } + if (lastvalidlen == len + 1) { + // Kornel: Don't know, why this is important + // All I can see, is that sometimes the last char + // is outside of a match although it should be inside + len++; } return len; } -- 2.39.2