summaryrefslogtreecommitdiff
path: root/Userland/Applications/HexEditor/HexEditor.cpp
diff options
context:
space:
mode:
authorCamisul <1397270-camisul@users.noreply.gitlab.com>2021-01-22 23:22:00 +0300
committerAndreas Kling <kling@serenityos.org>2021-01-24 19:23:31 +0100
commit0678dab7dca0bba07e9370140637cbac64625a42 (patch)
tree6898bc4da689a10dc04d7c23b13139b0925ac4d3 /Userland/Applications/HexEditor/HexEditor.cpp
parent509e39ac00fb512fcd192e73b7d69564268d87a7 (diff)
downloadserenity-0678dab7dca0bba07e9370140637cbac64625a42.zip
HexEditor: Find
Added search submenu with options to find or find again. Find allows to search for ASII string or sequence of Hex value.
Diffstat (limited to 'Userland/Applications/HexEditor/HexEditor.cpp')
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp
index edd98b8c56..fbb7ae0146 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -582,3 +582,28 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
}
}
}
+
+int HexEditor::find_and_highlight(ByteBuffer& needle, int start)
+{
+ if (m_buffer.is_empty())
+ return -1;
+
+ if (needle.is_null()) {
+ dbgln("needle is null");
+ return -1;
+ }
+
+ auto raw_offset = memmem(m_buffer.data() + start, m_buffer.size(), needle.data(), needle.size());
+ if (raw_offset == NULL)
+ return -1;
+
+ int relative_offset = static_cast<const u8*>(raw_offset) - m_buffer.data();
+ dbgln("find_and_highlight: start={} raw_offset={} relative_offset={}", start, raw_offset, relative_offset);
+
+ auto end_of_match = relative_offset + needle;
+ set_position(relative_offset);
+ m_selection_start = relative_offset;
+ m_selection_end = end_of_match;
+
+ return end_of_match;
+}