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//go:build ignore
6// +build ignore
7
8package main
9
10// This file generates derivative tables based on the language package itself.
11
12import (
13	"fmt"
14	"log"
15	"sort"
16	"strings"
17
18	"golang.org/x/text/internal/language"
19)
20
21// Compact indices:
22// Note -va-X variants only apply to localization variants.
23// BCP variants only ever apply to language.
24// The only ambiguity between tags is with regions.
25
26func (b *builder) writeCompactIndex() {
27	// Collect all language tags for which we have any data in CLDR.
28	m := map[language.Tag]bool{}
29	for _, lang := range b.data.Locales() {
30		// We include all locales unconditionally to be consistent with en_US.
31		// We want en_US, even though it has no data associated with it.
32
33		// TODO: put any of the languages for which no data exists at the end
34		// of the index. This allows all components based on ICU to use that
35		// as the cutoff point.
36		// if x := data.RawLDML(lang); false ||
37		// 	x.LocaleDisplayNames != nil ||
38		// 	x.Characters != nil ||
39		// 	x.Delimiters != nil ||
40		// 	x.Measurement != nil ||
41		// 	x.Dates != nil ||
42		// 	x.Numbers != nil ||
43		// 	x.Units != nil ||
44		// 	x.ListPatterns != nil ||
45		// 	x.Collations != nil ||
46		// 	x.Segmentations != nil ||
47		// 	x.Rbnf != nil ||
48		// 	x.Annotations != nil ||
49		// 	x.Metadata != nil {
50
51		// TODO: support POSIX natively, albeit non-standard.
52		tag := language.Make(strings.Replace(lang, "_POSIX", "-u-va-posix", 1))
53		m[tag] = true
54		// }
55	}
56
57	// TODO: plural rules are also defined for the deprecated tags:
58	//    iw mo sh tl
59	// Consider removing these as compact tags.
60
61	// Include locales for plural rules, which uses a different structure.
62	for _, plurals := range b.supp.Plurals {
63		for _, rules := range plurals.PluralRules {
64			for _, lang := range strings.Split(rules.Locales, " ") {
65				m[language.Make(lang)] = true
66			}
67		}
68	}
69
70	var coreTags []language.CompactCoreInfo
71	var special []string
72
73	for t := range m {
74		if x := t.Extensions(); len(x) != 0 && fmt.Sprint(x) != "[u-va-posix]" {
75			log.Fatalf("Unexpected extension %v in %v", x, t)
76		}
77		if len(t.Variants()) == 0 && len(t.Extensions()) == 0 {
78			cci, ok := language.GetCompactCore(t)
79			if !ok {
80				log.Fatalf("Locale for non-basic language %q", t)
81			}
82			coreTags = append(coreTags, cci)
83		} else {
84			special = append(special, t.String())
85		}
86	}
87
88	w := b.w
89
90	sort.Slice(coreTags, func(i, j int) bool { return coreTags[i] < coreTags[j] })
91	sort.Strings(special)
92
93	w.WriteComment(`
94	NumCompactTags is the number of common tags. The maximum tag is
95	NumCompactTags-1.`)
96	w.WriteConst("NumCompactTags", len(m))
97
98	fmt.Fprintln(w, "const (")
99	for i, t := range coreTags {
100		fmt.Fprintf(w, "%s ID = %d\n", ident(t.Tag().String()), i)
101	}
102	for i, t := range special {
103		fmt.Fprintf(w, "%s ID = %d\n", ident(t), i+len(coreTags))
104	}
105	fmt.Fprintln(w, ")")
106
107	w.WriteVar("coreTags", coreTags)
108
109	w.WriteConst("specialTagsStr", strings.Join(special, " "))
110}
111
112func ident(s string) string {
113	return strings.Replace(s, "-", "", -1) + "Index"
114}
115