Added generic types to sizeof
This commit is contained in:
parent
b02b722542
commit
d71bbd51cf
3 changed files with 8 additions and 6 deletions
|
@ -3,15 +3,17 @@ package sizeof
|
|||
import "math"
|
||||
|
||||
// Signed tells you how many bytes are needed to encode this signed number.
|
||||
func Signed(number int64) int {
|
||||
func Signed[T int | int8 | int16 | int32 | int64](number T) int {
|
||||
x := int64(number)
|
||||
|
||||
switch {
|
||||
case number >= math.MinInt8 && number <= math.MaxInt8:
|
||||
case x >= math.MinInt8 && x <= math.MaxInt8:
|
||||
return 1
|
||||
|
||||
case number >= math.MinInt16 && number <= math.MaxInt16:
|
||||
case x >= math.MinInt16 && x <= math.MaxInt16:
|
||||
return 2
|
||||
|
||||
case number >= math.MinInt32 && number <= math.MaxInt32:
|
||||
case x >= math.MinInt32 && x <= math.MaxInt32:
|
||||
return 4
|
||||
|
||||
default:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue