You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
3.8 KiB

  1. // Copyright 2014 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. //go:generate go run gen.go gen_trieval.go
  5. // Package cases provides general and language-specific case mappers.
  6. package cases // import "golang.org/x/text/cases"
  7. import (
  8. "golang.org/x/text/language"
  9. "golang.org/x/text/transform"
  10. )
  11. // References:
  12. // - Unicode Reference Manual Chapter 3.13, 4.2, and 5.18.
  13. // - http://www.unicode.org/reports/tr29/
  14. // - http://www.unicode.org/Public/6.3.0/ucd/CaseFolding.txt
  15. // - http://www.unicode.org/Public/6.3.0/ucd/SpecialCasing.txt
  16. // - http://www.unicode.org/Public/6.3.0/ucd/DerivedCoreProperties.txt
  17. // - http://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakProperty.txt
  18. // - http://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakTest.txt
  19. // - http://userguide.icu-project.org/transforms/casemappings
  20. // TODO:
  21. // - Case folding
  22. // - Wide and Narrow?
  23. // - Segmenter option for title casing.
  24. // - ASCII fast paths
  25. // - Encode Soft-Dotted property within trie somehow.
  26. // A Caser transforms given input to a certain case. It implements
  27. // transform.Transformer.
  28. //
  29. // A Caser may be stateful and should therefore not be shared between
  30. // goroutines.
  31. type Caser struct {
  32. t transform.Transformer
  33. }
  34. // Bytes returns a new byte slice with the result of converting b to the case
  35. // form implemented by c.
  36. func (c Caser) Bytes(b []byte) []byte {
  37. b, _, _ = transform.Bytes(c.t, b)
  38. return b
  39. }
  40. // String returns a string with the result of transforming s to the case form
  41. // implemented by c.
  42. func (c Caser) String(s string) string {
  43. s, _, _ = transform.String(c.t, s)
  44. return s
  45. }
  46. // Reset resets the Caser to be reused for new input after a previous call to
  47. // Transform.
  48. func (c Caser) Reset() { c.t.Reset() }
  49. // Transform implements the Transformer interface and transforms the given input
  50. // to the case form implemented by c.
  51. func (c Caser) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  52. return c.t.Transform(dst, src, atEOF)
  53. }
  54. // Upper returns a Caser for language-specific uppercasing.
  55. func Upper(t language.Tag, opts ...Option) Caser {
  56. return Caser{makeUpper(t, getOpts(opts...))}
  57. }
  58. // Lower returns a Caser for language-specific lowercasing.
  59. func Lower(t language.Tag, opts ...Option) Caser {
  60. return Caser{makeLower(t, getOpts(opts...))}
  61. }
  62. // Title returns a Caser for language-specific title casing. It uses an
  63. // approximation of the default Unicode Word Break algorithm.
  64. func Title(t language.Tag, opts ...Option) Caser {
  65. return Caser{makeTitle(t, getOpts(opts...))}
  66. }
  67. // Fold returns a Caser that implements Unicode case folding. The returned Caser
  68. // is stateless and safe to use concurrently by multiple goroutines.
  69. //
  70. // Case folding does not normalize the input and may not preserve a normal form.
  71. // Use the collate or search package for more convenient and linguistically
  72. // sound comparisons. Use unicode/precis for string comparisons where security
  73. // aspects are a concern.
  74. func Fold(opts ...Option) Caser {
  75. return Caser{makeFold(getOpts(opts...))}
  76. }
  77. // An Option is used to modify the behavior of a Caser.
  78. type Option func(o *options)
  79. var (
  80. // NoLower disables the lowercasing of non-leading letters for a title
  81. // caser.
  82. NoLower Option = noLower
  83. // Compact omits mappings in case folding for characters that would grow the
  84. // input. (Unimplemented.)
  85. Compact Option = compact
  86. )
  87. // TODO: option to preserve a normal form, if applicable?
  88. type options struct {
  89. noLower bool
  90. simple bool
  91. // TODO: segmenter, max ignorable, alternative versions, etc.
  92. noFinalSigma bool // Only used for testing.
  93. }
  94. func getOpts(o ...Option) (res options) {
  95. for _, f := range o {
  96. f(&res)
  97. }
  98. return
  99. }
  100. func noLower(o *options) {
  101. o.noLower = true
  102. }
  103. func compact(o *options) {
  104. o.simple = true
  105. }