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.

strings_nickname.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //NOTE(dan): I need this because the default PRECIS API does not allow a way to retrieve the casefolded version of strings.
  5. // See also: https://github.com/golang/go/issues/17386
  6. // the content of this file is taken wholesale from the proper PRECIS API:
  7. // https://github.com/golang/text/tree/master/secure/precis
  8. package irc
  9. import (
  10. "unicode"
  11. "unicode/utf8"
  12. "golang.org/x/text/secure/precis"
  13. "golang.org/x/text/transform"
  14. "golang.org/x/text/unicode/norm"
  15. )
  16. type nickAdditionalMapping struct {
  17. // TODO: This transformer needs to be stateless somehow…
  18. notStart bool
  19. prevSpace bool
  20. }
  21. func (t *nickAdditionalMapping) Reset() {
  22. t.prevSpace = false
  23. t.notStart = false
  24. }
  25. func (t *nickAdditionalMapping) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  26. // RFC 7700 §2.1. Rules
  27. //
  28. // 2. Additional Mapping Rule: The additional mapping rule consists of
  29. // the following sub-rules.
  30. //
  31. // 1. Any instances of non-ASCII space MUST be mapped to ASCII
  32. // space (U+0020); a non-ASCII space is any Unicode code point
  33. // having a general category of "Zs", naturally with the
  34. // exception of U+0020.
  35. //
  36. // 2. Any instances of the ASCII space character at the beginning
  37. // or end of a nickname MUST be removed (e.g., "stpeter " is
  38. // mapped to "stpeter").
  39. //
  40. // 3. Interior sequences of more than one ASCII space character
  41. // MUST be mapped to a single ASCII space character (e.g.,
  42. // "St Peter" is mapped to "St Peter").
  43. for nSrc < len(src) {
  44. r, size := utf8.DecodeRune(src[nSrc:])
  45. if size == 0 { // Incomplete UTF-8 encoding
  46. if !atEOF {
  47. return nDst, nSrc, transform.ErrShortSrc
  48. }
  49. size = 1
  50. }
  51. if unicode.Is(unicode.Zs, r) {
  52. t.prevSpace = true
  53. } else {
  54. if t.prevSpace && t.notStart {
  55. dst[nDst] = ' '
  56. nDst += 1
  57. }
  58. if size != copy(dst[nDst:], src[nSrc:nSrc+size]) {
  59. nDst += size
  60. return nDst, nSrc, transform.ErrShortDst
  61. }
  62. nDst += size
  63. t.prevSpace = false
  64. t.notStart = true
  65. }
  66. nSrc += size
  67. }
  68. return nDst, nSrc, nil
  69. }
  70. var (
  71. NicknameProfile = precis.NewFreeform(
  72. precis.AdditionalMapping(func() transform.Transformer {
  73. return &nickAdditionalMapping{}
  74. }),
  75. precis.LowerCase(),
  76. precis.Norm(norm.NFKC),
  77. precis.DisallowEmpty,
  78. )
  79. )