mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-07 12:38:38 -10:00
AK: Make Default be the default formatting mode for Bytes
There is no way to opt out of the HexDump formatting mode. So let's use the default `Span` formatter and allow the user to opt in the HexDump mode for `Span<u8>` aka `Bytes`. This is mainly useful to print `Vector<u8>`, as the `Vector` formatter is implemented on top of the `Span` formatter.
This commit is contained in:
1 parent
0cb7138105
commit
4498dff68b
2 files changed
+18
-27
No files matched your search
+6
-27
@@ -438,13 +438,7 @@ struct Formatter<StringView> : StandardFormatter {
|
||||
|
||||
template<typename T>
|
||||
requires(HasFormatter<T>)
|
||||
struct Formatter<ReadonlySpan<T>> : StandardFormatter {
|
||||
Formatter() = default;
|
||||
explicit Formatter(StandardFormatter formatter)
|
||||
: StandardFormatter(move(formatter))
|
||||
{
|
||||
}
|
||||
|
||||
struct Formatter<ReadonlySpan<T>> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, ReadonlySpan<T> value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
@@ -453,6 +447,11 @@ struct Formatter<ReadonlySpan<T>> : StandardFormatter {
|
||||
return {};
|
||||
}
|
||||
|
||||
if constexpr (IsSame<T, u8>) {
|
||||
if (m_mode == Mode::HexDump)
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
|
||||
if (m_sign_mode != FormatBuilder::SignMode::Default)
|
||||
VERIFY_NOT_REACHED();
|
||||
if (m_alternative_form)
|
||||
@@ -501,26 +500,6 @@ struct Formatter<Vector<T, inline_capacity>> : Formatter<ReadonlySpan<T>> {
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<ReadonlyBytes> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, ReadonlyBytes value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
return formatter.format(builder, reinterpret_cast<FlatPtr>(value.data()));
|
||||
}
|
||||
if (m_mode == Mode::Default || m_mode == Mode::HexDump) {
|
||||
m_mode = Mode::HexDump;
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
return Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
|
||||
};
|
||||
|
||||
// FIXME: Printing raw char pointers is inherently dangerous. Remove this and
|
||||
// its users and prefer StringView over it.
|
||||
template<>
|
||||
|
||||
@@ -397,6 +397,18 @@ TEST_CASE(vector_format)
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(read_only_bytes_format)
|
||||
{
|
||||
{
|
||||
Vector<u8> v { 1, 2, 3, 4 };
|
||||
EXPECT_EQ(ByteString::formatted("{}", v), "[ 1, 2, 3, 4 ]");
|
||||
}
|
||||
{
|
||||
Vector<u8> v { 1, 2, 3, 4 };
|
||||
EXPECT_EQ(ByteString::formatted("{:hex-dump}", v), "01020304");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(format_wchar)
|
||||
{
|
||||
EXPECT_EQ(ByteString::formatted("{}", L'a'), "a");
|
||||
|
||||
Reference in new issue
Block a user