File: gen_parents.go

package info (click to toggle)
golang-golang-x-text 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-backports
  • size: 41,612 kB
  • sloc: xml: 1,022; makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,189 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore

package main

import (
	"log"

	"golang.org/x/text/internal/gen"
	"golang.org/x/text/internal/language"
	"golang.org/x/text/internal/language/compact"
	"golang.org/x/text/unicode/cldr"
)

func main() {
	r := gen.OpenCLDRCoreZip()
	defer r.Close()

	d := &cldr.Decoder{}
	data, err := d.DecodeZip(r)
	if err != nil {
		log.Fatalf("DecodeZip: %v", err)
	}

	w := gen.NewCodeWriter()
	defer w.WriteGoFile("parents.go", "compact")

	// Create parents table.
	type ID uint16
	parents := make([]ID, compact.NumCompactTags)
	for _, loc := range data.Locales() {
		tag := language.MustParse(loc)
		index, ok := compact.FromTag(tag)
		if !ok {
			continue
		}
		parentIndex := compact.ID(0) // und
		for p := tag.Parent(); p != language.Und; p = p.Parent() {
			if x, ok := compact.FromTag(p); ok {
				parentIndex = x
				break
			}
		}
		parents[index] = ID(parentIndex)
	}

	w.WriteComment(`
	parents maps a compact index of a tag to the compact index of the parent of
	this tag.`)
	w.WriteVar("parents", parents)
}