mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 05:27:45 +00:00
AK: Add a :hex-dump mode to AK::Format
This will just hexdump the given value. Note that not all formatters respect this, the ones that do are: - (Readonly)Bytes: formatter added in this commit - StringView / char const* - integral types
This commit is contained in:
parent
10f56166e5
commit
7eda164c25
2 changed files with 63 additions and 2 deletions
27
AK/Format.h
27
AK/Format.h
|
@ -196,6 +196,11 @@ public:
|
|||
SignMode sign_mode = SignMode::OnlyIfNeeded);
|
||||
#endif
|
||||
|
||||
void put_hexdump(
|
||||
ReadonlyBytes,
|
||||
size_t width,
|
||||
char fill = ' ');
|
||||
|
||||
const StringBuilder& builder() const
|
||||
{
|
||||
return m_builder;
|
||||
|
@ -261,6 +266,7 @@ struct StandardFormatter {
|
|||
Float,
|
||||
Hexfloat,
|
||||
HexfloatUppercase,
|
||||
HexDump,
|
||||
};
|
||||
|
||||
FormatBuilder::Align m_align = FormatBuilder::Align::Default;
|
||||
|
@ -296,6 +302,27 @@ struct Formatter<StringView> : StandardFormatter {
|
|||
|
||||
void format(FormatBuilder&, StringView value);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<ReadonlyBytes> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, ReadonlyBytes const& value)
|
||||
{
|
||||
if (m_mode == Mode::Pointer) {
|
||||
Formatter<FlatPtr> formatter { *this };
|
||||
formatter.format(builder, reinterpret_cast<FlatPtr>(value.data()));
|
||||
} else if (m_mode == Mode::Default || m_mode == Mode::HexDump) {
|
||||
m_mode = Mode::HexDump;
|
||||
Formatter<StringView>::format(builder, value);
|
||||
} else {
|
||||
Formatter<StringView>::format(builder, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<Bytes> : Formatter<ReadonlyBytes> {
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<const char*> : Formatter<StringView> {
|
||||
void format(FormatBuilder& builder, const char* value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue