wordrules.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package uniseg
  2. import "unicode/utf8"
  3. // The states of the word break parser.
  4. const (
  5. wbAny = iota
  6. wbCR
  7. wbLF
  8. wbNewline
  9. wbWSegSpace
  10. wbHebrewLetter
  11. wbALetter
  12. wbWB7
  13. wbWB7c
  14. wbNumeric
  15. wbWB11
  16. wbKatakana
  17. wbExtendNumLet
  18. wbOddRI
  19. wbEvenRI
  20. wbZWJBit = 16 // This bit is set for any states followed by at least one zero-width joiner (see WB4 and WB3c).
  21. )
  22. // The word break parser's breaking instructions.
  23. const (
  24. wbDontBreak = iota
  25. wbBreak
  26. )
  27. // The word break parser's state transitions. It's anologous to grTransitions,
  28. // see comments there for details. Unicode version 14.0.0.
  29. var wbTransitions = map[[2]int][3]int{
  30. // WB3b.
  31. {wbAny, prNewline}: {wbNewline, wbBreak, 32},
  32. {wbAny, prCR}: {wbCR, wbBreak, 32},
  33. {wbAny, prLF}: {wbLF, wbBreak, 32},
  34. // WB3a.
  35. {wbNewline, prAny}: {wbAny, wbBreak, 31},
  36. {wbCR, prAny}: {wbAny, wbBreak, 31},
  37. {wbLF, prAny}: {wbAny, wbBreak, 31},
  38. // WB3.
  39. {wbCR, prLF}: {wbLF, wbDontBreak, 30},
  40. // WB3d.
  41. {wbAny, prWSegSpace}: {wbWSegSpace, wbBreak, 9990},
  42. {wbWSegSpace, prWSegSpace}: {wbWSegSpace, wbDontBreak, 34},
  43. // WB5.
  44. {wbAny, prALetter}: {wbALetter, wbBreak, 9990},
  45. {wbAny, prHebrewLetter}: {wbHebrewLetter, wbBreak, 9990},
  46. {wbALetter, prALetter}: {wbALetter, wbDontBreak, 50},
  47. {wbALetter, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 50},
  48. {wbHebrewLetter, prALetter}: {wbALetter, wbDontBreak, 50},
  49. {wbHebrewLetter, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 50},
  50. // WB7. Transitions to wbWB7 handled by transitionWordBreakState().
  51. {wbWB7, prALetter}: {wbALetter, wbDontBreak, 70},
  52. {wbWB7, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 70},
  53. // WB7a.
  54. {wbHebrewLetter, prSingleQuote}: {wbAny, wbDontBreak, 71},
  55. // WB7c. Transitions to wbWB7c handled by transitionWordBreakState().
  56. {wbWB7c, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 73},
  57. // WB8.
  58. {wbAny, prNumeric}: {wbNumeric, wbBreak, 9990},
  59. {wbNumeric, prNumeric}: {wbNumeric, wbDontBreak, 80},
  60. // WB9.
  61. {wbALetter, prNumeric}: {wbNumeric, wbDontBreak, 90},
  62. {wbHebrewLetter, prNumeric}: {wbNumeric, wbDontBreak, 90},
  63. // WB10.
  64. {wbNumeric, prALetter}: {wbALetter, wbDontBreak, 100},
  65. {wbNumeric, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 100},
  66. // WB11. Transitions to wbWB11 handled by transitionWordBreakState().
  67. {wbWB11, prNumeric}: {wbNumeric, wbDontBreak, 110},
  68. // WB13.
  69. {wbAny, prKatakana}: {wbKatakana, wbBreak, 9990},
  70. {wbKatakana, prKatakana}: {wbKatakana, wbDontBreak, 130},
  71. // WB13a.
  72. {wbAny, prExtendNumLet}: {wbExtendNumLet, wbBreak, 9990},
  73. {wbALetter, prExtendNumLet}: {wbExtendNumLet, wbDontBreak, 131},
  74. {wbHebrewLetter, prExtendNumLet}: {wbExtendNumLet, wbDontBreak, 131},
  75. {wbNumeric, prExtendNumLet}: {wbExtendNumLet, wbDontBreak, 131},
  76. {wbKatakana, prExtendNumLet}: {wbExtendNumLet, wbDontBreak, 131},
  77. {wbExtendNumLet, prExtendNumLet}: {wbExtendNumLet, wbDontBreak, 131},
  78. // WB13b.
  79. {wbExtendNumLet, prALetter}: {wbALetter, wbDontBreak, 132},
  80. {wbExtendNumLet, prHebrewLetter}: {wbHebrewLetter, wbDontBreak, 132},
  81. {wbExtendNumLet, prNumeric}: {wbNumeric, wbDontBreak, 132},
  82. {wbExtendNumLet, prKatakana}: {prKatakana, wbDontBreak, 132},
  83. }
  84. // transitionWordBreakState determines the new state of the word break parser
  85. // given the current state and the next code point. It also returns whether a
  86. // word boundary was detected. If more than one code point is needed to
  87. // determine the new state, the byte slice or the string starting after rune "r"
  88. // can be used (whichever is not nil or empty) for further lookups.
  89. func transitionWordBreakState(state int, r rune, b []byte, str string) (newState int, wordBreak bool) {
  90. // Determine the property of the next character.
  91. nextProperty := property(workBreakCodePoints, r)
  92. // "Replacing Ignore Rules".
  93. if nextProperty == prZWJ {
  94. // WB4 (for zero-width joiners).
  95. if state == wbNewline || state == wbCR || state == wbLF {
  96. return wbAny | wbZWJBit, true // Make sure we don't apply WB4 to WB3a.
  97. }
  98. if state < 0 {
  99. return wbAny | wbZWJBit, false
  100. }
  101. return state | wbZWJBit, false
  102. } else if nextProperty == prExtend || nextProperty == prFormat {
  103. // WB4 (for Extend and Format).
  104. if state == wbNewline || state == wbCR || state == wbLF {
  105. return wbAny, true // Make sure we don't apply WB4 to WB3a.
  106. }
  107. if state == wbWSegSpace || state == wbAny|wbZWJBit {
  108. return wbAny, false // We don't break but this is also not WB3d or WB3c.
  109. }
  110. if state < 0 {
  111. return wbAny, false
  112. }
  113. return state, false
  114. } else if nextProperty == prExtendedPictographic && state >= 0 && state&wbZWJBit != 0 {
  115. // WB3c.
  116. return wbAny, false
  117. }
  118. if state >= 0 {
  119. state = state &^ wbZWJBit
  120. }
  121. // Find the applicable transition in the table.
  122. var rule int
  123. transition, ok := wbTransitions[[2]int{state, nextProperty}]
  124. if ok {
  125. // We have a specific transition. We'll use it.
  126. newState, wordBreak, rule = transition[0], transition[1] == wbBreak, transition[2]
  127. } else {
  128. // No specific transition found. Try the less specific ones.
  129. transAnyProp, okAnyProp := wbTransitions[[2]int{state, prAny}]
  130. transAnyState, okAnyState := wbTransitions[[2]int{wbAny, nextProperty}]
  131. if okAnyProp && okAnyState {
  132. // Both apply. We'll use a mix (see comments for grTransitions).
  133. newState, wordBreak, rule = transAnyState[0], transAnyState[1] == wbBreak, transAnyState[2]
  134. if transAnyProp[2] < transAnyState[2] {
  135. wordBreak, rule = transAnyProp[1] == wbBreak, transAnyProp[2]
  136. }
  137. } else if okAnyProp {
  138. // We only have a specific state.
  139. newState, wordBreak, rule = transAnyProp[0], transAnyProp[1] == wbBreak, transAnyProp[2]
  140. // This branch will probably never be reached because okAnyState will
  141. // always be true given the current transition map. But we keep it here
  142. // for future modifications to the transition map where this may not be
  143. // true anymore.
  144. } else if okAnyState {
  145. // We only have a specific property.
  146. newState, wordBreak, rule = transAnyState[0], transAnyState[1] == wbBreak, transAnyState[2]
  147. } else {
  148. // No known transition. WB999: Any ÷ Any.
  149. newState, wordBreak, rule = wbAny, true, 9990
  150. }
  151. }
  152. // For those rules that need to look up runes further in the string, we
  153. // determine the property after nextProperty, skipping over Format, Extend,
  154. // and ZWJ (according to WB4). It's -1 if not needed, if such a rune cannot
  155. // be determined (because the text ends or the rune is faulty).
  156. farProperty := -1
  157. if rule > 60 &&
  158. (state == wbALetter || state == wbHebrewLetter || state == wbNumeric) &&
  159. (nextProperty == prMidLetter || nextProperty == prMidNumLet || nextProperty == prSingleQuote || // WB6.
  160. nextProperty == prDoubleQuote || // WB7b.
  161. nextProperty == prMidNum) { // WB12.
  162. for {
  163. var (
  164. r rune
  165. length int
  166. )
  167. if b != nil { // Byte slice version.
  168. r, length = utf8.DecodeRune(b)
  169. b = b[length:]
  170. } else { // String version.
  171. r, length = utf8.DecodeRuneInString(str)
  172. str = str[length:]
  173. }
  174. if r == utf8.RuneError {
  175. break
  176. }
  177. prop := property(workBreakCodePoints, r)
  178. if prop == prExtend || prop == prFormat || prop == prZWJ {
  179. continue
  180. }
  181. farProperty = prop
  182. break
  183. }
  184. }
  185. // WB6.
  186. if rule > 60 &&
  187. (state == wbALetter || state == wbHebrewLetter) &&
  188. (nextProperty == prMidLetter || nextProperty == prMidNumLet || nextProperty == prSingleQuote) &&
  189. (farProperty == prALetter || farProperty == prHebrewLetter) {
  190. return wbWB7, false
  191. }
  192. // WB7b.
  193. if rule > 72 &&
  194. state == wbHebrewLetter &&
  195. nextProperty == prDoubleQuote &&
  196. farProperty == prHebrewLetter {
  197. return wbWB7c, false
  198. }
  199. // WB12.
  200. if rule > 120 &&
  201. state == wbNumeric &&
  202. (nextProperty == prMidNum || nextProperty == prMidNumLet || nextProperty == prSingleQuote) &&
  203. farProperty == prNumeric {
  204. return wbWB11, false
  205. }
  206. // WB15 and WB16.
  207. if newState == wbAny && nextProperty == prRegionalIndicator {
  208. if state != wbOddRI && state != wbEvenRI { // Includes state == -1.
  209. // Transition into the first RI.
  210. return wbOddRI, true
  211. }
  212. if state == wbOddRI {
  213. // Don't break pairs of Regional Indicators.
  214. return wbEvenRI, false
  215. }
  216. return wbOddRI, true // We can break after a pair.
  217. }
  218. return
  219. }