forked from periph/conn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.go
654 lines (623 loc) · 13.8 KB
/
unit.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Modifications 2024 Sam Anthony.
package unit
import (
"errors"
"strconv"
"strings"
"unicode/utf8"
)
func prefixZeros(digits, v int) string {
// digits is expected to be around 2~3.
s := strconv.Itoa(v)
for len(s) < digits {
// O(n²) but since digits is expected to run 2~3 times at most, it doesn't
// matter.
s = "0" + s
}
return s
}
// nanoAsString converts a value in S.I. unit in a string with the predefined
// prefix.
func nanoAsString(v int64) string {
sign := ""
if v < 0 {
if v == -9223372036854775808 {
v++
}
sign = "-"
v = -v
}
var frac int
var base int
var precision int64
unit := ""
switch {
case v >= 999999500000000001:
precision = v % 1000000000000000
base = int(v / 1000000000000000)
if precision > 500000000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "G"
case v >= 999999500000001:
precision = v % 1000000000000
base = int(v / 1000000000000)
if precision > 500000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "M"
case v >= 999999500001:
precision = v % 1000000000
base = int(v / 1000000000)
if precision > 500000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "k"
case v >= 999999501:
precision = v % 1000000
base = int(v / 1000000)
if precision > 500000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = ""
case v >= 1000000:
precision = v % 1000
base = int(v / 1000)
if precision > 500 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "m"
case v >= 1000:
frac = int(v) % 1000
base = int(v) / 1000
unit = "µ"
default:
if v == 0 {
return "0"
}
base = int(v)
unit = "n"
}
if frac == 0 {
return sign + strconv.Itoa(base) + unit
}
return sign + strconv.Itoa(base) + "." + prefixZeros(3, frac) + unit
}
// microAsString converts a value in S.I. unit in a string with the predefined
// prefix.
func microAsString(v int64) string {
sign := ""
if v < 0 {
if v == -9223372036854775808 {
v++
}
sign = "-"
v = -v
}
var frac int
var base int
var precision int64
unit := ""
switch {
case v >= 999999500000000001:
precision = v % 1000000000000000
base = int(v / 1000000000000000)
if precision > 500000000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "T"
case v >= 999999500000001:
precision = v % 1000000000000
base = int(v / 1000000000000)
if precision > 500000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "G"
case v >= 999999500001:
precision = v % 1000000000
base = int(v / 1000000000)
if precision > 500000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "M"
case v >= 999999501:
precision = v % 1000000
base = int(v / 1000000)
if precision > 500000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "k"
case v >= 1000000:
precision = v % 1000
base = int(v / 1000)
if precision > 500 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = ""
case v >= 1000:
frac = int(v) % 1000
base = int(v) / 1000
unit = "m"
default:
if v == 0 {
return "0"
}
base = int(v)
unit = "µ"
}
if frac == 0 {
return sign + strconv.Itoa(base) + unit
}
return sign + strconv.Itoa(base) + "." + prefixZeros(3, frac) + unit
}
// picoAsString converts a value in S.I. unit in a string with the predefined
// prefix.
func picoAsString(v int64) string {
sign := ""
if v < 0 {
if v == -9223372036854775808 {
v++
}
sign = "-"
v = -v
}
var frac int
var base int
var precision int64
unit := ""
switch {
case v >= 999999500000000001:
precision = v % 1000000000000000
base = int(v / 1000000000000000)
if precision > 500000000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "M"
case v >= 999999500000001:
precision = v % 1000000000000
base = int(v / 1000000000000)
if precision > 500000000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "k"
case v >= 999999500001:
precision = v % 1000000000
base = int(v / 1000000000)
if precision > 500000000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = ""
case v >= 999999501:
precision = v % 1000000
base = int(v / 1000000)
if precision > 500000 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "m"
case v >= 1000000:
precision = v % 1000
base = int(v / 1000)
if precision > 500 {
base++
}
frac = (base % 1000)
base = base / 1000
unit = "µ"
case v >= 1000:
frac = int(v) % 1000
base = int(v) / 1000
unit = "n"
default:
if v == 0 {
return "0"
}
base = int(v)
unit = "p"
}
if frac == 0 {
return sign + strconv.Itoa(base) + unit
}
return sign + strconv.Itoa(base) + "." + prefixZeros(3, frac) + unit
}
// Decimal is the representation of decimal number.
type decimal struct {
// base hold the significant digits.
base uint64
// exponent is the left or right decimal shift. (powers of ten).
exp int
// neg it true if the number is negative.
neg bool
}
// Positive powers of 10 in the form such that powerOF10[index] = 10^index.
var powerOf10 = [...]uint64{
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
}
// Maximum value for a int64.
const maxInt64 = (1 << 63) - 1
var maxInt64Str = "9223372036854775807"
var (
errOverflowsInt64 = errors.New("exceeds maximum")
errOverflowsInt64Negative = errors.New("exceeds minimum")
errNotANumber = errors.New("not a number")
)
// Converts from decimal to int64.
//
// Scale is combined with the decimal exponent to maximise the resolution and is
// in powers of ten.
//
// Returns true if the value overflowed.
func dtoi(d decimal, scale int) (int64, bool) {
// Get the total magnitude of the number.
// a^x * b^y = a*b^(x+y) since scale is of the order unity this becomes
// 1^x * b^y = b^(x+y).
// mag must be positive to use as index in to powerOf10 array.
u := d.base
mag := d.exp + scale
if mag < 0 {
mag = -mag
}
var n int64
if mag > 18 {
return 0, true
}
// Divide is = 10^(-mag)
switch {
case d.exp+scale < 0:
u = (u + powerOf10[mag]/2) / powerOf10[mag]
case mag == 0:
if u > maxInt64 {
return 0, true
}
default:
check := u * powerOf10[mag]
if check/powerOf10[mag] != u || check > maxInt64 {
return 0, true
}
u *= powerOf10[mag]
}
n = int64(u)
if d.neg {
n = -n
}
return n, false
}
// Converts a string to a decimal form. The return int is how many bytes of the
// string are considered numeric. The string may contain +-0 prefixes and
// arbitrary suffixes as trailing non number characters are ignored.
// Significant digits are stored without leading or trailing zeros, rather a
// base and exponent is used. Significant digits are stored as uint64, max size
// of significant digits is int64
func atod(s string) (decimal, int, error) {
var d decimal
start := 0
dp := 0
end := len(s)
seenDigit := false
seenZero := false
isPoint := false
seenPlus := false
// Strip leading zeros, +/- and mark DP.
for i := 0; i < len(s); i++ {
switch {
case s[i] == '-':
if seenDigit {
end = i
break
}
if seenPlus {
return decimal{}, 0, &parseError{
errors.New("contains both plus and minus symbols"),
}
}
if d.neg {
return decimal{}, 0, &parseError{
errors.New("contains multiple minus symbols"),
}
}
d.neg = true
start++
case s[i] == '+':
if seenDigit {
end = i
break
}
if d.neg {
return decimal{}, 0, &parseError{
errors.New("contains both plus and minus symbols"),
}
}
if seenPlus {
return decimal{}, 0, &parseError{
errors.New("contains multiple plus symbols"),
}
}
seenPlus = true
start++
case s[i] == '.':
if isPoint {
return decimal{}, 0, &parseError{
errors.New("contains multiple decimal points"),
}
}
isPoint = true
dp = i
if !seenDigit {
start++
}
case s[i] == '0':
if !seenDigit {
start++
}
seenZero = true
case s[i] >= '1' && s[i] <= '9':
seenDigit = true
default:
if !seenDigit && !seenZero {
return decimal{}, 0, &parseError{errNotANumber}
}
end = i
}
}
last := end
seenDigit = false
exp := 0
// Strip non significant zeros to find base exponent.
for i := end - 1; i > start-1; i-- {
switch {
case s[i] >= '1' && s[i] <= '9':
seenDigit = true
case s[i] == '.':
if !seenDigit {
end--
}
case s[i] == '0':
if !seenDigit {
if i > dp {
end--
}
if i <= dp || dp == 0 {
exp++
}
}
default:
last--
end--
}
}
for i := start; i < end; i++ {
c := s[i]
// Check that is is a digit.
if c >= '0' && c <= '9' {
// *10 is decimal shift left.
d.base *= 10
// Convert ascii digit into number.
check := d.base + uint64(c-'0')
// Check should always be larger than u unless we have overflowed.
// Similarly if check > max it will overflow when converted to int64.
if check < d.base || check > maxInt64 {
if d.neg {
return decimal{}, 0, &parseError{errOverflowsInt64Negative}
}
return decimal{}, 0, &parseError{errOverflowsInt64}
}
d.base = check
} else if c != '.' {
return decimal{}, 0, &parseError{errNotANumber}
}
}
if !isPoint {
d.exp = exp
} else {
if dp > start && dp < end {
// Decimal Point is in the middle of a number.
end--
}
// Find the exponent based on decimal point distance from left and the
// length of the number.
d.exp = (dp - start) - (end - start)
if dp <= start {
// Account for numbers of the form 1 > n < -1 eg 0.0001.
d.exp++
}
}
return d, last, nil
}
// valueOfUnitString is a helper for converting a string and a prefix in to a
// physic unit. It can be used when characters of the units do not conflict with
// any of the SI prefixes.
func valueOfUnitString(s string, base prefix) (int64, int, error) {
d, n, err := atod(s)
if err != nil {
return 0, n, err
}
si := prefix(unit)
if n != len(s) {
r, rsize := utf8.DecodeRuneInString(s[n:])
if r <= 1 || rsize == 0 {
return 0, 0, &parseError{
errors.New("unexpected end of string"),
}
}
var siSize int
si, siSize = parseSIPrefix(r)
n += siSize
}
v, overflow := dtoi(d, int(si-base))
if overflow {
if d.neg {
return -maxInt64, 0, &parseError{errOverflowsInt64Negative}
}
return maxInt64, 0, &parseError{errOverflowsInt64}
}
return v, n, nil
}
// decimalMul calcululates the product of two decimals; a and b, keeping the
// base less than maxInt64. Returns the number of times a figure was trimmed
// from either base coefficients. This function is to aid in the multiplication
// of numbers whose product have more than 18 significant figures. The minimum
// accuracy of the end product that has been truncated is 9 significant figures.
func decimalMul(a, b decimal) (decimal, uint) {
switch {
case a.base == 0 || b.base == 0:
// Anything multiplied by zero is zero. Special case to set exponent to
// zero.
return decimal{}, 0
case a.base > (1<<64)-6 || b.base > (1<<64)-6:
// In normal usage base will never be greater than 1<<63. However since
// base could be large as (1<<64 -1) this is to prevent an infinite loop
// when ((1<<64)-6)+5 overflows in the truncate least significant digit
// loop during rounding without adding addition bounds checking at that
// point.
break
default:
exp := a.exp + b.exp
neg := a.neg != b.neg
ab := a.base
bb := b.base
for i := uint(0); i < 21; i++ {
if ab <= 1 || bb <= 1 {
// This will always fit inside uint64.
return decimal{ab * bb, exp, neg}, i
}
if base := ab * bb; (base/ab == bb) && base < maxInt64 {
// Return if product did not overflow or exceed int64.
return decimal{base, exp, neg}, i
}
// Truncate least significant digit in product.
if bb > ab {
bb = (bb + 5) / 10
// Compact trailing zeros if any.
for bb > 0 && bb%10 == 0 {
bb /= 10
exp++
}
} else {
ab = (ab + 5) / 10
// Compact trailing zeros if any.
for ab > 0 && ab%10 == 0 {
ab /= 10
exp++
}
}
exp++
}
}
return decimal{}, 21
}
// hasSuffixes returns the first suffix found and the prefix content.
func hasSuffixes(s string, suffixes ...string) string {
for _, suffix := range suffixes {
if strings.HasSuffix(s, suffix) {
return suffix
}
}
return ""
}
type parseError struct {
error
}
func noUnitErr(valid string) error {
return errors.New("no unit provided; need " + valid)
}
func incorrectUnitErr(valid string) error {
return errors.New("unknown unit provided; need " + valid)
}
func unknownUnitPrefixErr(unit, valid string) error {
return errors.New("unknown unit prefix; valid prefixes for \"" + unit + "\" are " + valid)
}
func maxValueErr(valid string) error {
return errors.New("maximum value is " + valid)
}
func minValueErr(valid string) error {
return errors.New("minimum value is " + valid)
}
func notNumberUnitErr(unit string) error {
return errors.New("does not contain number or unit " + unit)
}
type prefix int
const (
pico prefix = -12
nano prefix = -9
micro prefix = -6
milli prefix = -3
unit prefix = 0
deca prefix = 1
hecto prefix = 2
kilo prefix = 3
mega prefix = 6
giga prefix = 9
tera prefix = 12
)
func parseSIPrefix(r rune) (prefix, int) {
switch r {
case 'p':
return pico, len("p")
case 'n':
return nano, len("n")
case 'u':
return micro, len("u")
case 'µ':
return micro, len("µ")
case 'm':
return milli, len("m")
case 'k':
return kilo, len("k")
case 'M':
return mega, len("M")
case 'G':
return giga, len("G")
case 'T':
return tera, len("T")
default:
return unit, 0
}
}