github.com/go-xe2/third@v1.0.3/golang.org/x/text/unicode/rangetable/gen.go (about) 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 5 // +build ignore 6 7 package main 8 9 import ( 10 "bytes" 11 "flag" 12 "fmt" 13 "io" 14 "log" 15 "reflect" 16 "strings" 17 "unicode" 18 19 "github.com/go-xe2/third/golang.org/x/text/collate" 20 "github.com/go-xe2/third/golang.org/x/text/internal/gen" 21 "github.com/go-xe2/third/golang.org/x/text/internal/ucd" 22 "github.com/go-xe2/third/golang.org/x/text/language" 23 "github.com/go-xe2/third/golang.org/x/text/unicode/rangetable" 24 ) 25 26 var versionList = flag.String("versions", "", 27 "list of versions for which to generate RangeTables") 28 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 32 33 and ensure that the latest versions are included by checking: 34 http://www.unicode.org/Public/` 35 36 func getVersions() []string { 37 if *versionList == "" { 38 log.Fatal(bootstrapMessage) 39 } 40 41 c := collate.New(language.Und, collate.Numeric) 42 versions := strings.Split(*versionList, ",") 43 c.SortStrings(versions) 44 45 // Ensure that at least the current version is included. 46 for _, v := range versions { 47 if v == gen.UnicodeVersion() { 48 return versions 49 } 50 } 51 52 versions = append(versions, gen.UnicodeVersion()) 53 c.SortStrings(versions) 54 return versions 55 } 56 57 func main() { 58 gen.Init() 59 60 versions := getVersions() 61 62 w := &bytes.Buffer{} 63 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") 66 67 vstr := func(s string) string { return strings.Replace(s, ".", "_", -1) } 68 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)) 72 } 73 fmt.Fprintf(w, "}\n\n") 74 75 var size int 76 for _, v := range versions { 77 assigned := []rune{} 78 79 r := gen.Open("http://www.unicode.org/Public/", "", v+"/ucd/UnicodeData.txt") 80 ucd.Parse(r, func(p *ucd.Parser) { 81 assigned = append(assigned, p.Rune(0)) 82 }) 83 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) 88 89 fmt.Fprintf(w, "// size %d bytes (%d KiB)\n", sz, sz/1024) 90 fmt.Fprintf(w, "var assigned%s = ", vstr(v)) 91 print(w, rt) 92 93 size += sz 94 } 95 96 fmt.Fprintf(w, "// Total size %d bytes (%d KiB)\n", size, size/1024) 97 98 gen.WriteGoFile("tables.go", "rangetable", w.Bytes()) 99 } 100 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) 106 } 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) 111 } 112 fmt.Fprintln(w, "\t},") 113 fmt.Fprintf(w, "\tLatinOffset: %d,\n", rt.LatinOffset) 114 fmt.Fprintf(w, "}\n\n") 115 }