// string converts x to a string using digits from a charset; a digit with
// value d is represented by charset[d]. The conversion base is determined
// by len(charset), which must be >= 2 and <= 256.
string converts x to a string using digits from a charset; a digit with
value d is represented by charset[d]. The conversion base is determined
by len(charset), which must be >= 2 and <= 256.
// string converts x to a string using digits from a charset ; a digit with // value d is represented by charset [ d ] . The conversion base is determined // by len ( charset ) which must be > = 2 and < = 256 .
func (x nat) string(charset string) string {
	b := Word(len(charset))
	switch {
	case b < 2 || MaxBase > 256:
		panic("illegal base")
	case len(x) == 0:
		return string(charset[0])
	}
	i := int(float64(x.bitLen())/math.Log2(float64(b))) + 1 
	s := make([]byte, i)
	if b == b&-b {
		shift := uint(trailingZeroBits(b)) 
		mask := Word(1)<<shift - 1
		w := x[0]
		nbits := uint(_W) 
		for k := 1; k < len(x); k++ {
			for nbits >= shift {
				i--
				s[i] = charset[w&mask]
				w >>= shift
				nbits -= shift
			}
			if nbits == 0 {
				w = x[k]
				nbits = _W
			} else {
				w |= x[k] << nbits
				i--
				s[i] = charset[w&mask]
				w = x[k] >> (shift - nbits)
				nbits = _W - (shift - nbits)
			}
		}
		for nbits >= 0 && w != 0 {
			i--
			s[i] = charset[w&mask]
			w >>= shift
			nbits -= shift
		}
	} else {
		bb := b      
		ndigits := 1 
		for max := Word(_M / b); bb <= max; bb *= b {
			ndigits++ 
		}
		table := divisors(len(x), b, ndigits, bb)
		q := nat(nil).set(x)
		q.convertWords(s, charset, b, ndigits, bb, table)
		i = 0
		for zero := charset[0]; s[i] == zero; {
			i++
		}
	}
	return string(s[i:])
}
func ( x nat ) string ( charset string ) string { b := Word ( len ( charset ) ) switch { case b < 2 || MaxBase > 256 : panic ( " " ) case len ( x ) == 0 : return string ( charset [ 0 ] ) } i := int ( float64 ( x . bitLen ( ) ) / math . Log2 ( float64 ( b ) ) ) + 1 s := make ( [ ] byte , i ) if b == b & - b { shift := uint ( trailingZeroBits ( b ) ) mask := Word ( 1 ) << shift - 1 w := x [ 0 ] nbits := uint ( _W ) for k := 1 ; k < len ( x ) ; k ++ { for nbits >= shift { i -- s [ i ] = charset [ w & mask ] w >>= shift nbits -= shift } if nbits == 0 { w = x [ k ] nbits = _W } else { w |= x [ k ] << nbits i -- s [ i ] = charset [ w & mask ] w = x [ k ] >> ( shift - nbits ) nbits = _W - ( shift - nbits ) } } for nbits >= 0 && w != 0 { i -- s [ i ] = charset [ w & mask ] w >>= shift nbits -= shift } } else { bb := b ndigits := 1 for max := Word ( _M / b ) ; bb <= max ; bb *= b { ndigits ++ } table := divisors ( len ( x ) , b , ndigits , bb ) q := nat ( nil ) . set ( x ) q . convertWords ( s , charset , b , ndigits , bb , table ) i = 0 for zero := charset [ 0 ] ; s [ i ] == zero ; { i ++ } } return string ( s [ i : ] ) }
string converts x to a string using digits from a charset; a digit with
value d is represented by charset[d].
string converts x to a string using digits from a charset ; a digit with value d is represented by charset [ d ] .
// special cases // allocate buffer for conversion // off by one at most // convert power of two and non power of two bases separately // shift is base-b digit size in bits // shift > 0 because b >= 2 // number of unprocessed bits in w // convert less-significant words // convert full digits // convert any partial leading digit and advance to next word // no partial digit remaining, just advance // partial digit in current (k-1) and next (k) word // advance // convert digits of most-significant word (omit leading zeros) // determine "big base"; i.e., the largest possible value bb // that is a power of base b and still fits into a Word // (as in 10^19 for 19 decimal digits in a 64bit Word) // big base is b**ndigits // number of base b digits // maximize ndigits where bb = b**ndigits, bb <= _M // construct table of successive squares of bb*leafSize to use in subdivisions // result (table != nil) <=> (len(x) > leafSize > 0) // preserve x, create local copy for use by convertWords // convert q to string s in base b // strip leading zeros // (x != 0; thus s must contain at least one non-zero digit // and the loop will terminate)