/
bigint.go
261 lines (219 loc) · 4.97 KB
/
bigint.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
package prototyp
import (
"database/sql/driver"
"fmt"
"math/big"
"strconv"
"strings"
)
// BigInt is a type alias for big.Int used for JSON/Database marshalling.
//
// For JSON values we encoded BigInt's as strings.
//
// For Database values we encoded BigInt's as NUMERIC(78).
type BigInt big.Int
func NewBigInt(n int64) BigInt {
b := big.NewInt(n)
return BigInt(*b)
}
func NewBigIntFromNumberString(hs string) BigInt {
return NewBigIntFromString(hs, 10)
}
func NewBigIntFromHexString(hs string) BigInt {
return NewBigIntFromString(hs, 16)
}
func NewBigIntFromString(s string, base int) BigInt {
b := big.NewInt(0)
b, ok := b.SetString(s, base)
if !ok {
n, _ := ParseNumberString(s, base)
b = big.NewInt(n)
}
return BigInt(*b)
}
func ToBigInt(b *big.Int) BigInt {
if b == nil {
return BigInt{}
}
c := big.NewInt(0).Set(b)
return BigInt(*c)
}
func ToBigIntArray(bs []*big.Int) []BigInt {
var pbs []BigInt
for _, b := range bs {
pbs = append(pbs, ToBigInt(b))
}
return pbs
}
func ToBigIntArrayFromStringArray(s []string, base int) ([]BigInt, error) {
var pbs []BigInt
for _, v := range s {
b, ok := (&big.Int{}).SetString(v, base)
if !ok {
return nil, fmt.Errorf("invalid number %s", s)
}
pbs = append(pbs, ToBigInt(b))
}
return pbs, nil
}
func ToBigIntFromInt64(n int64) BigInt {
return ToBigInt(big.NewInt(n))
}
func (b *BigInt) SetString(s string, base int) bool {
v := big.Int(*b)
n, ok := v.SetString(s, base)
if !ok {
return false
}
*b = BigInt(*n)
return true
}
func (b BigInt) String() string {
return b.Int().String()
}
func (b BigInt) Int() *big.Int {
v := big.Int(b)
return &v
}
func (b BigInt) Uint64() uint64 {
return b.Int().Uint64()
}
func (b BigInt) Int64() int64 {
return b.Int().Int64()
}
func (b *BigInt) Add(n *big.Int) {
z := b.Int().Add(b.Int(), n)
*b = BigInt(*z)
}
func (b *BigInt) Sub(n *big.Int) {
z := b.Int().Sub(b.Int(), n)
*b = BigInt(*z)
}
func (b BigInt) Equals(n *big.Int) bool {
return b.Int().Cmp(n) == 0
}
func (b BigInt) Gt(n *big.Int) bool {
return b.Int().Cmp(n) == 1
}
func (b BigInt) Gte(n *big.Int) bool {
return b.Int().Cmp(n) == 0 || b.Int().Cmp(n) == 1
}
func (b BigInt) Lt(n *big.Int) bool {
return b.Int().Cmp(n) == -1
}
func (b BigInt) Lte(n *big.Int) bool {
return b.Int().Cmp(n) == 0 || b.Int().Cmp(n) == -1
}
// MarshalText implements encoding.TextMarshaler.
func (b BigInt) MarshalText() ([]byte, error) {
v := fmt.Sprintf("\"%s\"", b.String())
return []byte(v), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *BigInt) UnmarshalText(text []byte) error {
t := string(text)
if len(text) <= 2 || t == "null" || t == "" {
return nil
}
i, _ := big.NewInt(0).SetString(string(text[1:len(text)-1]), 10)
*b = BigInt(*i)
return nil
}
// MarshalJSON implements json.Marshaler
func (b BigInt) MarshalJSON() ([]byte, error) {
return b.MarshalText()
}
// UnmarshalJSON implements json.Unmarshaler
func (b *BigInt) UnmarshalJSON(text []byte) error {
if string(text) == "null" {
return nil
}
return b.UnmarshalText(text)
}
func (b BigInt) Value() (driver.Value, error) {
return b.String(), nil
}
func (b *BigInt) Scan(src interface{}) error {
if src == nil {
return nil
}
var svalue string
switch v := src.(type) {
case string:
svalue = v
case []byte:
svalue = string(v)
default:
return fmt.Errorf("BigInt.Scan: unexpected type %T", src)
}
// pgx driver returns NeX where N is digits and X is exponent
parts := strings.SplitN(svalue, "e", 2)
var ok bool
i := &big.Int{}
i, ok = i.SetString(parts[0], 10)
if !ok {
return fmt.Errorf("BigInt.Scan: failed to scan value %q", svalue)
}
if len(parts) >= 2 {
exp := big.NewInt(0)
exp, ok = exp.SetString(parts[1], 10)
if !ok {
return fmt.Errorf("BigInt.Scan failed to scan exp component %q", svalue)
}
i = i.Mul(i, big.NewInt(1).Exp(big.NewInt(10), exp, nil))
}
*b = BigInt(*i)
return nil
}
func (b *BigInt) ExtensionType() int8 {
return 12
}
func (b *BigInt) Len() int {
nb, _ := b.MarshalText()
return len(nb)
}
func (b *BigInt) MarshalBinaryTo(buff []byte) error {
nb, _ := b.MarshalText()
copy(buff, nb)
return nil
}
func (b *BigInt) MarshalBinary() (data []byte, err error) {
return b.MarshalText()
}
func (b *BigInt) UnmarshalBinary(buff []byte) error {
return b.UnmarshalText(buff)
}
func ParseNumberString(s string, base int) (int64, bool) {
var ns strings.Builder
if base == 10 {
for _, char := range s {
if char >= 48 && char <= 57 {
ns.WriteRune(char)
}
}
s = ns.String()
} else if base == 16 {
for _, char := range s {
if (char >= 48 && char <= 57) || (char == 120) || (char >= 65 && char <= 70) || (char >= 97 && char <= 102) {
ns.WriteRune(char)
}
}
s = ns.String()
s = strings.TrimPrefix(s, "0x")
} else {
return 0, false
}
n, err := strconv.ParseInt(s, base, 64)
if err != nil {
return 0, false
}
return n, true
}
func IsValidNumberString(s string) bool {
for _, char := range s {
if char < 48 || char > 57 {
return false
}
}
return true
}