1
0
mirror of https://github.com/golang/go synced 2024-11-26 21:11:57 -07:00

unicode/utf16: speed up and clean up Decode

name                        old time/op  new time/op  delta
DecodeValidASCII-4          94.7ns ± 1%  87.4ns ± 1%  -7.71%  (p=0.000 n=10+9)
DecodeValidJapaneseChars-4  91.0ns ± 2%  84.8ns ± 0%  -6.77%  (p=0.000 n=9+10)
DecodeRune-4                16.5ns ± 0%  16.6ns ± 2%    ~     (p=0.108 n=9+10)

For #6957

Change-Id: I618c15c2a42ef7ec6a5cd163b7c3f1a65ca4ad01
Reviewed-on: https://go-review.googlesource.com/19826
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Alberto Donizetti 2016-02-23 18:45:38 +01:00 committed by Rob Pike
parent 9877900c8c
commit 7786f97905

View File

@ -36,7 +36,7 @@ func IsSurrogate(r rune) bool {
// the Unicode replacement code point U+FFFD.
func DecodeRune(r1, r2 rune) rune {
if surr1 <= r1 && r1 < surr2 && surr2 <= r2 && r2 < surr3 {
return (r1-surr1)<<10 | (r2 - surr2) + 0x10000
return (r1-surr1)<<10 | (r2 - surr2) + surrSelf
}
return replacementChar
}
@ -88,21 +88,19 @@ func Decode(s []uint16) []rune {
n := 0
for i := 0; i < len(s); i++ {
switch r := s[i]; {
case r < surr1, surr3 <= r:
// normal rune
a[n] = rune(r)
case surr1 <= r && r < surr2 && i+1 < len(s) &&
surr2 <= s[i+1] && s[i+1] < surr3:
// valid surrogate sequence
a[n] = DecodeRune(rune(r), rune(s[i+1]))
i++
n++
case surr1 <= r && r < surr3:
default:
// invalid surrogate sequence
a[n] = replacementChar
n++
default:
// normal rune
a[n] = rune(r)
n++
}
n++
}
return a[0:n]
return a[:n]
}