]> Cypherpunks.ru repositories - gostls13.git/commitdiff
unicode/utf8: table-based algorithm for decoding
authorMarcel van Lohuizen <mpvl@golang.org>
Mon, 16 Nov 2015 13:00:31 +0000 (14:00 +0100)
committerMarcel van Lohuizen <mpvl@golang.org>
Mon, 16 Nov 2015 21:16:51 +0000 (21:16 +0000)
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/16940
Reviewed-by: Russ Cox <rsc@golang.org>
src/unicode/utf8/utf8.go
src/unicode/utf8/utf8_test.go

index 5d29ec080e789c9b407bd2fd7b499e93a20232d9..bcb0de402e61671190c54705bf720e152bf301f0 100644 (file)
@@ -40,175 +40,106 @@ const (
        rune1Max = 1<<7 - 1
        rune2Max = 1<<11 - 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?
-       if c0 < t2 {
-               return RuneError, 1, false
-       }
-
-       // need first continuation byte
-       if n < 2 {
-               return RuneError, 1, true
-       }
-       c1 := p[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 := p[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 := p[3]
-       if c3 < tx || t2 <= c3 {
-               return RuneError, 1, false
-       }
 
-       // 4-byte, 21-bit sequence?
-       if c0 < t5 {
-               r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
-               if r <= rune3Max || MaxRune < r {
-                       return RuneError, 1, false
-               }
-               return r, 4, false
-       }
+       // The default lowest and highest continuation byte.
+       locb = 0x80 // 1000 0000
+       hicb = 0xBF // 1011 1111
+
+       // These names of these constants are chosen to give nice alignment in the
+       // table below. The first nibble is an index into acceptRanges or F for
+       // special one-byte cases. The second nibble is the Rune length or the
+       // Status for the special one-byte case.
+       xx = 0xF1 // invalid: size 1
+       as = 0xF0 // ASCII: size 1
+       s1 = 0x02 // accept 0, size 2
+       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
+)
 
-       // error
-       return RuneError, 1, false
+// first is information about the first byte in a UTF-8 sequence.
+var first = [256]uint8{
+       //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
+       as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
+       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
+       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
 }
 
-func decodeRuneInStringInternal(s string) (r rune, size int, short bool) {
-       n := len(s)
-       if n < 1 {
-               return RuneError, 0, true
-       }
-       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]
-       if c3 < tx || t2 <= c3 {
-               return RuneError, 1, false
-       }
-
-       // 4-byte, 21-bit sequence?
-       if c0 < t5 {
-               r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
-               if r <= rune3Max || MaxRune < r {
-                       return RuneError, 1, false
-               }
-               return r, 4, false
-       }
+// acceptRange gives the range of valid values for the second byte in a UTF-8
+// sequence.
+type acceptRange struct {
+       lo uint8 // lowest value for second byte.
+       hi uint8 // highest value for second byte.
+}
 
-       // error
-       return RuneError, 1, false
+var acceptRanges = [...]acceptRange{
+       0: {locb, hicb},
+       1: {0xA0, hicb},
+       2: {locb, 0x9F},
+       3: {0x90, hicb},
+       4: {locb, 0x8F},
 }
 
 // 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
+       n := len(p)
+       if n == 0 {
+               return false
+       }
+       x := first[p[0]]
+       if n >= int(x&7) {
+               return true // ASCII, invalid or valid.
+       }
+       // Must be short or invalid.
+       accept := acceptRanges[x>>4]
+       if n > 1 {
+               if c := p[1]; c < accept.lo || accept.hi < c {
+                       return true
+               } else if n > 2 && (p[2] < locb || hicb < p[2]) {
+                       return true
+               }
+       }
+       return false
 }
 
 // FullRuneInString is like FullRune but its input is a string.
 func FullRuneInString(s string) bool {
-       _, _, short := decodeRuneInStringInternal(s)
-       return !short
+       n := len(s)
+       if n == 0 {
+               return false
+       }
+       x := first[s[0]]
+       if n >= int(x&7) {
+               return true // ASCII, invalid, or valid.
+       }
+       // Must be short or invalid.
+       accept := acceptRanges[x>>4]
+       if n > 1 {
+               if c := s[1]; c < accept.lo || accept.hi < c {
+                       return true
+               } else if n > 2 && (s[2] < locb || hicb < s[2]) {
+                       return true
+               }
+       }
+       return false
 }
 
 // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and
@@ -220,8 +151,43 @@ func FullRuneInString(s string) bool {
 // out of range, or is not the shortest possible UTF-8 encoding for the
 // value. No other validation is performed.
 func DecodeRune(p []byte) (r rune, size int) {
-       r, size, _ = decodeRuneInternal(p)
-       return
+       n := len(p)
+       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
@@ -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
 // value. No other validation is performed.
 func DecodeRuneInString(s string) (r rune, size int) {
-       r, size, _ = decodeRuneInStringInternal(s)
-       return
+       n := len(s)
+       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
@@ -367,73 +368,141 @@ func EncodeRune(p []byte, r rune) int {
 // RuneCount returns the number of runes in p.  Erroneous and short
 // encodings are treated as single runes of width 1 byte.
 func RuneCount(p []byte) int {
-       i := 0
+       np := len(p)
        var n int
-       for n = 0; i < len(p); n++ {
-               if p[i] < RuneSelf {
+       for i := 0; i < np; {
+               n++
+               c := p[i]
+               if c < RuneSelf {
+                       // ASCII fast path
                        i++
-               } else {
-                       _, size := DecodeRune(p[i:])
-                       i += size
+                       continue
+               }
+               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
 }
 
 // RuneCountInString is like RuneCount but its input is a string.
 func RuneCountInString(s string) (n int) {
-       for i := 0; i < len(s); {
-               n++
-               if s[i] < RuneSelf {
+       ns := len(s)
+       for i := 0; i < ns; n++ {
+               c := s[i]
+               if c < RuneSelf {
+                       // ASCII fast path
                        i++
-               } else {
-                       _, size := DecodeRuneInString(s[i:])
-                       i += size
+                       continue
+               }
+               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
 }
 
-// RuneStart reports whether the byte could be the first byte of
-// an encoded rune.  Second and subsequent bytes always have the top
-// two bits set to 10.
+// RuneStart reports whether the byte could be the first byte of an encoded,
+// possibly invalid rune.  Second and subsequent bytes always have the top two
+// bits set to 10.
 func RuneStart(b byte) bool { return b&0xC0 != 0x80 }
 
 // Valid reports whether p consists entirely of valid UTF-8-encoded runes.
 func Valid(p []byte) bool {
-       i := 0
-       for i < len(p) {
-               if p[i] < RuneSelf {
+       n := len(p)
+       for i := 0; i < n; {
+               pi := p[i]
+               if pi < RuneSelf {
                        i++
-               } else {
-                       _, size := DecodeRune(p[i:])
-                       if size == 1 {
-                               // All valid runes of size 1 (those
-                               // below RuneSelf) were handled above.
-                               // This must be a RuneError.
-                               return false
-                       }
-                       i += size
+                       continue
+               }
+               x := first[pi]
+               if x == xx {
+                       return false // Illegal starter byte.
                }
+               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
+               }
+               i += size
        }
        return true
 }
 
 // ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
 func ValidString(s string) bool {
-       for i := 0; i < len(s); {
-               if s[i] < RuneSelf {
+       n := len(s)
+       for i := 0; i < n; {
+               si := s[i]
+               if si < RuneSelf {
                        i++
-               } else {
-                       _, size := DecodeRuneInString(s[i:])
-                       if size == 1 {
-                               // All valid runes of size 1 (those
-                               // below RuneSelf) were handled above.
-                               // This must be a RuneError.
-                               return false
-                       }
-                       i += size
+                       continue
+               }
+               x := first[si]
+               if x == xx {
+                       return false // Illegal starter byte.
+               }
+               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
                }
+               i += size
        }
        return true
 }
index 36d3959eee909718ca7e5b1823b3fe7748ac1046..60ce362f09aad14f0a37a94509192ee530f1c09e 100644 (file)
@@ -300,6 +300,8 @@ var runecounttests = []RuneCountTest{
        {"☺☻☹", 3},
        {"1,2,3,4", 7},
        {"\xe2\x00", 2},
+       {"\xe2\x80", 2},
+       {"a\xe2\x80", 3},
 }
 
 func TestRuneCount(t *testing.T) {
@@ -352,6 +354,7 @@ var validTests = []ValidTest{
        {"ЖЖ", true},
        {"брэд-ЛГТМ", true},
        {"☺☻☹", true},
+       {"aa\xe2", false},
        {string([]byte{66, 250}), false},
        {string([]byte{66, 250, 67}), false},
        {"a\uFFFDb", true},