| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package puny |
| 7 |
|
| 8 |
import ( |
| 9 |
"errors" |
| 10 |
"strings" |
| 11 |
"unicode/utf8" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
maxInt32 int32 = 2147483647 |
| 16 |
base int32 = 36 |
| 17 |
tMin int32 = 1 |
| 18 |
baseMinusTMin = base - tMin |
| 19 |
tMax int32 = 26 |
| 20 |
skew int32 = 38 |
| 21 |
damp int32 = 700 |
| 22 |
initialBias int32 = 72 |
| 23 |
initialN int32 = 128 |
| 24 |
) |
| 25 |
|
| 26 |
var ( |
| 27 |
ErrOverflow = errors.New("overflow: input needs wider integers to process") |
| 28 |
ErrNotBasic = errors.New("illegal input >= 0x80 (not a basic code point)") |
| 29 |
ErrInvalidInput = errors.New("invalid input") |
| 30 |
) |
| 31 |
|
| 32 |
func adapt(delta, numPoints int32, firstTime bool) int32 { |
| 33 |
if firstTime { |
| 34 |
delta /= damp |
| 35 |
} else { |
| 36 |
delta /= 2 |
| 37 |
} |
| 38 |
delta += delta / numPoints |
| 39 |
k := int32(0) |
| 40 |
for delta > baseMinusTMin*tMax/2 { |
| 41 |
delta /= baseMinusTMin |
| 42 |
k += base |
| 43 |
} |
| 44 |
return k + (baseMinusTMin+1)*delta/(delta+skew) |
| 45 |
} |
| 46 |
|
| 47 |
func basicToDigit(b byte) int32 { |
| 48 |
switch { |
| 49 |
case b >= '0' && b <= '9': |
| 50 |
return int32(b - 22) |
| 51 |
case b >= 'A' && b <= 'Z': |
| 52 |
return int32(b - 'A') |
| 53 |
case b >= 'a' && b <= 'z': |
| 54 |
return int32(b - 'a') |
| 55 |
} |
| 56 |
return base |
| 57 |
} |
| 58 |
|
| 59 |
func digitToBasic(digit int32) byte { |
| 60 |
switch { |
| 61 |
case digit >= 0 && digit <= 25: |
| 62 |
return byte(digit) + 'a' |
| 63 |
case digit >= 26 && digit <= 35: |
| 64 |
return byte(digit) - 26 + '0' |
| 65 |
} |
| 66 |
panic("unreachable") |
| 67 |
} |
| 68 |
|
| 69 |
func lastIndex(s string, c byte) int { |
| 70 |
for i := len(s) - 1; i >= 0; i-- { |
| 71 |
if s[i] == c { |
| 72 |
return i |
| 73 |
} |
| 74 |
} |
| 75 |
return -1 |
| 76 |
} |
| 77 |
|
| 78 |
func ascii(s string) bool { |
| 79 |
for _, r := range s { |
| 80 |
if r > 0x7e { |
| 81 |
return false |
| 82 |
} |
| 83 |
} |
| 84 |
return true |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func Decode(s string) (string, error) { |
| 89 |
basic := lastIndex(s, '-') |
| 90 |
output := make([]rune, 0, len(s)) |
| 91 |
for i := 0; i < basic; i++ { |
| 92 |
b := s[i] |
| 93 |
if b >= 0x80 { |
| 94 |
return "", ErrNotBasic |
| 95 |
} |
| 96 |
output = append(output, rune(b)) |
| 97 |
} |
| 98 |
|
| 99 |
i, n, bias, pos := int32(0), initialN, initialBias, basic+1 |
| 100 |
|
| 101 |
for pos < len(s) { |
| 102 |
oldi, w, k := i, int32(1), base |
| 103 |
for { |
| 104 |
digit := basicToDigit(s[pos]) |
| 105 |
pos++ |
| 106 |
|
| 107 |
if digit >= base || digit > (maxInt32-i)/w { |
| 108 |
return "", ErrOverflow |
| 109 |
} |
| 110 |
|
| 111 |
i += digit * w |
| 112 |
|
| 113 |
t := k - bias |
| 114 |
if t < tMin { |
| 115 |
t = tMin |
| 116 |
} else if t > tMax { |
| 117 |
t = tMax |
| 118 |
} |
| 119 |
|
| 120 |
if digit < t { |
| 121 |
break |
| 122 |
} |
| 123 |
|
| 124 |
if pos == len(s) { |
| 125 |
return "", ErrInvalidInput |
| 126 |
} |
| 127 |
|
| 128 |
baseMinusT := base - t |
| 129 |
if w > maxInt32/baseMinusT { |
| 130 |
return "", ErrOverflow |
| 131 |
} |
| 132 |
|
| 133 |
w *= baseMinusT |
| 134 |
k += base |
| 135 |
} |
| 136 |
|
| 137 |
out := int32(len(output) + 1) |
| 138 |
bias = adapt(i-oldi, out, oldi == 0) |
| 139 |
|
| 140 |
if i/out > maxInt32-n { |
| 141 |
return "", ErrOverflow |
| 142 |
} |
| 143 |
|
| 144 |
n += i / out |
| 145 |
i %= out |
| 146 |
|
| 147 |
output = append(output, 0) |
| 148 |
copy(output[i+1:], output[i:]) |
| 149 |
output[i] = n |
| 150 |
|
| 151 |
i++ |
| 152 |
} |
| 153 |
|
| 154 |
return string(output), nil |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
func Encode(input string) (string, error) { |
| 160 |
n := initialN |
| 161 |
delta := int32(0) |
| 162 |
bias := initialBias |
| 163 |
|
| 164 |
var output []byte |
| 165 |
runes := 0 |
| 166 |
for _, r := range input { |
| 167 |
if r >= 0x80 { |
| 168 |
runes++ |
| 169 |
continue |
| 170 |
} |
| 171 |
output = append(output, byte(r)) |
| 172 |
} |
| 173 |
|
| 174 |
basicLength := len(output) |
| 175 |
handledCPCount := basicLength |
| 176 |
|
| 177 |
if basicLength > 0 { |
| 178 |
output = append(output, '-') |
| 179 |
} |
| 180 |
|
| 181 |
for runes > 0 { |
| 182 |
m := maxInt32 |
| 183 |
for _, r := range input { |
| 184 |
if r >= n && r < m { |
| 185 |
m = r |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
handledCPCountPlusOne := int32(handledCPCount + 1) |
| 190 |
if m-n > (maxInt32-delta)/handledCPCountPlusOne { |
| 191 |
return "", ErrOverflow |
| 192 |
} |
| 193 |
|
| 194 |
delta += (m - n) * handledCPCountPlusOne |
| 195 |
n = m |
| 196 |
|
| 197 |
for _, r := range input { |
| 198 |
if r < n { |
| 199 |
delta++ |
| 200 |
if delta < 0 { |
| 201 |
return "", ErrOverflow |
| 202 |
} |
| 203 |
continue |
| 204 |
} |
| 205 |
if r > n { |
| 206 |
continue |
| 207 |
} |
| 208 |
q := delta |
| 209 |
for k := base; ; k += base { |
| 210 |
t := k - bias |
| 211 |
if t < tMin { |
| 212 |
t = tMin |
| 213 |
} else if t > tMax { |
| 214 |
t = tMax |
| 215 |
} |
| 216 |
if q < t { |
| 217 |
break |
| 218 |
} |
| 219 |
qMinusT := q - t |
| 220 |
baseMinusT := base - t |
| 221 |
output = append(output, digitToBasic(t+qMinusT%baseMinusT)) |
| 222 |
q = qMinusT / baseMinusT |
| 223 |
} |
| 224 |
|
| 225 |
output = append(output, digitToBasic(q)) |
| 226 |
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength) |
| 227 |
delta = 0 |
| 228 |
handledCPCount++ |
| 229 |
runes-- |
| 230 |
} |
| 231 |
delta++ |
| 232 |
n++ |
| 233 |
} |
| 234 |
return string(output), nil |
| 235 |
} |
| 236 |
|
| 237 |
func sep(r rune) bool { return r == '.' || r == '。' || r == '.' || r == '。' } |
| 238 |
|
| 239 |
func mapLabels(s string, fn func(string) string) string { |
| 240 |
var result string |
| 241 |
i := strings.IndexByte(s, '@') |
| 242 |
if i != -1 { |
| 243 |
result = s[:i+1] |
| 244 |
s = s[i+1:] |
| 245 |
} |
| 246 |
var labels []string |
| 247 |
start := 0 |
| 248 |
for i, r := range s { |
| 249 |
if !sep(r) { |
| 250 |
continue |
| 251 |
} |
| 252 |
labels = append(labels, fn(s[start:i])) |
| 253 |
start = i + utf8.RuneLen(r) |
| 254 |
} |
| 255 |
labels = append(labels, fn(s[start:])) |
| 256 |
return result + strings.Join(labels, ".") |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
func ToUnicode(s string) string { |
| 262 |
return mapLabels(s, func(s string) string { |
| 263 |
if !strings.HasPrefix(s, "xn--") { |
| 264 |
return s |
| 265 |
} |
| 266 |
d, err := Decode(strings.ToLower(s[4:])) |
| 267 |
if err != nil { |
| 268 |
return s |
| 269 |
} |
| 270 |
return d |
| 271 |
}) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
func ToASCII(s string) string { |
| 277 |
return mapLabels(s, func(s string) string { |
| 278 |
if ascii(s) { |
| 279 |
return s |
| 280 |
} |
| 281 |
d, err := Encode(s) |
| 282 |
if err != nil { |
| 283 |
return s |
| 284 |
} |
| 285 |
return "xn--" + d |
| 286 |
}) |
| 287 |
} |