github.com/liquid-dev/text@v0.3.3-liquid/internal/language/compact/gen_parents.go (about)

     1  // Copyright 2018 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  	"log"
    11  
    12  	"github.com/liquid-dev/text/internal/gen"
    13  	"github.com/liquid-dev/text/internal/language"
    14  	"github.com/liquid-dev/text/internal/language/compact"
    15  	"github.com/liquid-dev/text/unicode/cldr"
    16  )
    17  
    18  func main() {
    19  	r := gen.OpenCLDRCoreZip()
    20  	defer r.Close()
    21  
    22  	d := &cldr.Decoder{}
    23  	data, err := d.DecodeZip(r)
    24  	if err != nil {
    25  		log.Fatalf("DecodeZip: %v", err)
    26  	}
    27  
    28  	w := gen.NewCodeWriter()
    29  	defer w.WriteGoFile("parents.go", "compact")
    30  
    31  	// Create parents table.
    32  	type ID uint16
    33  	parents := make([]ID, compact.NumCompactTags)
    34  	for _, loc := range data.Locales() {
    35  		tag := language.MustParse(loc)
    36  		index, ok := compact.FromTag(tag)
    37  		if !ok {
    38  			continue
    39  		}
    40  		parentIndex := compact.ID(0) // und
    41  		for p := tag.Parent(); p != language.Und; p = p.Parent() {
    42  			if x, ok := compact.FromTag(p); ok {
    43  				parentIndex = x
    44  				break
    45  			}
    46  		}
    47  		parents[index] = ID(parentIndex)
    48  	}
    49  
    50  	w.WriteComment(`
    51  	parents maps a compact index of a tag to the compact index of the parent of
    52  	this tag.`)
    53  	w.WriteVar("parents", parents)
    54  }