-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl9.go
66 lines (54 loc) · 1.23 KB
/
l9.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
package base10quant
import (
"errors"
)
var ErrL9InvalidFormat = errors.New("l9: invalid format")
const (
MaxL9 = 999_999_999
MinL9 = 0
)
// L9 is base10 chars encoding least significant 9 decimals of uint32.
type L9 struct{ v uint32 }
func L9FromUint32(v uint32) L9 { return L9{v: v} }
func L9FromString(s string) (L9, error) {
var v L9
err := (&v).UnmarshalText([]byte(s))
return v, err
}
func (s L9) UInt32() uint32 { return s.v }
func (s L9) IsEmpty() bool { return s.v == 0 }
func (s L9) AppendBytes(b []byte) {
v := s.v
v, b[8] = v/10, '0'+byte(v%10)
v, b[7] = v/10, '0'+byte(v%10)
v, b[6] = v/10, '0'+byte(v%10)
v, b[5] = v/10, '0'+byte(v%10)
v, b[4] = v/10, '0'+byte(v%10)
v, b[3] = v/10, '0'+byte(v%10)
v, b[2] = v/10, '0'+byte(v%10)
v, b[1] = v/10, '0'+byte(v%10)
_, b[0] = v/10, '0'+byte(v%10)
}
func (s L9) MarshalText() ([]byte, error) {
b := make([]byte, 9)
s.AppendBytes(b)
return b, nil
}
func (s *L9) UnmarshalText(b []byte) error {
if len(b) != 9 {
return ErrL9InvalidFormat
}
s.v = 0
for i := 0; i < 9; i++ {
if b[i] < '0' || b[i] > '9' {
return ErrL9InvalidFormat
}
s.v *= 10
s.v += uint32(b[i] - '0')
}
return nil
}
func (s L9) String() string {
b, _ := s.MarshalText()
return string(b)
}