nickname.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "unicode/utf8"
  8. "golang.org/x/text/transform"
  9. )
  10. type nickAdditionalMapping struct {
  11. // TODO: This transformer needs to be stateless somehow…
  12. notStart bool
  13. prevSpace bool
  14. }
  15. func (t *nickAdditionalMapping) Reset() {
  16. t.prevSpace = false
  17. t.notStart = false
  18. }
  19. func (t *nickAdditionalMapping) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  20. // RFC 8266 §2.1. Rules
  21. //
  22. // 2. Additional Mapping Rule: The additional mapping rule consists of
  23. // the following sub-rules.
  24. //
  25. // a. Map any instances of non-ASCII space to SPACE (U+0020); a
  26. // non-ASCII space is any Unicode code point having a general
  27. // category of "Zs", naturally with the exception of SPACE
  28. // (U+0020). (The inclusion of only ASCII space prevents
  29. // confusion with various non-ASCII space code points, many of
  30. // which are difficult to reproduce across different input
  31. // methods.)
  32. //
  33. // b. Remove any instances of the ASCII space character at the
  34. // beginning or end of a nickname (e.g., "stpeter " is mapped to
  35. // "stpeter").
  36. //
  37. // c. Map interior sequences of more than one ASCII space character
  38. // to a single ASCII space character (e.g., "St Peter" is
  39. // mapped to "St Peter").
  40. for nSrc < len(src) {
  41. r, size := utf8.DecodeRune(src[nSrc:])
  42. if size == 0 { // Incomplete UTF-8 encoding
  43. if !atEOF {
  44. return nDst, nSrc, transform.ErrShortSrc
  45. }
  46. size = 1
  47. }
  48. if unicode.Is(unicode.Zs, r) {
  49. t.prevSpace = true
  50. } else {
  51. if t.prevSpace && t.notStart {
  52. dst[nDst] = ' '
  53. nDst += 1
  54. }
  55. if size != copy(dst[nDst:], src[nSrc:nSrc+size]) {
  56. nDst += size
  57. return nDst, nSrc, transform.ErrShortDst
  58. }
  59. nDst += size
  60. t.prevSpace = false
  61. t.notStart = true
  62. }
  63. nSrc += size
  64. }
  65. return nDst, nSrc, nil
  66. }