1 // Copyright 2015 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.
5 // Package tag contains functionality handling tags and related data.
6 package tag // import "golang.org/x/text/internal/tag"
10 // An Index converts tags to a compact numeric value.
12 // All elements are of size 4. Tags may be up to 4 bytes long. Excess bytes can
13 // be used to store additional information about the tag.
16 // Elem returns the element data at the given index.
17 func (s Index) Elem(x int) string {
18 return string(s[x*4 : x*4+4])
21 // Index reports the index of the given key or -1 if it could not be found.
22 // Only the first len(key) bytes from the start of the 4-byte entries will be
23 // considered for the search and the first match in Index will be returned.
24 func (s Index) Index(key []byte) int {
26 // search the index of the first entry with an equal or higher value than
28 index := sort.Search(len(s)/4, func(i int) bool {
29 return cmp(s[i*4:i*4+n], key) != -1
32 if cmp(s[i:i+len(key)], key) != 0 {
38 // Next finds the next occurrence of key after index x, which must have been
39 // obtained from a call to Index using the same key. It returns x+1 or -1.
40 func (s Index) Next(key []byte, x int) int {
41 if x++; x*4 < len(s) && cmp(s[x*4:x*4+len(key)], key) == 0 {
47 // cmp returns an integer comparing a and b lexicographically.
48 func cmp(a Index, b []byte) int {
53 for i, c := range b[:n] {
70 // Compare returns an integer comparing a and b lexicographically.
71 func Compare(a string, b []byte) int {
72 return cmp(Index(a), b)
75 // FixCase reformats b to the same pattern of cases as form.
76 // If returns false if string b is malformed.
77 func FixCase(form string, b []byte) bool {
78 if len(form) != len(b) {
86 if c < 'A' || 'Z' < c {
93 if c < 'a' || 'z' < c {