-
Notifications
You must be signed in to change notification settings - Fork 11
/
decimal64fmt.go
295 lines (251 loc) · 6.3 KB
/
decimal64fmt.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package decimal
import (
"bytes"
"fmt"
"strconv"
)
var _ fmt.Formatter = Zero64
var _ fmt.Scanner = (*Decimal64)(nil)
var _ fmt.Stringer = Zero64
// DefaultFormatContext64 is the default context use for formatting Decimal64.
// Unlike [DefaultContext64], it uses HalfEven rounding to conform to standard
// Go formatting for float types.
var DefaultFormatContext64 = Context64{Rounding: HalfEven}
var zeros = [16]byte{
'0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0',
}
type appender struct {
wid int
prec int
flagger interface{ Flag(int) bool }
}
type noFlagsInt int
var noFlags interface{ Flag(int) bool } = noFlagsInt(0)
func (noFlagsInt) Flag(c int) bool {
return false
}
func appendZeros(buf []byte, n int) []byte {
if n <= 0 {
return buf
}
l := len(zeros)
for ; n > l; n -= l {
buf = append(buf, zeros[:]...)
}
return append(buf, zeros[:n]...)
}
func dotZeros(buf []byte, n int) []byte {
if n > 0 {
buf = append(buf, '.')
buf = appendZeros(buf, n)
}
return buf
}
func appendUint64(buf []byte, n, limit uint64) []byte {
// TODO: avoid dividing by a variable.
zeroPrefix := false
for limit > 0 {
msd := n / limit
if msd > 0 || zeroPrefix {
buf = append(buf, byte('0'+msd))
zeroPrefix = true
}
n -= limit * msd
limit /= 10
}
return buf
}
func (a *appender) uint64New(buf []byte, n uint64) []byte {
return strconv.AppendUint(buf, n, 10)
}
func appendFracF(buf []byte, n uint64, width, prec int) []byte {
if width > prec {
n /= tenToThe[width-prec]
width = prec
}
buf = formatBits10(buf, n%tenToThe[width], width)
return appendZeros(buf, prec-width)
}
func appendFracE(buf []byte, n uint64) []byte {
w := 15
for n%10 == 0 {
n /= 10
w--
}
return formatBits10(buf, n, w)
}
// Append appends the text representation of d to buf.
func (d Decimal64) Append(buf []byte, format byte, prec int) []byte {
return DefaultFormatContext64.append(d, buf, -1, prec, noFlags, rune(format))
}
func precScale(prec int) Decimal64 {
return new64(newFromPartsRaw(0, -15-max(0, int16(prec)), decimal64Base).bits)
}
// Append appends the text representation of d to buf.
func (ctx Context64) append(
d Decimal64,
buf []byte,
wid int,
prec int,
flagger interface{ Flag(int) bool },
verb rune,
) []byte {
if buf == nil {
buf = make([]byte, 0, 32)
}
a := appender{wid, prec, flagger}
flav, sign, exp, significand := d.parts()
if sign == 1 {
buf = append(buf, '-')
}
switch flav {
case flQNaN, flSNaN:
buf = append(buf, []byte("NaN")...)
if significand != 0 {
return appendUint64(buf, significand, 10000)
}
return buf
case flInf:
return append(buf, []byte("inf")...)
}
formatBlock:
switch verb {
case 'e', 'E':
exp, significand = unsubnormal(exp, significand)
whole := significand / decimal64Base
buf = append(buf, byte('0'+whole))
frac := significand - decimal64Base*whole
if frac > 0 {
buf = append(buf, '.')
buf = appendFracE(buf, frac)
}
if significand == 0 {
return buf
}
exp += 15
if exp != 0 {
buf = append(buf, byte(verb))
if exp < 0 {
buf = append(buf, '-')
exp = -exp
} else {
buf = append(buf, '+')
}
return appendUint64(buf, uint64(exp), 1000)
}
return buf
case 'f', 'F':
if 0 <= a.prec && a.prec <= 16 {
_, _, exp, significand = ctx.roundRaw(d, precScale(a.prec)).parts()
}
if significand == 0 {
buf = append(buf, '0')
return dotZeros(buf, a.prec)
}
exp, significand = unsubnormal(exp, significand)
// pure integer
if exp >= 0 {
buf = a.uint64New(buf, significand)
buf = appendZeros(buf, int(exp))
return dotZeros(buf, a.prec)
}
// integer part
fracDigits := int(min(-exp, 16))
unit := tenToThe[fracDigits]
buf = a.uint64New(buf, significand/unit)
buf = appendZeros(buf, int(exp))
// empty fractional part
if significand%unit == 0 {
return dotZeros(buf, a.prec)
}
buf = append(buf, '.')
// fractional part
prefix := int(max(0, -exp-16))
if a.prec < 0 {
buf = appendZeros(buf, prefix)
buf = appendFracF(buf, significand, fracDigits, 16)
buf = bytes.TrimRight(buf, "0")
return buf
}
buf = appendZeros(buf, min(a.prec, prefix))
return appendFracF(buf, significand, fracDigits, a.prec-prefix)
case 'g', 'G':
if exp < -decimal64Digits-3 ||
a.prec >= 0 && int(exp) > a.prec ||
a.prec < 0 && exp > -decimal64Digits+6 {
verb -= 'g' - 'e'
} else {
verb -= 'g' - 'f'
}
goto formatBlock
default:
return append(buf, '%', byte(verb))
}
}
// Format implements fmt.Formatter.
func (d Decimal64) Format(s fmt.State, verb rune) {
DefaultFormatContext64.format(d, s, verb)
}
// format implements fmt.Formatter.
func (ctx Context64) format(d Decimal64, s fmt.State, verb rune) {
prec := optInt(s.Precision())
switch verb {
case 'e', 'E', 'f', 'F':
if prec < 0 {
prec = 6
}
case 'g', 'G':
case 'v':
verb = 'g'
default:
fmt.Fprintf(s, "%%!%c(decimal.Decimal64=%s)", verb, d.String())
return
}
wid := optInt(s.Width())
s.Write(ctx.append(d, nil, wid, prec, s, verb)) //nolint:errcheck
}
func optInt(i int, has bool) int {
if has {
return i
}
return -1
}
// String returns a string representation of d.
func (d Decimal64) String() string {
return DefaultFormatContext64.str(d)
}
func (ctx Context64) str(d Decimal64) string {
if s, has := small64Strings[d.bits]; has {
return s
}
return ctx.text(d, 'g', -1, -1)
}
// Text converts the floating-point number x to a string according to the given
// format and precision prec.
func (d Decimal64) Text(format byte, prec int) string {
return DefaultFormatContext64.text(d, rune(format), -1, prec)
}
func (ctx Context64) text(d Decimal64, verb rune, width, prec int) string {
var buf [32]byte
return string(ctx.append(d, buf[:0], width, prec, noFlags, verb))
}
// Contextual binds a [Decimal64] to a [Context64] for greater control of formatting.
// It implements [fmt.Stringer] and [fmt.Formatter] on behalf of the number,
// using the context to control formatting.
type Contextual struct {
ctx Context64
d Decimal64
}
func (c Contextual) String() string {
return c.ctx.str(c.d)
}
func (c Contextual) Format(s fmt.State, verb rune) {
c.ctx.format(c.d, s, verb)
}
func (c Contextual) Text(verb rune, width, prec int) string {
return c.ctx.text(c.d, verb, width, prec)
}
func (ctx Context64) With(d Decimal64) Contextual {
return Contextual{ctx, d}
}