gen.go 2.9 KB

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