Sometimes it's useful to determine how many 16-bit words it'll take to encode a unicode rune with UTF16. We already have utf8.RuneLen, and one for utf16 could be useful.

It would look something like this (those are private constants inside of utf16):

// RuneLen returns the number of 16-bit words required to encode the rune.
// It returns -1 if the rune is not a valid value to encode in UTF-16.
func RuneLen(r rune) int {
    switch {
    case 0 <= r && r < surr1, surr3 <= r && r < surrSelf:
        return 1
    case surrSelf <= r && r <= maxRune:
        return 2
    default:
        return -1
    }
}

Comment From: ianlancetaylor

I don't see utf8.EncodedLen. Do you mean EncodedLen or RuneLen? We do have utf8.RuneLen.

Comment From: bouk

Woops yes, I meant RuneLen. Fixed.

Comment From: rsc

This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. — rsc for the proposal review group

Comment From: rsc

Based on the discussion above, this proposal seems like a likely accept. — rsc for the proposal review group

Comment From: rsc

No change in consensus, so accepted. 🎉 This issue now tracks the work of implementing the proposal. — rsc for the proposal review group

Comment From: bouk

Excellent! I will work on a patch

Comment From: qmuntal

@bouk any update? I'm interested in this proposal landing soon, if you are focused on other projects I can take over this implementation.

Comment From: bouk

I've created a PR: https://github.com/golang/go/pull/51894

Comment From: gopherbot

Change https://go.dev/cl/395114 mentions this issue: unicode/utf16: add RuneLen

Comment From: gopherbot

Change https://go.dev/cl/569755 mentions this issue: unicode/utf16: add func RuneLen