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

unicode/utf16: add RuneLen #44940

Closed
bouk opened this issue Mar 11, 2021 · 10 comments
Closed

unicode/utf16: add RuneLen #44940

bouk opened this issue Mar 11, 2021 · 10 comments

Comments

@bouk
Copy link

bouk commented Mar 11, 2021

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
	}
}
@mdlayher mdlayher changed the title Add utf16.EncodedLen proposal: unicode/utf16: add EncodedLen Mar 11, 2021
@gopherbot gopherbot added this to the Proposal milestone Mar 11, 2021
@ianlancetaylor

This comment has been minimized.

@bouk bouk changed the title proposal: unicode/utf16: add EncodedLen proposal: unicode/utf16: add RuneLen Mar 12, 2021
@bouk

This comment has been minimized.

@rsc
Copy link
Contributor

rsc commented Apr 7, 2021

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

@rsc
Copy link
Contributor

rsc commented Apr 14, 2021

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

@rsc
Copy link
Contributor

rsc commented Apr 21, 2021

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

@rsc rsc changed the title proposal: unicode/utf16: add RuneLen unicode/utf16: add RuneLen Apr 21, 2021
@rsc rsc modified the milestones: Proposal, Backlog Apr 21, 2021
@bouk
Copy link
Author

bouk commented Apr 21, 2021

Excellent! I will work on a patch

@qmuntal
Copy link
Contributor

qmuntal commented Mar 23, 2022

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

bouk added a commit to bouk/go that referenced this issue Mar 23, 2022
RuneLen returns the number of 16-bit words required to encode a rune.

Fixes golang#44940
@bouk
Copy link
Author

bouk commented Mar 23, 2022

I've created a PR: #51894

@gopherbot
Copy link
Contributor

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

@rsc rsc moved this to Accepted in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@gopherbot
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Accepted
Development

Successfully merging a pull request may close this issue.

6 participants