1 // Code generated by running "go run gen.go -core" in golang.org/x/text. DO NOT EDIT.
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
9 // This file implements the Punycode algorithm from RFC 3492.
17 // These parameter values are specified in section 5.
19 // All computation is done with int32s, so that overflow behavior is identical
20 // regardless of whether int is 32-bit or 64-bit.
24 initialBias
int32 = 72
31 func punyError(s
string) error
{ return &labelError
{s
, "A3"} }
33 // decode decodes a string as specified in section 6.2.
34 func decode(encoded
string) (string, error
) {
38 pos
:= 1 + strings
.LastIndex(encoded
, "-")
40 return "", punyError(encoded
)
42 if pos
== len(encoded
) {
43 return encoded
[:len(encoded
)-1], nil
45 output
:= make([]rune
, 0, len(encoded
))
47 for _
, r
:= range encoded
[:pos
-1] {
48 output
= append(output
, r
)
51 i
, n
, bias
:= int32(0), initialN
, initialBias
52 for pos
< len(encoded
) {
53 oldI
, w
:= i
, int32(1)
54 for k
:= base
; ; k
+= base
{
55 if pos
== len(encoded
) {
56 return "", punyError(encoded
)
58 digit
, ok
:= decodeDigit(encoded
[pos
])
60 return "", punyError(encoded
)
65 return "", punyError(encoded
)
77 if w
>= math
.MaxInt32
/base
{
78 return "", punyError(encoded
)
81 x
:= int32(len(output
) + 1)
82 bias
= adapt(i
-oldI
, x
, oldI
== 0)
85 if n
> utf8
.MaxRune ||
len(output
) >= 1024 {
86 return "", punyError(encoded
)
88 output
= append(output
, 0)
89 copy(output
[i
+1:], output
[i
:])
93 return string(output
), nil
96 // encode encodes a string as specified in section 6.3 and prepends prefix to
99 // The "while h < length(input)" line in the specification becomes "for
100 // remaining != 0" in the Go code, because len(s) in Go is in bytes, not runes.
101 func encode(prefix
, s
string) (string, error
) {
102 output
:= make([]byte, len(prefix
), len(prefix
)+1+2*len(s
))
104 delta
, n
, bias
:= int32(0), initialN
, initialBias
105 b
, remaining
:= int32(0), int32(0)
106 for _
, r
:= range s
{
109 output
= append(output
, byte(r
))
116 output
= append(output
, '-')
119 m
:= int32(0x7fffffff)
120 for _
, r
:= range s
{
125 delta
+= (m
- n
) * (h
+ 1)
127 return "", punyError(s
)
130 for _
, r
:= range s
{
134 return "", punyError(s
)
142 for k
:= base
; ; k
+= base
{
152 output
= append(output
, encodeDigit(t
+(q
-t
)%(base
-t
)))
153 q
= (q
- t
) / (base
- t
)
155 output
= append(output
, encodeDigit(q
))
156 bias
= adapt(delta
, h
+1, h
== b
)
164 return string(output
), nil
167 func decodeDigit(x
byte) (digit
int32, ok
bool) {
169 case '0' <= x
&& x
<= '9':
170 return int32(x
- ('0' - 26)), true
171 case 'A' <= x
&& x
<= 'Z':
172 return int32(x
- 'A'), true
173 case 'a' <= x
&& x
<= 'z':
174 return int32(x
- 'a'), true
179 func encodeDigit(digit
int32) byte {
181 case 0 <= digit
&& digit
< 26:
182 return byte(digit
+ 'a')
183 case 26 <= digit
&& digit
< 36:
184 return byte(digit
+ ('0' - 26))
186 panic("idna: internal error in punycode encoding")
189 // adapt is the bias adaptation function specified in section 6.1.
190 func adapt(delta
, numPoints
int32, firstTime
bool) int32 {
196 delta
+= delta
/ numPoints
198 for delta
> ((base
-tmin
)*tmax
)/2 {
202 return k
+ (base
-tmin
+1)*delta
/(delta
+skew
)