gen.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2015 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. // +build ignore
  5. package main
  6. import (
  7. "bytes"
  8. "flag"
  9. "fmt"
  10. "io"
  11. "log"
  12. "reflect"
  13. "sort"
  14. "strings"
  15. "unicode"
  16. "golang.org/x/text/internal/gen"
  17. "golang.org/x/text/internal/ucd"
  18. "golang.org/x/text/unicode/rangetable"
  19. )
  20. var versionList = flag.String("versions", "",
  21. "list of versions for which to generate RangeTables")
  22. const bootstrapMessage = `No versions specified.
  23. To bootstrap the code generation, run:
  24. go run gen.go --versions=4.1.0,5.0.0,6.0.0,6.1.0,6.2.0,6.3.0,7.0.0
  25. and ensure that the latest versions are included by checking:
  26. http://www.unicode.org/Public/`
  27. func getVersions() []string {
  28. if *versionList == "" {
  29. log.Fatal(bootstrapMessage)
  30. }
  31. versions := strings.Split(*versionList, ",")
  32. sort.Strings(versions)
  33. // Ensure that at least the current version is included.
  34. for _, v := range versions {
  35. if v == gen.UnicodeVersion() {
  36. return versions
  37. }
  38. }
  39. versions = append(versions, gen.UnicodeVersion())
  40. sort.Strings(versions)
  41. return versions
  42. }
  43. func main() {
  44. gen.Init()
  45. versions := getVersions()
  46. w := &bytes.Buffer{}
  47. fmt.Fprintf(w, "//go:generate go run gen.go --versions=%s\n\n", strings.Join(versions, ","))
  48. fmt.Fprintf(w, "import \"unicode\"\n\n")
  49. vstr := func(s string) string { return strings.Replace(s, ".", "_", -1) }
  50. fmt.Fprintf(w, "var assigned = map[string]*unicode.RangeTable{\n")
  51. for _, v := range versions {
  52. fmt.Fprintf(w, "\t%q: assigned%s,\n", v, vstr(v))
  53. }
  54. fmt.Fprintf(w, "}\n\n")
  55. var size int
  56. for _, v := range versions {
  57. assigned := []rune{}
  58. r := gen.Open("http://www.unicode.org/Public/", "", v+"/ucd/UnicodeData.txt")
  59. ucd.Parse(r, func(p *ucd.Parser) {
  60. assigned = append(assigned, p.Rune(0))
  61. })
  62. rt := rangetable.New(assigned...)
  63. sz := int(reflect.TypeOf(unicode.RangeTable{}).Size())
  64. sz += int(reflect.TypeOf(unicode.Range16{}).Size()) * len(rt.R16)
  65. sz += int(reflect.TypeOf(unicode.Range32{}).Size()) * len(rt.R32)
  66. fmt.Fprintf(w, "// size %d bytes (%d KiB)\n", sz, sz/1024)
  67. fmt.Fprintf(w, "var assigned%s = ", vstr(v))
  68. print(w, rt)
  69. size += sz
  70. }
  71. fmt.Fprintf(w, "// Total size %d bytes (%d KiB)\n", size, size/1024)
  72. gen.WriteGoFile("tables.go", "rangetable", w.Bytes())
  73. }
  74. func print(w io.Writer, rt *unicode.RangeTable) {
  75. fmt.Fprintln(w, "&unicode.RangeTable{")
  76. fmt.Fprintln(w, "\tR16: []unicode.Range16{")
  77. for _, r := range rt.R16 {
  78. fmt.Fprintf(w, "\t\t{%#04x, %#04x, %d},\n", r.Lo, r.Hi, r.Stride)
  79. }
  80. fmt.Fprintln(w, "\t},")
  81. fmt.Fprintln(w, "\tR32: []unicode.Range32{")
  82. for _, r := range rt.R32 {
  83. fmt.Fprintf(w, "\t\t{%#08x, %#08x, %d},\n", r.Lo, r.Hi, r.Stride)
  84. }
  85. fmt.Fprintln(w, "\t},")
  86. fmt.Fprintf(w, "\tLatinOffset: %d,\n", rt.LatinOffset)
  87. fmt.Fprintf(w, "}\n\n")
  88. }