profiles.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "unicode"
  7. "golang.org/x/text/runes"
  8. "golang.org/x/text/transform"
  9. "golang.org/x/text/unicode/norm"
  10. )
  11. var (
  12. // Implements the Nickname profile specified in RFC 8266.
  13. Nickname *Profile = nickname
  14. // Implements the UsernameCaseMapped profile specified in RFC 8265.
  15. UsernameCaseMapped *Profile = usernameCaseMap
  16. // Implements the UsernameCasePreserved profile specified in RFC 8265.
  17. UsernameCasePreserved *Profile = usernameNoCaseMap
  18. // Implements the OpaqueString profile defined in RFC 8265 for passwords and
  19. // other secure labels.
  20. OpaqueString *Profile = opaquestring
  21. )
  22. var (
  23. nickname = &Profile{
  24. options: getOpts(
  25. AdditionalMapping(func() transform.Transformer {
  26. return &nickAdditionalMapping{}
  27. }),
  28. IgnoreCase,
  29. Norm(norm.NFKC),
  30. DisallowEmpty,
  31. repeat,
  32. ),
  33. class: freeform,
  34. }
  35. usernameCaseMap = &Profile{
  36. options: getOpts(
  37. FoldWidth,
  38. LowerCase(),
  39. Norm(norm.NFC),
  40. BidiRule,
  41. ),
  42. class: identifier,
  43. }
  44. usernameNoCaseMap = &Profile{
  45. options: getOpts(
  46. FoldWidth,
  47. Norm(norm.NFC),
  48. BidiRule,
  49. ),
  50. class: identifier,
  51. }
  52. opaquestring = &Profile{
  53. options: getOpts(
  54. AdditionalMapping(func() transform.Transformer {
  55. return mapSpaces
  56. }),
  57. Norm(norm.NFC),
  58. DisallowEmpty,
  59. ),
  60. class: freeform,
  61. }
  62. )
  63. // mapSpaces is a shared value of a runes.Map transformer.
  64. var mapSpaces transform.Transformer = runes.Map(func(r rune) rune {
  65. if unicode.Is(unicode.Zs, r) {
  66. return ' '
  67. }
  68. return r
  69. })