apple/swift-nio

ByteBuffer should have a bunch of hex dump helpers

weissi opened this issue · 5 comments

weissi commented

I frequently need at least the following kinds of functionality, no idea how many times I've implemented them in random projects:

  1. hex dumps of a ByteBuffer one (long) line xxd -r -p compatible (example 42 41 42 41)
  2. same as 1 but length-limited (as in produce xxd compatible output but only up to 1024 bytes, if more, keep beginning and end and replace middle with [...] or so)
  3. produce multi-line hexdump -C compatible output (in full)
  4. product multi-line hexdump -C-compatible output but length limited

and probably there are others. This is mostly an API-design problem I think because if we're smart we can probably fit this all into one method like buffer.hexDump(renderer: .xxdCompatible(maxLength: 1024)) or something.

Yeah this would be handy! I think I've seen something similar to this in Netty too.

@weissi, @glbrntt, hi there! I don't know if I'll be out of my depth in details, but I would love to try and work through this. Would you be open to guiding me and reviewing my code?

hey @natikgadzhi, I'd be happy to guide you through this 🙂

As Johannes points out the API should probably be one top-level function. However, we can break this down into a few chunks and do it bit-by-bit.

I think the rough approach should be:

  1. Add a function to ByteBuffer which produces a space separated hex dump of all bytes contained in the buffer (we can use this to build the other functions). If the buffer contains the bytes of the string "foo" then this function should return the string "66 6f 6f".
  2. Add a function to ByteBuffer to produce the xxd compatible hex dump of its contents which may be length limited. For the non-length-limited case we can just call the function above. For the length limited case we need to get slices from the front and back of the buffer and feed those into the function above and join them "..." or similar.
  3. This is where the fun starts! Add another function to ByteBuffer to produce the same output as hexdump -C and another version which limits the length. This one is a bit more involved so we can revisit it when you get to it.
  4. Add a public function to ByteBuffer (all of the above should be internal) which you can provide a 'format' option to similar to what Johannes mentioned in the post above. This would just drive one of the above functions.

@glbrntt, thank you! Yep, the approach you laid out makes sense — I thought about the same way to build it.

Let me try things out and write a rough draft — I'll put together a PR and tag you. Aiming for Wednesday.

UPD: The week got out of hand, still working. I've got a simple hexdump to work — just adding the variations and the format argument.

UPD2: Oh, this is what you mean by "This is where the fun starts!".

Almost there.

TODOs:

  1. Just got to add that hexdump -C compatible formatter that will also respect the desired max bytes length
  2. And then add the public "switcher" function, and make all other functions internal / private.