summaryrefslogtreecommitdiff
path: root/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go
blob: fc3baf6088a0f4afa1143fd69a7c4433b3dded9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package strconv

import (
	"math"
)

func ParseDecimal(b []byte) (float64, int) {
	i := 0
	start := i
	dot := -1
	trunk := -1
	n := uint64(0)
	for ; i < len(b); i++ {
		c := b[i]
		if '0' <= c && c <= '9' {
			if trunk == -1 {
				if math.MaxUint64/10 < n {
					trunk = i
				} else {
					n *= 10
					n += uint64(c - '0')
				}
			}
		} else if dot == -1 && c == '.' {
			dot = i
		} else {
			break
		}
	}
	if i == start || i == start+1 && dot == start {
		return 0.0, 0
	}

	f := float64(n)
	mantExp := int64(0)
	if dot != -1 {
		if trunk == -1 {
			trunk = i
		}
		mantExp = int64(trunk - dot - 1)
	} else if trunk != -1 {
		mantExp = int64(trunk - i)
	}
	exp := -mantExp

	// copied from strconv/atof.go
	if exp == 0 {
		return f, i
	} else if 0 < exp && exp <= 15+22 { // int * 10^k
		// If exponent is big but number of digits is not,
		// can move a few zeros into the integer part.
		if 22 < exp {
			f *= float64pow10[exp-22]
			exp = 22
		}
		if -1e15 <= f && f <= 1e15 {
			return f * float64pow10[exp], i
		}
	} else if exp < 0 && -22 <= exp { // int / 10^k
		return f / float64pow10[-exp], i
	}
	return f * math.Pow10(int(-mantExp)), i
}