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

feat: Add Char.byteCount #2238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

spotandjake
Copy link
Member

This adds Char.byteCount which can be used with a specified encoding to get the encoded length of a char.

Example use case for this is stepping through a byte array when you do Byte.getChar you have no clue how far to advance for the next char without this.

@ospencer
Copy link
Member

This would need to be called utf8EncodedLength or something like that to be specific about what byte length it's referring to.

@spotandjake
Copy link
Member Author

spotandjake commented Jan 26, 2025

This would need to be called utf8EncodedLength or something like that to be specific about what byte length it's referring to.

The function takes an encoding parameter and will return the byte count in the specified encoding. It takes the same encodings as our String.encode and String.decode functions.

let utf8ByteCount = usvEncodeLength(usv)
let utf8ByteCount = tagSimpleNumber(utf8ByteCount)
match (encoding) {
UTF32 => 4,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can check this at the beginning since it'll always be 4 but nbd

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth it as it makes the match non exhaustive and adds an extra branch, UTF32 probably isn't going to be used very often anyways so optimizing for it doesn't seem worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants