1
0
mirror of synced 2024-09-24 03:28:24 +02:00

add regex filter in string view (#345)

* Filter by regex in string view

* Dont recompile the regex for every string, display error message

* localization

* Use data->Buf for pattern creation / searching
The filter string seems to get updated after the callback finished.
Therefore the search string was always 1 character behind the actual
string in the textfield when calling find() / creating the regex.
This commit is contained in:
qdlmcfresh 2021-11-25 08:46:42 +01:00 committed by GitHub
parent 0d1686e170
commit 372908ba9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View File

@ -24,6 +24,8 @@ namespace hex {
private:
bool m_searching = false;
bool m_regex = false;
bool m_pattern_parsed = false;
std::vector<FoundString> m_foundStrings;
std::vector<size_t> m_filterIndices;

View File

@ -284,6 +284,7 @@ namespace hex::plugin::builtin {
{ "hex.view.strings.min_length", "Minimallänge" },
{ "hex.view.strings.filter", "Filter" },
{ "hex.view.strings.extract", "Extrahieren" },
{ "hex.view.strings.regex_error", "Ungültiges Regex" },
{ "hex.view.strings.searching", "Suchen..." },
{ "hex.view.strings.offset", "Offset" },
{ "hex.view.strings.size", "Grösse" },

View File

@ -287,6 +287,7 @@ namespace hex::plugin::builtin {
{ "hex.view.strings.min_length", "Minimum length" },
{ "hex.view.strings.filter", "Filter" },
{ "hex.view.strings.extract", "Extract" },
{ "hex.view.strings.regex_error", "Invalid regex" },
{ "hex.view.strings.searching", "Searching..." },
{ "hex.view.strings.offset", "Offset" },
{ "hex.view.strings.size", "Size" },

View File

@ -4,6 +4,7 @@
#include <cstring>
#include <thread>
#include <regex>
#include <llvm/Demangle/Demangle.h>
#include <imgui_imhex_extensions.h>
@ -99,18 +100,35 @@ namespace hex {
if (ImGui::InputInt("hex.view.strings.min_length"_lang, &this->m_minimumLength, 1, 0))
this->m_foundStrings.clear();
ImGui::Checkbox("Regex", &this->m_regex);
ImGui::InputText("hex.view.strings.filter"_lang, this->m_filter.data(), this->m_filter.capacity(), ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) {
auto &view = *static_cast<ViewStrings*>(data->UserData);
view.m_filter.resize(data->BufTextLen);
view.m_filterIndices.clear();
std::regex pattern;
if (view.m_regex) {
try {
pattern = std::regex(data->Buf);
view.m_pattern_parsed = true;
} catch (std::regex_error &e) {
view.m_pattern_parsed = false;
}
}
for (u64 i = 0; i < view.m_foundStrings.size(); i++) {
if (readString(view.m_foundStrings[i]).find(data->Buf) != std::string::npos)
if(view.m_regex){
if(view.m_pattern_parsed && std::regex_search(readString(view.m_foundStrings[i]), pattern))
view.m_filterIndices.push_back(i);
}
else if(readString(view.m_foundStrings[i]).find(data->Buf) != std::string::npos) {
view.m_filterIndices.push_back(i);
}
}
return 0;
}, this);
if(this->m_regex && !this->m_pattern_parsed){
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "hex.view.strings.regex_error"_lang);
}
if (ImGui::Button("hex.view.strings.extract"_lang))
this->searchStrings();