Skip to content

Commit

Permalink
Implemented the public hexdump API
Browse files Browse the repository at this point in the history
  • Loading branch information
natikgadzhi committed Aug 1, 2023
1 parent a31717e commit f188f67
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Sources/NIOCore/ByteBuffer-hexdump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ extension ByteBuffer {

result += "\(String(byte: lineOffset, padding: 8)) "

// Start with a xxd-format hexdump that we already have, then pad it
// and insert a space in the middle.
var lineHex = slice.hexDumpShort()
lineHex.append(String(repeating: " ", count: 49 - lineHex.count))
lineHex.insert(" ", at: lineHex.index(lineHex.startIndex, offsetBy: 24))
result += lineHex

// ASCII column
// ASCII column renders the line as ASCII characters, or "." if the character is not printable.
result += "|" + String(decoding: slice.getBytes(at: 0, length: slice.readableBytes)!, as: Unicode.UTF8.self).map {
$0 >= " " && $0 < "~" ? $0 : "."
} + "|\n"
Expand All @@ -79,4 +81,51 @@ extension ByteBuffer {
result += String(byte: lineOffset, padding: 8)
return result
}

// func hexDumpLong(limit: Int) -> String {
// var result = ""
//
// // Start with long-format dump of limit/2 bytes
// // then insert an empty line with "..." in the middle
// // Add a dump of the last limit/2 bytes
// // The last line should be the final lineOffset
//
//
//
// return result
// }

/// Describes a ByteBuffer hexDump format. Can be either xxd output compatible, or hexdump compatible.
/// You can provide a `maxLength` argument to both formats that will limit the maximum length of the buffer to dump before clipping the dump for readability.
public enum HexDumpFormat {
/// A hexdump format compatible with the xxd utility.
/// - parameters:
/// - maxLength: The maximum amount of bytes to dump before clipping for readability.
case xxdCompatible(maxLength: Int? = nil)

/// A hexdump format compatible with hexdump utility.
/// - parameters:
/// - maxLength: The maximum amount of bytes to dump before clipping for readability.
case hexDumpCompatible(maxLength: Int? = nil)
}


public func hexDump(format: HexDumpFormat) -> String {
switch(format) {
case .xxdCompatible(let maxLength):
if let maxLength {
return self.hexDumpShort(limit: maxLength)
} else {
return self.hexDumpShort()
}

case .hexDumpCompatible(let maxLength):
if let maxLength {
return self.hexDumpLong()
} else {
return self.hexDumpLong()
}
}
}

}
12 changes: 12 additions & 0 deletions Tests/NIOCoreTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,18 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual(expected, actual)
}

func testHexDumpSwitcher() {
let buf = ByteBuffer(string: "Goodbye, world! It was nice knowing you.\n")
let expected = """
00000000 47 6f 6f 64 62 79 65 2c 20 77 6f 72 6c 64 21 20 |Goodbye, world! |
00000010 49 74 20 77 61 73 20 6e 69 63 65 20 6b 6e 6f 77 |It was nice know|
00000020 69 6e 67 20 79 6f 75 2e 0a |ing you..|
00000029
"""
let actual = buf.hexDump(format: .hexDumpCompatible())
XCTAssertEqual(expected, actual)
}

func testReadableBytesView() throws {
self.buf.clear()
self.buf.writeString("hello world 012345678")
Expand Down

0 comments on commit f188f67

Please sign in to comment.