mirror-to-gitea/vendor/golang.org/x/text/internal/language/parse.go
vendor/golang.org/x/text/internal/language/parse.goRaw
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.
4
5package language
6
7import (
8 "bytes"
9 "errors"
10 "fmt"
11 "sort"
12
13 "golang.org/x/text/internal/tag"
14)
15
16// isAlpha returns true if the byte is not a digit.
17// b must be an ASCII letter or digit.
18func isAlpha(b byte) bool {
19 return b > '9'
20}
21
22// isAlphaNum returns true if the string contains only ASCII letters or digits.
23func isAlphaNum(s []byte) bool {
24 for _, c := range s {
25 if !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9') {
26 return false
27 }
28 }
29 return true
30}
31
32// ErrSyntax is returned by any of the parsing functions when the
33// input is not well-formed, according to BCP 47.
34// TODO: return the position at which the syntax error occurred?
35var ErrSyntax = errors.New("language: tag is not well-formed")
36
37// ErrDuplicateKey is returned when a tag contains the same key twice with
38// different values in the -u section.
39var ErrDuplicateKey = errors.New("language: different values for same key in -u extension")
40
41// ValueError is returned by any of the parsing functions when the
42// input is well-formed but the respective subtag is not recognized
43// as a valid value.
44type ValueError struct {
45 v [8]byte
46}
47
48// NewValueError creates a new ValueError.
49func NewValueError(tag []byte) ValueError {
50 var e ValueError
51 copy(e.v[:], tag)
52 return e
53}
54
55func (e ValueError) tag() []byte {
56 n := bytes.IndexByte(e.v[:], 0)
57 if n == -1 {
58 n = 8
59 }
60 return e.v[:n]
61}
62
63// Error implements the error interface.
64func (e ValueError) Error() string {
65 return fmt.Sprintf("language: subtag %q is well-formed but unknown", e.tag())
66}
67
68// Subtag returns the subtag for which the error occurred.
69func (e ValueError) Subtag() string {
70 return string(e.tag())
71}
72
73// scanner is used to scan BCP 47 tokens, which are separated by _ or -.
74type scanner struct {
75 b []byte
76 bytes [max99thPercentileSize]byte
77 token []byte
78 start int // start position of the current token
79 end int // end position of the current token
80 next int // next point for scan
81 err error
82 done bool
83}
84
85func makeScannerString(s string) scanner {
86 scan := scanner{}
87 if len(s) <= len(scan.bytes) {
88 scan.b = scan.bytes[:copy(scan.bytes[:], s)]
89 } else {
90 scan.b = []byte(s)
91 }
92 scan.init()
93 return scan
94}
95
96// makeScanner returns a scanner using b as the input buffer.
97// b is not copied and may be modified by the scanner routines.
98func makeScanner(b []byte) scanner {
99 scan := scanner{b: b}
100 scan.init()
101 return scan
102}
103
104func (s *scanner) init() {
105 for i, c := range s.b {
106 if c == '_' {
107 s.b[i] = '-'
108 }
109 }
110 s.scan()
111}
112
113// restToLower converts the string between start and end to lower case.
114func (s *scanner) toLower(start, end int) {
115 for i := start; i < end; i++ {
116 c := s.b[i]
117 if 'A' <= c && c <= 'Z' {
118 s.b[i] += 'a' - 'A'
119 }
120 }
121}
122
123func (s *scanner) setError(e error) {
124 if s.err == nil || (e == ErrSyntax && s.err != ErrSyntax) {
125 s.err = e
126 }
127}
128
129// resizeRange shrinks or grows the array at position oldStart such that
130// a new string of size newSize can fit between oldStart and oldEnd.
131// Sets the scan point to after the resized range.
132func (s *scanner) resizeRange(oldStart, oldEnd, newSize int) {
133 s.start = oldStart
134 if end := oldStart + newSize; end != oldEnd {
135 diff := end - oldEnd
136 var b []byte
137 if n := len(s.b) + diff; n > cap(s.b) {
138 b = make([]byte, n)
139 copy(b, s.b[:oldStart])
140 } else {
141 b = s.b[:n]
142 }
143 copy(b[end:], s.b[oldEnd:])
144 s.b = b
145 s.next = end + (s.next - s.end)
146 s.end = end
147 }
148}
149
150// replace replaces the current token with repl.
151func (s *scanner) replace(repl string) {
152 s.resizeRange(s.start, s.end, len(repl))
153 copy(s.b[s.start:], repl)
154}
155
156// gobble removes the current token from the input.
157// Caller must call scan after calling gobble.
158func (s *scanner) gobble(e error) {
159 s.setError(e)
160 if s.start == 0 {
161 s.b = s.b[:+copy(s.b, s.b[s.next:])]
162 s.end = 0
163 } else {
164 s.b = s.b[:s.start-1+copy(s.b[s.start-1:], s.b[s.end:])]
165 s.end = s.start - 1
166 }
167 s.next = s.start
168}
169
170// deleteRange removes the given range from s.b before the current token.
171func (s *scanner) deleteRange(start, end int) {
172 s.b = s.b[:start+copy(s.b[start:], s.b[end:])]
173 diff := end - start
174 s.next -= diff
175 s.start -= diff
176 s.end -= diff
177}
178
179// scan parses the next token of a BCP 47 string. Tokens that are larger
180// than 8 characters or include non-alphanumeric characters result in an error
181// and are gobbled and removed from the output.
182// It returns the end position of the last token consumed.
183func (s *scanner) scan() (end int) {
184 end = s.end
185 s.token = nil
186 for s.start = s.next; s.next < len(s.b); {
187 i := bytes.IndexByte(s.b[s.next:], '-')
188 if i == -1 {
189 s.end = len(s.b)
190 s.next = len(s.b)
191 i = s.end - s.start
192 } else {
193 s.end = s.next + i
194 s.next = s.end + 1
195 }
196 token := s.b[s.start:s.end]
197 if i < 1 || i > 8 || !isAlphaNum(token) {
198 s.gobble(ErrSyntax)
199 continue
200 }
201 s.token = token
202 return end
203 }
204 if n := len(s.b); n > 0 && s.b[n-1] == '-' {
205 s.setError(ErrSyntax)
206 s.b = s.b[:len(s.b)-1]
207 }
208 s.done = true
209 return end
210}
211
212// acceptMinSize parses multiple tokens of the given size or greater.
213// It returns the end position of the last token consumed.
214func (s *scanner) acceptMinSize(min int) (end int) {
215 end = s.end
216 s.scan()
217 for ; len(s.token) >= min; s.scan() {
218 end = s.end
219 }
220 return end
221}
222
223// Parse parses the given BCP 47 string and returns a valid Tag. If parsing
224// failed it returns an error and any part of the tag that could be parsed.
225// If parsing succeeded but an unknown value was found, it returns
226// ValueError. The Tag returned in this case is just stripped of the unknown
227// value. All other values are preserved. It accepts tags in the BCP 47 format
228// and extensions to this standard defined in
229// https://www.unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers.
230func Parse(s string) (t Tag, err error) {
231 // TODO: consider supporting old-style locale key-value pairs.
232 if s == "" {
233 return Und, ErrSyntax
234 }
235 if len(s) <= maxAltTaglen {
236 b := [maxAltTaglen]byte{}
237 for i, c := range s {
238 // Generating invalid UTF-8 is okay as it won't match.
239 if 'A' <= c && c <= 'Z' {
240 c += 'a' - 'A'
241 } else if c == '_' {
242 c = '-'
243 }
244 b[i] = byte(c)
245 }
246 if t, ok := grandfathered(b); ok {
247 return t, nil
248 }
249 }
250 scan := makeScannerString(s)
251 return parse(&scan, s)
252}
253
254func parse(scan *scanner, s string) (t Tag, err error) {
255 t = Und
256 var end int
257 if n := len(scan.token); n <= 1 {
258 scan.toLower(0, len(scan.b))
259 if n == 0 || scan.token[0] != 'x' {
260 return t, ErrSyntax
261 }
262 end = parseExtensions(scan)
263 } else if n >= 4 {
264 return Und, ErrSyntax
265 } else { // the usual case
266 t, end = parseTag(scan)
267 if n := len(scan.token); n == 1 {
268 t.pExt = uint16(end)
269 end = parseExtensions(scan)
270 } else if end < len(scan.b) {
271 scan.setError(ErrSyntax)
272 scan.b = scan.b[:end]
273 }
274 }
275 if int(t.pVariant) < len(scan.b) {
276 if end < len(s) {
277 s = s[:end]
278 }
279 if len(s) > 0 && tag.Compare(s, scan.b) == 0 {
280 t.str = s
281 } else {
282 t.str = string(scan.b)
283 }
284 } else {
285 t.pVariant, t.pExt = 0, 0
286 }
287 return t, scan.err
288}
289
290// parseTag parses language, script, region and variants.
291// It returns a Tag and the end position in the input that was parsed.
292func parseTag(scan *scanner) (t Tag, end int) {
293 var e error
294 // TODO: set an error if an unknown lang, script or region is encountered.
295 t.LangID, e = getLangID(scan.token)
296 scan.setError(e)
297 scan.replace(t.LangID.String())
298 langStart := scan.start
299 end = scan.scan()
300 for len(scan.token) == 3 && isAlpha(scan.token[0]) {
301 // From http://tools.ietf.org/html/bcp47, <lang>-<extlang> tags are equivalent
302 // to a tag of the form <extlang>.
303 lang, e := getLangID(scan.token)
304 if lang != 0 {
305 t.LangID = lang
306 copy(scan.b[langStart:], lang.String())
307 scan.b[langStart+3] = '-'
308 scan.start = langStart + 4
309 }
310 scan.gobble(e)
311 end = scan.scan()
312 }
313 if len(scan.token) == 4 && isAlpha(scan.token[0]) {
314 t.ScriptID, e = getScriptID(script, scan.token)
315 if t.ScriptID == 0 {
316 scan.gobble(e)
317 }
318 end = scan.scan()
319 }
320 if n := len(scan.token); n >= 2 && n <= 3 {
321 t.RegionID, e = getRegionID(scan.token)
322 if t.RegionID == 0 {
323 scan.gobble(e)
324 } else {
325 scan.replace(t.RegionID.String())
326 }
327 end = scan.scan()
328 }
329 scan.toLower(scan.start, len(scan.b))
330 t.pVariant = byte(end)
331 end = parseVariants(scan, end, t)
332 t.pExt = uint16(end)
333 return t, end
334}
335
336var separator = []byte{'-'}
337
338// parseVariants scans tokens as long as each token is a valid variant string.
339// Duplicate variants are removed.
340func parseVariants(scan *scanner, end int, t Tag) int {
341 start := scan.start
342 varIDBuf := [4]uint8{}
343 variantBuf := [4][]byte{}
344 varID := varIDBuf[:0]
345 variant := variantBuf[:0]
346 last := -1
347 needSort := false
348 for ; len(scan.token) >= 4; scan.scan() {
349 // TODO: measure the impact of needing this conversion and redesign
350 // the data structure if there is an issue.
351 v, ok := variantIndex[string(scan.token)]
352 if !ok {
353 // unknown variant
354 // TODO: allow user-defined variants?
355 scan.gobble(NewValueError(scan.token))
356 continue
357 }
358 varID = append(varID, v)
359 variant = append(variant, scan.token)
360 if !needSort {
361 if last < int(v) {
362 last = int(v)
363 } else {
364 needSort = true
365 // There is no legal combinations of more than 7 variants
366 // (and this is by no means a useful sequence).
367 const maxVariants = 8
368 if len(varID) > maxVariants {
369 break
370 }
371 }
372 }
373 end = scan.end
374 }
375 if needSort {
376 sort.Sort(variantsSort{varID, variant})
377 k, l := 0, -1
378 for i, v := range varID {
379 w := int(v)
380 if l == w {
381 // Remove duplicates.
382 continue
383 }
384 varID[k] = varID[i]
385 variant[k] = variant[i]
386 k++
387 l = w
388 }
389 if str := bytes.Join(variant[:k], separator); len(str) == 0 {
390 end = start - 1
391 } else {
392 scan.resizeRange(start, end, len(str))
393 copy(scan.b[scan.start:], str)
394 end = scan.end
395 }
396 }
397 return end
398}
399
400type variantsSort struct {
401 i []uint8
402 v [][]byte
403}
404
405func (s variantsSort) Len() int {
406 return len(s.i)
407}
408
409func (s variantsSort) Swap(i, j int) {
410 s.i[i], s.i[j] = s.i[j], s.i[i]
411 s.v[i], s.v[j] = s.v[j], s.v[i]
412}
413
414func (s variantsSort) Less(i, j int) bool {
415 return s.i[i] < s.i[j]
416}
417
418type bytesSort struct {
419 b [][]byte
420 n int // first n bytes to compare
421}
422
423func (b bytesSort) Len() int {
424 return len(b.b)
425}
426
427func (b bytesSort) Swap(i, j int) {
428 b.b[i], b.b[j] = b.b[j], b.b[i]
429}
430
431func (b bytesSort) Less(i, j int) bool {
432 for k := 0; k < b.n; k++ {
433 if b.b[i][k] == b.b[j][k] {
434 continue
435 }
436 return b.b[i][k] < b.b[j][k]
437 }
438 return false
439}
440
441// parseExtensions parses and normalizes the extensions in the buffer.
442// It returns the last position of scan.b that is part of any extension.
443// It also trims scan.b to remove excess parts accordingly.
444func parseExtensions(scan *scanner) int {
445 start := scan.start
446 exts := [][]byte{}
447 private := []byte{}
448 end := scan.end
449 for len(scan.token) == 1 {
450 extStart := scan.start
451 ext := scan.token[0]
452 end = parseExtension(scan)
453 extension := scan.b[extStart:end]
454 if len(extension) < 3 || (ext != 'x' && len(extension) < 4) {
455 scan.setError(ErrSyntax)
456 end = extStart
457 continue
458 } else if start == extStart && (ext == 'x' || scan.start == len(scan.b)) {
459 scan.b = scan.b[:end]
460 return end
461 } else if ext == 'x' {
462 private = extension
463 break
464 }
465 exts = append(exts, extension)
466 }
467 sort.Sort(bytesSort{exts, 1})
468 if len(private) > 0 {
469 exts = append(exts, private)
470 }
471 scan.b = scan.b[:start]
472 if len(exts) > 0 {
473 scan.b = append(scan.b, bytes.Join(exts, separator)...)
474 } else if start > 0 {
475 // Strip trailing '-'.
476 scan.b = scan.b[:start-1]
477 }
478 return end
479}
480
481// parseExtension parses a single extension and returns the position of
482// the extension end.
483func parseExtension(scan *scanner) int {
484 start, end := scan.start, scan.end
485 switch scan.token[0] {
486 case 'u': // https://www.ietf.org/rfc/rfc6067.txt
487 attrStart := end
488 scan.scan()
489 for last := []byte{}; len(scan.token) > 2; scan.scan() {
490 if bytes.Compare(scan.token, last) != -1 {
491 // Attributes are unsorted. Start over from scratch.
492 p := attrStart + 1
493 scan.next = p
494 attrs := [][]byte{}
495 for scan.scan(); len(scan.token) > 2; scan.scan() {
496 attrs = append(attrs, scan.token)
497 end = scan.end
498 }
499 sort.Sort(bytesSort{attrs, 3})
500 copy(scan.b[p:], bytes.Join(attrs, separator))
501 break
502 }
503 last = scan.token
504 end = scan.end
505 }
506 // Scan key-type sequences. A key is of length 2 and may be followed
507 // by 0 or more "type" subtags from 3 to the maximum of 8 letters.
508 var last, key []byte
509 for attrEnd := end; len(scan.token) == 2; last = key {
510 key = scan.token
511 end = scan.end
512 for scan.scan(); end < scan.end && len(scan.token) > 2; scan.scan() {
513 end = scan.end
514 }
515 // TODO: check key value validity
516 if bytes.Compare(key, last) != 1 || scan.err != nil {
517 // We have an invalid key or the keys are not sorted.
518 // Start scanning keys from scratch and reorder.
519 p := attrEnd + 1
520 scan.next = p
521 keys := [][]byte{}
522 for scan.scan(); len(scan.token) == 2; {
523 keyStart := scan.start
524 end = scan.end
525 for scan.scan(); end < scan.end && len(scan.token) > 2; scan.scan() {
526 end = scan.end
527 }
528 keys = append(keys, scan.b[keyStart:end])
529 }
530 sort.Stable(bytesSort{keys, 2})
531 if n := len(keys); n > 0 {
532 k := 0
533 for i := 1; i < n; i++ {
534 if !bytes.Equal(keys[k][:2], keys[i][:2]) {
535 k++
536 keys[k] = keys[i]
537 } else if !bytes.Equal(keys[k], keys[i]) {
538 scan.setError(ErrDuplicateKey)
539 }
540 }
541 keys = keys[:k+1]
542 }
543 reordered := bytes.Join(keys, separator)
544 if e := p + len(reordered); e < end {
545 scan.deleteRange(e, end)
546 end = e
547 }
548 copy(scan.b[p:], reordered)
549 break
550 }
551 }
552 case 't': // https://www.ietf.org/rfc/rfc6497.txt
553 scan.scan()
554 if n := len(scan.token); n >= 2 && n <= 3 && isAlpha(scan.token[1]) {
555 _, end = parseTag(scan)
556 scan.toLower(start, end)
557 }
558 for len(scan.token) == 2 && !isAlpha(scan.token[1]) {
559 end = scan.acceptMinSize(3)
560 }
561 case 'x':
562 end = scan.acceptMinSize(1)
563 default:
564 end = scan.acceptMinSize(2)
565 }
566 return end
567}
568
569// getExtension returns the name, body and end position of the extension.
570func getExtension(s string, p int) (end int, ext string) {
571 if s[p] == '-' {
572 p++
573 }
574 if s[p] == 'x' {
575 return len(s), s[p:]
576 }
577 end = nextExtension(s, p)
578 return end, s[p:end]
579}
580
581// nextExtension finds the next extension within the string, searching
582// for the -<char>- pattern from position p.
583// In the fast majority of cases, language tags will have at most
584// one extension and extensions tend to be small.
585func nextExtension(s string, p int) int {
586 for n := len(s) - 3; p < n; {
587 if s[p] == '-' {
588 if s[p+2] == '-' {
589 return p
590 }
591 p += 3
592 } else {
593 p++
594 }
595 }
596 return len(s)
597}