Selaa lähdekoodia

AK: Add LogStream overload for ReadonlyBytes.

This is extremely useful for debugging.
asynts 4 vuotta sitten
vanhempi
commit
cd2815ed87
2 muutettua tiedostoa jossa 42 lisäystä ja 0 poistoa
  1. 41 0
      AK/LogStream.cpp
  2. 1 0
      AK/LogStream.h

+ 41 - 0
AK/LogStream.cpp

@@ -209,4 +209,45 @@ const LogStream& operator<<(const LogStream& stream, float value)
 
 
 #endif
 #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();
+}
+
 }
 }

+ 1 - 0
AK/LogStream.h

@@ -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&, const void*);
+const LogStream& operator<<(const LogStream&, ReadonlyBytes);
 
 
 inline const LogStream& operator<<(const LogStream& stream, char value)
 inline const LogStream& operator<<(const LogStream& stream, char value)
 {
 {