1// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
2
3package cases
4
5// This file contains definitions for interpreting the trie value of the case
6// trie generated by "go run gen*.go". It is shared by both the generator
7// program and the resultant package. Sharing is achieved by the generator
8// copying gen_trieval.go to trieval.go and changing what's above this comment.
9
10// info holds case information for a single rune. It is the value returned
11// by a trie lookup. Most mapping information can be stored in a single 16-bit
12// value. If not, for example when a rune is mapped to multiple runes, the value
13// stores some basic case data and an index into an array with additional data.
14//
15// The per-rune values have the following format:
16//
17//   if (exception) {
18//     15..5  unsigned exception index
19//         4  unused
20//   } else {
21//     15..8  XOR pattern or index to XOR pattern for case mapping
22//            Only 13..8 are used for XOR patterns.
23//         7  inverseFold (fold to upper, not to lower)
24//         6  index: interpret the XOR pattern as an index
25//            or isMid if case mode is cIgnorableUncased.
26//      5..4  CCC: zero (normal or break), above or other
27//   }
28//      3  exception: interpret this value as an exception index
29//         (TODO: is this bit necessary? Probably implied from case mode.)
30//   2..0  case mode
31//
32// For the non-exceptional cases, a rune must be either uncased, lowercase or
33// uppercase. If the rune is cased, the XOR pattern maps either a lowercase
34// rune to uppercase or an uppercase rune to lowercase (applied to the 10
35// least-significant bits of the rune).
36//
37// See the definitions below for a more detailed description of the various
38// bits.
39type info uint16
40
41const (
42	casedMask      = 0x0003
43	fullCasedMask  = 0x0007
44	ignorableMask  = 0x0006
45	ignorableValue = 0x0004
46
47	inverseFoldBit = 1 << 7
48	isMidBit       = 1 << 6
49
50	exceptionBit     = 1 << 3
51	exceptionShift   = 5
52	numExceptionBits = 11
53
54	xorIndexBit = 1 << 6
55	xorShift    = 8
56
57	// There is no mapping if all xor bits and the exception bit are zero.
58	hasMappingMask = 0xff80 | exceptionBit
59)
60
61// The case mode bits encodes the case type of a rune. This includes uncased,
62// title, upper and lower case and case ignorable. (For a definition of these
63// terms see Chapter 3 of The Unicode Standard Core Specification.) In some rare
64// cases, a rune can be both cased and case-ignorable. This is encoded by
65// cIgnorableCased. A rune of this type is always lower case. Some runes are
66// cased while not having a mapping.
67//
68// A common pattern for scripts in the Unicode standard is for upper and lower
69// case runes to alternate for increasing rune values (e.g. the accented Latin
70// ranges starting from U+0100 and U+1E00 among others and some Cyrillic
71// characters). We use this property by defining a cXORCase mode, where the case
72// mode (always upper or lower case) is derived from the rune value. As the XOR
73// pattern for case mappings is often identical for successive runes, using
74// cXORCase can result in large series of identical trie values. This, in turn,
75// allows us to better compress the trie blocks.
76const (
77	cUncased          info = iota // 000
78	cTitle                        // 001
79	cLower                        // 010
80	cUpper                        // 011
81	cIgnorableUncased             // 100
82	cIgnorableCased               // 101 // lower case if mappings exist
83	cXORCase                      // 11x // case is cLower | ((rune&1) ^ x)
84
85	maxCaseMode = cUpper
86)
87
88func (c info) isCased() bool {
89	return c&casedMask != 0
90}
91
92func (c info) isCaseIgnorable() bool {
93	return c&ignorableMask == ignorableValue
94}
95
96func (c info) isNotCasedAndNotCaseIgnorable() bool {
97	return c&fullCasedMask == 0
98}
99
100func (c info) isCaseIgnorableAndNotCased() bool {
101	return c&fullCasedMask == cIgnorableUncased
102}
103
104func (c info) isMid() bool {
105	return c&(fullCasedMask|isMidBit) == isMidBit|cIgnorableUncased
106}
107
108// The case mapping implementation will need to know about various Canonical
109// Combining Class (CCC) values. We encode two of these in the trie value:
110// cccZero (0) and cccAbove (230). If the value is cccOther, it means that
111// CCC(r) > 0, but not 230. A value of cccBreak means that CCC(r) == 0 and that
112// the rune also has the break category Break (see below).
113const (
114	cccBreak info = iota << 4
115	cccZero
116	cccAbove
117	cccOther
118
119	cccMask = cccBreak | cccZero | cccAbove | cccOther
120)
121
122const (
123	starter       = 0
124	above         = 230
125	iotaSubscript = 240
126)
127
128// The exceptions slice holds data that does not fit in a normal info entry.
129// The entry is pointed to by the exception index in an entry. It has the
130// following format:
131//
132// Header
133// byte 0:
134//  7..6  unused
135//  5..4  CCC type (same bits as entry)
136//     3  unused
137//  2..0  length of fold
138//
139// byte 1:
140//   7..6  unused
141//   5..3  length of 1st mapping of case type
142//   2..0  length of 2nd mapping of case type
143//
144//   case     1st    2nd
145//   lower -> upper, title
146//   upper -> lower, title
147//   title -> lower, upper
148//
149// Lengths with the value 0x7 indicate no value and implies no change.
150// A length of 0 indicates a mapping to zero-length string.
151//
152// Body bytes:
153//   case folding bytes
154//   lowercase mapping bytes
155//   uppercase mapping bytes
156//   titlecase mapping bytes
157//   closure mapping bytes (for NFKC_Casefold). (TODO)
158//
159// Fallbacks:
160//   missing fold  -> lower
161//   missing title -> upper
162//   all missing   -> original rune
163//
164// exceptions starts with a dummy byte to enforce that there is no zero index
165// value.
166const (
167	lengthMask = 0x07
168	lengthBits = 3
169	noChange   = 0
170)
171
172// References to generated trie.
173
174var trie = newCaseTrie(0)
175
176var sparse = sparseBlocks{
177	values:  sparseValues[:],
178	offsets: sparseOffsets[:],
179}
180
181// Sparse block lookup code.
182
183// valueRange is an entry in a sparse block.
184type valueRange struct {
185	value  uint16
186	lo, hi byte
187}
188
189type sparseBlocks struct {
190	values  []valueRange
191	offsets []uint16
192}
193
194// lookup returns the value from values block n for byte b using binary search.
195func (s *sparseBlocks) lookup(n uint32, b byte) uint16 {
196	lo := s.offsets[n]
197	hi := s.offsets[n+1]
198	for lo < hi {
199		m := lo + (hi-lo)/2
200		r := s.values[m]
201		if r.lo <= b && b <= r.hi {
202			return r.value
203		}
204		if b < r.lo {
205			hi = m
206		} else {
207			lo = m + 1
208		}
209	}
210	return 0
211}
212
213// lastRuneForTesting is the last rune used for testing. Everything after this
214// is boring.
215const lastRuneForTesting = rune(0x1FFFF)
216