// Copyright 2009 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 strconv

import 

// FormatUint returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func ( uint64,  int) string {
	if  == 10 {
		if  < nSmalls {
			return small(int())
		}
		var  [24]byte
		 := numDigits()
		formatBase10([:], )
		return string([:])
	}
	,  := formatBits(nil, , , false, false)
	return 
}

// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func ( int64,  int) string {
	if  == 10 {
		if 0 <=  &&  < nSmalls {
			return small(int())
		}
		var  [24]byte
		 := uint64()
		if  < 0 {
			 = -
		}
		 := numDigits()
		formatBase10([1:1+], )
		if  < 0 {
			[0] = '-'
			return string([:1+])
		}
		return string([1 : 1+])
	}
	,  := formatBits(nil, uint64(), ,  < 0, false)
	return 
}

// Itoa is equivalent to [FormatInt](int64(i), 10).
func ( int) string {
	return FormatInt(int64(), 10)
}

// AppendInt appends the string form of the integer i,
// as generated by [FormatInt], to dst and returns the extended buffer.
func ( []byte,  int64,  int) []byte {
	 := uint64()
	if  < 0 {
		 = append(, '-')
		 = -
	}
	return AppendUint(, , )
}

// AppendUint appends the string form of the unsigned integer i,
// as generated by [FormatUint], to dst and returns the extended buffer.
func ( []byte,  uint64,  int) []byte {
	if  == 10 {
		if  < nSmalls {
			return append(, small(int())...)
		}
		var  [24]byte
		 := numDigits()
		formatBase10([:], )
		return append(, [:]...)
	}
	, _ = formatBits(, , , false, true)
	return 
}

const digits = "0123456789abcdefghijklmnopqrstuvwxyz"

// formatBits computes the string representation of u in the given base.
// If neg is set, u is treated as negative int64 value. If append_ is
// set, the string is appended to dst and the resulting byte slice is
// returned as the first result value; otherwise the string is returned
// as the second result value.
// The caller is expected to have handled base 10 separately for speed.
func ( []byte,  uint64,  int, ,  bool) ( []byte,  string) {
	if  < 2 ||  == 10 ||  > len(digits) {
		panic("strconv: illegal AppendInt/FormatInt base")
	}
	// 2 <= base && base <= len(digits)

	var  [64 + 1]byte // +1 for sign of 64bit value in base 2
	 := len()
	if  {
		 = -
	}

	// convert bits
	// We use uint values where we can because those will
	// fit into a single register even on a 32bit machine.
	if isPowerOfTwo() {
		// Use shifts and masks instead of / and %.
		 := uint(bits.TrailingZeros(uint()))
		 := uint64()
		 := uint() - 1 // == 1<<shift - 1
		for  >=  {
			--
			[] = digits[uint()&]
			 >>= 
		}
		// u < base
		--
		[] = digits[uint()]
	} else {
		// general case
		 := uint64()
		for  >=  {
			--
			// Avoid using r = a%b in addition to q = a/b
			// since 64bit division and modulo operations
			// are calculated by runtime functions on 32bit machines.
			 :=  / 
			[] = digits[uint(-*)]
			 = 
		}
		// u < base
		--
		[] = digits[uint()]
	}

	// add sign, if any
	if  {
		--
		[] = '-'
	}

	if  {
		 = append(, [:]...)
		return
	}
	 = string([:])
	return
}

func ( int) bool {
	return &(-1) == 0
}

const nSmalls = 100

// smalls is the formatting of 00..99 concatenated.
// It is then padded out with 56 x's to 256 bytes,
// so that smalls[x&0xFF] has no bounds check.
const smalls = "00010203040506070809" +
	"10111213141516171819" +
	"20212223242526272829" +
	"30313233343536373839" +
	"40414243444546474849" +
	"50515253545556575859" +
	"60616263646566676869" +
	"70717273747576777879" +
	"80818283848586878889" +
	"90919293949596979899"

// small returns the string for an i with 0 <= i < nSmalls.
func ( int) string {
	if  < 10 {
		return digits[ : +1]
	}
	return smalls[*2 : *2+2]
}

// RuntimeFormatBase10 formats u into the tail of a
// and returns the offset to the first byte written to a.
// It is only for use by package runtime.
// Other packages should use AppendUint.
func ( []byte,  uint64) int {
	// Note: numDigits requires an argument ≥ 1.
	// The |1 changes 0 to 1 without adding an extra digit
	// to any other value.
	 := len() - numDigits(|1)
	formatBase10([:], )
	return 
}

// formatBase10 formats the decimal representation of u into a.
// The caller is responsible for ensuring that a is big enough to hold u.
// If a is too big, leading zeros will be filled in as needed.
func ( []byte,  uint64) {
	 := len()
	for  >= 8 {
		// Format last 8 digits (4 pairs).
		 := uint32( % 1e8)
		 /= 1e8
		,  := /1e4, %1e4
		,  := (/100)*2, (%100)*2
		,  := (/100)*2, (%100)*2
		[-1], [-2] = smalls[+1], smalls[]
		[-3], [-4] = smalls[+1], smalls[]
		[-5], [-6] = smalls[+1], smalls[]
		[-7], [-8] = smalls[+1], smalls[]
		 -= 8
	}

	 := uint32()
	if  >= 4 {
		// Format last 4 digits (2 pairs).
		 :=  % 1e4
		 /= 1e4
		,  := (/100)*2, (%100)*2
		[-1], [-2] = smalls[+1], smalls[]
		[-3], [-4] = smalls[+1], smalls[]
		 -= 4
	}
	if  >= 2 {
		// Format last 2 digits.
		 := ( % 1e2) * 2
		 /= 1e2
		[-1], [-2] = smalls[+1], smalls[]
		 -= 2
	}
	if  > 0 {
		// Format final digit.
		[0] = byte('0' + )
	}
}