trie.go 1.3 KB

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