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