1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 type valueRange struct {
8 value uint16 // header: value:stride
9 lo, hi byte // header: lo:n
17 cutoff uint8 // indices >= cutoff are sparse
20 // lookupValue determines the type of block n and looks up the value for b.
21 // For n < t.cutoff, the block is a simple lookup table. Otherwise, the block
22 // is a list of ranges with an accompanying value. Given a matching range r,
23 // the value for b is by r.value + (b - r.lo) * stride.
24 func (t *trie) lookupValue(n uint8, b byte) uint16 {
26 return t.values[uint16(n)<<6+uint16(b&maskx)]
28 offset := t.sparseOffset[n-t.cutoff]
29 header := t.sparse[offset]
31 hi := lo + uint16(header.lo)
35 if r.lo <= b && b <= r.hi {
36 return r.value + uint16(b-r.lo)*header.value
48 t1 = 0x00 // 0000 0000
49 tx = 0x80 // 1000 0000
50 t2 = 0xC0 // 1100 0000
51 t3 = 0xE0 // 1110 0000
52 t4 = 0xF0 // 1111 0000
53 t5 = 0xF8 // 1111 1000
54 t6 = 0xFC // 1111 1100
55 te = 0xFE // 1111 1110
57 maskx = 0x3F // 0011 1111
58 mask2 = 0x1F // 0001 1111
59 mask3 = 0x0F // 0000 1111
60 mask4 = 0x07 // 0000 0111
63 // lookup returns the trie value for the first UTF-8 encoding in s and
64 // the width in bytes of this encoding. The size will be 0 if s does not
65 // hold enough bytes to complete the encoding. len(s) must be greater than 0.
66 func (t *trie) lookup(s []byte) (v uint16, sz int) {
70 return t.values[c0], 1
79 if c1 < tx || t2 <= c1 {
82 return t.lookupValue(i, c1), 2
89 if c1 < tx || t2 <= c1 {
92 o := uint16(i)<<6 + uint16(c1)&maskx
95 if c2 < tx || t2 <= c2 {
98 return t.lookupValue(i, c2), 3
105 if c1 < tx || t2 <= c1 {
108 o := uint16(i)<<6 + uint16(c1)&maskx
111 if c2 < tx || t2 <= c2 {
114 o = uint16(i)<<6 + uint16(c2)&maskx
117 if c3 < tx || t2 <= c3 {
120 return t.lookupValue(i, c3), 4
126 // lookupString returns the trie value for the first UTF-8 encoding in s and
127 // the width in bytes of this encoding. The size will be 0 if s does not
128 // hold enough bytes to complete the encoding. len(s) must be greater than 0.
129 func (t *trie) lookupString(s string) (v uint16, sz int) {
133 return t.values[c0], 1
142 if c1 < tx || t2 <= c1 {
145 return t.lookupValue(i, c1), 2
152 if c1 < tx || t2 <= c1 {
155 o := uint16(i)<<6 + uint16(c1)&maskx
158 if c2 < tx || t2 <= c2 {
161 return t.lookupValue(i, c2), 3
168 if c1 < tx || t2 <= c1 {
171 o := uint16(i)<<6 + uint16(c1)&maskx
174 if c2 < tx || t2 <= c2 {
177 o = uint16(i)<<6 + uint16(c2)&maskx
180 if c3 < tx || t2 <= c3 {
183 return t.lookupValue(i, c3), 4
189 // lookupUnsafe returns the trie value for the first UTF-8 encoding in s.
190 // s must hold a full encoding.
191 func (t *trie) lookupUnsafe(s []byte) uint16 {
201 return t.lookupValue(i, s[1])
203 i = t.index[uint16(i)<<6+uint16(s[1])&maskx]
205 return t.lookupValue(i, s[2])
207 i = t.index[uint16(i)<<6+uint16(s[2])&maskx]
209 return t.lookupValue(i, s[3])
214 // lookupStringUnsafe returns the trie value for the first UTF-8 encoding in s.
215 // s must hold a full encoding.
216 func (t *trie) lookupStringUnsafe(s string) uint16 {
226 return t.lookupValue(i, s[1])
228 i = t.index[uint16(i)<<6+uint16(s[1])&maskx]
230 return t.lookupValue(i, s[2])
232 i = t.index[uint16(i)<<6+uint16(s[2])&maskx]
234 return t.lookupValue(i, s[3])