feat(stdlib): Add Buffer.getChar#2262
Conversation
| @unsafe | ||
| provide let getChar = (index, buffer) => { | ||
| use WasmI32.{ (+), (&), (+), (==), (>) } | ||
| checkIsIndexInBounds(index, 1, buffer) |
There was a problem hiding this comment.
Why is this 1 if it's UTF-8?
There was a problem hiding this comment.
Characters can be between 1 and 4 bytes, we need to ensure the first byte exists so we can check the char size on line 406 and we do an additional length check on line 408 with the actual length.
This is why getChar needs to operate on the bytes directly rather than just using Bytes.getChar like our other helpers.
| } | ||
|
|
||
| /** | ||
| * Gets the UTF-8 encoded character at the given byte index. |
There was a problem hiding this comment.
If the operation is intended to get a character starting at a byte index then if you point your index in the middle of a UTF-8 character then what would the expectation be?
There was a problem hiding this comment.
I'm pretty sure it would either return a character in the case that the rest was a valid char (I can't recall if thats ever the case), but more likely MalformedUnicode.
ospencer
left a comment
There was a problem hiding this comment.
I'm somewhat against these get/set APIs (not just the ones introduced in this PR, the existing ones too). I think it really encourages the wrong use of Buffers; they're really just meant for appending data, and they add a bunch of mental burden of "am I supposed to use add or set?". I'm more in favor of removing the existing ones rather than adding more. What are the use cases here?
If we have any of the |
|
We only have them for integers (so they're missing for everything else), and they landed in the initial implementation... I think we were just kinda caught up in that implementation and didn't audit the full API. |
1ff67e8 to
48fd368
Compare
Buffer.setChar and Buffer.getCharBuffer.getChar
|
I removed |
48fd368 to
3ef3b66
Compare

This adds
Buffer.setCharandBuffer.getCharto complement their bytes counterparts.Note: We have to read the first byte before
getCharso we can correctly do the bounds check similar to how its done inBytes.getChar.Note: I realized we missed a piece of documentation on
Bytes.getCharso I added it here.