1// Copyright 2018 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// Package compact defines a compact representation of language tags. 6// 7// Common language tags (at least all for which locale information is defined 8// in CLDR) are assigned a unique index. Each Tag is associated with such an 9// ID for selecting language-related resources (such as translations) as well 10// as one for selecting regional defaults (currency, number formatting, etc.) 11// 12// It may want to export this functionality at some point, but at this point 13// this is only available for use within x/text. 14package compact // import "golang.org/x/text/internal/language/compact" 15 16import ( 17 "sort" 18 "strings" 19 20 "golang.org/x/text/internal/language" 21) 22 23// ID is an integer identifying a single tag. 24type ID uint16 25 26func getCoreIndex(t language.Tag) (id ID, ok bool) { 27 cci, ok := language.GetCompactCore(t) 28 if !ok { 29 return 0, false 30 } 31 i := sort.Search(len(coreTags), func(i int) bool { 32 return cci <= coreTags[i] 33 }) 34 if i == len(coreTags) || coreTags[i] != cci { 35 return 0, false 36 } 37 return ID(i), true 38} 39 40// Parent returns the ID of the parent or the root ID if id is already the root. 41func (id ID) Parent() ID { 42 return parents[id] 43} 44 45// Tag converts id to an internal language Tag. 46func (id ID) Tag() language.Tag { 47 if int(id) >= len(coreTags) { 48 return specialTags[int(id)-len(coreTags)] 49 } 50 return coreTags[id].Tag() 51} 52 53var specialTags []language.Tag 54 55func init() { 56 tags := strings.Split(specialTagsStr, " ") 57 specialTags = make([]language.Tag, len(tags)) 58 for i, t := range tags { 59 specialTags[i] = language.MustParse(t) 60 } 61} 62