Skip to content

Commit a9581e2

Browse files
ALTreebradfitz
authored andcommitted
unicode/utf16: speed up and clean up Encode and EncodeRune
name old time/op new time/op delta EncodeValidASCII-4 74.1ns ± 1% 70.1ns ± 1% -5.46% (p=0.000 n=10+10) EncodeValidJapaneseChars-4 61.3ns ± 0% 58.9ns ± 0% -3.82% (p=0.000 n=10+10) EncodeRune-4 13.1ns ± 1% 9.8ns ± 0% -25.24% (p=0.000 n=10+9) Fixes golang#6957 Change-Id: I9dde6d77420c34c6e2ef3e6213bb6be9b58a3074 Reviewed-on: https://go-review.googlesource.com/19891 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 47b0422 commit a9581e2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/unicode/utf16/utf16.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func DecodeRune(r1, r2 rune) rune {
4545
// If the rune is not a valid Unicode code point or does not need encoding,
4646
// EncodeRune returns U+FFFD, U+FFFD.
4747
func EncodeRune(r rune) (r1, r2 rune) {
48-
if r < surrSelf || r > maxRune || IsSurrogate(r) {
48+
if r < surrSelf || r > maxRune {
4949
return replacementChar, replacementChar
5050
}
5151
r -= surrSelf
@@ -65,20 +65,22 @@ func Encode(s []rune) []uint16 {
6565
n = 0
6666
for _, v := range s {
6767
switch {
68-
case v < 0, surr1 <= v && v < surr3, v > maxRune:
69-
v = replacementChar
70-
fallthrough
71-
case v < surrSelf:
68+
case 0 <= v && v < surr1, surr3 <= v && v < surrSelf:
69+
// normal rune
7270
a[n] = uint16(v)
7371
n++
74-
default:
72+
case surrSelf <= v && v <= maxRune:
73+
// needs surrogate sequence
7574
r1, r2 := EncodeRune(v)
7675
a[n] = uint16(r1)
7776
a[n+1] = uint16(r2)
7877
n += 2
78+
default:
79+
a[n] = uint16(replacementChar)
80+
n++
7981
}
8082
}
81-
return a[0:n]
83+
return a[:n]
8284
}
8385

8486
// Decode returns the Unicode code point sequence represented

0 commit comments

Comments
 (0)