|
@ -9,41 +9,37 @@ var ErrOverflowInt32 = errors.New("int32 overflow") |
|
|
var ErrOverflowUint8 = errors.New("uint8 overflow") |
|
|
var ErrOverflowUint8 = errors.New("uint8 overflow") |
|
|
var ErrOverflowInt8 = errors.New("int8 overflow") |
|
|
var ErrOverflowInt8 = errors.New("int8 overflow") |
|
|
|
|
|
|
|
|
// SafeAddInt32 adds two int32 integers
|
|
|
|
|
|
// If there is an overflow this will panic
|
|
|
|
|
|
func SafeAddInt32(a, b int32) int32 { |
|
|
|
|
|
|
|
|
// SafeAddInt32 adds two int32 integers.
|
|
|
|
|
|
func SafeAddInt32(a, b int32) (int32, error) { |
|
|
if b > 0 && (a > math.MaxInt32-b) { |
|
|
if b > 0 && (a > math.MaxInt32-b) { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} else if b < 0 && (a < math.MinInt32-b) { |
|
|
} else if b < 0 && (a < math.MinInt32-b) { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} |
|
|
} |
|
|
return a + b |
|
|
|
|
|
|
|
|
return a + b, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// SafeSubInt32 subtracts two int32 integers
|
|
|
|
|
|
// If there is an overflow this will panic
|
|
|
|
|
|
func SafeSubInt32(a, b int32) int32 { |
|
|
|
|
|
|
|
|
// SafeSubInt32 subtracts two int32 integers.
|
|
|
|
|
|
func SafeSubInt32(a, b int32) (int32, error) { |
|
|
if b > 0 && (a < math.MinInt32+b) { |
|
|
if b > 0 && (a < math.MinInt32+b) { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} else if b < 0 && (a > math.MaxInt32+b) { |
|
|
} else if b < 0 && (a > math.MaxInt32+b) { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} |
|
|
} |
|
|
return a - b |
|
|
|
|
|
|
|
|
return a - b, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// SafeConvertInt32 takes a int and checks if it overflows
|
|
|
|
|
|
// If there is an overflow this will panic
|
|
|
|
|
|
func SafeConvertInt32(a int64) int32 { |
|
|
|
|
|
|
|
|
// SafeConvertInt32 takes a int and checks if it overflows.
|
|
|
|
|
|
func SafeConvertInt32(a int64) (int32, error) { |
|
|
if a > math.MaxInt32 { |
|
|
if a > math.MaxInt32 { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} else if a < math.MinInt32 { |
|
|
} else if a < math.MinInt32 { |
|
|
panic(ErrOverflowInt32) |
|
|
|
|
|
|
|
|
return 0, ErrOverflowInt32 |
|
|
} |
|
|
} |
|
|
return int32(a) |
|
|
|
|
|
|
|
|
return int32(a), nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// SafeConvertUint8 takes an int64 and checks if it overflows
|
|
|
|
|
|
// If there is an overflow it returns an error
|
|
|
|
|
|
|
|
|
// SafeConvertUint8 takes an int64 and checks if it overflows.
|
|
|
func SafeConvertUint8(a int64) (uint8, error) { |
|
|
func SafeConvertUint8(a int64) (uint8, error) { |
|
|
if a > math.MaxUint8 { |
|
|
if a > math.MaxUint8 { |
|
|
return 0, ErrOverflowUint8 |
|
|
return 0, ErrOverflowUint8 |
|
@ -53,8 +49,7 @@ func SafeConvertUint8(a int64) (uint8, error) { |
|
|
return uint8(a), nil |
|
|
return uint8(a), nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// SafeConvertInt8 takes an int64 and checks if it overflows
|
|
|
|
|
|
// If there is an overflow it returns an error
|
|
|
|
|
|
|
|
|
// SafeConvertInt8 takes an int64 and checks if it overflows.
|
|
|
func SafeConvertInt8(a int64) (int8, error) { |
|
|
func SafeConvertInt8(a int64) (int8, error) { |
|
|
if a > math.MaxInt8 { |
|
|
if a > math.MaxInt8 { |
|
|
return 0, ErrOverflowInt8 |
|
|
return 0, ErrOverflowInt8 |
|
|