package strutil

import (
	
	
	

	
)

//
// -------------------- convert base --------------------
//

const (
	Base10Chars = "0123456789"
	Base16Chars = "0123456789abcdef"
	Base32Chars = "0123456789abcdefghjkmnpqrstvwxyz"
	Base36Chars = "0123456789abcdefghijklmnopqrstuvwxyz"
	Base48Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL"
	Base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Base64Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
)

// Base10Conv convert base10 string to new base string.
func ( string,  int) string { return BaseConv(, 10, ) }

// BaseConv convert base string by from and to base.
//
// Note: from and to base must be in [2, 64]
//
// Usage:
//
//	BaseConv("123", 10, 16) // Output: "7b"
//	BaseConv("7b", 16, 10) // Output: "123"
func ( string, ,  int) string {
	if  > 64 ||  < 2 {
		 = 10
	}
	if  > 64 ||  < 2 {
		 = 16
	}
	return BaseConvByTpl(, Base64Chars[:], Base64Chars[:])
}

// BaseConvInt convert base int to new base string.
//
// Usage:
//
//	BaseConv(123, 16) // Output: "7b"
func ( uint64,  int) string {
	if  > 64 ||  < 2 {
		 = 16
	}

	// bigInt 支持 2-62 进制转换处理 TODO
	if  <= 36 {
		return strconv.FormatUint(, )
	}
	if  <= 62 {
		 := new(big.Int).SetUint64()
		return .Text()
	}

	return BaseConvIntByTpl(, Base64Chars[:])
}

// BaseConvByTpl convert base string by template.
//
// Usage:
//
//	BaseConvert("123", Base62Chars, Base16Chars) // Output: "1e"
//	BaseConvert("1e", Base16Chars, Base62Chars) // Output: "123"
func ( string, ,  string) string {
	if  ==  {
		return 
	}

	// convert to base 10
	var  uint64
	if  == Base10Chars {
		var  error
		,  = strconv.ParseUint(, 10, 0)
		if  != nil {
			basefn.Panicf("input is not a valid decimal number: %s(%v)", , )
		}
	} else {
		 := uint64(len())
		for ,  := range  {
			 = * + uint64(strings.IndexRune(, ))
		}
	}

	// convert to new base
	return BaseConvIntByTpl(, )
}

// BaseConvIntByTpl convert base int to new base string.
func ( uint64,  string) string {
	// convert to new base
	var  string
	 := uint64(len())
	for  > 0 {
		 = string([%]) + 
		 /= 
	}
	return 
}