mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
AK: Add a basic formatter for wchar_t
This commit is contained in:
parent
4ef3ed4ba3
commit
67a579aab0
Notes:
sideshowbarker
2024-07-18 03:09:55 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/67a579aab01 Pull-request: https://github.com/SerenityOS/serenity/pull/10170 Reviewed-by: https://github.com/BertalanD ✅ Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/kleinesfilmroellchen
@ -688,6 +688,19 @@ void Formatter<char>::format(FormatBuilder& builder, char value)
|
||||
return formatter.format(builder, { &value, 1 });
|
||||
}
|
||||
}
|
||||
void Formatter<wchar_t>::format(FormatBuilder& builder, wchar_t value)
|
||||
{
|
||||
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<u32> formatter { *this };
|
||||
return formatter.format(builder, static_cast<u32>(value));
|
||||
} else {
|
||||
StringBuilder codepoint;
|
||||
codepoint.append_code_point(value);
|
||||
|
||||
Formatter<StringView> formatter { *this };
|
||||
return formatter.format(builder, codepoint.to_string());
|
||||
}
|
||||
}
|
||||
void Formatter<bool>::format(FormatBuilder& builder, bool value)
|
||||
{
|
||||
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) {
|
||||
|
@ -423,6 +423,10 @@ struct Formatter<char> : StandardFormatter {
|
||||
void format(FormatBuilder&, char value);
|
||||
};
|
||||
template<>
|
||||
struct Formatter<wchar_t> : StandardFormatter {
|
||||
void format(FormatBuilder& builder, wchar_t value);
|
||||
};
|
||||
template<>
|
||||
struct Formatter<bool> : StandardFormatter {
|
||||
void format(FormatBuilder&, bool value);
|
||||
};
|
||||
|
@ -323,3 +323,17 @@ TEST_CASE(vector_format)
|
||||
EXPECT_EQ(String::formatted("{}", v), "[ [ 1, 2 ], [ 3, 4 ] ]");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(format_wchar)
|
||||
{
|
||||
EXPECT_EQ(String::formatted("{}", L'a'), "a");
|
||||
EXPECT_EQ(String::formatted("{}", L'\U0001F41E'), "\xF0\x9F\x90\x9E");
|
||||
EXPECT_EQ(String::formatted("{:x}", L'a'), "61");
|
||||
EXPECT_EQ(String::formatted("{:x}", L'\U0001F41E'), "1f41e");
|
||||
EXPECT_EQ(String::formatted("{:d}", L'a'), "97");
|
||||
EXPECT_EQ(String::formatted("{:d}", L'\U0001F41E'), "128030");
|
||||
|
||||
EXPECT_EQ(String::formatted("{:6}", L'a'), "a ");
|
||||
EXPECT_EQ(String::formatted("{:6d}", L'a'), " 97");
|
||||
EXPECT_EQ(String::formatted("{:#x}", L'\U0001F41E'), "0x1f41e");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user