Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByteBuffer should have hex bytes initialiser / write method #2748

Open
weissi opened this issue Jun 21, 2024 · 9 comments
Open

ByteBuffer should have hex bytes initialiser / write method #2748

weissi opened this issue Jun 21, 2024 · 9 comments
Labels
good first issue Good for newcomers kind/enhancement Improvements to existing feature. semver/minor Adds new public API.

Comments

@weissi
Copy link
Member

weissi commented Jun 21, 2024

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:

  • ByteBuffer(hexEncodedBytes: "68 65 6c 6c 6f 20 77 6f 72 6c 64 0a")
  • buffer.writeHexEncodedBytes("68656c6c6f20776f726c640a")

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)
@weissi weissi added the good first issue Good for newcomers label Jun 21, 2024
@weissi weissi changed the title ByteBuffer should have hex bytes initialisers ByteBuffer should have hex bytes initialiser / write method Jun 21, 2024
@weissi weissi added kind/enhancement Improvements to existing feature. semver/minor Adds new public API. labels Jun 21, 2024
@ali-ahsan-ali
Copy link
Contributor

I have taken a look and would like to pick this up after #2734.

@weissi
Copy link
Member Author

weissi commented Aug 5, 2024

Of course this should also parse the .compat format suggested in #2825

@weissi
Copy link
Member Author

weissi commented Aug 5, 2024

@ali-ahsan-ali are you still interested in this?

@ali-ahsan-ali
Copy link
Contributor

Happy if someone else is eager to give it a go. I have started looking at it but no real progress. It feels overly ambitious to try and start working on a second PR when my first has taken quite a while.

@weissi
Copy link
Member Author

weissi commented Aug 5, 2024

Happy if someone else is eager to give it a go. I have started looking at it but no real progress. It feels overly ambitious to try and start working on a second PR when my first has taken quite a while.

I did re-request review on your other PR. Remember to click the ♻️ (re-request) review once you addressed the changed suggested/requested. I'm sure @FranzBusch & @Lukasa just missed that you pushed updates to your existing PR.

Separately, if you're up for it, you can totally have multiple PRs open at the same time, shouldn't slow things down.

@ali-ahsan-ali
Copy link
Contributor

ali-ahsan-ali commented Aug 7, 2024

The "good for newcomers" in this repo are the hardest thing ever 😅 (which I VERY much enjoy).

Experimenting with the below and seeing if writing that to the buffer is the right outcome for writing a hex encoded string to buffer.

extension String {
    var hexData: some Sequence<UInt8> {
        sequence(state: self, next: { remainder in
            guard remainder.count >= 2 else { return nil }
            let nextTwo = remainder.prefix(2)
            remainder.removeFirst(2)
            return UInt8(nextTwo, radix: 16) // compatible
        })
    }
}

TLDR: Thanks for the motivation/push. Working on it!

@weissi
Copy link
Member Author

weissi commented Aug 7, 2024

The "good for newcomers" in this repo are the hardest thing every 😅 (which I VERY much enjoy).

Experimenting with the below and seeing if writing that to the buffer is the right outcome for writing a hex encoded string to buffer.

extension String {
    var hexData: some Sequence<UInt8> {
        sequence(state: self, next: { remainder in
            guard remainder.count >= 2 else { return nil }
            let nextTwo = remainder.prefix(2)
            remainder.removeFirst(2)
            return UInt8(nextTwo, radix: 16) // compatible
        })
    }
}

TLDR: Thanks for the push. Working on it!

Awesome, FWIW, you can use

extension String {
   var hexData: some Sequence<UInt8> {
       return ByteBuffer(string: .self).hexDump(format: .plain).utf8
   }
}

to get a sequence of UInt8s. String's .utf8 gives you a UTF8View which is a Sequence<UInt8>

@ali-ahsan-ali
Copy link
Contributor

ali-ahsan-ali commented Aug 11, 2024

@weissi please take a look at the PR when you find the time

Lukasa added a commit that referenced this issue Sep 30, 2024
Add ability for below two functions 
`ByteBuffer(hexEncodedBytes: "68 65 6c 6c 6f 20 77 6f 72 6c 64 0a")`
` buffer.writeHexEncodedBytes("68656c6c6f20776f726c640a")`

### Motivation:

#2748

---------

Co-authored-by: Ali Ali <[email protected]>
Co-authored-by: Cory Benfield <[email protected]>
@ali-ahsan-ali
Copy link
Contributor

I think this can be closed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers kind/enhancement Improvements to existing feature. semver/minor Adds new public API.
Projects
None yet
Development

No branches or pull requests

2 participants