-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
179 lines (162 loc) · 3.53 KB
/
int.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
// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package big
import (
"bytes"
"database/sql/driver"
"fmt"
"math/big"
"strings"
)
var intZero = NewInt(0)
// Int extends the standard big.Int package.
type Int struct {
big.Int
}
// NewInt create and initialize new Int value from v or nil if v is invalid
// type that cannot be converted to Int.
func NewInt(v interface{}) (i *Int) {
i = &Int{}
got := toInt(v, i)
if got == nil {
return nil
}
return i
}
// Add set the i value to i + v and return the i as the result.
func (i *Int) Add(v interface{}) *Int {
vv := toInt(v, nil)
if vv == nil {
// Its equal to `i+0`
return i
}
i.Int.Add(&i.Int, &vv.Int)
return i
}
// IsGreater will return true if i > v.
func (i *Int) IsGreater(v interface{}) bool {
vv := toInt(v, nil)
if vv == nil {
return false
}
return i.Cmp(&vv.Int) > 0
}
// IsLess will return true if i < v.
func (i *Int) IsLess(v interface{}) bool {
vv := toInt(v, nil)
if vv == nil {
return false
}
return i.Cmp(&vv.Int) < 0
}
// IsZero will return true if `i == 0`.
func (i *Int) IsZero() bool {
return i.Cmp(&intZero.Int) == 0
}
// MarshalJSON implement the json.Marshaler interface and return the output of
// String method.
//
// If the global variable MarshalJSONAsString is true, the Int value will
// be encoded as string.
func (i *Int) MarshalJSON() ([]byte, error) {
var s string
if i == nil {
s = "0"
} else {
s = i.String()
}
if MarshalJSONAsString {
s = `"` + s + `"`
}
return []byte(s), nil
}
// Scan implement the database's sql.Scan interface.
func (i *Int) Scan(src interface{}) error {
got := toInt(src, i)
if got == nil {
return fmt.Errorf("Int.Scan: unknown type %T", src)
}
return nil
}
// UnmarshalJSON convert the JSON byte value into Int.
func (i *Int) UnmarshalJSON(in []byte) (err error) {
in = bytes.Trim(in, `"`)
i.SetInt64(0)
_, ok := i.Int.SetString(string(in), 10)
if !ok {
return fmt.Errorf("Int.UnmarshalJSON: cannot convert %T(%v) to Int", in, in)
}
return nil
}
// Value implement the sql/driver.Valuer.
func (i *Int) Value() (driver.Value, error) {
var s = `0`
if i != nil {
s = i.String()
}
return []byte(s), nil
}
// toInt convert any type to Int or nil if type is unknown.
// If in is not nil, it will be set to out.
func toInt(v interface{}, in *Int) (out *Int) {
out = &Int{}
switch v := v.(type) {
case []byte:
if len(v) == 0 {
out.SetInt64(0)
} else {
v = bytes.ReplaceAll(v, []byte{'_'}, nil)
vals := bytes.Split(v, []byte{'.'})
_, ok := out.Int.SetString(string(vals[0]), 10)
if !ok {
return nil
}
}
case string:
if len(v) == 0 {
out.SetInt64(0)
} else {
// Replace the underscore character, so we can write the
// number as "0.000_000_1".
v = strings.ReplaceAll(v, "_", "")
vals := strings.Split(v, ".")
_, ok := out.Int.SetString(vals[0], 10)
if !ok {
return nil
}
}
case byte:
out.SetInt64(int64(v))
case int:
out.SetInt64(int64(v))
case int32:
out.SetInt64(int64(v))
case int64:
out.SetInt64(v)
case uint64:
out.SetUint64(v)
case float32:
out.SetInt64(int64(v))
case float64:
out.SetInt64(int64(v))
case *Int:
*out = *v
case *big.Int:
out.Set(v)
case *Rat:
vals := strings.Split(v.String(), ".")
out.SetString(vals[0], 10)
case *big.Rat:
vals := strings.Split(v.String(), ".")
out.SetString(vals[0], 10)
default:
return nil
}
if in != nil {
in.Set(&out.Int)
} else {
in = out
}
return out
}