options.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 precis
  5. import (
  6. "golang.org/x/text/cases"
  7. "golang.org/x/text/runes"
  8. "golang.org/x/text/transform"
  9. "golang.org/x/text/unicode/norm"
  10. "golang.org/x/text/width"
  11. )
  12. // An Option is used to define the behavior and rules of a Profile.
  13. type Option func(*options)
  14. type options struct {
  15. // Preparation options
  16. foldWidth bool
  17. // Enforcement options
  18. cases transform.Transformer
  19. disallow runes.Set
  20. norm norm.Form
  21. additional []func() transform.Transformer
  22. width *width.Transformer
  23. disallowEmpty bool
  24. bidiRule bool
  25. // Comparison options
  26. ignorecase bool
  27. }
  28. func getOpts(o ...Option) (res options) {
  29. for _, f := range o {
  30. f(&res)
  31. }
  32. return
  33. }
  34. var (
  35. // The IgnoreCase option causes the profile to perform a case insensitive
  36. // comparison during the PRECIS comparison step.
  37. IgnoreCase Option = ignoreCase
  38. // The FoldWidth option causes the profile to map non-canonical wide and
  39. // narrow variants to their decomposition mapping. This is useful for
  40. // profiles that are based on the identifier class which would otherwise
  41. // disallow such characters.
  42. FoldWidth Option = foldWidth
  43. // The DisallowEmpty option causes the enforcement step to return an error if
  44. // the resulting string would be empty.
  45. DisallowEmpty Option = disallowEmpty
  46. // The BidiRule option causes the Bidi Rule defined in RFC 5893 to be
  47. // applied.
  48. BidiRule Option = bidiRule
  49. )
  50. var (
  51. ignoreCase = func(o *options) {
  52. o.ignorecase = true
  53. }
  54. foldWidth = func(o *options) {
  55. o.foldWidth = true
  56. }
  57. disallowEmpty = func(o *options) {
  58. o.disallowEmpty = true
  59. }
  60. bidiRule = func(o *options) {
  61. o.bidiRule = true
  62. }
  63. )
  64. // The AdditionalMapping option defines the additional mapping rule for the
  65. // Profile by applying Transformer's in sequence.
  66. func AdditionalMapping(t ...func() transform.Transformer) Option {
  67. return func(o *options) {
  68. o.additional = t
  69. }
  70. }
  71. // The Norm option defines a Profile's normalization rule. Defaults to NFC.
  72. func Norm(f norm.Form) Option {
  73. return func(o *options) {
  74. o.norm = f
  75. }
  76. }
  77. // The FoldCase option defines a Profile's case mapping rule. Options can be
  78. // provided to determine the type of case folding used.
  79. func FoldCase(opts ...cases.Option) Option {
  80. return func(o *options) {
  81. o.cases = cases.Fold(opts...)
  82. }
  83. }
  84. // The Disallow option further restricts a Profile's allowed characters beyond
  85. // what is disallowed by the underlying string class.
  86. func Disallow(set runes.Set) Option {
  87. return func(o *options) {
  88. o.disallow = set
  89. }
  90. }