Skip to content

Commit

Permalink
Making ByteBuffer's description more useful (#2864)
Browse files Browse the repository at this point in the history
### Motivation:

Resolving the following issue:
#2863

### Modifications:

Rewrote `description` and `debugDescription` on `ByteBuffer` to make it
more useful. Also fixed a bug in the in `hexDumpCompact`.

### Result:

A more useful `description` and `debugDescription`.

---------

Co-authored-by: Franz Busch <f.busch@apple.com>
  • Loading branch information
supersonicbyte and FranzBusch authored Sep 4, 2024
1 parent 9746cf8 commit 3615f9c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
31 changes: 13 additions & 18 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -957,37 +957,32 @@ public struct ByteBuffer {
}

extension ByteBuffer: CustomStringConvertible, CustomDebugStringConvertible {
/// A `String` describing this `ByteBuffer`. Example:
/// A `String` describing this `ByteBuffer` including length and the bytes it contains (partially).
///
/// ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 512, storageCapacity: 1024, slice: 256..<768, storage: 0x0000000103001000 (1024 bytes)}
/// For a `ByteBuffer` initialised with `hello world` the description would be the following:
///
/// [68656c6c6f20776f726c64](11 bytes)
///
/// Buffers larger that 64 bytes will get truncated when printing out.
/// The format of the description is not API.
///
/// - returns: A description of this `ByteBuffer`.
public var description: String {
"""
ByteBuffer { \
readerIndex: \(self.readerIndex), \
writerIndex: \(self.writerIndex), \
readableBytes: \(self.readableBytes), \
capacity: \(self.capacity), \
storageCapacity: \(self.storageCapacity), \
slice: \(self._slice), \
storage: \(self._storage.bytes) (\(self._storage.capacity) bytes) \
}
"""
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
}

/// A `String` describing this `ByteBuffer` with some portion of the readable bytes dumped too. Example:
/// A `String` describing this `ByteBuffer` including length and the bytes it contains (partially).
///
/// ByteBuffer { readerIndex: 0, writerIndex: 4, readableBytes: 4, capacity: 512, slice: 256..<768, storage: 0x0000000103001000 (1024 bytes)}
/// readable bytes (max 1k): [ 00 01 02 03 ]
/// For a `ByteBuffer` initialised with `hello world` the description would be the following:
///
/// [68656c6c6f20776f726c64](11 bytes)
///
/// Buffers larger that 64 bytes will get truncated when printing out.
/// The format of the description is not API.
///
/// - returns: A description of this `ByteBuffer` useful for debugging.
/// - returns: A description of this `ByteBuffer`.
public var debugDescription: String {
"\(self.description)\nreadable bytes (max 1k): \(self._storage.dumpBytes(slice: self._slice, offset: self.readerIndex, length: min(1024, self.readableBytes)))"
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOCore/ByteBuffer-hexdump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extension ByteBuffer {
private func _hexDump(maxBytes: Int, separateWithWhitespace: Bool) -> String {
// If the buffer length fits in the max bytes limit in the hex dump, just dump the whole thing.
if self.readableBytes <= maxBytes {
return self.hexDump(format: .plain)
return self._hexDump(separateWithWhitespace: separateWithWhitespace)
}

var buffer = self
Expand Down
33 changes: 33 additions & 0 deletions Tests/NIOCoreTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,11 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual("48656c6c6f", buf.hexDump(format: .compact))
}

func testHexDumpCompactReadableBytesLessThenMaxBytes() {
let buf = ByteBuffer(string: "hello world")
XCTAssertEqual("68656c6c6f20776f726c64", buf.hexDump(format: .compact(maxBytes: 100)))
}

func testHexDumpCompactEmptyBuffer() {
let buf = ByteBuffer(string: "")
XCTAssertEqual("", buf.hexDump(format: .compact))
Expand Down Expand Up @@ -3603,4 +3608,32 @@ extension ByteBufferTest {
}
}

func testByteBufferDescription() {
let buffer = ByteBuffer(string: "hello world")

XCTAssertEqual(buffer.description, "[68656c6c6f20776f726c64](11 bytes)")

XCTAssertEqual(buffer.description, buffer.debugDescription)
}

func testByteBufferDescriptionEmpty() {
let buffer = ByteBuffer()

XCTAssertEqual(buffer.description, "[](0 bytes)")

XCTAssertEqual(buffer.description, buffer.debugDescription)
}

func testByteBufferDescriptionTruncated() {
let buffer = ByteBuffer(
string: "iloveswiftnioiloveswiftnioiloveswiftnioiloveswiftnioiloveswiftnioiloveswiftnio"
)

XCTAssertEqual(
buffer.description,
"[696c6f766573776966746e696f696c6f766573776966746e696f696c6f766573...6966746e696f696c6f766573776966746e696f696c6f766573776966746e696f](78 bytes)"
)

XCTAssertEqual(buffer.description, buffer.debugDescription)
}
}
2 changes: 1 addition & 1 deletion Tests/NIOCoreTests/NIOAnyDebugTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NIOAnyDebugTest: XCTestCase {
let bb = ByteBuffer(string: "byte buffer string")
XCTAssertTrue(
wrappedInNIOAnyBlock(bb).contains(
"NIOAny { ByteBuffer { readerIndex: 0, writerIndex: 18, readableBytes: 18, capacity: 32, storageCapacity: 32, slice: _ByteBufferSlice { 0..<32 }, storage: "
"NIOAny { [627974652062756666657220737472696e67](18 bytes) }"
)
)
XCTAssertTrue(wrappedInNIOAnyBlock(bb).hasSuffix(" }"))
Expand Down

0 comments on commit 3615f9c

Please sign in to comment.