Skip to content

Commit 3193f92

Browse files
authored
Merge pull request #3 from gaze-network/feat/is-uint-funcs
Add IsUint64(), IsUint32(), IsUint16(), isUint8()
2 parents abffdc5 + 705b45d commit 3193f92

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project is a fork of [lukechampine/uint128](https://github.com/lukechampine
1010
- Add `AddOverflow`, `Add64Overflow`, `SubOverflow`, `Sub64Overflow`, `MulOverflow`, `Mul64Overflow` functions which returns additional boolean indicating whether the operation overflowed. The original functions now ignores overflow instead of panicking.
1111
- Change `FromBig` function behaviour to return errors instead of panicking.
1212
2. Add conversion functions `Uint64()`, `Uint32()`, `Uint16()`, `Uint8()` to convert `Uint128` to native types.
13+
3. Add functions `IsUint64()`, `IsUint32()`, `IsUint16()`, `IsUint8()` to check if `Uint128` can be converted to native types without overflow.
1314

1415
`uint128` provides a high-performance `Uint128` type that supports standard arithmetic
1516
operations. Unlike `math/big`, operations on `Uint128` values always produce new values

uint128.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ func (u Uint128) Uint8() uint8 {
5252
return uint8(u.Lo)
5353
}
5454

55+
// Uint returns true if u can be safely represented as a uint64.
56+
func (u Uint128) IsUint64() bool {
57+
return u.Hi == 0
58+
}
59+
60+
// Uint returns true if u can be safely represented as a uint32.
61+
func (u Uint128) IsUint32() bool {
62+
return u.Hi == 0 && u.Lo <= math.MaxUint32
63+
}
64+
65+
// Uint returns true if u can be safely represented as a uint16.
66+
func (u Uint128) IsUint16() bool {
67+
return u.Hi == 0 && u.Lo <= math.MaxUint16
68+
}
69+
70+
// Uint returns true if u can be safely represented as a uint8.
71+
func (u Uint128) IsUint8() bool {
72+
return u.Hi == 0 && u.Lo <= math.MaxUint8
73+
}
74+
5575
// Equals returns true if u == v.
5676
//
5777
// Uint128 values can be compared directly with ==, but use of the Equals method

uint128_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ func TestUint128(t *testing.T) {
5454
if x.Uint8() != uint8(x.Lo) {
5555
t.Fatal("Uint8 is not the lowest 8 bits of Lo for", x)
5656
}
57+
if x.IsUint64() != (x.Hi == 0) {
58+
t.Fatal("IsUint64 is not equivalent to Hi == 0 for", x)
59+
}
60+
if x.IsUint32() != (x.Hi == 0 && x.Lo <= math.MaxUint32) {
61+
t.Fatal("IsUint32 is not equivalent to Hi == 0 && Lo <= math.MaxUint32 for", x)
62+
}
63+
if x.IsUint16() != (x.Hi == 0 && x.Lo <= math.MaxUint16) {
64+
t.Fatal("IsUint16 is not equivalent to Hi == 0 && Lo <= math.MaxUint16 for", x)
65+
}
66+
if x.IsUint8() != (x.Hi == 0 && x.Lo <= math.MaxUint8) {
67+
t.Fatal("IsUint8 is not equivalent to Hi == 0 && Lo <= math.MaxUint8 for", x)
68+
}
5769

5870
b := make([]byte, 16)
5971
x.PutBytes(b)

0 commit comments

Comments
 (0)