trieval.go 6.3 KB

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