Commit bf5b4e71 authored by Marcel van Lohuizen's avatar Marcel van Lohuizen

unicode/utf8: table-based algorithm for decoding

This simplifies covering all cases, reducing the number of branches
and making unrolling for simpler functions manageable.
This significantly improves performance of non-ASCII input.

This change will also allow addressing Issue #11733 in an efficient
manner.

RuneCountTenASCIIChars-8             13.7ns ± 4%  13.5ns ± 2%     ~     (p=0.116 n=7+8)
RuneCountTenJapaneseChars-8           153ns ± 3%    74ns ± 2%  -51.42%  (p=0.000 n=8+8)
RuneCountInStringTenASCIIChars-8     13.5ns ± 2%  12.5ns ± 3%   -7.13%  (p=0.000 n=8+7)
RuneCountInStringTenJapaneseChars-8   145ns ± 2%    68ns ± 2%  -53.21%  (p=0.000 n=8+8)
ValidTenASCIIChars-8                 14.1ns ± 3%  12.5ns ± 5%  -11.38%  (p=0.000 n=8+8)
ValidTenJapaneseChars-8               147ns ± 3%    71ns ± 4%  -51.72%  (p=0.000 n=8+8)
ValidStringTenASCIIChars-8           12.5ns ± 3%  12.3ns ± 3%     ~     (p=0.095 n=8+8)
ValidStringTenJapaneseChars-8         146ns ± 4%    70ns ± 2%  -51.62%  (p=0.000 n=8+7)
DecodeASCIIRune-8                    5.91ns ± 2%  4.83ns ± 3%  -18.28%  (p=0.001 n=7+7)
DecodeJapaneseRune-8                 12.2ns ± 7%   8.5ns ± 3%  -29.79%  (p=0.000 n=8+7)
FullASCIIRune-8                      5.95ns ± 3%  4.27ns ± 1%  -28.23%  (p=0.000 n=8+7)
FullJapaneseRune-8                   12.0ns ± 6%   4.3ns ± 3%  -64.39%  (p=0.000 n=8+8)

Change-Id: Iea1d6b0180cbbee1739659a0a38038126beecaca
Reviewed-on: https://go-review.googlesource.com/16940Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 2c11164d
...@@ -40,175 +40,106 @@ const ( ...@@ -40,175 +40,106 @@ const (
rune1Max = 1<<7 - 1 rune1Max = 1<<7 - 1
rune2Max = 1<<11 - 1 rune2Max = 1<<11 - 1
rune3Max = 1<<16 - 1 rune3Max = 1<<16 - 1
)
func decodeRuneInternal(p []byte) (r rune, size int, short bool) {
n := len(p)
if n < 1 {
return RuneError, 0, true
}
c0 := p[0]
// 1-byte, 7-bit sequence?
if c0 < tx {
return rune(c0), 1, false
}
// unexpected continuation byte? // The default lowest and highest continuation byte.
if c0 < t2 { locb = 0x80 // 1000 0000
return RuneError, 1, false hicb = 0xBF // 1011 1111
}
// These names of these constants are chosen to give nice alignment in the
// need first continuation byte // table below. The first nibble is an index into acceptRanges or F for
if n < 2 { // special one-byte cases. The second nibble is the Rune length or the
return RuneError, 1, true // Status for the special one-byte case.
} xx = 0xF1 // invalid: size 1
c1 := p[1] as = 0xF0 // ASCII: size 1
if c1 < tx || t2 <= c1 { s1 = 0x02 // accept 0, size 2
return RuneError, 1, false s2 = 0x13 // accept 1, size 3
} s3 = 0x03 // accept 0, size 3
s4 = 0x23 // accept 2, size 3
s5 = 0x34 // accept 3, size 4
s6 = 0x04 // accept 0, size 4
s7 = 0x44 // accept 4, size 4
)
// 2-byte, 11-bit sequence? // first is information about the first byte in a UTF-8 sequence.
if c0 < t3 { var first = [256]uint8{
r = rune(c0&mask2)<<6 | rune(c1&maskx) // 1 2 3 4 5 6 7 8 9 A B C D E F
if r <= rune1Max { as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
return RuneError, 1, false as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
} as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
return r, 2, false as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
} as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
// 1 2 3 4 5 6 7 8 9 A B C D E F
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
}
// need second continuation byte // acceptRange gives the range of valid values for the second byte in a UTF-8
if n < 3 { // sequence.
return RuneError, 1, true type acceptRange struct {
} lo uint8 // lowest value for second byte.
c2 := p[2] hi uint8 // highest value for second byte.
if c2 < tx || t2 <= c2 { }
return RuneError, 1, false
}
// 3-byte, 16-bit sequence? var acceptRanges = [...]acceptRange{
if c0 < t4 { 0: {locb, hicb},
r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx) 1: {0xA0, hicb},
if r <= rune2Max { 2: {locb, 0x9F},
return RuneError, 1, false 3: {0x90, hicb},
} 4: {locb, 0x8F},
if surrogateMin <= r && r <= surrogateMax { }
return RuneError, 1, false
}
return r, 3, false
}
// need third continuation byte // FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
if n < 4 { // An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
return RuneError, 1, true func FullRune(p []byte) bool {
n := len(p)
if n == 0 {
return false
} }
c3 := p[3] x := first[p[0]]
if c3 < tx || t2 <= c3 { if n >= int(x&7) {
return RuneError, 1, false return true // ASCII, invalid or valid.
} }
// Must be short or invalid.
// 4-byte, 21-bit sequence? accept := acceptRanges[x>>4]
if c0 < t5 { if n > 1 {
r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx) if c := p[1]; c < accept.lo || accept.hi < c {
if r <= rune3Max || MaxRune < r { return true
return RuneError, 1, false } else if n > 2 && (p[2] < locb || hicb < p[2]) {
return true
} }
return r, 4, false
} }
return false
// error
return RuneError, 1, false
} }
func decodeRuneInStringInternal(s string) (r rune, size int, short bool) { // FullRuneInString is like FullRune but its input is a string.
func FullRuneInString(s string) bool {
n := len(s) n := len(s)
if n < 1 { if n == 0 {
return RuneError, 0, true return false
}
c0 := s[0]
// 1-byte, 7-bit sequence?
if c0 < tx {
return rune(c0), 1, false
}
// unexpected continuation byte?
if c0 < t2 {
return RuneError, 1, false
}
// need first continuation byte
if n < 2 {
return RuneError, 1, true
}
c1 := s[1]
if c1 < tx || t2 <= c1 {
return RuneError, 1, false
}
// 2-byte, 11-bit sequence?
if c0 < t3 {
r = rune(c0&mask2)<<6 | rune(c1&maskx)
if r <= rune1Max {
return RuneError, 1, false
}
return r, 2, false
}
// need second continuation byte
if n < 3 {
return RuneError, 1, true
}
c2 := s[2]
if c2 < tx || t2 <= c2 {
return RuneError, 1, false
}
// 3-byte, 16-bit sequence?
if c0 < t4 {
r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx)
if r <= rune2Max {
return RuneError, 1, false
}
if surrogateMin <= r && r <= surrogateMax {
return RuneError, 1, false
}
return r, 3, false
}
// need third continuation byte
if n < 4 {
return RuneError, 1, true
} }
c3 := s[3] x := first[s[0]]
if c3 < tx || t2 <= c3 { if n >= int(x&7) {
return RuneError, 1, false return true // ASCII, invalid, or valid.
} }
// Must be short or invalid.
// 4-byte, 21-bit sequence? accept := acceptRanges[x>>4]
if c0 < t5 { if n > 1 {
r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx) if c := s[1]; c < accept.lo || accept.hi < c {
if r <= rune3Max || MaxRune < r { return true
return RuneError, 1, false } else if n > 2 && (s[2] < locb || hicb < s[2]) {
return true
} }
return r, 4, false
} }
return false
// error
return RuneError, 1, false
}
// FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
// An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
func FullRune(p []byte) bool {
_, _, short := decodeRuneInternal(p)
return !short
}
// FullRuneInString is like FullRune but its input is a string.
func FullRuneInString(s string) bool {
_, _, short := decodeRuneInStringInternal(s)
return !short
} }
// DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and
...@@ -220,8 +151,43 @@ func FullRuneInString(s string) bool { ...@@ -220,8 +151,43 @@ func FullRuneInString(s string) bool {
// out of range, or is not the shortest possible UTF-8 encoding for the // out of range, or is not the shortest possible UTF-8 encoding for the
// value. No other validation is performed. // value. No other validation is performed.
func DecodeRune(p []byte) (r rune, size int) { func DecodeRune(p []byte) (r rune, size int) {
r, size, _ = decodeRuneInternal(p) n := len(p)
return if n < 1 {
return RuneError, 0
}
p0 := p[0]
x := first[p0]
if x >= as {
// The following code simulates an additional check for x == xx and
// handling the ASCII and invalid cases accordingly. This mask-and-or
// approach prevents an additional branch.
mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF.
return rune(p[0])&^mask | RuneError&mask, 1
}
sz := x & 7
accept := acceptRanges[x>>4]
if n < int(sz) {
return RuneError, 1
}
b1 := p[1]
if b1 < accept.lo || accept.hi < b1 {
return RuneError, 1
}
if sz == 2 {
return rune(p0&mask2)<<6 | rune(b1&maskx), 2
}
b2 := p[2]
if b2 < locb || hicb < b2 {
return RuneError, 1
}
if sz == 3 {
return rune(p0&mask3)<<12 | rune(b1&maskx)<<6 | rune(b2&maskx), 3
}
b3 := p[3]
if b3 < locb || hicb < b3 {
return RuneError, 1
}
return rune(p0&mask4)<<18 | rune(b1&maskx)<<12 | rune(b2&maskx)<<6 | rune(b3&maskx), 4
} }
// DecodeRuneInString is like DecodeRune but its input is a string. If s is // DecodeRuneInString is like DecodeRune but its input is a string. If s is
...@@ -232,8 +198,43 @@ func DecodeRune(p []byte) (r rune, size int) { ...@@ -232,8 +198,43 @@ func DecodeRune(p []byte) (r rune, size int) {
// out of range, or is not the shortest possible UTF-8 encoding for the // out of range, or is not the shortest possible UTF-8 encoding for the
// value. No other validation is performed. // value. No other validation is performed.
func DecodeRuneInString(s string) (r rune, size int) { func DecodeRuneInString(s string) (r rune, size int) {
r, size, _ = decodeRuneInStringInternal(s) n := len(s)
return if n < 1 {
return RuneError, 0
}
s0 := s[0]
x := first[s0]
if x >= as {
// The following code simulates an additional check for x == xx and
// handling the ASCII and invalid cases accordingly. This mask-and-or
// approach prevents an additional branch.
mask := rune(x) << 31 >> 31 // Create 0x0000 or 0xFFFF.
return rune(s[0])&^mask | RuneError&mask, 1
}
sz := x & 7
accept := acceptRanges[x>>4]
if n < int(sz) {
return RuneError, 1
}
s1 := s[1]
if s1 < accept.lo || accept.hi < s1 {
return RuneError, 1
}
if sz == 2 {
return rune(s0&mask2)<<6 | rune(s1&maskx), 2
}
s2 := s[2]
if s2 < locb || hicb < s2 {
return RuneError, 1
}
if sz == 3 {
return rune(s0&mask3)<<12 | rune(s1&maskx)<<6 | rune(s2&maskx), 3
}
s3 := s[3]
if s3 < locb || hicb < s3 {
return RuneError, 1
}
return rune(s0&mask4)<<18 | rune(s1&maskx)<<12 | rune(s2&maskx)<<6 | rune(s3&maskx), 4
} }
// DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and
...@@ -367,74 +368,142 @@ func EncodeRune(p []byte, r rune) int { ...@@ -367,74 +368,142 @@ func EncodeRune(p []byte, r rune) int {
// RuneCount returns the number of runes in p. Erroneous and short // RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte. // encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int { func RuneCount(p []byte) int {
i := 0 np := len(p)
var n int var n int
for n = 0; i < len(p); n++ { for i := 0; i < np; {
if p[i] < RuneSelf { n++
c := p[i]
if c < RuneSelf {
// ASCII fast path
i++ i++
} else { continue
_, size := DecodeRune(p[i:]) }
i += size x := first[c]
if x == xx {
i++ // invalid.
continue
}
size := int(x & 7)
if i+size > np {
i++ // Short or invalid.
continue
}
accept := acceptRanges[x>>4]
if c := p[i+1]; c < accept.lo || accept.hi < c {
size = 1
} else if size == 2 {
} else if c := p[i+2]; c < locb || hicb < c {
size = 1
} else if size == 3 {
} else if c := p[i+3]; c < locb || hicb < c {
size = 1
} }
i += size
} }
return n return n
} }
// RuneCountInString is like RuneCount but its input is a string. // RuneCountInString is like RuneCount but its input is a string.
func RuneCountInString(s string) (n int) { func RuneCountInString(s string) (n int) {
for i := 0; i < len(s); { ns := len(s)
n++ for i := 0; i < ns; n++ {
if s[i] < RuneSelf { c := s[i]
if c < RuneSelf {
// ASCII fast path
i++ i++
} else { continue
_, size := DecodeRuneInString(s[i:]) }
i += size x := first[c]
if x == xx {
i++ // invalid.
continue
}
size := int(x & 7)
if i+size > ns {
i++ // Short or invalid.
continue
}
accept := acceptRanges[x>>4]
if c := s[i+1]; c < accept.lo || accept.hi < c {
size = 1
} else if size == 2 {
} else if c := s[i+2]; c < locb || hicb < c {
size = 1
} else if size == 3 {
} else if c := s[i+3]; c < locb || hicb < c {
size = 1
} }
i += size
} }
return n return n
} }
// RuneStart reports whether the byte could be the first byte of // RuneStart reports whether the byte could be the first byte of an encoded,
// an encoded rune. Second and subsequent bytes always have the top // possibly invalid rune. Second and subsequent bytes always have the top two
// two bits set to 10. // bits set to 10.
func RuneStart(b byte) bool { return b&0xC0 != 0x80 } func RuneStart(b byte) bool { return b&0xC0 != 0x80 }
// Valid reports whether p consists entirely of valid UTF-8-encoded runes. // Valid reports whether p consists entirely of valid UTF-8-encoded runes.
func Valid(p []byte) bool { func Valid(p []byte) bool {
i := 0 n := len(p)
for i < len(p) { for i := 0; i < n; {
if p[i] < RuneSelf { pi := p[i]
if pi < RuneSelf {
i++ i++
} else { continue
_, size := DecodeRune(p[i:]) }
if size == 1 { x := first[pi]
// All valid runes of size 1 (those if x == xx {
// below RuneSelf) were handled above. return false // Illegal starter byte.
// This must be a RuneError. }
size := int(x & 7)
if i+size > n {
return false // Short or invalid.
}
accept := acceptRanges[x>>4]
if c := p[i+1]; c < accept.lo || accept.hi < c {
return false
} else if size == 2 {
} else if c := p[i+2]; c < locb || hicb < c {
return false
} else if size == 3 {
} else if c := p[i+3]; c < locb || hicb < c {
return false return false
} }
i += size i += size
} }
}
return true return true
} }
// ValidString reports whether s consists entirely of valid UTF-8-encoded runes. // ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
func ValidString(s string) bool { func ValidString(s string) bool {
for i := 0; i < len(s); { n := len(s)
if s[i] < RuneSelf { for i := 0; i < n; {
si := s[i]
if si < RuneSelf {
i++ i++
} else { continue
_, size := DecodeRuneInString(s[i:]) }
if size == 1 { x := first[si]
// All valid runes of size 1 (those if x == xx {
// below RuneSelf) were handled above. return false // Illegal starter byte.
// This must be a RuneError. }
size := int(x & 7)
if i+size > n {
return false // Short or invalid.
}
accept := acceptRanges[x>>4]
if c := s[i+1]; c < accept.lo || accept.hi < c {
return false
} else if size == 2 {
} else if c := s[i+2]; c < locb || hicb < c {
return false
} else if size == 3 {
} else if c := s[i+3]; c < locb || hicb < c {
return false return false
} }
i += size i += size
} }
}
return true return true
} }
......
...@@ -300,6 +300,8 @@ var runecounttests = []RuneCountTest{ ...@@ -300,6 +300,8 @@ var runecounttests = []RuneCountTest{
{"☺☻☹", 3}, {"☺☻☹", 3},
{"1,2,3,4", 7}, {"1,2,3,4", 7},
{"\xe2\x00", 2}, {"\xe2\x00", 2},
{"\xe2\x80", 2},
{"a\xe2\x80", 3},
} }
func TestRuneCount(t *testing.T) { func TestRuneCount(t *testing.T) {
...@@ -352,6 +354,7 @@ var validTests = []ValidTest{ ...@@ -352,6 +354,7 @@ var validTests = []ValidTest{
{"ЖЖ", true}, {"ЖЖ", true},
{"брэд-ЛГТМ", true}, {"брэд-ЛГТМ", true},
{"☺☻☹", true}, {"☺☻☹", true},
{"aa\xe2", false},
{string([]byte{66, 250}), false}, {string([]byte{66, 250}), false},
{string([]byte{66, 250, 67}), false}, {string([]byte{66, 250, 67}), false},
{"a\uFFFDb", true}, {"a\uFFFDb", true},
......
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