25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

214 lines
6.2 KiB

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