mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-19 09:17:38 +03:00
AK: Add LogStream overload for ReadonlyBytes.
This is extremely useful for debugging.
This commit is contained in:
parent
bc48181939
commit
cd2815ed87
Notes:
sideshowbarker
2024-07-19 02:52:29 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/cd2815ed87c Pull-request: https://github.com/SerenityOS/serenity/pull/3409
@ -209,4 +209,45 @@ const LogStream& operator<<(const LogStream& stream, float value)
|
||||
|
||||
#endif
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, ReadonlyBytes bytes)
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
u8 buffered_byte = 0;
|
||||
size_t nrepeat = 0;
|
||||
const char* prefix = "";
|
||||
|
||||
auto flush = [&]() {
|
||||
if (nrepeat > 0) {
|
||||
if (nrepeat == 1)
|
||||
builder.appendf("%s0x%02x", prefix, static_cast<int>(buffered_byte));
|
||||
else
|
||||
builder.appendf("%s%zu * 0x%02x", prefix, nrepeat, static_cast<int>(buffered_byte));
|
||||
|
||||
nrepeat = 0;
|
||||
prefix = ", ";
|
||||
}
|
||||
};
|
||||
|
||||
builder.append("{ ");
|
||||
|
||||
for (auto byte : bytes) {
|
||||
if (nrepeat > 0) {
|
||||
if (byte != buffered_byte)
|
||||
flush();
|
||||
|
||||
buffered_byte = byte;
|
||||
nrepeat++;
|
||||
} else {
|
||||
buffered_byte = byte;
|
||||
nrepeat = 1;
|
||||
}
|
||||
}
|
||||
flush();
|
||||
|
||||
builder.append(" }");
|
||||
|
||||
return stream << builder.to_string();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -184,6 +184,7 @@ const LogStream& operator<<(const LogStream& stream, Span<T> span)
|
||||
}
|
||||
|
||||
const LogStream& operator<<(const LogStream&, const void*);
|
||||
const LogStream& operator<<(const LogStream&, ReadonlyBytes);
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, char value)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user