-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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/utf8: add AppendRune #47609
Comments
/cc @robpike |
This proposal has been added to the active column of the proposals project |
This does seem reasonable. Thanks @dsnet for doing the scan of the libraries: that makes the case. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://golang.org/cl/345571 mentions this issue: |
The existing
utf8.EncodeRune
is not user friendly.Analyzing all usages in the module proxy, usages fall into approximately two patterns.
Pattern 1: encode and increment the total length
This pattern starts with a buffer that is large enough and then calls
utf8.EncodeRune
to directly write into the buffer and then increment the total known length.This pattern is dangerous as it is especially prone to panicking. It assumes that
b[n:]
is always large enough (most code do not check to make sure that at least 4B available). Even worse, this is a bug that rarely manifests and is unlikely to occur unless the unit test writes a large number of multi-byte runes.Pattern 2: appending to a slice through intermediate array
This pattern is much safer than pattern 1 in that it never panics. However, it 1) incurs a performance penalty encoding into an intermediate array that is then appended to the primary buffer, and 2) requires 3 lines of code instead of just 1.
Prevalence
Between the two common patterns:
Proposal
Since both pattern 1 and 2 are both ultimately concerned with appending into a slice, I propose the addition of:
The signature matches many other append-like APIs in the standard library (e.g.,
strconv.AppendFloat
).The text was updated successfully, but these errors were encountered: