-
Notifications
You must be signed in to change notification settings - Fork 0
/
nf.go
220 lines (192 loc) · 4.86 KB
/
nf.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
package objectid
/*
nf.go provides NumberForm methods and types.
NOTE: uint128-related code written by Luke Champine, per https://github.com/lukechampine/uint128.
It has been incorporated into this package (unexported), and simplified to serve in the capacity
of OID numberForm storage.
*/
import (
"fmt"
"math/bits"
)
var nilNF NumberForm
/*
NumberForm is an unsigned 128-bit number. This type is based on
github.com/lukechampine/uint128. It has been incorporated
into this package to produce unsigned 128-bit OID numberForm
support (i.e.: UUID-based OIDs).
*/
type NumberForm struct {
lo, hi uint64
parsed bool
}
// isZero returns true if a == 0.
func (a NumberForm) IsZero() bool {
if &a == nil {
return true
}
// NOTE: we do not compare against Zero, because that
// is a global variable that could be modified.
return a.lo == uint64(0) && a.hi == uint64(0)
}
/*
Equal returns a boolean value indicative of whether the receiver is equal to
the value provided. Valid input types are string, uint64, int and NumberForm.
Any input that represents a negative number guarantees a false return.
*/
func (a NumberForm) Equal(n any) bool {
switch tv := n.(type) {
case NumberForm:
return a == tv
case string:
if nf, err := NewNumberForm(tv); err == nil {
return a == nf
}
case uint64:
return a.lo == tv && a.hi == 0
case int:
if 0 <= tv {
return a.lo == uint64(tv) && a.hi == 0
}
}
return false
}
/*
Gt returns a boolean value indicative of whether the receiver is greater than
the value provided. Valid input types are string, uint64, int and NumberForm.
Any input that represents a negative number guarantees a false return.
*/
func (a NumberForm) Gt(n any) bool {
switch tv := n.(type) {
case NumberForm, string:
return a.gtLt(tv, false)
case uint64:
return a.lo > tv && a.hi == uint64(0)
case int:
if 0 <= tv {
return a.lo > uint64(tv) && a.hi == uint64(0)
}
}
return false
}
/*
Lt returns a boolean value indicative of whether the receiver is less than
the value provided. Valid input types are string, uint64, int and NumberForm.
Any input that represents a negative number guarantees a false return.
*/
func (a NumberForm) Lt(n any) bool {
switch tv := n.(type) {
case NumberForm, string:
return a.gtLt(tv, true)
case uint64:
return a.lo < tv && a.hi == uint64(0)
case int:
if 0 <= tv {
return a.lo < uint64(tv) && a.hi == uint64(0)
}
}
return false
}
func (a NumberForm) gtLt(x any, lt bool) bool {
var nf NumberForm
switch tv := x.(type) {
case string:
nf, _ = NewNumberForm(tv)
case NumberForm:
nf = tv
default:
return false
}
if lt {
return a.hi < nf.hi || (a.hi == nf.hi && a.lo < nf.lo)
}
return a.hi > nf.hi || (a.hi == nf.hi && a.lo > nf.lo)
}
/*
Valid returns a boolean valud indicative of proper instantiation.
*/
func (a NumberForm) Valid() bool {
return a.parsed
}
/*
leadingZeros returns the number of leading zero bits in u;
the result is 128 for a == 0.
*/
func (a NumberForm) leadingZeros() int {
if a.hi > 0 {
return bits.LeadingZeros64(a.hi)
}
return 64 + bits.LeadingZeros64(a.lo)
}
/*
Len returns the minimum number of bits required to represent u;
the result is 0 for a == 0.
*/
func (a NumberForm) len() int {
return 128 - a.leadingZeros()
}
// String returns the base-10 representation of a as a string.
func (a NumberForm) String() string {
if a.IsZero() {
return "0"
} else if !a.parsed {
return "0"
}
buf := []byte("0000000000000000000000000000000000000000") // log10(2^128) < 40
for i := len(buf); ; i -= 19 {
q, r := a.quoRem64(1e19) // largest power of 10 that fits in a uint64
var n int
for ; r != 0; r /= 10 {
n++
buf[i-n] += byte(r % 10)
}
if q.IsZero() {
return string(buf[i-n:])
}
a = q
}
}
/*
Scan implements fmt.Scanner, and is only present to allow conversion
of an NumberForm into a string value per fmt.Sscan. Users need not
execute this method directly.
*/
func (a *NumberForm) Scan(s fmt.ScanState, ch rune) error {
return sscan(s, ch, a)
}
// quoRem64 returns q = u/v and r = u%v.
func (a NumberForm) quoRem64(v uint64) (q NumberForm, r uint64) {
if a.hi < v {
q.lo, r = bits.Div64(a.hi, a.lo, v)
} else {
q.hi, r = bits.Div64(0, a.hi, v)
q.lo, r = bits.Div64(r, a.lo, v)
}
return
}
// NewNumberForm returns the NumberForm value.
func newNumberForm(lo, hi uint64) NumberForm {
return NumberForm{lo: lo, hi: hi, parsed: true}
}
/*
NewNumberForm converts v into an instance of NumberForm, which
is returned alongside an error.
Acceptable input types are string, int and uint64. No decimal value,
whether string or int, can ever be negative.
*/
func NewNumberForm(v any) (a NumberForm, err error) {
switch tv := v.(type) {
case string:
_, err = fmt.Sscan(tv, &a)
a.parsed = err == nil
case int:
if tv < 0 {
err = errorf("A NumberForm cannot be negative")
break
}
a = newNumberForm(uint64(tv), 0)
case uint64:
a = newNumberForm(tv, 0)
}
return
}