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.
19 "golang.org/x/text/collate"
20 "golang.org/x/text/internal/gen"
21 "golang.org/x/text/internal/ucd"
22 "golang.org/x/text/language"
23 "golang.org/x/text/unicode/rangetable"
26 var versionList = flag.String("versions", "",
27 "list of versions for which to generate RangeTables")
29 const bootstrapMessage = `No versions specified.
30 To bootstrap the code generation, run:
31 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
33 and ensure that the latest versions are included by checking:
34 https://www.unicode.org/Public/`
36 func getVersions() []string {
37 if *versionList == "" {
38 log.Fatal(bootstrapMessage)
41 c := collate.New(language.Und, collate.Numeric)
42 versions := strings.Split(*versionList, ",")
43 c.SortStrings(versions)
45 // Ensure that at least the current version is included.
46 for _, v := range versions {
47 if v == gen.UnicodeVersion() {
52 versions = append(versions, gen.UnicodeVersion())
53 c.SortStrings(versions)
60 versions := getVersions()
64 fmt.Fprintf(w, "//go:generate go run gen.go --versions=%s\n\n", strings.Join(versions, ","))
65 fmt.Fprintf(w, "import \"unicode\"\n\n")
67 vstr := func(s string) string { return strings.Replace(s, ".", "_", -1) }
69 fmt.Fprintf(w, "var assigned = map[string]*unicode.RangeTable{\n")
70 for _, v := range versions {
71 fmt.Fprintf(w, "\t%q: assigned%s,\n", v, vstr(v))
73 fmt.Fprintf(w, "}\n\n")
76 for _, v := range versions {
79 r := gen.Open("https://www.unicode.org/Public/", "", v+"/ucd/UnicodeData.txt")
80 ucd.Parse(r, func(p *ucd.Parser) {
81 assigned = append(assigned, p.Rune(0))
84 rt := rangetable.New(assigned...)
85 sz := int(reflect.TypeOf(unicode.RangeTable{}).Size())
86 sz += int(reflect.TypeOf(unicode.Range16{}).Size()) * len(rt.R16)
87 sz += int(reflect.TypeOf(unicode.Range32{}).Size()) * len(rt.R32)
89 fmt.Fprintf(w, "// size %d bytes (%d KiB)\n", sz, sz/1024)
90 fmt.Fprintf(w, "var assigned%s = ", vstr(v))
96 fmt.Fprintf(w, "// Total size %d bytes (%d KiB)\n", size, size/1024)
98 gen.WriteVersionedGoFile("tables.go", "rangetable", w.Bytes())
101 func print(w io.Writer, rt *unicode.RangeTable) {
102 fmt.Fprintln(w, "&unicode.RangeTable{")
103 fmt.Fprintln(w, "\tR16: []unicode.Range16{")
104 for _, r := range rt.R16 {
105 fmt.Fprintf(w, "\t\t{%#04x, %#04x, %d},\n", r.Lo, r.Hi, r.Stride)
107 fmt.Fprintln(w, "\t},")
108 fmt.Fprintln(w, "\tR32: []unicode.Range32{")
109 for _, r := range rt.R32 {
110 fmt.Fprintf(w, "\t\t{%#08x, %#08x, %d},\n", r.Lo, r.Hi, r.Stride)
112 fmt.Fprintln(w, "\t},")
113 fmt.Fprintf(w, "\tLatinOffset: %d,\n", rt.LatinOffset)
114 fmt.Fprintf(w, "}\n\n")