github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/text/unicode/norm/trie.go (about)

     1  // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
     2  
     3  // Copyright 2011 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  
     7  package norm
     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 nfcSparse = sparseBlocks{
    20  	values: nfcSparseValues[:],
    21  	offset: nfcSparseOffset[:],
    22  }
    23  
    24  var nfkcSparse = sparseBlocks{
    25  	values: nfkcSparseValues[:],
    26  	offset: nfkcSparseOffset[:],
    27  }
    28  
    29  var (
    30  	nfcData  = newNfcTrie(0)
    31  	nfkcData = newNfkcTrie(0)
    32  )
    33  
    34  // lookupValue determines the type of block n and looks up the value for b.
    35  // For n < t.cutoff, the block is a simple lookup table. Otherwise, the block
    36  // is a list of ranges with an accompanying value. Given a matching range r,
    37  // the value for b is by r.value + (b - r.lo) * stride.
    38  func (t *sparseBlocks) lookup(n uint32, b byte) uint16 {
    39  	offset := t.offset[n]
    40  	header := t.values[offset]
    41  	lo := offset + 1
    42  	hi := lo + uint16(header.lo)
    43  	for lo < hi {
    44  		m := lo + (hi-lo)/2
    45  		r := t.values[m]
    46  		if r.lo <= b && b <= r.hi {
    47  			return r.value + uint16(b-r.lo)*header.value
    48  		}
    49  		if b < r.lo {
    50  			hi = m
    51  		} else {
    52  			lo = m + 1
    53  		}
    54  	}
    55  	return 0
    56  }