Documentation
¶
Overview ¶
Package safeconv provides safe integer type conversions with overflow detection. All conversion functions return an error if the value cannot be represented in the target type without overflow. Clamping variants return the nearest valid value instead of an error.
This package exports a comprehensive set of conversion functions for various integer types. While not all functions may be used internally, they are provided as a complete API for safe integer conversions throughout the codebase.
Index ¶
- Variables
- func AddInt(a, b int) (int, error)
- func ClampInt32ToUint8(v int32) uint8
- func ClampInt64ToInt(v int64) int
- func ClampIntToInt32(v int) int32
- func ClampIntToUint(v int) uint
- func ClampIntToUint8(v int) uint8
- func ClampIntToUint16(v int) uint16
- func ClampIntToUint32(v int) uint32
- func ClampToInt8(v int64) int8
- func ClampToInt16(v int64) int16
- func ClampToInt32(v int64) int32
- func ClampToUint8(v int64) uint8
- func ClampToUint16(v int64) uint16
- func ClampToUint32(v int64) uint32
- func ClampToUint64(v int64) uint64
- func ClampUint16ToUint8(v uint16) uint8
- func ClampUint32ToUint8(v uint32) uint8
- func ClampUint32ToUint16(v uint32) uint16
- func ClampUint64ToUint32(v uint64) uint32
- func Int32ToUint8(v int32) (uint8, error)
- func Int64ToInt(v int64) (int, error)
- func IntToInt32(v int) (int32, error)
- func IntToUint(v int) (uint, error)
- func IntToUint8(v int) (uint8, error)
- func IntToUint16(v int) (uint16, error)
- func IntToUint32(v int) (uint32, error)
- func IntToUint64(v int) (uint64, error)
- func MultiplyInt(a, b int) (int, error)
- func MustInt64ToInt(v int64) int
- func MustIntToInt32(v int) int32
- func MustIntToUint(v int) uint
- func MustIntToUint8(v int) uint8
- func MustIntToUint16(v int) uint16
- func MustIntToUint32(v int) uint32
- func MustIntToUint64(v int) uint64
- func MustToInt8(v int64) int8
- func MustToInt16(v int64) int16
- func MustToInt32(v int64) int32
- func MustToUint8(v int64) uint8
- func MustToUint16(v int64) uint16
- func MustToUint32(v int64) uint32
- func MustToUint64(v int64) uint64
- func MustUint64ToInt(v uint64) int
- func MustUint64ToInt64(v uint64) int64
- func MustUint64ToUint32(v uint64) uint32
- func SafeShiftAmount(shift int) uint
- func ToInt(v uint64) (int, error)
- func ToInt8(v int64) (int8, error)
- func ToInt16(v int64) (int16, error)
- func ToInt32(v int64) (int32, error)
- func ToInt64(v uint64) (int64, error)
- func ToUint(v int64) (uint, error)
- func ToUint8(v int64) (uint8, error)
- func ToUint16(v int64) (uint16, error)
- func ToUint32(v int64) (uint32, error)
- func ToUint64(v int64) (uint64, error)
- func Uint16ToInt(v uint16) (int, error)
- func Uint16ToInt16(v uint16) int16
- func Uint16ToUint8(v uint16) (uint8, error)
- func Uint32ToInt(v uint32) (int, error)
- func Uint32ToUint8(v uint32) (uint8, error)
- func Uint32ToUint16(v uint32) (uint16, error)
- func Uint64ToInt(v uint64) (int, error)
- func Uint64ToInt64(v uint64) (int64, error)
- func Uint64ToUint32(v uint64) (uint32, error)
Constants ¶
This section is empty.
Variables ¶
var ErrOverflow = errors.New("integer overflow during conversion")
ErrOverflow is returned when a conversion would overflow the target type.
Functions ¶
func AddInt ¶
AddInt safely adds two int values. Returns ErrOverflow if the result would overflow int.
func ClampInt32ToUint8 ¶
ClampInt32ToUint8 converts an int32 to uint8, clamping to the valid range [0, 255].
func ClampInt64ToInt ¶
ClampInt64ToInt converts an int64 to int, clamping to the valid range of int.
func ClampIntToInt32 ¶
ClampIntToInt32 converts an int to int32, clamping to the valid range [-2147483648, 2147483647].
func ClampIntToUint ¶
ClampIntToUint converts an int to uint, clamping negative values to 0.
func ClampIntToUint8 ¶
ClampIntToUint8 converts an int to uint8, clamping to the valid range [0, 255].
func ClampIntToUint16 ¶
ClampIntToUint16 converts an int to uint16, clamping to the valid range [0, 65535].
func ClampIntToUint32 ¶
ClampIntToUint32 converts an int to uint32, clamping to the valid range [0, 4294967295].
func ClampToInt8 ¶
ClampToInt8 converts an int64 to int8, clamping to the valid range [-128, 127].
func ClampToInt16 ¶
ClampToInt16 converts an int64 to int16, clamping to the valid range [-32768, 32767].
func ClampToInt32 ¶
ClampToInt32 converts an int64 to int32, clamping to the valid range [-2147483648, 2147483647].
func ClampToUint8 ¶
ClampToUint8 converts an int64 to uint8, clamping to the valid range [0, 255].
func ClampToUint16 ¶
ClampToUint16 converts an int64 to uint16, clamping to the valid range [0, 65535].
func ClampToUint32 ¶
ClampToUint32 converts an int64 to uint32, clamping to the valid range [0, 4294967295].
func ClampToUint64 ¶
ClampToUint64 converts an int64 to uint64, clamping negative values to 0.
func ClampUint16ToUint8 ¶
ClampUint16ToUint8 converts a uint16 to uint8, clamping to 255.
func ClampUint32ToUint8 ¶
ClampUint32ToUint8 converts a uint32 to uint8, clamping to 255.
func ClampUint32ToUint16 ¶
ClampUint32ToUint16 converts a uint32 to uint16, clamping to 65535.
func ClampUint64ToUint32 ¶
ClampUint64ToUint32 converts a uint64 to uint32, clamping to MaxUint32.
func Int32ToUint8 ¶
Int32ToUint8 safely converts an int32 to uint8. Returns ErrOverflow if the value is outside the range [0, 255].
func Int64ToInt ¶
Int64ToInt safely converts an int64 to int. Returns ErrOverflow if the value is outside the range of int. Note: On 64-bit systems, int is 64 bits, so this never overflows. On 32-bit systems, int is 32 bits, so this checks the int32 range.
func IntToInt32 ¶
IntToInt32 safely converts an int to int32. Returns ErrOverflow if the value is outside the range [-2147483648, 2147483647].
func IntToUint ¶
IntToUint safely converts an int to uint. Returns ErrOverflow if the value is negative.
func IntToUint8 ¶
IntToUint8 safely converts an int to uint8. Returns ErrOverflow if the value is outside the range [0, 255].
func IntToUint16 ¶
IntToUint16 safely converts an int to uint16. Returns ErrOverflow if the value is outside the range [0, 65535].
func IntToUint32 ¶
IntToUint32 safely converts an int to uint32. Returns ErrOverflow if the value is outside the range [0, 4294967295].
func IntToUint64 ¶
IntToUint64 safely converts an int to uint64. Returns ErrOverflow if the value is negative.
func MultiplyInt ¶
MultiplyInt safely multiplies two int values. Returns ErrOverflow if the result would overflow int.
func MustInt64ToInt ¶
MustInt64ToInt converts an int64 to int, panicking on overflow. Use only when overflow is logically impossible.
func MustIntToInt32 ¶
MustIntToInt32 converts an int to int32, panicking on overflow. Use only when overflow is logically impossible.
func MustIntToUint ¶
MustIntToUint converts an int to uint, panicking on overflow. Use only when the value is guaranteed to be non-negative.
func MustIntToUint8 ¶
MustIntToUint8 converts an int to uint8, panicking on overflow. Use only when overflow is logically impossible.
func MustIntToUint16 ¶
MustIntToUint16 converts an int to uint16, panicking on overflow. Use only when overflow is logically impossible.
func MustIntToUint32 ¶
MustIntToUint32 converts an int to uint32, panicking on overflow. Use only when overflow is logically impossible.
func MustIntToUint64 ¶
MustIntToUint64 converts an int to uint64, panicking on overflow. Use only when the value is guaranteed to be non-negative.
func MustToInt8 ¶
MustToInt8 converts an int64 to int8, panicking on overflow. Use only when overflow is logically impossible.
func MustToInt16 ¶
MustToInt16 converts an int64 to int16, panicking on overflow. Use only when overflow is logically impossible.
func MustToInt32 ¶
MustToInt32 converts an int64 to int32, panicking on overflow. Use only when overflow is logically impossible.
func MustToUint8 ¶
MustToUint8 converts an int64 to uint8, panicking on overflow. Use only when overflow is logically impossible (e.g., value is from a validated source or is the result of a modulo operation that guarantees the range).
func MustToUint16 ¶
MustToUint16 converts an int64 to uint16, panicking on overflow. Use only when overflow is logically impossible.
func MustToUint32 ¶
MustToUint32 converts an int64 to uint32, panicking on overflow. Use only when overflow is logically impossible.
func MustToUint64 ¶
MustToUint64 converts an int64 to uint64, panicking on overflow. Use only when the value is guaranteed to be non-negative.
func MustUint64ToInt ¶
MustUint64ToInt converts a uint64 to int, panicking on overflow. Use only when overflow is logically impossible.
func MustUint64ToInt64 ¶
MustUint64ToInt64 converts a uint64 to int64, panicking on overflow. Use only when overflow is logically impossible.
func MustUint64ToUint32 ¶
MustUint64ToUint32 converts a uint64 to uint32, panicking on overflow. Use only when overflow is logically impossible.
func SafeShiftAmount ¶
SafeShiftAmount clamps a shift amount to a safe range [0, 63] to prevent panics from shifting by >= 64 bits. For JPEG, precision is typically 8-16, so this only affects malformed/malicious inputs.
func ToInt ¶
ToInt safely converts a uint64 to int. Returns ErrOverflow if the value exceeds MaxInt.
func ToInt8 ¶
ToInt8 safely converts an int64 to int8. Returns ErrOverflow if the value is outside the range [-128, 127].
func ToInt16 ¶
ToInt16 safely converts an int64 to int16. Returns ErrOverflow if the value is outside the range [-32768, 32767].
func ToInt32 ¶
ToInt32 safely converts an int64 to int32. Returns ErrOverflow if the value is outside the range [-2147483648, 2147483647].
func ToInt64 ¶
ToInt64 safely converts a uint64 to int64. Returns ErrOverflow if the value exceeds MaxInt64.
func ToUint ¶
ToUint safely converts an int64 to uint. Returns ErrOverflow if the value is negative.
func ToUint8 ¶
ToUint8 safely converts an int64 to uint8. Returns ErrOverflow if the value is outside the range [0, 255].
func ToUint16 ¶
ToUint16 safely converts an int64 to uint16. Returns ErrOverflow if the value is outside the range [0, 65535].
func ToUint32 ¶
ToUint32 safely converts an int64 to uint32. Returns ErrOverflow if the value is outside the range [0, 4294967295].
func ToUint64 ¶
ToUint64 safely converts an int64 to uint64. Returns ErrOverflow if the value is negative.
func Uint16ToInt ¶
Uint16ToInt safely converts a uint16 to int. This conversion is always safe as uint16 max (65535) fits in int.
func Uint16ToInt16 ¶
Uint16ToInt16 reinterprets a uint16 as an int16 preserving the bit pattern. This is used for binary formats where unsigned bytes represent signed values. Values 0-32767 map to 0-32767, values 32768-65535 map to -32768 to -1. This conversion is intentional bit reinterpretation and never overflows.
func Uint16ToUint8 ¶
Uint16ToUint8 safely converts a uint16 to uint8. Returns ErrOverflow if the value exceeds 255.
func Uint32ToInt ¶
Uint32ToInt safely converts a uint32 to int. Returns ErrOverflow if the value exceeds MaxInt (on 32-bit systems).
func Uint32ToUint8 ¶
Uint32ToUint8 safely converts a uint32 to uint8. Returns ErrOverflow if the value exceeds 255.
func Uint32ToUint16 ¶
Uint32ToUint16 safely converts a uint32 to uint16. Returns ErrOverflow if the value exceeds 65535.
func Uint64ToInt ¶
Uint64ToInt safely converts a uint64 to int. Returns ErrOverflow if the value exceeds MaxInt.
func Uint64ToInt64 ¶
Uint64ToInt64 safely converts a uint64 to int64. Returns ErrOverflow if the value exceeds MaxInt64.
func Uint64ToUint32 ¶
Uint64ToUint32 safely converts a uint64 to uint32. Returns ErrOverflow if the value exceeds MaxUint32.
Types ¶
This section is empty.