-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvarint.go
59 lines (45 loc) · 1.26 KB
/
varint.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
package varint
import "github.com/golang-infrastructure/go-gtypes"
// VarInt 用于表示一个可变长无符号整数
type VarInt []byte
// From 从无符号类型创建一个variant
func From[T gtypes.Unsigned](value T) VarInt {
return Encode[T](value)
}
func (x *VarInt) ToUint() uint {
return Decode[uint](*x)
}
func (x *VarInt) ToUint64() uint64 {
return Decode[uint64](*x)
}
func (x *VarInt) Add(v VarInt) VarInt {
newValue := x.ToUint64() + v.ToUint64()
return Encode[uint64](newValue)
}
func (x *VarInt) Sub(v VarInt) VarInt {
newValue := x.ToUint64() - v.ToUint64()
return Encode[uint64](newValue)
}
func (x *VarInt) Multi(v VarInt) VarInt {
newValue := x.ToUint64() * v.ToUint64()
return Encode[uint64](newValue)
}
func (x *VarInt) Divide(v VarInt) VarInt {
newValue := x.ToUint64() / v.ToUint64()
return Encode[uint64](newValue)
}
func (x *VarInt) GreatThan(v VarInt) bool {
return x.ToUint64() > v.ToUint64()
}
func (x *VarInt) GreatThanOrEquals(v VarInt) bool {
return x.ToUint64() >= v.ToUint64()
}
func (x *VarInt) LessThan(v VarInt) bool {
return x.ToUint64() < v.ToUint64()
}
func (x *VarInt) LessThanOrEqual(v VarInt) bool {
return x.ToUint64() <= v.ToUint64()
}
func (x *VarInt) Equals(v VarInt) bool {
return x.ToUint64() == v.ToUint64()
}