package idna
import (
)
const (
base int32 = 36
damp int32 = 700
initialBias int32 = 72
initialN int32 = 128
skew int32 = 38
tmax int32 = 26
tmin int32 = 1
)
func ( string) error { return &labelError{, "A3"} }
func ( string) (string, error) {
if == "" {
return "", nil
}
:= 1 + strings.LastIndex(, "-")
if == 1 {
return "", punyError()
}
if == len() {
return [:len()-1], nil
}
:= make([]rune, 0, len())
if != 0 {
for , := range [:-1] {
= append(, )
}
}
, , := int32(0), initialN, initialBias
:= false
for < len() {
, := , int32(1)
for := base; ; += base {
if == len() {
return "", punyError()
}
, := decodeDigit([])
if ! {
return "", punyError()
}
++
, = madd(, , )
if {
return "", punyError()
}
:= -
if <= {
= tmin
} else if >= +tmax {
= tmax
}
if < {
break
}
, = madd(0, , base-)
if {
return "", punyError()
}
}
if len() >= 1024 {
return "", punyError()
}
:= int32(len() + 1)
= adapt(-, , == 0)
+= /
%=
if < 0 || > utf8.MaxRune {
return "", punyError()
}
= append(, 0)
copy([+1:], [:])
[] =
++
}
return string(), nil
}
func (, string) (string, error) {
:= make([]byte, len(), len()+1+2*len())
copy(, )
, , := int32(0), initialN, initialBias
, := int32(0), int32(0)
for , := range {
if < 0x80 {
++
= append(, byte())
} else {
++
}
}
:=
if > 0 {
= append(, '-')
}
:= false
for != 0 {
:= int32(0x7fffffff)
for , := range {
if > && >= {
=
}
}
, = madd(, -, +1)
if {
return "", punyError()
}
=
for , := range {
if < {
++
if < 0 {
return "", punyError()
}
continue
}
if > {
continue
}
:=
for := base; ; += base {
:= -
if <= {
= tmin
} else if >= +tmax {
= tmax
}
if < {
break
}
= append(, encodeDigit(+(-)%(base-)))
= ( - ) / (base - )
}
= append(, encodeDigit())
= adapt(, +1, == )
= 0
++
--
}
++
++
}
return string(), nil
}
func (, , int32) ( int32, bool) {
:= int64() * int64()
if > math.MaxInt32-int64() {
return 0, true
}
return + int32(), false
}
func ( byte) ( int32, bool) {
switch {
case '0' <= && <= '9':
return int32( - ('0' - 26)), true
case 'A' <= && <= 'Z':
return int32( - 'A'), true
case 'a' <= && <= 'z':
return int32( - 'a'), true
}
return 0, false
}
func ( int32) byte {
switch {
case 0 <= && < 26:
return byte( + 'a')
case 26 <= && < 36:
return byte( + ('0' - 26))
}
panic("idna: internal error in punycode encoding")
}
func (, int32, bool) int32 {
if {
/= damp
} else {
/= 2
}
+= /
:= int32(0)
for > ((base-tmin)*tmax)/2 {
/= base - tmin
+= base
}
return + (base-tmin+1)*/(+skew)
}