mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add formatter for boolean values.
This commit is contained in:
parent
7e9cd8a860
commit
ebafc5b4d2
Notes:
sideshowbarker
2024-07-19 02:06:25 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/ebafc5b4d23 Pull-request: https://github.com/SerenityOS/serenity/pull/3648 Reviewed-by: https://github.com/awesomekling
3 changed files with 39 additions and 0 deletions
|
@ -460,6 +460,17 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(StringB
|
|||
PrintfImplementation::convert_signed_to_string(value, builder, base, m_alternative_form, upper_case, m_zero_pad, align, width, m_fill, sign_mode);
|
||||
}
|
||||
|
||||
void Formatter<bool>::format(StringBuilder& builder, bool value, FormatterContext& context)
|
||||
{
|
||||
if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) {
|
||||
Formatter<u8> formatter { *this };
|
||||
return formatter.format(builder, static_cast<u8>(value), context);
|
||||
} else {
|
||||
Formatter<StringView> formatter { *this };
|
||||
formatter.format(builder, value ? "true" : "false", context);
|
||||
}
|
||||
}
|
||||
|
||||
template struct Formatter<unsigned char, void>;
|
||||
template struct Formatter<unsigned short, void>;
|
||||
template struct Formatter<unsigned int, void>;
|
||||
|
|
17
AK/Format.h
17
AK/Format.h
|
@ -166,6 +166,12 @@ struct StandardFormatter {
|
|||
|
||||
template<>
|
||||
struct Formatter<StringView> : StandardFormatter {
|
||||
Formatter() { }
|
||||
explicit Formatter(StandardFormatter formatter)
|
||||
: StandardFormatter(formatter)
|
||||
{
|
||||
}
|
||||
|
||||
void format(StringBuilder& builder, StringView value, FormatterContext&);
|
||||
};
|
||||
template<>
|
||||
|
@ -183,9 +189,20 @@ struct Formatter<String> : Formatter<StringView> {
|
|||
|
||||
template<typename T>
|
||||
struct Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type> : StandardFormatter {
|
||||
Formatter() { }
|
||||
explicit Formatter(StandardFormatter formatter)
|
||||
: StandardFormatter(formatter)
|
||||
{
|
||||
}
|
||||
|
||||
void format(StringBuilder&, T value, FormatterContext&);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<bool> : StandardFormatter {
|
||||
void format(StringBuilder&, bool value, FormatterContext&);
|
||||
};
|
||||
|
||||
template<typename... Parameters>
|
||||
Array<TypeErasedParameter, sizeof...(Parameters)> make_type_erased_parameters(const Parameters&... parameters)
|
||||
{
|
||||
|
|
|
@ -136,4 +136,15 @@ TEST_CASE(cast_integer_to_character)
|
|||
EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
|
||||
}
|
||||
|
||||
TEST_CASE(boolean_values)
|
||||
{
|
||||
EXPECT_EQ(String::formatted("{}", true), "true");
|
||||
EXPECT_EQ(String::formatted("{}", false), "false");
|
||||
EXPECT_EQ(String::formatted("{:6}", true), "true ");
|
||||
EXPECT_EQ(String::formatted("{:>4}", false), "false");
|
||||
EXPECT_EQ(String::formatted("{:d}", false), "0");
|
||||
EXPECT_EQ(String::formatted("{:d}", true), "1");
|
||||
EXPECT_EQ(String::formatted("{:#08x}", true), "0x000001");
|
||||
}
|
||||
|
||||
TEST_MAIN(Format)
|
||||
|
|
Loading…
Reference in a new issue