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
5package build
6
7import (
8	"fmt"
9	"io"
10	"reflect"
11
12	"golang.org/x/text/internal/colltab"
13)
14
15// table is an intermediate structure that roughly resembles the table in collate.
16type table struct {
17	colltab.Table
18	trie trie
19	root *trieHandle
20}
21
22// print writes the table as Go compilable code to w. It prefixes the
23// variable names with name. It returns the number of bytes written
24// and the size of the resulting table.
25func (t *table) fprint(w io.Writer, name string) (n, size int, err error) {
26	update := func(nn, sz int, e error) {
27		n += nn
28		if err == nil {
29			err = e
30		}
31		size += sz
32	}
33	// Write arrays needed for the structure.
34	update(printColElems(w, t.ExpandElem, name+"ExpandElem"))
35	update(printColElems(w, t.ContractElem, name+"ContractElem"))
36	update(t.trie.printArrays(w, name))
37	update(printArray(t.ContractTries, w, name))
38
39	nn, e := fmt.Fprintf(w, "// Total size of %sTable is %d bytes\n", name, size)
40	update(nn, 0, e)
41	return
42}
43
44func (t *table) fprintIndex(w io.Writer, h *trieHandle, id string) (n int, err error) {
45	p := func(f string, a ...interface{}) {
46		nn, e := fmt.Fprintf(w, f, a...)
47		n += nn
48		if err == nil {
49			err = e
50		}
51	}
52	p("\t{ // %s\n", id)
53	p("\t\tlookupOffset: 0x%x,\n", h.lookupStart)
54	p("\t\tvaluesOffset: 0x%x,\n", h.valueStart)
55	p("\t},\n")
56	return
57}
58
59func printColElems(w io.Writer, a []uint32, name string) (n, sz int, err error) {
60	p := func(f string, a ...interface{}) {
61		nn, e := fmt.Fprintf(w, f, a...)
62		n += nn
63		if err == nil {
64			err = e
65		}
66	}
67	sz = len(a) * int(reflect.TypeOf(uint32(0)).Size())
68	p("// %s: %d entries, %d bytes\n", name, len(a), sz)
69	p("var %s = [%d]uint32 {", name, len(a))
70	for i, c := range a {
71		switch {
72		case i%64 == 0:
73			p("\n\t// Block %d, offset 0x%x\n", i/64, i)
74		case (i%64)%6 == 0:
75			p("\n\t")
76		}
77		p("0x%.8X, ", c)
78	}
79	p("\n}\n\n")
80	return
81}
82