Debian Codegraph @

internal/encoder/string_scan_table.go

raw
1 package encoder
2
3 // nibbleTables are two tables of 16 bytes for the scan of a string by SIMD: a byte b may need an escape if
4 // Lo[b&0xf] & Hi[b>>4] is not zero. The bytes of a high nibble which need an escape are a set of low
5 // nibbles; every distinct set gets a bit, which Hi has for the high nibbles of the set and Lo has for the
6 // low nibbles in the set. Two lookups of a byte per lane replace a compare per character to escape.
7 type nibbleTables struct {
8 Lo [16]byte
9 Hi [16]byte
10 }
11
12 // newNibbleTables returns the tables for the bytes which need an escape. It panics if more than 8 distinct
13 // sets of low nibbles are needed, which the tables of the encoder never need.
14 func newNibbleTables(need *[256]bool) nibbleTables {
15 var t nibbleTables
16 bits := map[uint16]byte{} // the set of the low nibbles, as a bit set -> the bit of the set
17 next := byte(1)
18 for hi := 0; hi < 16; hi++ {
19 var set uint16
20 for lo := 0; lo < 16; lo++ {
21 if need[hi<<4|lo] {
22 set |= 1 << lo
23 }
24 }
25 if set == 0 {
26 continue
27 }
28 bit, ok := bits[set]
29 if !ok {
30 if next == 0 {
31 panic("too many sets of the low nibbles for the tables of the scan")
32 }
33 bit = next
34 next <<= 1
35 bits[set] = bit
36 for lo := 0; lo < 16; lo++ {
37 if set&(1<<lo) != 0 {
38 t.Lo[lo] |= bit
39 }
40 }
41 }
42 t.Hi[hi] = bit
43 }
44 return t
45 }
46
47 // utf8Tables are the tables of the validation of UTF-8 by SIMD, by the lookups of "Validating UTF-8 In Less Than
48 // One Instruction Per Byte" ( John Keiser, Daniel Lemire, 2020 ): the errors of two bytes in a row are found by
49 // three lookups of 16 bytes, by the high and the low nibble of the first byte and the high nibble of the second
50 // one; each bit is a kind of error, which the three agree on only if the two bytes are that error. What they miss,
51 // that the third or the fourth byte of a character is not a continuation byte or the reverse, is found from the
52 // bytes two and three before.
53 type utf8Tables struct {
54 Byte1High [16]byte
55 Byte1Low [16]byte
56 Byte2High [16]byte
57 }
58
59 // The kinds of the errors of two bytes in a row ( see utf8Tables ).
60 const (
61 utf8TooShort = 1 << 0 // 11______ 0_______, 11______ 11______: a leading byte without its continuation
62 utf8TooLong = 1 << 1 // 0_______ 10______: a continuation byte without its leading byte
63 utf8Overlong3 = 1 << 2 // 11100000 100_____
64 utf8TooLarge = 1 << 3 // 11110100 1001____, 11110100 101_____, 11110101 1001____, ...: above U+10FFFF
65 utf8Surrogate = 1 << 4 // 11101101 101_____: U+D800 to U+DFFF
66 utf8Overlong2 = 1 << 5 // 1100000_ 10______
67 utf8TooLarge1000 = 1 << 6 // 11110101 1000____, ...: above U+10FFFF
68 utf8Overlong4 = 1 << 6 // 11110000 1000____
69 utf8TwoConts = 1 << 7 // 10______ 10______: a continuation byte, unless it is the third or the fourth
70 utf8Carry = utf8TooShort | utf8TooLong | utf8TwoConts
71 )
72
73 var utf8ValidationTables = utf8Tables{
74 Byte1High: [16]byte{
75 // 0_______: ASCII
76 utf8TooLong, utf8TooLong, utf8TooLong, utf8TooLong, utf8TooLong, utf8TooLong, utf8TooLong, utf8TooLong,
77 // 10______: a continuation byte
78 utf8TwoConts, utf8TwoConts, utf8TwoConts, utf8TwoConts,
79 // 1100____, 1101____: the leading byte of two bytes
80 utf8TooShort | utf8Overlong2,
81 utf8TooShort,
82 // 1110____: the leading byte of three bytes
83 utf8TooShort | utf8Overlong3 | utf8Surrogate,
84 // 1111____: the leading byte of four bytes
85 utf8TooShort | utf8TooLarge | utf8TooLarge1000 | utf8Overlong4,
86 },
87 Byte1Low: [16]byte{
88 utf8Carry | utf8Overlong3 | utf8Overlong2 | utf8Overlong4, // ____0000
89 utf8Carry | utf8Overlong2, // ____0001
90 utf8Carry, // ____0010
91 utf8Carry, // ____0011
92 utf8Carry | utf8TooLarge, // ____0100
93 utf8Carry | utf8TooLarge | utf8TooLarge1000, // ____0101
94 utf8Carry | utf8TooLarge | utf8TooLarge1000,
95 utf8Carry | utf8TooLarge | utf8TooLarge1000,
96 utf8Carry | utf8TooLarge | utf8TooLarge1000,
97 utf8Carry | utf8TooLarge | utf8TooLarge1000,
98 utf8Carry | utf8TooLarge | utf8TooLarge1000,
99 utf8Carry | utf8TooLarge | utf8TooLarge1000,
100 utf8Carry | utf8TooLarge | utf8TooLarge1000,
101 utf8Carry | utf8TooLarge | utf8TooLarge1000 | utf8Surrogate, // ____1101
102 utf8Carry | utf8TooLarge | utf8TooLarge1000,
103 utf8Carry | utf8TooLarge | utf8TooLarge1000,
104 },
105 Byte2High: [16]byte{
106 // ________ 0_______: ASCII
107 utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort,
108 // ________ 1000____
109 utf8TooLong | utf8Overlong2 | utf8TwoConts | utf8Overlong3 | utf8TooLarge1000 | utf8Overlong4,
110 // ________ 1001____
111 utf8TooLong | utf8Overlong2 | utf8TwoConts | utf8Overlong3 | utf8TooLarge,
112 // ________ 101_____
113 utf8TooLong | utf8Overlong2 | utf8TwoConts | utf8Surrogate | utf8TooLarge,
114 utf8TooLong | utf8Overlong2 | utf8TwoConts | utf8Surrogate | utf8TooLarge,
115 // ________ 11______: a leading byte
116 utf8TooShort, utf8TooShort, utf8TooShort, utf8TooShort,
117 },
118 }