-
Notifications
You must be signed in to change notification settings - Fork 458
/
bytes_amount.go
181 lines (151 loc) · 4.8 KB
/
bytes_amount.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
package types
import (
"encoding/json"
"math/big"
"github.com/filecoin-project/go-leb128"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/pkg/errors"
"github.com/polydawn/refmt/obj/atlas"
)
// NOTE -- All *BytesAmount methods must call ensureBytesAmounts with refs to every user-supplied value before use.
func init() {
cbor.RegisterCborType(bytesAmountAtlasEntry)
ZeroBytes = NewBytesAmount(0)
}
// ZeroBytes represents a BytesAmount of 0
var ZeroBytes *BytesAmount
// ensureBytesAmounts takes a variable number of refs -- variables holding *BytesAmount -- and sets their values
// to ZeroBytes (the zero value for the type) if their values are nil.
func ensureBytesAmounts(refs ...**BytesAmount) {
for _, ref := range refs {
if *ref == nil {
*ref = ZeroBytes
}
}
}
var bytesAmountAtlasEntry = atlas.BuildEntry(BytesAmount{}).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(i BytesAmount) ([]byte, error) {
return i.Bytes(), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []byte) (BytesAmount, error) {
return *NewBytesAmountFromBytes(x), nil
})).
Complete()
// UnmarshalJSON converts a byte array to a BytesAmount.
func (z *BytesAmount) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
bytes, ok := NewBytesAmountFromString(s, 10)
if !ok {
return errors.New("Cannot convert string to bytes amount")
}
*z = *bytes
return nil
}
// MarshalJSON converts a BytesAmount to a byte array and returns it.
func (z BytesAmount) MarshalJSON() ([]byte, error) {
return json.Marshal(z.val.String())
}
// An BytesAmount represents a signed multi-precision integer.
// The zero value for a BytesAmount represents the value 0.
type BytesAmount struct{ val *big.Int }
// NewBytesAmount allocates and returns a new BytesAmount set to x.
func NewBytesAmount(x uint64) *BytesAmount {
return &BytesAmount{val: big.NewInt(0).SetUint64(x)}
}
// NewBytesAmountFromBytes allocates and returns a new BytesAmount set
// to the value of buf as the bytes of a big-endian unsigned integer.
func NewBytesAmountFromBytes(buf []byte) *BytesAmount {
ba := NewBytesAmount(0)
ba.val = leb128.ToBigInt(buf)
return ba
}
// NewBytesAmountFromString allocates a new BytesAmount set to the value of s,
// interpreted in the given base, and returns it and a boolean indicating success.
func NewBytesAmountFromString(s string, base int) (*BytesAmount, bool) {
ba := NewBytesAmount(0)
val, ok := ba.val.SetString(s, base)
ba.val = val // overkill
return ba, ok
}
// Add sets z to the sum x+y and returns z.
func (z *BytesAmount) Add(y *BytesAmount) *BytesAmount {
ensureBytesAmounts(&z, &y)
newVal := big.NewInt(0)
newVal.Add(z.val, y.val)
newZ := BytesAmount{val: newVal}
return &newZ
}
// Sub sets z to the difference x-y and returns z.
func (z *BytesAmount) Sub(y *BytesAmount) *BytesAmount {
ensureBytesAmounts(&z, &y)
newVal := big.NewInt(0)
newVal.Sub(z.val, y.val)
newZ := BytesAmount{val: newVal}
return &newZ
}
// Mul sets z to x*y and returns z.
func (z *BytesAmount) Mul(y *BytesAmount) *BytesAmount {
ensureBytesAmounts(&z, &y)
newVal := big.NewInt(0)
newVal.Mul(z.val, y.val)
newZ := BytesAmount{val: newVal}
return &newZ
}
// Equal returns true if z = y
func (z *BytesAmount) Equal(y *BytesAmount) bool {
ensureBytesAmounts(&z, &y)
return z.val.Cmp(y.val) == 0
}
// LessThan returns true if z < y
func (z *BytesAmount) LessThan(y *BytesAmount) bool {
ensureBytesAmounts(&z, &y)
return z.val.Cmp(y.val) < 0
}
// GreaterThan returns true if z > y
func (z *BytesAmount) GreaterThan(y *BytesAmount) bool {
ensureBytesAmounts(&z, &y)
return z.val.Cmp(y.val) > 0
}
// LessEqual returns true if z <= y
func (z *BytesAmount) LessEqual(y *BytesAmount) bool {
ensureBytesAmounts(&z, &y)
return z.val.Cmp(y.val) <= 0
}
// GreaterEqual returns true if z >= y
func (z *BytesAmount) GreaterEqual(y *BytesAmount) bool {
ensureBytesAmounts(&z, &y)
return z.val.Cmp(y.val) >= 0
}
// IsPositive returns true if z is greater than zero.
func (z *BytesAmount) IsPositive() bool {
ensureBytesAmounts(&z)
return z.GreaterThan(ZeroBytes)
}
// IsNegative returns true if z is less than zero.
func (z *BytesAmount) IsNegative() bool {
ensureBytesAmounts(&z)
return z.LessThan(ZeroBytes)
}
// IsZero returns true if z equals zero.
func (z *BytesAmount) IsZero() bool {
ensureBytesAmounts(&z)
return z.Equal(ZeroBytes)
}
// Bytes returns the absolute value of x as a big-endian byte slice.
func (z *BytesAmount) Bytes() []byte {
ensureBytesAmounts(&z)
return leb128.FromBigInt(z.val)
}
func (z *BytesAmount) String() string {
ensureBytesAmounts(&z)
return z.val.String()
}
// Uint64 returns the uint64 representation of x. If x cannot be represented in a uint64, the result is undefined.
func (z *BytesAmount) Uint64() uint64 {
return z.val.Uint64()
}