-
-
Notifications
You must be signed in to change notification settings - Fork 115
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(stdlib): Add String.encode and String.decode functions to standard library #683
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Largely looks good. Curious to hear your thoughts on the shared utilities.
May a suggest that it would be useful to have a version of encode that writes to an existing instance of Bytes into a specific offset? I'm currently using Bytes as a big buffer where I append multiple strings and have to create a temporary Bytes with Bytes.fromString for each write operation, only to then copy it over into my big buffer with Bytes.move. Also I think it would be useful to add offset and length parameters to the decode function for things like reading multiple strings from structured binary formats. |
@cician I am not opposed (in principle, at least), but there is a question about what the API would look like: Option 1 would be to add two new functions which do the read/write based on the existing buffer. The design question here is what these variants would be named. Option 2 would be to wait until #388 is resolved, and then have a buffer/offset be optional arguments to the existing functions. I lean towards this option, but, of course, this means waiting until #388 is resolved. @ospencer, what are your thoughts? |
I'd say go ahead and add them in now, as we'll end up refactoring the whole stdlib once we've got optional args. I'd call them |
For encode i think it would make sense to just have a separate function like this:
For decode optional parameters would be nice, though maybe a bit strange if the user could pass one, but not the other.
Or encodeBuffered/decodeBuffered I'm not picky about names. |
I've added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave the implementation review to @ospencer since I'm very dumb when it comes to the ptr logic; however, I do think we need to split up the APIs a bit since we don't support optional arguments yet.
3769701
to
12ac705
Compare
@phated Everything should be addressed now. I've updated the original post with the present user-facing API. (the default |
cc @ospencer on the updated API ☝️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks for updating the API 🎊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not a fan of the encodeInto
name, because to me it's confusing what "into" means. To me it really sounds like encode
has some default behavior and encodeInto
lets you pick what encoding you want to encode the string into, not having anything to do with offsets within a Bytes
instance. I'm not going to block on it though—once #710 is merged, we should refactor encode
to write into a Buffer. Then encodeInto
isn't needed and we can just delete it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We chatted and largely aligned on what the future API would look like. In the meanwhile, we're gonna go with encodeAt
. This PR has my blessing once that change is done 👼 (approving now for those beautiful European summer mornings 🌞)
bc9b11c
to
33839d5
Compare
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
33839d5
to
6c70e4b
Compare
🇪🇺 |
This pull request adds two new functions to the
String
module in the standard library:These help enable use cases such as those described by @cician in #661, in which users need to marshal Grain strings into nonstandard encodings.