From 12a32f145c72a69ef48044c89154897b6a4e34a8 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Tue, 16 Jul 2013 12:36:14 +0300 Subject: [PATCH] BinEditor: Support wrapping on find Change-Id: I7a87908d084208ac1d975d0686905cd3412e120f Reviewed-by: hjk --- src/plugins/bineditor/bineditor.cpp | 2 ++ src/plugins/bineditor/bineditorplugin.cpp | 30 ++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/plugins/bineditor/bineditor.cpp b/src/plugins/bineditor/bineditor.cpp index d2eefbb2222..95ccf225708 100644 --- a/src/plugins/bineditor/bineditor.cpp +++ b/src/plugins/bineditor/bineditor.cpp @@ -582,6 +582,8 @@ int BinEditorWidget::dataLastIndexOf(const QByteArray &pattern, int from, bool c buffer.resize(m_blockSize + trailing); char *b = buffer.data(); + if (from == -1) + from = m_size; int block = from / m_blockSize; const int lowerBound = qMax(0, from - SearchStride); while (from > lowerBound) { diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp index f8f4ab5b7e3..dbd43cde041 100644 --- a/src/plugins/bineditor/bineditorplugin.cpp +++ b/src/plugins/bineditor/bineditorplugin.cpp @@ -73,6 +73,7 @@ public: { m_widget = widget; m_incrementalStartPos = m_contPos = -1; + m_incrementalWrappedState = false; } bool supportsReplace() const { return false; } @@ -87,6 +88,7 @@ public: void resetIncrementalSearch() { m_incrementalStartPos = m_contPos = -1; + m_incrementalWrappedState = false; } virtual void highlightAll(const QString &txt, Find::FindFlags findFlags) @@ -99,14 +101,25 @@ public: m_widget->highlightSearchResults(QByteArray()); } - int find(const QByteArray &pattern, int pos, Find::FindFlags findFlags) + int find(const QByteArray &pattern, int pos, Find::FindFlags findFlags, bool *wrapped) { + if (wrapped) + *wrapped = false; if (pattern.isEmpty()) { m_widget->setCursorPosition(pos); return pos; } - return m_widget->find(pattern, pos, Find::textDocumentFlagsForFindFlags(findFlags)); + int res = m_widget->find(pattern, pos, Find::textDocumentFlagsForFindFlags(findFlags)); + if (res < 0) { + pos = (findFlags & Find::FindBackward) ? -1 : 0; + res = m_widget->find(pattern, pos, Find::textDocumentFlagsForFindFlags(findFlags)); + if (res < 0) + return res; + if (wrapped) + *wrapped = true; + } + return res; } Result findIncremental(const QString &txt, Find::FindFlags findFlags) { @@ -118,7 +131,12 @@ public: m_incrementalStartPos = m_widget->selectionStart(); if (m_contPos == -1) m_contPos = m_incrementalStartPos; - int found = find(pattern, m_contPos, findFlags); + bool wrapped; + int found = find(pattern, m_contPos, findFlags, &wrapped); + if (wrapped != m_incrementalWrappedState && (found >= 0)) { + m_incrementalWrappedState = wrapped; + showWrapIndicator(m_widget); + } Result result; if (found >= 0) { result = Found; @@ -147,7 +165,10 @@ public: if (findFlags & Find::FindBackward) m_contPos = m_widget->selectionStart()-1; } - int found = find(pattern, m_contPos, findFlags); + bool wrapped; + int found = find(pattern, m_contPos, findFlags, &wrapped); + if (wrapped) + showWrapIndicator(m_widget); Result result; if (found >= 0) { result = Found; @@ -171,6 +192,7 @@ private: BinEditorWidget *m_widget; int m_incrementalStartPos; int m_contPos; // Only valid if last result was NotYetFound. + bool m_incrementalWrappedState; QByteArray m_lastPattern; };