forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringprep.go
98 lines (82 loc) · 1.92 KB
/
stringprep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sasl
import (
"unicode"
"unicode/utf8"
"github.com/twstrike/coyim/Godeps/_workspace/src/golang.org/x/text/transform"
"github.com/twstrike/coyim/Godeps/_workspace/src/golang.org/x/text/unicode/norm"
)
type replaceTransformer func(r rune) rune
func (t replaceTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
var runeBytes [utf8.UTFMax]byte
for r, sz := rune(0), 0; len(src) > 0; src = src[sz:] {
if r = rune(src[0]); r < utf8.RuneSelf {
sz = 1
} else {
r, sz = utf8.DecodeRune(src)
if sz == 1 {
// Invalid rune.
if !atEOF && !utf8.FullRune(src) {
err = transform.ErrShortSrc
break
}
}
}
dsz := utf8.EncodeRune(runeBytes[:], t(r))
if nDst+dsz > len(dst) {
err = transform.ErrShortDst
break
}
nDst += copy(dst[nDst:], runeBytes[:dsz])
nSrc += sz
}
return
}
func (t replaceTransformer) Reset() {}
var nonASCIISpaceTransformer = replaceTransformer(func(r rune) rune {
space := '\u0020'
if unicode.Is(unicode.Zs, r) {
return space
}
//TODO: replace by a unicode.RangeTable but I could not understand the Stride thing
if r == '\u200B' {
return space
}
return r
})
var mappedToNothing = transform.RemoveFunc(func(r rune) bool {
//TODO: replace by a unicode.RangeTable
switch r {
case '\u00AD':
case '\u034F':
case '\u1806':
case '\u180B':
case '\u180C':
case '\u180D':
case '\u200C':
case '\u200D':
case '\u2060':
case '\uFE00':
case '\uFE01':
case '\uFE02':
case '\uFE03':
case '\uFE04':
case '\uFE05':
case '\uFE06':
case '\uFE07':
case '\uFE08':
case '\uFE09':
case '\uFE0A':
case '\uFE0B':
case '\uFE0C':
case '\uFE0D':
case '\uFE0E':
case '\uFE0F':
case '\uFEFF':
default:
return false
}
return true
})
// Stringprep implements Stringprep Profile for User Names and Passwords (RFC 4013)
// as a transform.Transformer
var Stringprep = transform.Chain(nonASCIISpaceTransformer, mappedToNothing, norm.NFKC)