-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidna.v
287 lines (224 loc) · 5.89 KB
/
idna.v
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
module idna
import encoding.utf8
import math
const (
max_int = 2147483647 // 0x7FFFFFFF
base = 36
t_min = 1
t_max = 26
skew = 38
damp = 700
initial_bias = 72
initial_n = 128 // 0x80
delimiter = '-' // '\x2D'
)
pub fn encode(input2 string) string {
mut output := ''
mut basic_output := ''
input := ucs2decode(input2)
// Copy all basic code points to the output
for c in input {
if c < 0x80 {
basic_output += u8(c).ascii_str()
}
}
basic_output_len := basic_output.len
output += basic_output
if basic_output_len > 0 {
output += delimiter
}
mut n := initial_n
mut delta := 0
mut bias := initial_bias
mut h := basic_output_len
for h < input.len {
mut m := max_int
for c in input {
if c >= n && c < m {
m = c
}
}
// @TODO: Guard against overflow here!
if (m - n) > math.floor((max_int - delta) / (h + 1)) {
error('overflow')
}
delta += (m - n) * (h + 1)
n = m
for c in input {
if c < n && delta + 1 > max_int {
error("overflow")
}
if c < n {
delta += 1
}
if c == n {
mut q := delta
mut k := base
for {
t := if k <= bias { t_min } else if k >= bias + t_max { t_max } else { k - bias }
if q < t {
break
}
mut q_minus_t := q -t
mut base_minus_t := base - t
output += digit_to_basic(t + q_minus_t % base_minus_t, 0).ascii_str()
q = int(math.floor(q_minus_t / base_minus_t))
k += base
}
output += digit_to_basic(q, 0).ascii_str()
bias = adapt(delta, h+1, h == basic_output_len)
delta = 0
h += 1
}
}
delta += 1
n += 1
}
return output
}
pub fn decode(input_raw string) string {
mut input := input_raw.to_lower()
// Check if the input is already in decoded form
if !input.starts_with('xn--') && !contains_only_ascii_and_hyphen(input) {
return input_raw // Return the original input if it's not Punycode
}
mut output := []rune{}
mut n := u16(initial_n)
mut i := 0
mut bias := initial_bias
mut basic := input.last_index_u8('-'[0])
if basic == -1 {
basic = 0
}
for j in 0..basic {
if input[j] >= 0x80 {
error('not-basic')
}
output << input[j]
}
mut index := if basic > 0 { basic + 1 } else { 0 }
for index < input.runes().len {
old_i := i
mut w := 1
mut k := base
for {
if index >= input.len {
error('invalid-input-index')
}
digit := basic_to_digit(input[index])
index += 1
if digit >= base {
error('invalid-input-digit')
}
if digit > (max_int - i) / w {
error('overflow-d')
}
i += digit * w
t := if k <= bias { t_min } else { if k >= bias + t_max { t_max } else { k - bias } }
if digit < t {
break
}
if w > max_int / (base - t) {
error('overflow-w')
}
w *= (base - t)
k += base
}
out := output.len + 1
bias = adapt(i - old_i, out, old_i == 0)
if i / out > max_int - n {
error('overflow')
}
n += u16(i / out)
i %= out
output.insert(i, n)
i += 1
}
return output.string()
}
@[inline]
fn contains_only_ascii_and_hyphen(s string) bool {
for c in s {
if (c < 0x21 || c > 0x7E) && c != `-` {
return false
}
}
return true
}
@[inline]
fn adapt(delta int, num_points int, first_time bool) int {
mut d := delta
if first_time {
d /= damp
} else {
d /= 2
}
d += d / num_points
mut k := 0
for d > (base - t_min) * t_max / 2 {
d /= (base - t_min)
k += base
}
return k + (base - t_min + 1) * d / (d + skew)
}
fn decode_digit(digit int) int {
if digit < 26 {
return digit + 26
}
return digit - 26
}
fn digit_to_basic(digit int, flag int) u8 {
return u8(digit + 22 + 75 * u8(digit < 26) - (u8(flag != 0) << 5))
}
// @TODO: works with 'cafè', but not with 'café.com' - splitting not working as desired
pub fn to_unicode(input string) string {
labels := input.to_lower().split('.')
decoded_labels := labels.map(fn (label string) string {
if label.len >= 4 && label[0..4] == "xn--" {
return decode(label[4..])
}
return label
})
return decoded_labels.join('.')
}
pub fn to_ascii(input string) string {
labels := input.split('.')
encoded_labels := labels.map(fn (label string) string {
if non_ascii_check(label) {
return "xn--" + encode(label)
}
return label
})
return encoded_labels.join('.')
}
fn ucs2decode(input string) []int {
mut output := []int{}
mut c := 0
mut i := 0
for r in input.runes() {
c = utf8.get_uchar(input, i)
output << c
i += r.str().len
}
return output
}
fn non_ascii_check(input string) bool {
for c in input {
if c > initial_n {
return true
}
}
return false
}
fn basic_to_digit(codepoint u8) u8 {
if codepoint >= 0x30 && codepoint < 0x3A {
return 26 + (codepoint - 0x30)
}
if codepoint >= 0x41 && codepoint < 0x5B {
return codepoint - 0x41
}
if codepoint >= 0x61 && codepoint < 0x7B {
return codepoint - 0x61
}
return base
}