/
float.go
197 lines (171 loc) · 4.25 KB
/
float.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package reader
import (
"math"
"github.com/Soreing/parsley/reader/floatconv"
)
// float32Seq extracts float32 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) float32Seq(idx int) (res []float32, err error) {
var n float32
if n, err = r.Float32(); err == nil {
if r.Next() {
res, err = r.float32Seq(idx + 1)
} else {
res = make([]float32, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Float32s extracts an array of float32 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Float32s() (res []float32, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []float32{}
err = r.CloseArray()
} else if res, err = r.float32Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Float32 extracts the next float32 value from the data and skips all
// whitespace after it.
func (r *Reader) Float32() (flt float32, err error) {
dat, done := r.dat[r.pos:], false
m, d, e, n, t, dp, sp, i, ok := readFloat(dat)
if !ok {
if i == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[i], r.pos+i)
}
}
// adjusting exponent
ae := e + dotExp(d, dp, sp, t)
if d == 0 {
flt, done = 0, true
} else if !t {
if f, ok := floatconv.Atof32exact(m, ae, n); ok {
flt, done = f, true
}
}
if !done {
f, ok := floatconv.EiselLemire32(m, ae, n)
if ok {
if !t {
flt, done = f, true
}
fu, ok := floatconv.EiselLemire32(m, ae, n)
if ok && f == fu {
flt, done = f, true
}
}
}
if !done {
var dec floatconv.Decimal
dec.Set(dat[sp:i], e, n, t, dp)
b, ovf := dec.FloatBits(&floatconv.Float32info)
flt = math.Float32frombits(uint32(b))
if ovf {
return flt, newNumberOutOfRangeError(dat[sp:i], r.pos)
}
}
r.pos += i
r.SkipWhiteSpace()
return
}
// Float32p extracts the next float32 value and returns a pointer variable.
func (r *Reader) Float32p() (res *float32, err error) {
if v, err := r.Float32(); err == nil {
res = &v
}
return
}
// float64Seq extracts float64 values recursively until the closing bracket
// is found, then assigns the elements to the allocated slice.
func (r *Reader) float64Seq(idx int) (res []float64, err error) {
var n float64
if n, err = r.Float64(); err == nil {
if r.Next() {
res, err = r.float64Seq(idx + 1)
} else {
res = make([]float64, idx+1)
}
if err == nil {
res[idx] = n
}
}
return
}
// Float64s extracts an array of float64 values from the data and skips all
// whitespace after it. The values must be enclosed in square brackets "[...]"
// and the values must be separated by commas.
func (r *Reader) Float64s() (res []float64, err error) {
if err = r.OpenArray(); err == nil {
if r.Token() == TerminatorToken {
res = []float64{}
err = r.CloseArray()
} else if res, err = r.float64Seq(0); err == nil {
err = r.CloseArray()
}
}
return
}
// Float64 extracts the next float64 value from the data and skips all
// whitespace after it.
func (r *Reader) Float64() (flt float64, err error) {
dat, done := r.dat[r.pos:], false
m, d, e, n, t, dp, sp, i, ok := readFloat(dat)
if !ok {
if i == len(dat) {
return 0, newEndOfFileError()
} else {
return 0, newInvalidCharacterError(dat[i], r.pos+i)
}
}
// adjusting exponent
ae := e + dotExp(d, dp, sp, t)
if d == 0 {
flt, done = 0, true
} else if !t {
if f, ok := floatconv.Atof64exact(m, ae, n); ok {
flt, done = f, true
}
}
if !done {
f, ok := floatconv.EiselLemire64(m, ae, n)
if ok {
if !t {
flt, done = f, true
}
fu, ok := floatconv.EiselLemire64(m, ae, n)
if ok && f == fu {
flt, done = f, true
}
}
}
if !done {
var dec floatconv.Decimal
dec.Set(dat[sp:i], e, n, t, dp)
b, ovf := dec.FloatBits(&floatconv.Float64info)
flt = math.Float64frombits(b)
if ovf {
return flt, newNumberOutOfRangeError(dat[sp:i], r.pos)
}
}
r.pos += i
r.SkipWhiteSpace()
return
}
// Float64p extracts the next float64 value and returns a pointer variable.
func (r *Reader) Float64p() (res *float64, err error) {
if v, err := r.Float64(); err == nil {
res = &v
}
return
}