aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/unicode
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2014-03-23 15:44:29 -0700
committerIan Lance Taylor <iant@golang.org>2014-03-23 15:44:29 -0700
commit446d90d727b1820f8f4ef2f4e22d6ce1cd88df4d (patch)
treeefe3c6d32c744b8a1ed28ad8da8dcaf4c4e275e0 /src/pkg/unicode
parent160649ff9adaffddf91c45da2767f3fdc99f6d73 (diff)
downloadgo-446d90d727b1820f8f4ef2f4e22d6ce1cd88df4d.tar.xz
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
Diffstat (limited to 'src/pkg/unicode')
-rw-r--r--src/pkg/unicode/utf8/utf8.go32
1 files changed, 12 insertions, 20 deletions
diff --git a/src/pkg/unicode/utf8/utf8.go b/src/pkg/unicode/utf8/utf8.go
index 93d0be5e0c..0dc859a041 100644
--- a/src/pkg/unicode/utf8/utf8.go
+++ b/src/pkg/unicode/utf8/utf8.go
@@ -329,37 +329,29 @@ func RuneLen(r rune) int {
// It returns the number of bytes written.
func EncodeRune(p []byte, r rune) int {
// 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)
return 1
- }
-
- if uint32(r) <= rune2Max {
+ case i <= rune2Max:
p[0] = t2 | byte(r>>6)
p[1] = tx | byte(r)&maskx
return 2
- }
-
- if uint32(r) > MaxRune {
+ case i > MaxRune, surrogateMin <= i && i <= surrogateMax:
r = RuneError
- }
-
- if surrogateMin <= r && r <= surrogateMax {
- r = RuneError
- }
-
- if uint32(r) <= rune3Max {
+ fallthrough
+ case i <= rune3Max:
p[0] = t3 | byte(r>>12)
p[1] = tx | byte(r>>6)&maskx
p[2] = tx | byte(r)&maskx
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