gen.go 2.9 KB

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