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