您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

83 行
2.9 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package cases
  5. func (c info) cccVal() info {
  6. if c&exceptionBit != 0 {
  7. return info(exceptions[c>>exceptionShift]) & cccMask
  8. }
  9. return c & cccMask
  10. }
  11. func (c info) cccType() info {
  12. ccc := c.cccVal()
  13. if ccc <= cccZero {
  14. return cccZero
  15. }
  16. return ccc
  17. }
  18. // TODO: Implement full Unicode breaking algorithm:
  19. // 1) Implement breaking in separate package.
  20. // 2) Use the breaker here.
  21. // 3) Compare table size and performance of using the more generic breaker.
  22. //
  23. // Note that we can extend the current algorithm to be much more accurate. This
  24. // only makes sense, though, if the performance and/or space penalty of using
  25. // the generic breaker is big. Extra data will only be needed for non-cased
  26. // runes, which means there are sufficient bits left in the caseType.
  27. // ICU prohibits breaking in such cases as well.
  28. // For the purpose of title casing we use an approximation of the Unicode Word
  29. // Breaking algorithm defined in Annex #29:
  30. // https://www.unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table.
  31. //
  32. // For our approximation, we group the Word Break types into the following
  33. // categories, with associated rules:
  34. //
  35. // 1) Letter:
  36. // ALetter, Hebrew_Letter, Numeric, ExtendNumLet, Extend, Format_FE, ZWJ.
  37. // Rule: Never break between consecutive runes of this category.
  38. //
  39. // 2) Mid:
  40. // MidLetter, MidNumLet, Single_Quote.
  41. // (Cf. case-ignorable: MidLetter, MidNumLet, Single_Quote or cat is Mn,
  42. // Me, Cf, Lm or Sk).
  43. // Rule: Don't break between Letter and Mid, but break between two Mids.
  44. //
  45. // 3) Break:
  46. // Any other category: NewLine, MidNum, CR, LF, Double_Quote, Katakana, and
  47. // Other.
  48. // These categories should always result in a break between two cased letters.
  49. // Rule: Always break.
  50. //
  51. // Note 1: the Katakana and MidNum categories can, in esoteric cases, result in
  52. // preventing a break between two cased letters. For now we will ignore this
  53. // (e.g. [ALetter] [ExtendNumLet] [Katakana] [ExtendNumLet] [ALetter] and
  54. // [ALetter] [Numeric] [MidNum] [Numeric] [ALetter].)
  55. //
  56. // Note 2: the rule for Mid is very approximate, but works in most cases. To
  57. // improve, we could store the categories in the trie value and use a FA to
  58. // manage breaks. See TODO comment above.
  59. //
  60. // Note 3: according to the spec, it is possible for the Extend category to
  61. // introduce breaks between other categories grouped in Letter. However, this
  62. // is undesirable for our purposes. ICU prevents breaks in such cases as well.
  63. // isBreak returns whether this rune should introduce a break.
  64. func (c info) isBreak() bool {
  65. return c.cccVal() == cccBreak
  66. }
  67. // isLetter returns whether the rune is of break type ALetter, Hebrew_Letter,
  68. // Numeric, ExtendNumLet, or Extend.
  69. func (c info) isLetter() bool {
  70. ccc := c.cccVal()
  71. if ccc == cccZero {
  72. return !c.isCaseIgnorable()
  73. }
  74. return ccc != cccBreak
  75. }