-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrac.go
341 lines (282 loc) · 7.59 KB
/
frac.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Missing feature of the Go standard library: parsing and formatting integers as
fractional numeric strings, without any rounding or bignums, by using a fixed
fraction size. Supports arbitrary radixes from 2 to 36.
See `readme.md` for examples.
*/
package frac
import (
"fmt"
"unsafe"
)
// Shortcut for `Parse(src, frac, 2)`.
func ParseBin(src string, frac uint) (int64, error) {
return Parse(src, frac, 2)
}
// Shortcut for `Parse(src, frac, 8)`.
func ParseOct(src string, frac uint) (int64, error) {
return Parse(src, frac, 8)
}
// Shortcut for `Parse(src, frac, 10)`.
func ParseDec(src string, frac uint) (int64, error) {
return Parse(src, frac, 10)
}
// Shortcut for `Parse(src, frac, 16)`.
func ParseHex(src string, frac uint) (int64, error) {
return Parse(src, frac, 16)
}
/*
Parses a string that represents a fractional number, with an optional leading
"+" or "-", into an integer whose value is virtually "multiplied" by the
provided fractional precision.
For example, for `frac = 2, radix = 10`, "123.45" is parsed into the number
12345, while "123.456" is rejected with an error because it exceeds the
allotted precision.
See `readme.md` for examples.
*/
func Parse(src string, frac uint, radix uint) (num int64, err error) {
if len(src) == 0 {
return 0, fmt.Errorf(`unable to parse empty input as number`)
}
if !(radix >= radixMin && radix <= radixMax) {
return 0, fmt.Errorf(`unable to parse %q as number: unsupported radix %v`, src, radix)
}
var sign int64 = 1
var expDigs uint
const (
stepSign = iota
stepMantStart
stepMant
stepExpStart
stepExp
)
step := stepSign
for ind, char := range []byte(src) {
if step == stepSign {
if char == '+' {
step = stepMantStart
continue
}
if char == '-' {
sign = -1
step = stepMantStart
continue
}
step = stepMantStart
}
if step == stepMant && char == '.' {
step = stepExpStart
continue
}
if step == stepMantStart {
step = stepMant
} else if step == stepExpStart {
step = stepExp
}
digit := toDigit(char)
if digit == unDigit || uint(digit) >= radix {
return 0, fmt.Errorf(
`unable to parse %q as number (radix %v, fraction %v): found non-digit character %q`,
src, radix, frac, runeAt(src, ind),
)
}
if step == stepExp {
expDigs++
if expDigs > frac {
if digit == 0 {
continue
}
return 0, fmt.Errorf(
`unable to parse %q as number (radix %v, fraction %v): exponent exceeds allotted fractional precision`,
src, radix, frac,
)
}
}
if num == 0 {
num = sign * int64(digit)
continue
}
num, err = inc(src, num, radix, sign, digit)
if err != nil {
return 0, err
}
}
for expDigs < frac {
num, err = inc(src, num, radix, sign, 0)
if err != nil {
return 0, err
}
expDigs++
}
if step != stepMant && step != stepExp {
return 0, fmt.Errorf(
`unable to parse %q as number (radix %v, fraction %v): unexpected end of input`,
src, radix, frac,
)
}
return num, nil
}
// Shortcut for `UnmarshalBin(src, frac, 2)`.
func UnmarshalBin(src []byte, frac uint) (int64, error) {
return Unmarshal(src, frac, 2)
}
// Shortcut for `UnmarshalOct(src, frac, 8)`.
func UnmarshalOct(src []byte, frac uint) (int64, error) {
return Unmarshal(src, frac, 8)
}
// Shortcut for `UnmarshalDec(src, frac, 10)`.
func UnmarshalDec(src []byte, frac uint) (int64, error) {
return Unmarshal(src, frac, 10)
}
// Shortcut for `UnmarshalHex(src, frac, 16)`.
func UnmarshalHex(src []byte, frac uint) (int64, error) {
return Unmarshal(src, frac, 16)
}
// Same as `Parse` but takes a byte slice.
func Unmarshal(src []byte, frac uint, radix uint) (int64, error) {
return Parse(bytesToMutableString(src), frac, radix)
}
// Shortcut for `Format(num, frac, 2)`.
func FormatBin(num int64, frac uint) (string, error) {
return Format(num, frac, 2)
}
// Shortcut for `Format(num, frac, 8)`.
func FormatOct(num int64, frac uint) (string, error) {
return Format(num, frac, 8)
}
// Shortcut for `Format(num, frac, 10)`.
func FormatDec(num int64, frac uint) (string, error) {
return Format(num, frac, 10)
}
// Shortcut for `Format(num, frac, 16)`.
func FormatHex(num int64, frac uint) (string, error) {
return Format(num, frac, 16)
}
/*
Formats an integer as a fractional number, virtually "divided" by the given
fractional precision.
For example, for `frac = 2, radix = 10`, the number 12345 is encoded
as "123.45", while the number 12300 is encoded as simply "123".
*/
func Format(num int64, frac uint, radix uint) (string, error) {
buf, err := Append(nil, num, frac, radix)
return bytesToMutableString(buf), err
}
// Shortcut for `Append(buf, num, frac, 2)`.
func AppendBin(buf []byte, num int64, frac uint) ([]byte, error) {
return Append(buf, num, frac, 2)
}
// Shortcut for `Append(buf, num, frac, 8)`.
func AppendOct(buf []byte, num int64, frac uint) ([]byte, error) {
return Append(buf, num, frac, 8)
}
// Shortcut for `Append(buf, num, frac, 10)`.
func AppendDec(buf []byte, num int64, frac uint) ([]byte, error) {
return Append(buf, num, frac, 10)
}
// Shortcut for `Append(buf, num, frac, 16)`.
func AppendHex(buf []byte, num int64, frac uint) ([]byte, error) {
return Append(buf, num, frac, 16)
}
/*
Same as `Format` but appends the resulting text to the provided buffer,
returning the resulting union. When there's an error, the buffer is returned
as-is with no hidden modifications.
*/
func Append(buf []byte, num int64, frac uint, radix uint) ([]byte, error) {
if !(radix >= radixMin && radix <= radixMax) {
return buf, fmt.Errorf(`unable to format %v: unsupported radix %v`, num, radix)
}
const bits = unsafe.Sizeof(num) * 8
if frac > uint(bits) {
return buf, fmt.Errorf(`unable to format %v: fractional precision %v exceeds limit %v`, num, frac, bits)
}
var local [int(bits) + len(`-0.`)]byte
ind := len(local)
var neg bool
var unum uint64
if num < 0 {
neg = true
unum = uint64(-num)
} else {
unum = uint64(num)
}
rad := uint64(radix)
trailing := true
var digit uint64
for frac > 0 {
frac--
unum, digit = pop(unum, rad)
if digit == 0 && trailing {
continue
}
trailing = false
ind--
local[ind] = digits[digit]
if frac == 0 {
ind--
local[ind] = '.'
}
}
for unum >= rad {
unum, digit = pop(unum, rad)
ind--
local[ind] = digits[digit]
}
ind--
local[ind] = digits[unum]
if neg {
ind--
local[ind] = '-'
}
return append(buf, local[ind:]...), nil
}
const (
digits = `0123456789abcdefghijklmnopqrstuvwxyz`
radixMin = uint(2)
radixMax = uint(len(digits))
)
func inc(src string, prev int64, radix uint, sign int64, digit byte) (int64, error) {
next := prev*int64(radix) + sign*int64(digit)
if prev > 0 && next < prev {
return 0, fmt.Errorf(`unable to parse %q as number: overflow of %T`, src, next)
}
if prev < 0 && next > prev {
return 0, fmt.Errorf(`unable to parse %q as number: underflow of %T`, src, next)
}
return next, nil
}
const unDigit byte = 255
func toDigit(char byte) byte {
if char >= '0' && char <= '9' {
return char - '0'
}
char = lower(char)
if char >= 'a' && char <= 'z' {
return char - 'a' + 10
}
return unDigit
}
func lower(char byte) byte {
return char | ('a' - 'A')
}
func runeAt(str string, index int) rune {
for ind, char := range str {
if ind == index {
return char
}
}
panic(fmt.Errorf(`failed to get rune from %q at %v`, str, index))
}
func pop(num uint64, radix uint64) (uint64, uint64) {
quot := num / radix
digit := num - quot*radix
return quot, digit
}
/*
Allocation-free conversion. Reinterprets a byte slice as a string. Borrowed from
the standard library. Reasonably safe.
*/
func bytesToMutableString(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}