table.go 1.9 KB

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