1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:55:08 +00:00

AK+Format: Exclude prefix from width calculation.

When we write the format specifier '{:#08x}' we are asking for eight
significant digits, zero padding and the prefix '0x'.

However, previously we got only six significant digits because the
prefix counted towards the width. (The number '8' here is the total
width and not the number of significant digits.)

Both fmtlib and printf shared this behaviour. However, I am introducing
a special case here because when we do zero padding we really only care
about the digits and not the width.

Notice that zero padding is a special case anyways, because zero padding
goes after the prefix as opposed to any other padding which goes before
it.
This commit is contained in:
asynts 2020-10-06 13:32:00 +02:00 committed by Andreas Kling
parent aea5fff0d1
commit 7c2cd81edb
5 changed files with 30 additions and 15 deletions

View file

@ -164,7 +164,7 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
output_string_builder.appendff("unsigned char raw_data[{}] = {{\n", (m_selection_end - m_selection_start) + 1);
output_string_builder.append(" ");
for (int i = m_selection_start, j = 1; i <= m_selection_end; i++, j++) {
output_string_builder.appendff("{:#04X}", m_buffer.data()[i]);
output_string_builder.appendff("{:#02X}", m_buffer.data()[i]);
if (i != m_selection_end)
output_string_builder.append(", ");
if ((j % 12) == 0) {
@ -516,7 +516,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
};
bool is_current_line = (m_position / bytes_per_row()) == i;
auto line = String::formatted("{:#010X}", i * bytes_per_row());
auto line = String::formatted("{:#08X}", i * bytes_per_row());
painter.draw_text(
side_offset_rect,
line,