From 27adbd179207d9f475f4aa3c70091597c78d1797 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sun, 31 Dec 2023 16:42:41 +0000 Subject: [PATCH] HexEditor: Stop allocating a String for every character printed When we have a single char, we can directly construct a StringView for it instead, which doesn't require a String allocation. --- Userland/Applications/HexEditor/HexEditor.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index c8be061371..1f4722e100 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -724,15 +724,16 @@ void HexEditor::paint_event(GUI::PaintEvent& event) }; painter.fill_rect(text_background_rect, background_color_text); - auto character = String::formatted("{:c}", isprint(cell_value) ? cell_value : '.').release_value_but_fixme_should_propagate_errors(); if (m_edit_mode == EditMode::Text) draw_cursor_rect(); + char const character = is_ascii_printable(cell_value) ? cell_value : '.'; + auto character_sv = StringView { &character, 1 }; if (byte_position == m_position) - painter.draw_text(text_display_rect, character, Gfx::TextAlignment::TopLeft, palette().selection_text()); + painter.draw_text(text_display_rect, character_sv, Gfx::TextAlignment::TopLeft, palette().selection_text()); else - painter.draw_text(text_display_rect, character, Gfx::TextAlignment::TopLeft, text_color_text); + painter.draw_text(text_display_rect, character_sv, Gfx::TextAlignment::TopLeft, text_color_text); } } }