HexEditor: Add selection strings to the value inspector

Strings include ASCII, UTF-8, and UTF-16

Co-authored-by: Andreas Krohn <hamburger1984@gmail.com>
This commit is contained in:
MetallicSquid 2022-04-26 14:14:05 +01:00 committed by Sam Atkins
parent 815442b2b5
commit eb27b397b8
Notes: sideshowbarker 2024-07-17 00:24:20 +09:00
5 changed files with 40 additions and 1 deletions

View file

@ -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)

View file

@ -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) {

View file

@ -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();

View file

@ -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();
}

View file

@ -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 "";
}