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.
11 "golang.org/x/text/transform"
14 type nickAdditionalMapping struct {
15 // TODO: This transformer needs to be stateless somehow…
20 func (t *nickAdditionalMapping) Reset() {
25 func (t *nickAdditionalMapping) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
26 // RFC 7700 §2.1. Rules
28 // 2. Additional Mapping Rule: The additional mapping rule consists of
29 // the following sub-rules.
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.
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").
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").
45 r, size := utf8.DecodeRune(src[nSrc:])
46 if size == 0 { // Incomplete UTF-8 encoding
48 return nDst, nSrc, transform.ErrShortSrc
52 if unicode.Is(unicode.Zs, r) {
55 if t.prevSpace && t.notStart {
59 if size != copy(dst[nDst:], src[nSrc:nSrc+size]) {
61 return nDst, nSrc, transform.ErrShortDst
69 return nDst, nSrc, nil