optimize parseNumericString

pull/1698/head
horpto 2023-07-24 17:23:55 +03:00 committed by Jack Christensen
parent 4091eedf03
commit f4533dc906
1 changed files with 8 additions and 8 deletions

View File

@ -144,20 +144,20 @@ func (n *Numeric) toBigInt() (*big.Int, error) {
} }
func parseNumericString(str string) (n *big.Int, exp int32, err error) { func parseNumericString(str string) (n *big.Int, exp int32, err error) {
parts := strings.SplitN(str, ".", 2) idx := strings.IndexByte(str, '.')
digits := strings.Join(parts, "")
if len(parts) > 1 { if idx == -1 {
exp = int32(-len(parts[1])) for len(str) > 1 && str[len(str)-1] == '0' && str[len(str)-2] != '-' {
} else { str = str[:len(str)-1]
for len(digits) > 1 && digits[len(digits)-1] == '0' && digits[len(digits)-2] != '-' {
digits = digits[:len(digits)-1]
exp++ exp++
} }
} else {
exp = int32(-(len(str) - idx - 1))
str = str[:idx] + str[idx+1:]
} }
accum := &big.Int{} accum := &big.Int{}
if _, ok := accum.SetString(digits, 10); !ok { if _, ok := accum.SetString(str, 10); !ok {
return nil, 0, fmt.Errorf("%s is not a number", str) return nil, 0, fmt.Errorf("%s is not a number", str)
} }