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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package puny provides functions for encoding/decoding to/from punycode.
package puny
import (
"errors"
"strings"
"unicode/utf8"
)
const (
maxInt32 int32 = 2147483647
base int32 = 36
tMin int32 = 1
baseMinusTMin = base - tMin
tMax int32 = 26
skew int32 = 38
damp int32 = 700
initialBias int32 = 72
initialN int32 = 128
)
var (
ErrOverflow = errors.New("overflow: input needs wider integers to process")
ErrNotBasic = errors.New("illegal input >= 0x80 (not a basic code point)")
ErrInvalidInput = errors.New("invalid input")
)
func adapt(delta, numPoints int32, firstTime bool) int32 {
if firstTime {
delta /= damp
} else {
delta /= 2
}
delta += delta / numPoints
k := int32(0)
for delta > baseMinusTMin*tMax/2 {
delta = delta / baseMinusTMin
k += base
}
return k + (baseMinusTMin+1)*delta/(delta+skew)
}
func basicToDigit(b byte) int32 {
switch {
case b >= '0' && b <= '9':
return int32(b - 22)
case b >= 'A' && b <= 'Z':
return int32(b - 'A')
case b >= 'a' && b <= 'z':
return int32(b - 'a')
}
return base
}
func digitToBasic(digit int32) byte {
switch {
case digit >= 0 && digit <= 25:
return byte(digit) + 'a'
case digit >= 26 && digit <= 35:
return byte(digit) - 26 + '0'
}
panic("unreachable")
}
func lastIndex(s string, c byte) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
}
func ascii(s string) bool {
for _, r := range s {
if r > 0x7e {
return false
}
}
return true
}
// Decode converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
func Decode(s string) (string, error) {
basic := lastIndex(s, '-')
output := make([]rune, 0, len(s))
for i := 0; i < basic; i++ {
b := s[i]
if b >= 0x80 {
return "", ErrNotBasic
}
output = append(output, rune(b))
}
i, n, bias, pos := int32(0), initialN, initialBias, basic+1
for pos < len(s) {
oldi, w, k := i, int32(1), base
for {
digit := basicToDigit(s[pos])
pos++
if digit >= base || digit > (maxInt32-i)/w {
return "", ErrOverflow
}
i += digit * w
t := k - bias
if t < tMin {
t = tMin
} else if t > tMax {
t = tMax
}
if digit < t {
break
}
if pos == len(s) {
return "", ErrInvalidInput
}
baseMinusT := base - t
if w > maxInt32/baseMinusT {
return "", ErrOverflow
}
w *= baseMinusT
k += base
}
out := int32(len(output) + 1)
bias = adapt(i-oldi, out, oldi == 0)
if i/out > maxInt32-n {
return "", ErrOverflow
}
n += i / out
i %= out
output = append(output, 0)
copy(output[i+1:], output[i:])
output[i] = rune(n)
i++
}
return string(output), nil
}
// Encode converts a string of Unicode symbols (e.g. a domain name label) to a
// Punycode string of ASCII-only symbols.
func Encode(input string) (string, error) {
n := initialN
delta := int32(0)
bias := initialBias
var output []byte
runes := 0
for _, r := range input {
if r >= 0x80 {
runes++
continue
}
output = append(output, byte(r))
}
basicLength := len(output)
handledCPCount := basicLength
if basicLength > 0 {
output = append(output, '-')
}
for runes > 0 {
m := maxInt32
for _, r := range input {
if r >= n && r < m {
m = r
}
}
handledCPCountPlusOne := int32(handledCPCount + 1)
if m-n > (maxInt32-delta)/handledCPCountPlusOne {
return "", ErrOverflow
}
delta += (m - n) * handledCPCountPlusOne
n = m
for _, r := range input {
if r < n {
delta++
if delta < 0 {
return "", ErrOverflow
}
continue
}
if r > n {
continue
}
q := delta
for k := base; ; k += base {
t := k - bias
if t < tMin {
t = tMin
} else if t > tMax {
t = tMax
}
if q < t {
break
}
qMinusT := q - t
baseMinusT := base - t
output = append(output, digitToBasic(t+qMinusT%baseMinusT))
q = qMinusT / baseMinusT
}
output = append(output, digitToBasic(q))
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength)
delta = 0
handledCPCount++
runes--
}
delta++
n++
}
return string(output), nil
}
func sep(r rune) bool { return r == '.' || r == '。' || r == '.' || r == '。' }
func mapLabels(s string, fn func(string) string) string {
var result string
i := strings.IndexByte(s, '@')
if i != -1 {
result = s[:i+1]
s = s[i+1:]
}
var labels []string
start := 0
for i, r := range s {
if !sep(r) {
continue
}
labels = append(labels, fn(s[start:i]))
start = i + utf8.RuneLen(r)
}
labels = append(labels, fn(s[start:]))
return result + strings.Join(labels, ".")
}
// ToUnicode converts a Punycode string representing a domain name or an email address
// to Unicode. Only the Punycoded parts of the input will be converted.
func ToUnicode(s string) string {
return mapLabels(s, func(s string) string {
if !strings.HasPrefix(s, "xn--") {
return s
}
d, err := Decode(strings.ToLower(s[4:]))
if err != nil {
return s
}
return d
})
}
// ToASCII converts a Unicode string representing a domain name or an email address to
// Punycode. Only the non-ASCII parts of the domain name will be converted.
func ToASCII(s string) string {
return mapLabels(s, func(s string) string {
if ascii(s) {
return s
}
d, err := Encode(s)
if err != nil {
return s
}
return "xn--" + d
})
}
|