Debian Codegraph @

internal/export/idna/trie.go

raw
1 // Copyright 2016 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.
4
5 package idna
6
7 // Sparse block handling code.
8
9 type valueRange struct {
10 value uint16 // header: value:stride
11 lo, hi byte // header: lo:n
12 }
13
14 type sparseBlocks struct {
15 values []valueRange
16 offset []uint16
17 }
18
19 var idnaSparse = sparseBlocks{
20 values: idnaSparseValues[:],
21 offset: idnaSparseOffset[:],
22 }
23
24 // Don't use newIdnaTrie to avoid unconditional linking in of the table.
25 var trie = &idnaTrie{}
26
27 // lookup determines the type of block n and looks up the value for b.
28 // For n < t.cutoff, the block is a simple lookup table. Otherwise, the block
29 // is a list of ranges with an accompanying value. Given a matching range r,
30 // the value for b is by r.value + (b - r.lo) * stride.
31 func (t *sparseBlocks) lookup(n uint32, b byte) uint16 {
32 offset := t.offset[n]
33 header := t.values[offset]
34 lo := offset + 1
35 hi := lo + uint16(header.lo)
36 for lo < hi {
37 m := lo + (hi-lo)/2
38 r := t.values[m]
39 if r.lo <= b && b <= r.hi {
40 return r.value + uint16(b-r.lo)*header.value
41 }
42 if b < r.lo {
43 hi = m
44 } else {
45 lo = m + 1
46 }
47 }
48 return 0
49 }