Commit 446d90d7 authored by Rui Ueyama's avatar Rui Ueyama Committed by Ian Lance Taylor

unicode/utf8: minor code simplification

It's a little bit waste to check if r is not a surrogate
code point because RuneError is not a surrogate code point.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/79230043
parent 160649ff
...@@ -329,37 +329,29 @@ func RuneLen(r rune) int { ...@@ -329,37 +329,29 @@ func RuneLen(r rune) int {
// It returns the number of bytes written. // It returns the number of bytes written.
func EncodeRune(p []byte, r rune) int { func EncodeRune(p []byte, r rune) int {
// Negative values are erroneous. Making it unsigned addresses the problem. // Negative values are erroneous. Making it unsigned addresses the problem.
if uint32(r) <= rune1Max { switch i := uint32(r); {
case i <= rune1Max:
p[0] = byte(r) p[0] = byte(r)
return 1 return 1
} case i <= rune2Max:
if uint32(r) <= rune2Max {
p[0] = t2 | byte(r>>6) p[0] = t2 | byte(r>>6)
p[1] = tx | byte(r)&maskx p[1] = tx | byte(r)&maskx
return 2 return 2
} case i > MaxRune, surrogateMin <= i && i <= surrogateMax:
if uint32(r) > MaxRune {
r = RuneError r = RuneError
} fallthrough
case i <= rune3Max:
if surrogateMin <= r && r <= surrogateMax {
r = RuneError
}
if uint32(r) <= rune3Max {
p[0] = t3 | byte(r>>12) p[0] = t3 | byte(r>>12)
p[1] = tx | byte(r>>6)&maskx p[1] = tx | byte(r>>6)&maskx
p[2] = tx | byte(r)&maskx p[2] = tx | byte(r)&maskx
return 3 return 3
default:
p[0] = t4 | byte(r>>18)
p[1] = tx | byte(r>>12)&maskx
p[2] = tx | byte(r>>6)&maskx
p[3] = tx | byte(r)&maskx
return 4
} }
p[0] = t4 | byte(r>>18)
p[1] = tx | byte(r>>12)&maskx
p[2] = tx | byte(r>>6)&maskx
p[3] = tx | byte(r)&maskx
return 4
} }
// RuneCount returns the number of runes in p. Erroneous and short // RuneCount returns the number of runes in p. Erroneous and short
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment