From 06ddfcde89fc649968bf3867aff628e5dcb8dba7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 17 Jul 2021 18:04:59 -0400 Subject: [PATCH] AK: Allow setting both width and precision when formatting a string --- AK/Format.cpp | 2 -- Tests/AK/TestFormat.cpp | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/AK/Format.cpp b/AK/Format.cpp index 6aaca3fcf4f7d2..e7081abb9b1508 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -550,8 +550,6 @@ void Formatter::format(FormatBuilder& builder, StringView value) VERIFY_NOT_REACHED(); if (m_mode != Mode::Default && m_mode != Mode::String && m_mode != Mode::Character && m_mode != Mode::HexDump) VERIFY_NOT_REACHED(); - if (m_width.has_value() && m_precision.has_value()) - VERIFY_NOT_REACHED(); m_width = m_width.value_or(0); m_precision = m_precision.value_or(NumericLimits::max()); diff --git a/Tests/AK/TestFormat.cpp b/Tests/AK/TestFormat.cpp index 17814d79ac97ca..3c6117987919b9 100644 --- a/Tests/AK/TestFormat.cpp +++ b/Tests/AK/TestFormat.cpp @@ -125,6 +125,9 @@ TEST_CASE(complex_string_specifiers) EXPECT_EQ(String::formatted("{:9}", "abcd"), "abcd "); EXPECT_EQ(String::formatted("{:>9}", "abcd"), " abcd"); EXPECT_EQ(String::formatted("{:^9}", "abcd"), " abcd "); + EXPECT_EQ(String::formatted("{:4.6}", "a"), "a "); + EXPECT_EQ(String::formatted("{:4.6}", "abcdef"), "abcdef"); + EXPECT_EQ(String::formatted("{:4.6}", "abcdefghi"), "abcdef"); } TEST_CASE(cast_integer_to_character)