github.com/andeya/ameda@v1.5.3/atoi_x.go (about) 1 package ameda 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "math" 8 ) 9 10 // ParseUintByDict convert numStr into corresponding uint64 according to dict. 11 func ParseUintByDict(dict []byte, numStr string) (uint64, error) { 12 if len(dict) == 0 { 13 return 0, errors.New("dict is empty") 14 } 15 base := float64(len(dict)) 16 len := len(numStr) 17 var number float64 18 for i := 0; i < len; i++ { 19 char := numStr[i : i+1] 20 pos := bytes.IndexAny(dict, char) 21 if pos == -1 { 22 return 0, fmt.Errorf("found a char not included in the dict: %q", char) 23 } 24 number = math.Pow(base, float64(len-i-1))*float64(pos) + number 25 } 26 return uint64(number), nil 27 }