mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 17:31:58 -05:00
AK: Add a basic formatter for wchar_t
This commit is contained in:
parent
4ef3ed4ba3
commit
67a579aab0
3 changed files with 31 additions and 0 deletions
|
@ -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 a new issue