go-utf8/len.go

23 lines
280 B
Go
Raw Normal View History

2018-07-02 18:03:47 +00:00
package utf8s
// Len returns the number of bytes in a UTF-8 encoding of this Unicode code point.
func Len(r rune) int {
switch {
2018-07-02 18:11:03 +00:00
case 127 >= r:
2018-07-02 18:03:47 +00:00
return 1
case 0x7FF >= r:
return 2
case 0xFFFF >= r:
return 3
case 0x10FFFF >= r:
return 4
default:
return 0
}
}