mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +00:00
AK+Format: Use the new format backend in the implementation.
This commit is contained in:
parent
2111fc5f63
commit
01915a3027
3 changed files with 83 additions and 24 deletions
|
@ -209,12 +209,16 @@ void StandardFormatter::parse(StringView specifier)
|
||||||
|
|
||||||
if (lexer.consume_specific('b'))
|
if (lexer.consume_specific('b'))
|
||||||
m_mode = Mode::Binary;
|
m_mode = Mode::Binary;
|
||||||
|
else if (lexer.consume_specific('B'))
|
||||||
|
m_mode = Mode::BinaryUppercase;
|
||||||
else if (lexer.consume_specific('d'))
|
else if (lexer.consume_specific('d'))
|
||||||
m_mode = Mode::Decimal;
|
m_mode = Mode::Decimal;
|
||||||
else if (lexer.consume_specific('o'))
|
else if (lexer.consume_specific('o'))
|
||||||
m_mode = Mode::Octal;
|
m_mode = Mode::Octal;
|
||||||
else if (lexer.consume_specific('x'))
|
else if (lexer.consume_specific('x'))
|
||||||
m_mode = Mode::Hexadecimal;
|
m_mode = Mode::Hexadecimal;
|
||||||
|
else if (lexer.consume_specific('X'))
|
||||||
|
m_mode = Mode::HexadecimalUppercase;
|
||||||
else if (lexer.consume_specific('c'))
|
else if (lexer.consume_specific('c'))
|
||||||
m_mode = Mode::Character;
|
m_mode = Mode::Character;
|
||||||
else if (lexer.consume_specific('s'))
|
else if (lexer.consume_specific('s'))
|
||||||
|
@ -251,28 +255,30 @@ void Formatter<StringView>::format(StringBuilder& builder, StringView value, Spa
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(StringBuilder& builder, T value, Span<const TypeErasedParameter> parameters)
|
void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(StringBuilder& builder, T value, Span<const TypeErasedParameter> parameters)
|
||||||
{
|
{
|
||||||
if (m_align != Align::Default)
|
|
||||||
TODO();
|
|
||||||
if (m_sign != Sign::Default)
|
|
||||||
TODO();
|
|
||||||
if (m_alternative_form)
|
|
||||||
TODO();
|
|
||||||
if (m_precision != value_not_set)
|
if (m_precision != value_not_set)
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
if (m_mode == Mode::Character)
|
if (m_mode == Mode::Character)
|
||||||
TODO();
|
TODO();
|
||||||
|
|
||||||
int base;
|
u8 base;
|
||||||
if (m_mode == Mode::Binary)
|
bool upper_case = false;
|
||||||
TODO();
|
if (m_mode == Mode::Binary) {
|
||||||
else if (m_mode == Mode::Octal)
|
base = 2;
|
||||||
TODO();
|
} else if (m_mode == Mode::BinaryUppercase) {
|
||||||
else if (m_mode == Mode::Decimal || m_mode == Mode::Default)
|
base = 2;
|
||||||
|
upper_case = true;
|
||||||
|
} else if (m_mode == Mode::Octal) {
|
||||||
|
base = 8;
|
||||||
|
} else if (m_mode == Mode::Decimal || m_mode == Mode::Default) {
|
||||||
base = 10;
|
base = 10;
|
||||||
else if (m_mode == Mode::Hexadecimal)
|
} else if (m_mode == Mode::Hexadecimal) {
|
||||||
base = 16;
|
base = 16;
|
||||||
else
|
} else if (m_mode == Mode::HexadecimalUppercase) {
|
||||||
|
base = 16;
|
||||||
|
upper_case = true;
|
||||||
|
} else {
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
size_t width = m_width;
|
size_t width = m_width;
|
||||||
if (m_width >= value_from_arg) {
|
if (m_width >= value_from_arg) {
|
||||||
|
@ -283,16 +289,34 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(StringB
|
||||||
width = *reinterpret_cast<const size_t*>(parameter.value);
|
width = *reinterpret_cast<const size_t*>(parameter.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: We really need one canonical print implementation that just takes a base.
|
PrintfImplementation::Align align;
|
||||||
(void)base;
|
if (m_align == Align::Left)
|
||||||
|
align = PrintfImplementation::Align::Left;
|
||||||
char* bufptr;
|
else if (m_align == Align::Right)
|
||||||
if (m_mode == Mode::Hexadecimal)
|
align = PrintfImplementation::Align::Right;
|
||||||
PrintfImplementation::print_hex([&](auto, char ch) { builder.append(ch); }, bufptr, value, false, false, false, m_zero_pad, width);
|
else if (m_align == Align::Center)
|
||||||
else if (IsSame<typename MakeUnsigned<T>::Type, T>::value)
|
align = PrintfImplementation::Align::Center;
|
||||||
PrintfImplementation::print_u64([&](auto, char ch) { builder.append(ch); }, bufptr, value, false, m_zero_pad, width);
|
else if (m_align == Align::Default)
|
||||||
|
align = PrintfImplementation::Align::Right;
|
||||||
else
|
else
|
||||||
PrintfImplementation::print_i64([&](auto, char ch) { builder.append(ch); }, bufptr, value, false, m_zero_pad, width);
|
ASSERT_NOT_REACHED();
|
||||||
|
|
||||||
|
PrintfImplementation::SignMode sign_mode;
|
||||||
|
if (m_sign == Sign::Default)
|
||||||
|
sign_mode = PrintfImplementation::SignMode::OnlyIfNeeded;
|
||||||
|
else if (m_sign == Sign::NegativeOnly)
|
||||||
|
sign_mode = PrintfImplementation::SignMode::OnlyIfNeeded;
|
||||||
|
else if (m_sign == Sign::PositiveAndNegative)
|
||||||
|
sign_mode = PrintfImplementation::SignMode::Always;
|
||||||
|
else if (m_sign == Sign::ReserveSpace)
|
||||||
|
sign_mode = PrintfImplementation::SignMode::Reserved;
|
||||||
|
else
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
|
||||||
|
if (IsSame<typename MakeUnsigned<T>::Type, T>::value)
|
||||||
|
PrintfImplementation::convert_unsigned_to_string(value, builder, base, m_alternative_form, upper_case, m_zero_pad, align, width, m_fill, sign_mode);
|
||||||
|
else
|
||||||
|
PrintfImplementation::convert_signed_to_string(value, builder, base, m_alternative_form, upper_case, m_zero_pad, align, width, m_fill, sign_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
template struct Formatter<unsigned char, void>;
|
template struct Formatter<unsigned char, void>;
|
||||||
|
|
|
@ -81,9 +81,11 @@ struct StandardFormatter {
|
||||||
enum class Mode {
|
enum class Mode {
|
||||||
Default,
|
Default,
|
||||||
Binary,
|
Binary,
|
||||||
|
BinaryUppercase,
|
||||||
Decimal,
|
Decimal,
|
||||||
Octal,
|
Octal,
|
||||||
Hexadecimal,
|
Hexadecimal,
|
||||||
|
HexadecimalUppercase,
|
||||||
Character,
|
Character,
|
||||||
String,
|
String,
|
||||||
Pointer,
|
Pointer,
|
||||||
|
|
|
@ -40,11 +40,12 @@ TEST_CASE(format_integers)
|
||||||
EXPECT_EQ(String::formatted("{}", 42u), "42");
|
EXPECT_EQ(String::formatted("{}", 42u), "42");
|
||||||
EXPECT_EQ(String::formatted("{:4}", 42u), " 42");
|
EXPECT_EQ(String::formatted("{:4}", 42u), " 42");
|
||||||
EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
|
EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
|
||||||
// EXPECT_EQ(String::formatted("{:7}", -17), " -17");
|
EXPECT_EQ(String::formatted("{:7}", -17), " -17");
|
||||||
EXPECT_EQ(String::formatted("{}", -17), "-17");
|
EXPECT_EQ(String::formatted("{}", -17), "-17");
|
||||||
EXPECT_EQ(String::formatted("{:04}", 13), "0013");
|
EXPECT_EQ(String::formatted("{:04}", 13), "0013");
|
||||||
EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
|
EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
|
||||||
EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
|
EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
|
||||||
|
EXPECT_EQ(String::formatted("{:4}", 12345678), "12345678");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE(reorder_format_arguments)
|
TEST_CASE(reorder_format_arguments)
|
||||||
|
@ -80,4 +81,36 @@ TEST_CASE(format_without_arguments)
|
||||||
EXPECT_EQ(String::formatted("foo"), "foo");
|
EXPECT_EQ(String::formatted("foo"), "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(format_upper_case_integer)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(String::formatted("{:4X}", 0xff), " FF");
|
||||||
|
EXPECT_EQ(String::formatted("{:#4X}", 0xff), "0XFF");
|
||||||
|
|
||||||
|
EXPECT_EQ(String::formatted("{:b}", 0xff), "11111111");
|
||||||
|
EXPECT_EQ(String::formatted("{:B}", 0xff), "11111111");
|
||||||
|
EXPECT_EQ(String::formatted("{:#b}", 0xff), "0b11111111");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(format_aligned)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(String::formatted("{:*<8}", 13), "13******");
|
||||||
|
EXPECT_EQ(String::formatted("{:*^8}", 13), "***13***");
|
||||||
|
EXPECT_EQ(String::formatted("{:*>8}", 13), "******13");
|
||||||
|
EXPECT_EQ(String::formatted("{:*>+8}", 13), "*****+13");
|
||||||
|
EXPECT_EQ(String::formatted("{:*^ 8}", 13), "** 13***");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(format_octal)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(String::formatted("{:o}", 0744), "744");
|
||||||
|
EXPECT_EQ(String::formatted("{:#o}", 0744), "0744");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(zero_pad)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(String::formatted("{: <010}", 42), "42 ");
|
||||||
|
EXPECT_EQ(String::formatted("{:010}", 42), "0000000042");
|
||||||
|
EXPECT_EQ(String::formatted("{:/^010}", 42), "////42////");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_MAIN(Format)
|
TEST_MAIN(Format)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue