summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorMetallicSquid <gcmacneil@icloud.com>2022-04-26 14:14:05 +0100
committerSam Atkins <atkinssj@gmail.com>2023-02-15 12:13:31 +0000
commiteb27b397b82d7cd4fd901a0af7685fbfb0bc2152 (patch)
tree962d98b5be24cfbed8940dc4168e07d5e97176e1 /Userland/Applications
parent815442b2b5edb441dac0d4478990d89fb66e2992 (diff)
downloadserenity-eb27b397b82d7cd4fd901a0af7685fbfb0bc2152.zip
HexEditor: Add selection strings to the value inspector
Strings include ASCII, UTF-8, and UTF-16 Co-authored-by: Andreas Krohn <hamburger1984@gmail.com>
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/HexEditor/CMakeLists.txt2
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp12
-rw-r--r--Userland/Applications/HexEditor/HexEditor.h1
-rw-r--r--Userland/Applications/HexEditor/HexEditorWidget.cpp17
-rw-r--r--Userland/Applications/HexEditor/ValueInspectorModel.h9
5 files changed, 40 insertions, 1 deletions
diff --git a/Userland/Applications/HexEditor/CMakeLists.txt b/Userland/Applications/HexEditor/CMakeLists.txt
index ca3a3cfa0a..652d75ab58 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 18922eeb76..86b61facd5 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -245,6 +245,18 @@ Optional<u8> 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 89eeeb3b5b..37797b3cf6 100644
--- a/Userland/Applications/HexEditor/HexEditor.h
+++ b/Userland/Applications/HexEditor/HexEditor.h
@@ -38,6 +38,7 @@ public:
void open_file(NonnullOwnPtr<Core::File> file);
ErrorOr<void> fill_selection(u8 fill_byte);
Optional<u8> get_byte(size_t position);
+ ByteBuffer get_selected_bytes();
ErrorOr<void> save_as(NonnullOwnPtr<Core::File>);
ErrorOr<void> save();
diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp
index 104f8d861d..5feae61de1 100644
--- a/Userland/Applications/HexEditor/HexEditorWidget.cpp
+++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp
@@ -33,6 +33,7 @@
#include <LibGUI/TextBox.h>
#include <LibGUI/Toolbar.h>
#include <LibGUI/ToolbarContainer.h>
+#include <LibTextCodec/Decoder.h>
#include <string.h>
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 2778564988..27f22f853b 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 "";
}