apple / swift-nio

Event-driven network application framework for high performance protocol servers & clients, non-blocking.
https://swiftpackageindex.com/apple/swift-nio/documentation
Apache License 2.0
7.85k stars 633 forks source link

ByteBuffer should have hex bytes initialiser / write method #2748

Open weissi opened 2 weeks ago

weissi commented 2 weeks ago

This is pretty much a companion of #2447 / #2475. The aforementioned bugs/patches introduce the ability to get the hex dump from a ByteBuffer which is awesome.

Next, I'd like to construct/write into a ByteBuffer from a hex dump:

I'd say it just needs to support the .plain format (i.e. 2 hex-digit values with whitespace ignored)


Super basic test:

var buffer = ByteBuffer(hexEncodedBytes: "68 65 6c 6c 6f 20 77 6f 72 6c 64 0a")
buffer.writeHexEncodedBytes("68656c6c6f20776f726c64")
buffer.writeHexEncodedBytes("")
buffer.writeHexEncodedBytes("0a")
XCTAssertEqual(ByteBuffer(string: "hello world\nhello world\n"), buffer)

another one:

var allBytes = ByteBuffer()
for x in UInt8.min ... UInt8.max {
    buffer.writeInteger(x)
}

let allBytesHex = allBytes.hexDump(format: .plain)
let allBytesDecoded = ByteBuffer(hexEncodedBytes: allBytesHex)

XCTAssertEqual(allBytes, allBytesDecoded)
ali-ahsan-ali commented 1 week ago

I have taken a look and would like to pick this up after https://github.com/apple/swift-nio/issues/2734.