-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
unicode/utf16: add AppendRune #51896
Comments
I like your suggestion a lot. It would match nicely utf8.AppendRune and there is no need for My syscall example would look like this: func Foo(s string) {
p := []rune(s + "\x00")
a := make([]uint16, 0, 32)
for _, r := range p {
a = utf16.AppendRune(a, r)
}
syscall.Syscall6(procBCryptGetProperty.Addr(), 6, 0, uintptr(unsafe.Pointer(&a[0])), 0, 0, 0, 0)
return
} |
Change https://go.dev/cl/409054 mentions this issue: |
Prototyped the proposal in CL 409054. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Background
utf16.Encode always allocates a
[]uint16
large enough to fit the UTF-16 encoded sequence, which is really ergonomic but forces one allocation.Proposal
Update, May 27 2022: The proposed API has changed (see #51896 (comment)) to:
Update, May 27 2022: The following functions were been superseded by the previous
AppendRune
.For those cases that the extra allocation matters,
unicode/utf16
could provide an additional encoding function which accepts a pre-allocated (and large enough) backing slice.The signature would look like this:
Optionally, in order to know the minimum size of the backing array,
unicode/utf16
could provide an additional function which counts the number of code units in a code point sequence.It would look something like this:
It worth northing that
utf16.Encode
could then be implemented usingutf16.Count
andutf16.EncodeInto
.Examples
My specific use case is to allow
x/sys/windows/mkwinsyscall
generate syscall wrappers which acceptstring
arguments without allocating, at least for short strings. Check this comment for more context.If I had
utf16.EncodeInto
I could implement a non-allocating wrapper as follows:The text was updated successfully, but these errors were encountered: