// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.

// Copyright 2016 The Go 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 idna

// This file implements the Punycode algorithm from RFC 3492.

import (
	
	
	
)

// These parameter values are specified in section 5.
//
// All computation is done with int32s, so that overflow behavior is identical
// regardless of whether int is 32-bit or 64-bit.
const (
	base        int32 = 36
	damp        int32 = 700
	initialBias int32 = 72
	initialN    int32 = 128
	skew        int32 = 38
	tmax        int32 = 26
	tmin        int32 = 1
)

func punyError( string) error { return &labelError{, "A3"} }

// decode decodes a string as specified in section 6.2.
func decode( 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
}

// encode encodes a string as specified in section 6.3 and prepends prefix to
// the result.
//
// The "while h < length(input)" line in the specification becomes "for
// remaining != 0" in the Go code, because len(s) in Go is in bytes, not runes.
func encode(,  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
}

// madd computes a + (b * c), detecting overflow.
func madd(, ,  int32) ( int32,  bool) {
	 := int64() * int64()
	if  > math.MaxInt32-int64() {
		return 0, true
	}
	return  + int32(), false
}

func decodeDigit( 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 encodeDigit( int32) byte {
	switch {
	case 0 <=  &&  < 26:
		return byte( + 'a')
	case 26 <=  &&  < 36:
		return byte( + ('0' - 26))
	}
	panic("idna: internal error in punycode encoding")
}

// adapt is the bias adaptation function specified in section 6.1.
func adapt(,  int32,  bool) int32 {
	if  {
		 /= damp
	} else {
		 /= 2
	}
	 +=  / 
	 := int32(0)
	for  > ((base-tmin)*tmax)/2 {
		 /= base - tmin
		 += base
	}
	return  + (base-tmin+1)*/(+skew)
}