From e86ca29b8c538ee750b1a85338b283183faf4c24 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 24 Aug 2022 00:31:34 +0200 Subject: [PATCH] sys: Improved searching behaviour --- .../source/content/views/view_hex_editor.cpp | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 4fdd6f220..afcbfaa0c 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -143,6 +143,17 @@ namespace hex::plugin::builtin { class PopupFind : public ViewHexEditor::Popup { public: + PopupFind() { + EventManager::subscribe(this, [this](Region region) { + this->m_searchPosition = this->m_nextSearchPosition.value_or(region.getStartAddress()); + this->m_nextSearchPosition.reset(); + }); + } + + ~PopupFind() override { + EventManager::unsubscribe(this); + } + void draw(ViewHexEditor *editor) override { std::vector searchSequence; @@ -221,6 +232,7 @@ namespace hex::plugin::builtin { this->m_shouldSearch = true; this->m_backwards = false; this->m_searchPosition.reset(); + this->m_nextSearchPosition.reset(); } ImGui::BeginDisabled(!this->m_searchPosition.has_value()); @@ -244,10 +256,7 @@ namespace hex::plugin::builtin { std::optional findSequence(ViewHexEditor *editor, const std::vector &sequence, bool backwards) { hex::prv::BufferedReader reader(ImHexApi::Provider::get()); - if (!editor->isSelectionValid()) - reader.seek(this->m_searchPosition.value_or(0x00)); - else - reader.seek(this->m_searchPosition.value_or(editor->getSelection().getStartAddress())); + reader.seek(this->m_searchPosition.value_or(0x00)); constexpr static auto searchFunction = [](const auto &haystackBegin, const auto &haystackEnd, const auto &needleBegin, const auto &needleEnd) { return std::search(haystackBegin, haystackEnd, std::boyer_moore_horspool_searcher(needleBegin, needleEnd)); @@ -256,16 +265,16 @@ namespace hex::plugin::builtin { if (!backwards) { auto occurrence = searchFunction(reader.begin(), reader.end(), sequence.begin(), sequence.end()); if (occurrence != reader.end()) { - this->m_searchPosition = occurrence.getAddress() + sequence.size(); + this->m_nextSearchPosition = occurrence.getAddress() + sequence.size(); return Region { occurrence.getAddress(), sequence.size() }; } } else { auto occurrence = searchFunction(reader.rbegin(), reader.rend(), sequence.begin(), sequence.end()); if (occurrence != reader.rend()) { if (occurrence.getAddress() < sequence.size()) - this->m_searchPosition = 0x00; + this->m_nextSearchPosition = 0x00; else - this->m_searchPosition = occurrence.getAddress() - sequence.size(); + this->m_nextSearchPosition = occurrence.getAddress() - sequence.size(); return Region { occurrence.getAddress(), sequence.size() }; } @@ -275,7 +284,7 @@ namespace hex::plugin::builtin { } std::string m_input; - std::optional m_searchPosition; + std::optional m_searchPosition, m_nextSearchPosition; bool m_requestFocus = true; std::atomic m_shouldSearch = false; @@ -1345,6 +1354,12 @@ namespace hex::plugin::builtin { }); EventManager::subscribe(this, [this](Region region) { + if (region == Region::Invalid()) { + this->m_selectionStart = InvalidSelection; + this->m_selectionEnd = InvalidSelection; + return; + } + auto provider = ImHexApi::Provider::get(); auto page = provider->getPageOfAddress(region.getStartAddress());