github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/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  	"sort"
    17  	"strings"
    18  	"unicode"
    19  
    20  	"golang.org/x/text/internal/gen"
    21  	"golang.org/x/text/internal/ucd"
    22  	"golang.org/x/text/unicode/rangetable"
    23  )
    24  
    25  var versionList = flag.String("versions", "",
    26  	"list of versions for which to generate RangeTables")
    27  
    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
    31  
    32  and ensure that the latest versions are included by checking:
    33  	http://www.unicode.org/Public/`
    34  
    35  func getVersions() []string {
    36  	if *versionList == "" {
    37  		log.Fatal(bootstrapMessage)
    38  	}
    39  
    40  	versions := strings.Split(*versionList, ",")
    41  	sort.Strings(versions)
    42  
    43  	// Ensure that at least the current version is included.
    44  	for _, v := range versions {
    45  		if v == gen.UnicodeVersion() {
    46  			return versions
    47  		}
    48  	}
    49  
    50  	versions = append(versions, gen.UnicodeVersion())
    51  	sort.Strings(versions)
    52  	return versions
    53  }
    54  
    55  func main() {
    56  	gen.Init()
    57  
    58  	versions := getVersions()
    59  
    60  	w := &bytes.Buffer{}
    61  
    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")
    64  
    65  	vstr := func(s string) string { return strings.Replace(s, ".", "_", -1) }
    66  
    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))
    70  	}
    71  	fmt.Fprintf(w, "}\n\n")
    72  
    73  	var size int
    74  	for _, v := range versions {
    75  		assigned := []rune{}
    76  
    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))
    80  		})
    81  
    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)
    86  
    87  		fmt.Fprintf(w, "// size %d bytes (%d KiB)\n", sz, sz/1024)
    88  		fmt.Fprintf(w, "var assigned%s = ", vstr(v))
    89  		print(w, rt)
    90  
    91  		size += sz
    92  	}
    93  
    94  	fmt.Fprintf(w, "// Total size %d bytes (%d KiB)\n", size, size/1024)
    95  
    96  	gen.WriteGoFile("tables.go", "rangetable", w.Bytes())
    97  }
    98  
    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)
   104  	}
   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)
   109  	}
   110  	fmt.Fprintln(w, "\t},")
   111  	fmt.Fprintf(w, "\tLatinOffset: %d,\n", rt.LatinOffset)
   112  	fmt.Fprintf(w, "}\n\n")
   113  }