Skip to content

Commit f4533dc

Browse files
horptojackc
authored andcommitted
optimize parseNumericString
1 parent 4091eed commit f4533dc

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pgtype/numeric.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,20 @@ func (n *Numeric) toBigInt() (*big.Int, error) {
144144
}
145145

146146
func parseNumericString(str string) (n *big.Int, exp int32, err error) {
147-
parts := strings.SplitN(str, ".", 2)
148-
digits := strings.Join(parts, "")
147+
idx := strings.IndexByte(str, '.')
149148

150-
if len(parts) > 1 {
151-
exp = int32(-len(parts[1]))
152-
} else {
153-
for len(digits) > 1 && digits[len(digits)-1] == '0' && digits[len(digits)-2] != '-' {
154-
digits = digits[:len(digits)-1]
149+
if idx == -1 {
150+
for len(str) > 1 && str[len(str)-1] == '0' && str[len(str)-2] != '-' {
151+
str = str[:len(str)-1]
155152
exp++
156153
}
154+
} else {
155+
exp = int32(-(len(str) - idx - 1))
156+
str = str[:idx] + str[idx+1:]
157157
}
158158

159159
accum := &big.Int{}
160-
if _, ok := accum.SetString(digits, 10); !ok {
160+
if _, ok := accum.SetString(str, 10); !ok {
161161
return nil, 0, fmt.Errorf("%s is not a number", str)
162162
}
163163

0 commit comments

Comments
 (0)