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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
// Copyright 2017 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.
// Package cldrtree builds and generates a CLDR index file, including all
// inheritance.
package cldrtree
//go:generate go test -gen
// cldrtree stores CLDR data in a tree-like structure called Tree. In the CLDR
// data each branch in the tree is indicated by either an element name or an
// attribute value. A Tree does not distinguish between these two cases, but
// rather assumes that all branches can be accessed by an enum with a compact
// range of positive integer values starting from 0.
//
// Each Tree consists of three parts:
// - a slice mapping compact language identifiers to an offset into a set of
// indices,
// - a set of indices, stored as a large blob of uint16 values that encode
// the actual tree structure of data, and
// - a set of buckets that each holds a collection of strings.
// each of which is explained in more detail below.
//
//
// Tree lookup
// A tree lookup is done by providing a locale and a "path", which is a
// sequence of enum values. The search starts with getting the index for the
// given locale and then incrementally jumping into the index using the path
// values. If an element cannot be found in the index, the search starts anew
// for the locale's parent locale. The path may change during lookup by means
// of aliasing, described below.
//
// Buckets
// Buckets hold the actual string data of the leaf values of the CLDR tree.
// This data is stored in buckets, rather than one large string, for multiple
// reasons:
// - it allows representing leaf values more compactly, by storing all leaf
// values in a single bucket and then needing only needing a uint16 to index
// into this bucket for all leaf values,
// - (TBD) allow multiple trees to share subsets of buckets, mostly to allow
// linking in a smaller amount of data if only a subset of the buckets is
// needed,
// - to be nice to go fmt and the compiler.
//
// indices
// An index is a slice of uint16 for which the values are interpreted in one of
// two ways: as a node or a set of leaf values.
// A set of leaf values has the following form:
// <max_size>, <bucket>, <offset>...
// max_size indicates the maximum enum value for which an offset is defined.
// An offset value of 0xFFFF (missingValue) also indicates an undefined value.
// If defined offset indicates the offset within the given bucket of the string.
// A node value has the following form:
// <max_size>, <offset_or_alias>...
// max_size indicates the maximum value for which an offset is defined.
// A missing offset may also be indicated with 0. If the high bit (0x8000, or
// inheritMask) is not set, the offset points to the offset within the index
// for the current locale.
// An offset with high bit set is an alias. In this case the uint16 has the form
// bits:
// 15: 1
// 14-12: negative offset into path relative to current position
// 0-11: new enum value for path element.
// On encountering an alias, the path is modified accordingly and the lookup is
// restarted for the given locale.
import (
"fmt"
"reflect"
"regexp"
"strings"
"unicode/utf8"
"golang.org/x/text/internal/gen"
"golang.org/x/text/language"
"golang.org/x/text/unicode/cldr"
)
// TODO:
// - allow two Trees to share the same set of buckets.
// A Builder allows storing CLDR data in compact form.
type Builder struct {
table []string
rootMeta *metaData
locales []locale
strToBucket map[string]stringInfo
buckets [][]byte
enums []*enum
err error
// Stats
size int
sizeAll int
bucketWaste int
}
const (
maxBucketSize = 8 * 1024 // 8K
maxStrlen = 254 // allow 0xFF sentinel
)
func (b *Builder) setError(err error) {
if b.err == nil {
b.err = err
}
}
func (b *Builder) addString(data string) stringInfo {
data = b.makeString(data)
info, ok := b.strToBucket[data]
if !ok {
b.size += len(data)
x := len(b.buckets) - 1
bucket := b.buckets[x]
if len(bucket)+len(data) < maxBucketSize {
info.bucket = uint16(x)
info.bucketPos = uint16(len(bucket))
b.buckets[x] = append(bucket, data...)
} else {
info.bucket = uint16(len(b.buckets))
info.bucketPos = 0
b.buckets = append(b.buckets, []byte(data))
}
b.strToBucket[data] = info
}
return info
}
func (b *Builder) addStringToBucket(data string, bucket uint16) stringInfo {
data = b.makeString(data)
info, ok := b.strToBucket[data]
if !ok || info.bucket != bucket {
if ok {
b.bucketWaste += len(data)
}
b.size += len(data)
bk := b.buckets[bucket]
info.bucket = bucket
info.bucketPos = uint16(len(bk))
b.buckets[bucket] = append(bk, data...)
b.strToBucket[data] = info
}
return info
}
func (b *Builder) makeString(data string) string {
if len(data) > maxStrlen {
b.setError(fmt.Errorf("string %q exceeds maximum length of %d", data, maxStrlen))
data = data[:maxStrlen]
for i := len(data) - 1; i > len(data)-4; i-- {
if utf8.RuneStart(data[i]) {
data = data[:i]
break
}
}
}
data = string([]byte{byte(len(data))}) + data
b.sizeAll += len(data)
return data
}
type stringInfo struct {
bufferPos uint32
bucket uint16
bucketPos uint16
}
// New creates a new Builder.
func New(tableName string) *Builder {
b := &Builder{
strToBucket: map[string]stringInfo{},
buckets: [][]byte{nil}, // initialize with first bucket.
}
b.rootMeta = &metaData{
b: b,
typeInfo: &typeInfo{},
}
return b
}
// Gen writes all the tables and types for the collected data.
func (b *Builder) Gen(w *gen.CodeWriter) error {
t, err := build(b)
if err != nil {
return err
}
return generate(b, t, w)
}
// GenTestData generates tables useful for testing data generated with Gen.
func (b *Builder) GenTestData(w *gen.CodeWriter) error {
return generateTestData(b, w)
}
type locale struct {
tag language.Tag
root *Index
}
// Locale creates an index for the given locale.
func (b *Builder) Locale(t language.Tag) *Index {
index := &Index{
meta: b.rootMeta,
}
b.locales = append(b.locales, locale{tag: t, root: index})
return index
}
// An Index holds a map of either leaf values or other indices.
type Index struct {
meta *metaData
subIndex []*Index
values []keyValue
}
func (i *Index) setError(err error) { i.meta.b.setError(err) }
type keyValue struct {
key enumIndex
value stringInfo
}
// Element is a CLDR XML element.
type Element interface {
GetCommon() *cldr.Common
}
// Index creates a subindex where the type and enum values are not shared
// with siblings by default. The name is derived from the elem. If elem is
// an alias reference, the alias will be resolved and linked. If elem is nil
// Index returns nil.
func (i *Index) Index(elem Element, opt ...Option) *Index {
if elem == nil || reflect.ValueOf(elem).IsNil() {
return nil
}
c := elem.GetCommon()
o := &options{
parent: i,
name: c.GetCommon().Element(),
}
o.fill(opt)
o.setAlias(elem)
return i.subIndexForKey(o)
}
// IndexWithName is like Section but derives the name from the given name.
func (i *Index) IndexWithName(name string, opt ...Option) *Index {
o := &options{parent: i, name: name}
o.fill(opt)
return i.subIndexForKey(o)
}
// IndexFromType creates a subindex the value of tye type attribute as key. It
// will also configure the Index to share the enumeration values with all
// sibling values. If elem is an alias, it will be resolved and linked.
func (i *Index) IndexFromType(elem Element, opts ...Option) *Index {
o := &options{
parent: i,
name: elem.GetCommon().Type,
}
o.fill(opts)
o.setAlias(elem)
useSharedType()(o)
return i.subIndexForKey(o)
}
// IndexFromAlt creates a subindex the value of tye alt attribute as key. It
// will also configure the Index to share the enumeration values with all
// sibling values. If elem is an alias, it will be resolved and linked.
func (i *Index) IndexFromAlt(elem Element, opts ...Option) *Index {
o := &options{
parent: i,
name: elem.GetCommon().Alt,
}
o.fill(opts)
o.setAlias(elem)
useSharedType()(o)
return i.subIndexForKey(o)
}
func (i *Index) subIndexForKey(opts *options) *Index {
key := opts.name
if len(i.values) > 0 {
panic(fmt.Errorf("cldrtree: adding Index for %q when value already exists", key))
}
meta := i.meta.sub(key, opts)
for _, x := range i.subIndex {
if x.meta == meta {
return x
}
}
if alias := opts.alias; alias != nil {
if a := alias.GetCommon().Alias; a != nil {
if a.Source != "locale" {
i.setError(fmt.Errorf("cldrtree: non-locale alias not supported %v", a.Path))
}
if meta.inheritOffset < 0 {
i.setError(fmt.Errorf("cldrtree: alias was already set %v", a.Path))
}
path := a.Path
for ; strings.HasPrefix(path, "../"); path = path[len("../"):] {
meta.inheritOffset--
}
m := aliasRe.FindStringSubmatch(path)
if m == nil {
i.setError(fmt.Errorf("cldrtree: could not parse alias %q", a.Path))
} else {
key := m[4]
if key == "" {
key = m[1]
}
meta.inheritIndex = key
}
}
}
x := &Index{meta: meta}
i.subIndex = append(i.subIndex, x)
return x
}
var aliasRe = regexp.MustCompile(`^([a-zA-Z]+)(\[@([a-zA-Z-]+)='([a-zA-Z-]+)'\])?`)
// SetValue sets the value, the data from a CLDR XML element, for the given key.
func (i *Index) SetValue(key string, value Element, opt ...Option) {
if len(i.subIndex) > 0 {
panic(fmt.Errorf("adding value for key %q when index already exists", key))
}
o := &options{parent: i}
o.fill(opt)
c := value.GetCommon()
if c.Alias != nil {
i.setError(fmt.Errorf("cldrtree: alias not supported for SetValue %v", c.Alias.Path))
}
i.setValue(key, c.Data(), o)
}
func (i *Index) setValue(key, data string, o *options) {
index, _ := i.meta.typeInfo.lookupSubtype(key, o)
kv := keyValue{key: index}
if len(i.values) > 0 {
// Add string to the same bucket as the other values.
bucket := i.values[0].value.bucket
kv.value = i.meta.b.addStringToBucket(data, bucket)
} else {
kv.value = i.meta.b.addString(data)
}
i.values = append(i.values, kv)
}
|