trie.go

 1// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
 2
 3// Copyright 2016 The Go Authors. All rights reserved.
 4// Use of this source code is governed by a BSD-style
 5// license that can be found in the LICENSE file.
 6
 7package idna
 8
 9// Sparse block handling code.
10
11type valueRange struct {
12	value  uint16 // header: value:stride
13	lo, hi byte   // header: lo:n
14}
15
16type sparseBlocks struct {
17	values []valueRange
18	offset []uint16
19}
20
21var idnaSparse = sparseBlocks{
22	values: idnaSparseValues[:],
23	offset: idnaSparseOffset[:],
24}
25
26// Don't use newIdnaTrie to avoid unconditional linking in of the table.
27var trie = &idnaTrie{}
28
29// lookup determines the type of block n and looks up the value for b.
30// For n < t.cutoff, the block is a simple lookup table. Otherwise, the block
31// is a list of ranges with an accompanying value. Given a matching range r,
32// the value for b is by r.value + (b - r.lo) * stride.
33func (t *sparseBlocks) lookup(n uint32, b byte) uint16 {
34	offset := t.offset[n]
35	header := t.values[offset]
36	lo := offset + 1
37	hi := lo + uint16(header.lo)
38	for lo < hi {
39		m := lo + (hi-lo)/2
40		r := t.values[m]
41		if r.lo <= b && b <= r.hi {
42			return r.value + uint16(b-r.lo)*header.value
43		}
44		if b < r.lo {
45			hi = m
46		} else {
47			lo = m + 1
48		}
49	}
50	return 0
51}