1 // Copyright 2013 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.
13 "golang.org/x/text/internal/tag"
16 // findIndex tries to find the given tag in idx and returns a standardized error
17 // if it could not be found.
18 func findIndex(idx tag.Index, key []byte, form string) (index int, err error) {
19 if !tag.FixCase(form, key) {
24 return 0, mkErrInvalid(key)
29 func searchUint(imap []uint16, key uint16) int {
30 return sort.Search(len(imap), func(i int) bool {
37 // getLangID returns the langID of s if s is a canonical subtag
38 // or langUnknown if s is not a canonical subtag.
39 func getLangID(s []byte) (langID, error) {
46 // mapLang returns the mapped langID of id according to mapping m.
47 func normLang(id langID) (langID, langAliasType) {
48 k := sort.Search(len(langAliasMap), func(i int) bool {
49 return langAliasMap[i].from >= uint16(id)
51 if k < len(langAliasMap) && langAliasMap[k].from == uint16(id) {
52 return langID(langAliasMap[k].to), langAliasTypes[k]
54 return id, langAliasTypeUnknown
57 // getLangISO2 returns the langID for the given 2-letter ISO language code
58 // or unknownLang if this does not exist.
59 func getLangISO2(s []byte) (langID, error) {
60 if !tag.FixCase("zz", s) {
63 if i := lang.Index(s); i != -1 && lang.Elem(i)[3] != 0 {
66 return 0, mkErrInvalid(s)
69 const base = 'z' - 'a' + 1
71 func strToInt(s []byte) uint {
73 for i := 0; i < len(s); i++ {
80 // converts the given integer to the original ASCII string passed to strToInt.
81 // len(s) must match the number of characters obtained.
82 func intToStr(v uint, s []byte) {
83 for i := len(s) - 1; i >= 0; i-- {
84 s[i] = byte(v%base) + 'a'
89 // getLangISO3 returns the langID for the given 3-letter ISO language code
90 // or unknownLang if this does not exist.
91 func getLangISO3(s []byte) (langID, error) {
92 if tag.FixCase("und", s) {
93 // first try to match canonical 3-letter entries
94 for i := lang.Index(s[:2]); i != -1; i = lang.Next(s[:2], i) {
95 if e := lang.Elem(i); e[3] == 0 && e[2] == s[2] {
96 // We treat "und" as special and always translate it to "unspecified".
97 // Note that ZZ and Zzzz are private use and are not treated as
98 // unspecified by default.
100 if id == nonCanonicalUnd {
106 if i := altLangISO3.Index(s); i != -1 {
107 return langID(altLangIndex[altLangISO3.Elem(i)[3]]), nil
110 if langNoIndex[n/8]&(1<<(n%8)) != 0 {
111 return langID(n) + langNoIndexOffset, nil
113 // Check for non-canonical uses of ISO3.
114 for i := lang.Index(s[:1]); i != -1; i = lang.Next(s[:1], i) {
115 if e := lang.Elem(i); e[2] == s[1] && e[3] == s[2] {
116 return langID(i), nil
119 return 0, mkErrInvalid(s)
124 // stringToBuf writes the string to b and returns the number of bytes
125 // written. cap(b) must be >= 3.
126 func (id langID) stringToBuf(b []byte) int {
127 if id >= langNoIndexOffset {
128 intToStr(uint(id)-langNoIndexOffset, b[:3])
131 return copy(b, "und")
135 return copy(b, l[:3])
137 return copy(b, l[:2])
140 // String returns the BCP 47 representation of the langID.
141 // Use b as variable name, instead of id, to ensure the variable
142 // used is consistent with that of Base in which this type is embedded.
143 func (b langID) String() string {
146 } else if b >= langNoIndexOffset {
147 b -= langNoIndexOffset
149 intToStr(uint(b), buf[:])
150 return string(buf[:])
152 l := lang.Elem(int(b))
159 // ISO3 returns the ISO 639-3 language code.
160 func (b langID) ISO3() string {
161 if b == 0 || b >= langNoIndexOffset {
164 l := lang.Elem(int(b))
167 } else if l[2] == 0 {
168 return altLangISO3.Elem(int(l[3]))[:3]
170 // This allocation will only happen for 3-letter ISO codes
171 // that are non-canonical BCP 47 language identifiers.
172 return l[0:1] + l[2:4]
175 // IsPrivateUse reports whether this language code is reserved for private use.
176 func (b langID) IsPrivateUse() bool {
177 return langPrivateStart <= b && b <= langPrivateEnd
182 // getRegionID returns the region id for s if s is a valid 2-letter region code
184 func getRegionID(s []byte) (regionID, error) {
187 return getRegionISO3(s)
189 if i, err := strconv.ParseUint(string(s), 10, 10); err == nil {
190 return getRegionM49(int(i))
193 return getRegionISO2(s)
196 // getRegionISO2 returns the regionID for the given 2-letter ISO country code
197 // or unknownRegion if this does not exist.
198 func getRegionISO2(s []byte) (regionID, error) {
199 i, err := findIndex(regionISO, s, "ZZ")
203 return regionID(i) + isoRegionOffset, nil
206 // getRegionISO3 returns the regionID for the given 3-letter ISO country code
207 // or unknownRegion if this does not exist.
208 func getRegionISO3(s []byte) (regionID, error) {
209 if tag.FixCase("ZZZ", s) {
210 for i := regionISO.Index(s[:1]); i != -1; i = regionISO.Next(s[:1], i) {
211 if e := regionISO.Elem(i); e[2] == s[1] && e[3] == s[2] {
212 return regionID(i) + isoRegionOffset, nil
215 for i := 0; i < len(altRegionISO3); i += 3 {
216 if tag.Compare(altRegionISO3[i:i+3], s) == 0 {
217 return regionID(altRegionIDs[i/3]), nil
220 return 0, mkErrInvalid(s)
225 func getRegionM49(n int) (regionID, error) {
226 if 0 < n && n <= 999 {
230 regionMask = 1<<regionBits - 1
232 idx := n >> searchBits
233 buf := fromM49[m49Index[idx]:m49Index[idx+1]]
234 val := uint16(n) << regionBits // we rely on bits shifting out
235 i := sort.Search(len(buf), func(i int) bool {
238 if r := fromM49[int(m49Index[idx])+i]; r&^regionMask == val {
239 return regionID(r & regionMask), nil
243 fmt.Fprint(bytes.NewBuffer([]byte(e.v[:])), n)
247 // normRegion returns a region if r is deprecated or 0 otherwise.
248 // TODO: consider supporting BYS (-> BLR), CSK (-> 200 or CZ), PHI (-> PHL) and AFI (-> DJ).
249 // TODO: consider mapping split up regions to new most populous one (like CLDR).
250 func normRegion(r regionID) regionID {
252 k := sort.Search(len(m), func(i int) bool {
253 return m[i].from >= uint16(r)
255 if k < len(m) && m[k].from == uint16(r) {
256 return regionID(m[k].to)
262 iso3166UserAssigned = 1 << iota
267 func (r regionID) typ() byte {
268 return regionTypes[r]
271 // String returns the BCP 47 representation for the region.
272 // It returns "ZZ" for an unspecified region.
273 func (r regionID) String() string {
274 if r < isoRegionOffset {
278 return fmt.Sprintf("%03d", r.M49())
281 return regionISO.Elem(int(r))[:2]
284 // ISO3 returns the 3-letter ISO code of r.
285 // Note that not all regions have a 3-letter ISO code.
286 // In such cases this method returns "ZZZ".
287 func (r regionID) ISO3() string {
288 if r < isoRegionOffset {
292 reg := regionISO.Elem(int(r))
295 return altRegionISO3[reg[3]:][:3]
299 return reg[0:1] + reg[2:4]
302 // M49 returns the UN M.49 encoding of r, or 0 if this encoding
303 // is not defined for r.
304 func (r regionID) M49() int {
308 // IsPrivateUse reports whether r has the ISO 3166 User-assigned status. This
309 // may include private-use tags that are assigned by CLDR and used in this
310 // implementation. So IsPrivateUse and IsCountry can be simultaneously true.
311 func (r regionID) IsPrivateUse() bool {
312 return r.typ()&iso3166UserAssigned != 0
317 // getScriptID returns the script id for string s. It assumes that s
318 // is of the format [A-Z][a-z]{3}.
319 func getScriptID(idx tag.Index, s []byte) (scriptID, error) {
320 i, err := findIndex(idx, s, "Zzzz")
321 return scriptID(i), err
324 // String returns the script code in title case.
325 // It returns "Zzzz" for an unspecified script.
326 func (s scriptID) String() string {
330 return script.Elem(int(s))
333 // IsPrivateUse reports whether this script code is reserved for private use.
334 func (s scriptID) IsPrivateUse() bool {
335 return _Qaaa <= s && s <= _Qabx
339 maxAltTaglen = len("en-US-POSIX")
340 maxLen = maxAltTaglen
344 // grandfatheredMap holds a mapping from legacy and grandfathered tags to
345 // their base language or index to more elaborate tag.
346 grandfatheredMap = map[[maxLen]byte]int16{
347 [maxLen]byte{'a', 'r', 't', '-', 'l', 'o', 'j', 'b', 'a', 'n'}: _jbo, // art-lojban
348 [maxLen]byte{'i', '-', 'a', 'm', 'i'}: _ami, // i-ami
349 [maxLen]byte{'i', '-', 'b', 'n', 'n'}: _bnn, // i-bnn
350 [maxLen]byte{'i', '-', 'h', 'a', 'k'}: _hak, // i-hak
351 [maxLen]byte{'i', '-', 'k', 'l', 'i', 'n', 'g', 'o', 'n'}: _tlh, // i-klingon
352 [maxLen]byte{'i', '-', 'l', 'u', 'x'}: _lb, // i-lux
353 [maxLen]byte{'i', '-', 'n', 'a', 'v', 'a', 'j', 'o'}: _nv, // i-navajo
354 [maxLen]byte{'i', '-', 'p', 'w', 'n'}: _pwn, // i-pwn
355 [maxLen]byte{'i', '-', 't', 'a', 'o'}: _tao, // i-tao
356 [maxLen]byte{'i', '-', 't', 'a', 'y'}: _tay, // i-tay
357 [maxLen]byte{'i', '-', 't', 's', 'u'}: _tsu, // i-tsu
358 [maxLen]byte{'n', 'o', '-', 'b', 'o', 'k'}: _nb, // no-bok
359 [maxLen]byte{'n', 'o', '-', 'n', 'y', 'n'}: _nn, // no-nyn
360 [maxLen]byte{'s', 'g', 'n', '-', 'b', 'e', '-', 'f', 'r'}: _sfb, // sgn-BE-FR
361 [maxLen]byte{'s', 'g', 'n', '-', 'b', 'e', '-', 'n', 'l'}: _vgt, // sgn-BE-NL
362 [maxLen]byte{'s', 'g', 'n', '-', 'c', 'h', '-', 'd', 'e'}: _sgg, // sgn-CH-DE
363 [maxLen]byte{'z', 'h', '-', 'g', 'u', 'o', 'y', 'u'}: _cmn, // zh-guoyu
364 [maxLen]byte{'z', 'h', '-', 'h', 'a', 'k', 'k', 'a'}: _hak, // zh-hakka
365 [maxLen]byte{'z', 'h', '-', 'm', 'i', 'n', '-', 'n', 'a', 'n'}: _nan, // zh-min-nan
366 [maxLen]byte{'z', 'h', '-', 'x', 'i', 'a', 'n', 'g'}: _hsn, // zh-xiang
368 // Grandfathered tags with no modern replacement will be converted as
370 [maxLen]byte{'c', 'e', 'l', '-', 'g', 'a', 'u', 'l', 'i', 's', 'h'}: -1, // cel-gaulish
371 [maxLen]byte{'e', 'n', '-', 'g', 'b', '-', 'o', 'e', 'd'}: -2, // en-GB-oed
372 [maxLen]byte{'i', '-', 'd', 'e', 'f', 'a', 'u', 'l', 't'}: -3, // i-default
373 [maxLen]byte{'i', '-', 'e', 'n', 'o', 'c', 'h', 'i', 'a', 'n'}: -4, // i-enochian
374 [maxLen]byte{'i', '-', 'm', 'i', 'n', 'g', 'o'}: -5, // i-mingo
375 [maxLen]byte{'z', 'h', '-', 'm', 'i', 'n'}: -6, // zh-min
377 // CLDR-specific tag.
378 [maxLen]byte{'r', 'o', 'o', 't'}: 0, // root
379 [maxLen]byte{'e', 'n', '-', 'u', 's', '-', 'p', 'o', 's', 'i', 'x'}: -7, // en_US_POSIX"
382 altTagIndex = [...]uint8{0, 17, 31, 45, 61, 74, 86, 102}
384 altTags = "xtg-x-cel-gaulishen-GB-oxendicten-x-i-defaultund-x-i-enochiansee-x-i-mingonan-x-zh-minen-US-u-va-posix"
387 func grandfathered(s [maxAltTaglen]byte) (t Tag, ok bool) {
388 if v, ok := grandfatheredMap[s]; ok {
390 return Make(altTags[altTagIndex[-v-1]:altTagIndex[-v]]), true