Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

216 lignes
6.3 KiB

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