From 7c5d585c84fbd6590e2723b2e1cf5ae60ecc6213 Mon Sep 17 00:00:00 2001 From: Stefan Schimanski Date: Mon, 25 Feb 2008 15:09:45 +0000 Subject: [PATCH] * binary search can be tricky. I hope I got it right now. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23230 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/GuiCompleter.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/frontends/qt4/GuiCompleter.cpp b/src/frontends/qt4/GuiCompleter.cpp index f594ea376a..a6049ba818 100644 --- a/src/frontends/qt4/GuiCompleter.cpp +++ b/src/frontends/qt4/GuiCompleter.cpp @@ -620,8 +620,7 @@ void GuiCompleter::setCurrentCompletion(QString const & s) // In sorted models, do binary search for s. i = 0; size_t r = n - 1; - int c; - do { + while (r >= i && i < n) { size_t mid = (r + i) / 2; QString const & mids = model.data(model.index(mid, 0), @@ -630,22 +629,23 @@ void GuiCompleter::setCurrentCompletion(QString const & s) // left or right? // FIXME: is this really the same order that the docstring // from the CompletionList has? - c = s.compare(mids, Qt::CaseSensitive); + int c = s.compare(mids, Qt::CaseSensitive); if (c == 0) { i = mid; break; + } else if (i == r) { + i = n; + break; } else if (c > 0) // middle is not far enough i = mid + 1; else // middle is too far - r = mid; - - } while (r - i > 0 && i < n); + r = mid - 1; + } - // loop was left with failed comparison? - // i.e. word was not found. - if (c != 0) + // loop was left without finding anything + if (r < i) i = n; } -- 2.39.5