github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/collate/build/table.go (about)

     1  // Copyright 2012 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 build
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"reflect"
    11  )
    12  
    13  // table is an intermediate structure that roughly resembles the table in collate.
    14  // It implements the non-exported interface collate.tableInitializer
    15  type table struct {
    16  	index trie // main trie
    17  	root  *trieHandle
    18  
    19  	// expansion info
    20  	expandElem []uint32
    21  
    22  	// contraction info
    23  	contractTries  contractTrieSet
    24  	contractElem   []uint32
    25  	maxContractLen int
    26  	variableTop    uint32
    27  }
    28  
    29  func (t *table) TrieIndex() []uint16 {
    30  	return t.index.index
    31  }
    32  
    33  func (t *table) TrieValues() []uint32 {
    34  	return t.index.values
    35  }
    36  
    37  func (t *table) FirstBlockOffsets() (i, v uint16) {
    38  	return t.root.lookupStart, t.root.valueStart
    39  }
    40  
    41  func (t *table) ExpandElems() []uint32 {
    42  	return t.expandElem
    43  }
    44  
    45  func (t *table) ContractTries() []struct{ l, h, n, i uint8 } {
    46  	return t.contractTries
    47  }
    48  
    49  func (t *table) ContractElems() []uint32 {
    50  	return t.contractElem
    51  }
    52  
    53  func (t *table) MaxContractLen() int {
    54  	return t.maxContractLen
    55  }
    56  
    57  func (t *table) VariableTop() uint32 {
    58  	return t.variableTop
    59  }
    60  
    61  // print writes the table as Go compilable code to w. It prefixes the
    62  // variable names with name. It returns the number of bytes written
    63  // and the size of the resulting table.
    64  func (t *table) fprint(w io.Writer, name string) (n, size int, err error) {
    65  	update := func(nn, sz int, e error) {
    66  		n += nn
    67  		if err == nil {
    68  			err = e
    69  		}
    70  		size += sz
    71  	}
    72  	// Write arrays needed for the structure.
    73  	update(printColElems(w, t.expandElem, name+"ExpandElem"))
    74  	update(printColElems(w, t.contractElem, name+"ContractElem"))
    75  	update(t.index.printArrays(w, name))
    76  	update(t.contractTries.printArray(w, name))
    77  
    78  	nn, e := fmt.Fprintf(w, "// Total size of %sTable is %d bytes\n", name, size)
    79  	update(nn, 0, e)
    80  	return
    81  }
    82  
    83  func (t *table) fprintIndex(w io.Writer, h *trieHandle, id string) (n int, err error) {
    84  	p := func(f string, a ...interface{}) {
    85  		nn, e := fmt.Fprintf(w, f, a...)
    86  		n += nn
    87  		if err == nil {
    88  			err = e
    89  		}
    90  	}
    91  	p("\t{ // %s\n", id)
    92  	p("\t\tlookupOffset: 0x%x,\n", h.lookupStart)
    93  	p("\t\tvaluesOffset: 0x%x,\n", h.valueStart)
    94  	p("\t},\n")
    95  	return
    96  }
    97  
    98  func printColElems(w io.Writer, a []uint32, name string) (n, sz int, err error) {
    99  	p := func(f string, a ...interface{}) {
   100  		nn, e := fmt.Fprintf(w, f, a...)
   101  		n += nn
   102  		if err == nil {
   103  			err = e
   104  		}
   105  	}
   106  	sz = len(a) * int(reflect.TypeOf(uint32(0)).Size())
   107  	p("// %s: %d entries, %d bytes\n", name, len(a), sz)
   108  	p("var %s = [%d]uint32 {", name, len(a))
   109  	for i, c := range a {
   110  		switch {
   111  		case i%64 == 0:
   112  			p("\n\t// Block %d, offset 0x%x\n", i/64, i)
   113  		case (i%64)%6 == 0:
   114  			p("\n\t")
   115  		}
   116  		p("0x%.8X, ", c)
   117  	}
   118  	p("\n}\n\n")
   119  	return
   120  }