1 // Copyright 2012 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.
5 // The trie in this file is used to associate the first full character in an
6 // UTF-8 string to a collation element. All but the last byte in a UTF-8 byte
7 // sequence are used to lookup offsets in the index table to be used for the
8 // next byte. The last byte is used to index into a table of collation elements.
9 // For a full description, see go.text/collate/build/trie.go.
16 Index0 []uint16 // index for first byte (0xC0-0xFF)
17 Values0 []uint32 // index for first byte (0x00-0x7F)
23 t1 = 0x00 // 0000 0000
24 tx = 0x80 // 1000 0000
25 t2 = 0xC0 // 1100 0000
26 t3 = 0xE0 // 1110 0000
27 t4 = 0xF0 // 1111 0000
28 t5 = 0xF8 // 1111 1000
29 t6 = 0xFC // 1111 1100
30 te = 0xFE // 1111 1110
33 func (t *Trie) lookupValue(n uint16, b byte) Elem {
34 return Elem(t.Values[int(n)<<6+int(b)])
37 // lookup returns the trie value for the first UTF-8 encoding in s and
38 // the width in bytes of this encoding. The size will be 0 if s does not
39 // hold enough bytes to complete the encoding. len(s) must be greater than 0.
40 func (t *Trie) lookup(s []byte) (v Elem, sz int) {
44 return Elem(t.Values0[c0]), 1
53 if c1 < tx || t2 <= c1 {
56 return t.lookupValue(i, c1), 2
63 if c1 < tx || t2 <= c1 {
66 o := int(i)<<6 + int(c1)
69 if c2 < tx || t2 <= c2 {
72 return t.lookupValue(i, c2), 3
79 if c1 < tx || t2 <= c1 {
82 o := int(i)<<6 + int(c1)
85 if c2 < tx || t2 <= c2 {
88 o = int(i)<<6 + int(c2)
91 if c3 < tx || t2 <= c3 {
94 return t.lookupValue(i, c3), 4
100 // The body of lookupString is a verbatim copy of that of lookup.
101 func (t *Trie) lookupString(s string) (v Elem, sz int) {
105 return Elem(t.Values0[c0]), 1
114 if c1 < tx || t2 <= c1 {
117 return t.lookupValue(i, c1), 2
124 if c1 < tx || t2 <= c1 {
127 o := int(i)<<6 + int(c1)
130 if c2 < tx || t2 <= c2 {
133 return t.lookupValue(i, c2), 3
140 if c1 < tx || t2 <= c1 {
143 o := int(i)<<6 + int(c1)
146 if c2 < tx || t2 <= c2 {
149 o = int(i)<<6 + int(c2)
152 if c3 < tx || t2 <= c3 {
155 return t.lookupValue(i, c3), 4