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.
20 "golang.org/x/text/internal/gen"
21 "golang.org/x/text/internal/ucd"
22 "golang.org/x/text/unicode/rangetable"
25 var versionList = flag.String("versions", "",
26 "list of versions for which to generate RangeTables")
28 const bootstrapMessage = `No versions specified.
29 To bootstrap the code generation, run:
30 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
32 and ensure that the latest versions are included by checking:
33 http://www.unicode.org/Public/`
35 func getVersions() []string {
36 if *versionList == "" {
37 log.Fatal(bootstrapMessage)
40 versions := strings.Split(*versionList, ",")
41 sort.Strings(versions)
43 // Ensure that at least the current version is included.
44 for _, v := range versions {
45 if v == gen.UnicodeVersion() {
50 versions = append(versions, gen.UnicodeVersion())
51 sort.Strings(versions)
58 versions := getVersions()
62 fmt.Fprintf(w, "//go:generate go run gen.go --versions=%s\n\n", strings.Join(versions, ","))
63 fmt.Fprintf(w, "import \"unicode\"\n\n")
65 vstr := func(s string) string { return strings.Replace(s, ".", "_", -1) }
67 fmt.Fprintf(w, "var assigned = map[string]*unicode.RangeTable{\n")
68 for _, v := range versions {
69 fmt.Fprintf(w, "\t%q: assigned%s,\n", v, vstr(v))
71 fmt.Fprintf(w, "}\n\n")
74 for _, v := range versions {
77 r := gen.Open("http://www.unicode.org/Public/", "", v+"/ucd/UnicodeData.txt")
78 ucd.Parse(r, func(p *ucd.Parser) {
79 assigned = append(assigned, p.Rune(0))
82 rt := rangetable.New(assigned...)
83 sz := int(reflect.TypeOf(unicode.RangeTable{}).Size())
84 sz += int(reflect.TypeOf(unicode.Range16{}).Size()) * len(rt.R16)
85 sz += int(reflect.TypeOf(unicode.Range32{}).Size()) * len(rt.R32)
87 fmt.Fprintf(w, "// size %d bytes (%d KiB)\n", sz, sz/1024)
88 fmt.Fprintf(w, "var assigned%s = ", vstr(v))
94 fmt.Fprintf(w, "// Total size %d bytes (%d KiB)\n", size, size/1024)
96 gen.WriteGoFile("tables.go", "rangetable", w.Bytes())
99 func print(w io.Writer, rt *unicode.RangeTable) {
100 fmt.Fprintln(w, "&unicode.RangeTable{")
101 fmt.Fprintln(w, "\tR16: []unicode.Range16{")
102 for _, r := range rt.R16 {
103 fmt.Fprintf(w, "\t\t{%#04x, %#04x, %d},\n", r.Lo, r.Hi, r.Stride)
105 fmt.Fprintln(w, "\t},")
106 fmt.Fprintln(w, "\tR32: []unicode.Range32{")
107 for _, r := range rt.R32 {
108 fmt.Fprintf(w, "\t\t{%#08x, %#08x, %d},\n", r.Lo, r.Hi, r.Stride)
110 fmt.Fprintln(w, "\t},")
111 fmt.Fprintf(w, "\tLatinOffset: %d,\n", rt.LatinOffset)
112 fmt.Fprintf(w, "}\n\n")