From eb27b397b82d7cd4fd901a0af7685fbfb0bc2152 Mon Sep 17 00:00:00 2001 From: MetallicSquid Date: Tue, 26 Apr 2022 14:14:05 +0100 Subject: [PATCH] HexEditor: Add selection strings to the value inspector Strings include ASCII, UTF-8, and UTF-16 Co-authored-by: Andreas Krohn --- Userland/Applications/HexEditor/CMakeLists.txt | 2 +- Userland/Applications/HexEditor/HexEditor.cpp | 12 ++++++++++++ Userland/Applications/HexEditor/HexEditor.h | 1 + .../Applications/HexEditor/HexEditorWidget.cpp | 17 +++++++++++++++++ .../HexEditor/ValueInspectorModel.h | 9 +++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Userland/Applications/HexEditor/CMakeLists.txt b/Userland/Applications/HexEditor/CMakeLists.txt index ca3a3cfa0ab..652d75ab585 100644 --- a/Userland/Applications/HexEditor/CMakeLists.txt +++ b/Userland/Applications/HexEditor/CMakeLists.txt @@ -24,4 +24,4 @@ set(GENERATED_SOURCES ) serenity_app(HexEditor ICON app-hex-editor) -target_link_libraries(HexEditor PRIVATE LibCore LibGfx LibGUI LibConfig LibDesktop LibFileSystemAccessClient LibMain) +target_link_libraries(HexEditor PRIVATE LibCore LibGfx LibGUI LibConfig LibDesktop LibFileSystemAccessClient LibMain LibTextCodec) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 18922eeb76e..86b61facd5a 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -245,6 +245,18 @@ Optional HexEditor::get_byte(size_t position) return {}; } +ByteBuffer HexEditor::get_selected_bytes() +{ + auto num_selected_bytes = m_selection_end - m_selection_start; + ByteBuffer data; + data.ensure_capacity(num_selected_bytes); + + for (size_t i = m_selection_start; i < m_selection_end; i++) + data.append(m_document->get(i).value); + + return data; +} + void HexEditor::mousedown_event(GUI::MouseEvent& event) { if (event.button() != GUI::MouseButton::Primary) { diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 89eeeb3b5b5..37797b3cf69 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -38,6 +38,7 @@ public: void open_file(NonnullOwnPtr file); ErrorOr fill_selection(u8 fill_byte); Optional get_byte(size_t position); + ByteBuffer get_selected_bytes(); ErrorOr save_as(NonnullOwnPtr); ErrorOr save(); diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index 104f8d861d6..5feae61de1b 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include REGISTER_WIDGET(HexEditor, HexEditor); @@ -376,8 +377,24 @@ void HexEditorWidget::update_inspector_values(size_t position) value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UTF16, ""); } + auto selected_bytes = m_editor->get_selected_bytes(); + + auto ascii_string = DeprecatedString { ReadonlyBytes { selected_bytes } }; + value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::ASCIIString, ascii_string); + + Utf8View utf8_string_view { ReadonlyBytes { selected_bytes } }; + utf8_string_view.validate(valid_bytes); + if (valid_bytes == 0) + value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UTF8String, ""); + else + // FIXME: replace control chars with something else - we don't want line breaks here ;) + value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UTF8String, utf8_string_view.as_string()); + // FIXME: Parse as other values like Timestamp etc + DeprecatedString utf16_string = TextCodec::decoder_for("utf-16le"sv)->to_utf8(StringView(selected_bytes.span())); + value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UTF16String, utf16_string); + m_value_inspector->set_model(value_inspector_model); m_value_inspector->update(); } diff --git a/Userland/Applications/HexEditor/ValueInspectorModel.h b/Userland/Applications/HexEditor/ValueInspectorModel.h index 27785649880..27f22f853b5 100644 --- a/Userland/Applications/HexEditor/ValueInspectorModel.h +++ b/Userland/Applications/HexEditor/ValueInspectorModel.h @@ -30,6 +30,9 @@ public: ASCII, UTF8, UTF16, + ASCIIString, + UTF8String, + UTF16String, __Count }; @@ -100,6 +103,12 @@ public: return "UTF-8"; case UTF16: return "UTF-16"; + case ASCIIString: + return "ASCII String"; + case UTF8String: + return "UTF-8 String"; + case UTF16String: + return "UTF-16 String"; default: return ""; }