diff options
| author | 2026-02-21 13:38:02 +0800 | |
|---|---|---|
| committer | 2026-02-21 14:28:15 +0800 | |
| commit | 94482cb2c97aa215f83940643c5d4c0933727dcb (patch) | |
| tree | bee22fa113542abd1b863ee251fdcf0f9bd409b5 /internal/intconv | |
| parent | diff: Add package-level doc comment (diff) | |
| signature | No signature | |
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'internal/intconv')
| -rw-r--r-- | internal/intconv/intconv.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/intconv/intconv.go b/internal/intconv/intconv.go new file mode 100644 index 00000000..8bc77d8e --- /dev/null +++ b/internal/intconv/intconv.go @@ -0,0 +1,39 @@ +// Package intconv provides checked integer conversion helpers. +package intconv + +import ( + "fmt" + "math" +) + +// Uint64ToInt converts v to int, returning an error if it overflows. +func Uint64ToInt(v uint64) (int, error) { + if v > uint64(math.MaxInt) { + return 0, fmt.Errorf("intconv: uint64 %d overflows int", v) + } + return int(v), nil +} + +// UintptrToInt converts v to int, returning an error if it overflows. +func UintptrToInt(v uintptr) (int, error) { + if v > uintptr(math.MaxInt) { + return 0, fmt.Errorf("intconv: uintptr %d overflows int", v) + } + return int(v), nil +} + +// IntToUint64 converts v to uint64, returning an error if v is negative. +func IntToUint64(v int) (uint64, error) { + if v < 0 { + return 0, fmt.Errorf("intconv: int %d is negative", v) + } + return uint64(v), nil +} + +// Int64ToInt32 converts v to int32, returning an error if it overflows. +func Int64ToInt32(v int64) (int32, error) { + if v < math.MinInt32 || v > math.MaxInt32 { + return 0, fmt.Errorf("intconv: int64 %d overflows int32", v) + } + return int32(v), nil +} |
