summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/text/unicode/runenames/gen.go
blob: 7e373a54c7cd2e4afe05ed5de688743a3d2e548d (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2016 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.

// +build ignore

package main

import (
	"log"
	"strings"
	"unicode"

	"golang.org/x/text/internal/gen"
	"golang.org/x/text/internal/ucd"
)

// snippet is a slice of data; data is the concatenation of all of the names.
type snippet struct {
	offset int
	length int
	s      string
}

func makeTable0EntryDirect(rOffset, rLength, dOffset, dLength int) uint64 {
	if rOffset >= 1<<bitsRuneOffset {
		log.Fatalf("makeTable0EntryDirect: rOffset %d is too large", rOffset)
	}
	if rLength >= 1<<bitsRuneLength {
		log.Fatalf("makeTable0EntryDirect: rLength %d is too large", rLength)
	}
	if dOffset >= 1<<bitsDataOffset {
		log.Fatalf("makeTable0EntryDirect: dOffset %d is too large", dOffset)
	}
	if dLength >= 1<<bitsRuneLength {
		log.Fatalf("makeTable0EntryDirect: dLength %d is too large", dLength)
	}
	return uint64(rOffset)<<shiftRuneOffset |
		uint64(rLength)<<shiftRuneLength |
		uint64(dOffset)<<shiftDataOffset |
		uint64(dLength)<<shiftDataLength |
		1 // Direct bit.
}

func makeTable0EntryIndirect(rOffset, rLength, dBase, t1Offset int) uint64 {
	if rOffset >= 1<<bitsRuneOffset {
		log.Fatalf("makeTable0EntryIndirect: rOffset %d is too large", rOffset)
	}
	if rLength >= 1<<bitsRuneLength {
		log.Fatalf("makeTable0EntryIndirect: rLength %d is too large", rLength)
	}
	if dBase >= 1<<bitsDataBase {
		log.Fatalf("makeTable0EntryIndirect: dBase %d is too large", dBase)
	}
	if t1Offset >= 1<<bitsTable1Offset {
		log.Fatalf("makeTable0EntryIndirect: t1Offset %d is too large", t1Offset)
	}
	return uint64(rOffset)<<shiftRuneOffset |
		uint64(rLength)<<shiftRuneLength |
		uint64(dBase)<<shiftDataBase |
		uint64(t1Offset)<<shiftTable1Offset |
		0 // Direct bit.
}

func makeTable1Entry(x int) uint16 {
	if x < 0 || 0xffff < x {
		log.Fatalf("makeTable1Entry: entry %d is out of range", x)
	}
	return uint16(x)
}

var (
	data     []byte
	snippets = make([]snippet, 1+unicode.MaxRune)
)

func main() {
	gen.Init()

	names, counts := parse()
	appendRepeatNames(names, counts)
	appendUniqueNames(names, counts)

	table0, table1 := makeTables()

	gen.Repackage("gen_bits.go", "bits.go", "runenames")

	w := gen.NewCodeWriter()
	w.WriteVar("table0", table0)
	w.WriteVar("table1", table1)
	w.WriteConst("data", string(data))
	w.WriteGoFile("tables.go", "runenames")
}

func parse() (names []string, counts map[string]int) {
	names = make([]string, 1+unicode.MaxRune)
	counts = map[string]int{}
	ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
		r, s := p.Rune(0), p.String(ucd.Name)
		if s == "" {
			return
		}
		if s[0] == '<' {
			const first = ", First>"
			if i := strings.Index(s, first); i >= 0 {
				s = s[:i] + ">"
			}
		}
		names[r] = s
		counts[s]++
	})
	return names, counts
}

func appendRepeatNames(names []string, counts map[string]int) {
	alreadySeen := map[string]snippet{}
	for r, s := range names {
		if s == "" || counts[s] == 1 {
			continue
		}
		if s[0] != '<' {
			log.Fatalf("Repeated name %q does not start with a '<'", s)
		}

		if z, ok := alreadySeen[s]; ok {
			snippets[r] = z
			continue
		}

		z := snippet{
			offset: len(data),
			length: len(s),
			s:      s,
		}
		data = append(data, s...)
		snippets[r] = z
		alreadySeen[s] = z
	}
}

func appendUniqueNames(names []string, counts map[string]int) {
	for r, s := range names {
		if s == "" || counts[s] != 1 {
			continue
		}
		if s[0] == '<' {
			log.Fatalf("Unique name %q starts with a '<'", s)
		}

		z := snippet{
			offset: len(data),
			length: len(s),
			s:      s,
		}
		data = append(data, s...)
		snippets[r] = z
	}
}

func makeTables() (table0 []uint64, table1 []uint16) {
	for i := 0; i < len(snippets); {
		zi := snippets[i]
		if zi == (snippet{}) {
			i++
			continue
		}

		// Look for repeat names. If we have one, we only need a table0 entry.
		j := i + 1
		for ; j < len(snippets) && zi == snippets[j]; j++ {
		}
		if j > i+1 {
			table0 = append(table0, makeTable0EntryDirect(i, j-i, zi.offset, zi.length))
			i = j
			continue
		}

		// Otherwise, we have a run of unique names. We need one table0 entry
		// and two or more table1 entries.
		base := zi.offset &^ (1<<dataBaseUnit - 1)
		t1Offset := len(table1) + 1
		table1 = append(table1, makeTable1Entry(zi.offset-base))
		table1 = append(table1, makeTable1Entry(zi.offset+zi.length-base))
		for ; j < len(snippets) && snippets[j] != (snippet{}); j++ {
			zj := snippets[j]
			if data[zj.offset] == '<' {
				break
			}
			table1 = append(table1, makeTable1Entry(zj.offset+zj.length-base))
		}
		table0 = append(table0, makeTable0EntryIndirect(i, j-i, base>>dataBaseUnit, t1Offset))
		i = j
	}
	return table0, table1
}