diff --git a/AK/LogStream.cpp b/AK/LogStream.cpp index 2fead23a5e..26f1ed5238 100644 --- a/AK/LogStream.cpp +++ b/AK/LogStream.cpp @@ -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(buffered_byte)); + else + builder.appendf("%s%zu * 0x%02x", prefix, nrepeat, static_cast(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(); +} + } diff --git a/AK/LogStream.h b/AK/LogStream.h index cd1ae9a63b..aa975c6540 100644 --- a/AK/LogStream.h +++ b/AK/LogStream.h @@ -184,6 +184,7 @@ const LogStream& operator<<(const LogStream& stream, Span span) } const LogStream& operator<<(const LogStream&, const void*); +const LogStream& operator<<(const LogStream&, ReadonlyBytes); inline const LogStream& operator<<(const LogStream& stream, char value) {