Skip to content

Commit

Permalink
Use writerIndex to figure out the convinience end of useful content i…
Browse files Browse the repository at this point in the history
…n the buffer
  • Loading branch information
natikgadzhi committed Jul 15, 2023
1 parent e23fb4b commit 4975ffa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ extension ByteBuffer {
/// - offset: The offset from the beginning of the buffer, in bytes. Defaults to 0.
/// - readableOnly: Whether to only dump the `readableBytes` part (before the `writerIndex`), or to dump the full buffer, including empty bytes in the end of the allocated space. Defaults to true.
func hexDumpShort(offset: Int = 0, readableOnly: Bool = true) -> String {
let length = (readableOnly ? self.readableBytes : self.capacity) - offset
let length = (readableOnly ? self.writerIndex : self.capacity) - offset
return self._storage.dumpBytes(slice: self._storage.fullSlice,
offset: offset,
length: length)
Expand Down
21 changes: 18 additions & 3 deletions Tests/NIOCoreTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,24 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual("6c 6c 6f", buf.hexDumpShort(offset: 2))
}

func testXXDClippedHexDump() {
func testHexDumpShortWithReaderIndexOffset() {
var buf = ByteBufferAllocator().buffer(string: "Hello")
let firstTwo = buf.readBytes(length: 2)!
XCTAssertEqual([72, 101], firstTwo)
XCTAssertEqual("48 65 6c 6c 6f", buf.hexDumpShort())
}

func testHexDumpShortWithReaderIndexOffsetAndReadableOnlyFalse() {
var buf = ByteBufferAllocator().buffer(string: "Hello")

let firstTwo = buf.readBytes(length: 2)!
XCTAssertEqual([72, 101], firstTwo)

let zeroes = String(repeating: " 00", count: buf.capacity - buf.writerIndex)
XCTAssertEqual("48 65 6c 6c 6f" + zeroes, buf.hexDumpShort(readableOnly: false))
}

func testHexDumpShortWithLimit() {
self.buf.clear()
for f in UInt8.min...UInt8.max {
self.buf.writeInteger(f)
Expand All @@ -1884,8 +1901,6 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual(expected, actual)
}



func testHexASCIIDump() {
let buf = ByteBufferAllocator().buffer(string: "Goodbye, world! It was nice knowing you.\n")
let expected = """
Expand Down

0 comments on commit 4975ffa

Please sign in to comment.