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