gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/net/idna/trie.go (about)

     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  
     7  package idna
     8  
     9  // appendMapping appends the mapping for the respective rune. isMapped must be
    10  // true. A mapping is a categorization of a rune as defined in UTS #46.
    11  func (c info) appendMapping(b []byte, s string) []byte {
    12  	index := int(c >> indexShift)
    13  	if c&xorBit == 0 {
    14  		s := mappings[index:]
    15  		return append(b, s[1:s[0]+1]...)
    16  	}
    17  	b = append(b, s...)
    18  	if c&inlineXOR == inlineXOR {
    19  		// TODO: support and handle two-byte inline masks
    20  		b[len(b)-1] ^= byte(index)
    21  	} else {
    22  		for p := len(b) - int(xorData[index]); p < len(b); p++ {
    23  			index++
    24  			b[p] ^= xorData[index]
    25  		}
    26  	}
    27  	return b
    28  }
    29  
    30  // Sparse block handling code.
    31  
    32  type valueRange struct {
    33  	value  uint16 // header: value:stride
    34  	lo, hi byte   // header: lo:n
    35  }
    36  
    37  type sparseBlocks struct {
    38  	values []valueRange
    39  	offset []uint16
    40  }
    41  
    42  var idnaSparse = sparseBlocks{
    43  	values: idnaSparseValues[:],
    44  	offset: idnaSparseOffset[:],
    45  }
    46  
    47  // Don't use newIdnaTrie to avoid unconditional linking in of the table.
    48  var trie = &idnaTrie{}
    49  
    50  // lookup determines the type of block n and looks up the value for b.
    51  // For n < t.cutoff, the block is a simple lookup table. Otherwise, the block
    52  // is a list of ranges with an accompanying value. Given a matching range r,
    53  // the value for b is by r.value + (b - r.lo) * stride.
    54  func (t *sparseBlocks) lookup(n uint32, b byte) uint16 {
    55  	offset := t.offset[n]
    56  	header := t.values[offset]
    57  	lo := offset + 1
    58  	hi := lo + uint16(header.lo)
    59  	for lo < hi {
    60  		m := lo + (hi-lo)/2
    61  		r := t.values[m]
    62  		if r.lo <= b && b <= r.hi {
    63  			return r.value + uint16(b-r.lo)*header.value
    64  		}
    65  		if b < r.lo {
    66  			hi = m
    67  		} else {
    68  			lo = m + 1
    69  		}
    70  	}
    71  	return 0
    72  }