diff options
| author | 2026-04-02 08:50:04 +0000 | |
|---|---|---|
| committer | 2026-04-02 08:50:04 +0000 | |
| commit | 69339e80dde55c03083f4e7ef98af5875a3b5cb2 (patch) | |
| tree | c1844b04d8212d680debeb2e4eea79e3aa263299 /internal | |
| parent | object/store: Add interfaces (diff) | |
| signature | No signature | |
internal/intconv: Actually it's fine for these to be in one file probably
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/intconv/i64_i32.go | 14 | ||||
| -rw-r--r-- | internal/intconv/i64_u64.go | 10 | ||||
| -rw-r--r-- | internal/intconv/i_u32.go | 14 | ||||
| -rw-r--r-- | internal/intconv/i_u64.go | 10 | ||||
| -rw-r--r-- | internal/intconv/ops.go | 93 | ||||
| -rw-r--r-- | internal/intconv/se_u8_u32.go | 10 | ||||
| -rw-r--r-- | internal/intconv/u32_i.go | 14 | ||||
| -rw-r--r-- | internal/intconv/u32_u8.go | 14 | ||||
| -rw-r--r-- | internal/intconv/u64_i.go | 14 | ||||
| -rw-r--r-- | internal/intconv/u64_i64.go | 14 | ||||
| -rw-r--r-- | internal/intconv/uptr_int.go | 14 |
11 files changed, 93 insertions, 128 deletions
diff --git a/internal/intconv/i64_i32.go b/internal/intconv/i64_i32.go deleted file mode 100644 index bcee9205..00000000 --- a/internal/intconv/i64_i32.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// 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, ErrOverflow - } - - return int32(v), nil -} diff --git a/internal/intconv/i64_u64.go b/internal/intconv/i64_u64.go deleted file mode 100644 index c04da3cd..00000000 --- a/internal/intconv/i64_u64.go +++ /dev/null @@ -1,10 +0,0 @@ -package intconv - -// Int64ToUint64 converts v to uint64, returning an error if v is negative. -func Int64ToUint64(v int64) (uint64, error) { - if v < 0 { - return 0, ErrOverflow - } - - return uint64(v), nil -} diff --git a/internal/intconv/i_u32.go b/internal/intconv/i_u32.go deleted file mode 100644 index b3c602ef..00000000 --- a/internal/intconv/i_u32.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// IntToUint32 converts v to uint32, returning an error if it overflows. -func IntToUint32(v int) (uint32, error) { - if v < 0 || v > math.MaxUint32 { - return 0, ErrOverflow - } - - return uint32(v), nil -} diff --git a/internal/intconv/i_u64.go b/internal/intconv/i_u64.go deleted file mode 100644 index cf57b677..00000000 --- a/internal/intconv/i_u64.go +++ /dev/null @@ -1,10 +0,0 @@ -package intconv - -// IntToUint64 converts v to uint64, returning an error if v is negative. -func IntToUint64(v int) (uint64, error) { - if v < 0 { - return 0, ErrOverflow - } - - return uint64(v), nil -} diff --git a/internal/intconv/ops.go b/internal/intconv/ops.go new file mode 100644 index 00000000..7f64e979 --- /dev/null +++ b/internal/intconv/ops.go @@ -0,0 +1,93 @@ +package intconv + +import "math" + +// 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, ErrOverflow + } + + return int32(v), nil +} + +// Int64ToUint64 converts v to uint64, returning an error if v is negative. +func Int64ToUint64(v int64) (uint64, error) { + if v < 0 { + return 0, ErrOverflow + } + + return uint64(v), nil +} + +// IntToUint32 converts v to uint32, returning an error if it overflows. +func IntToUint32(v int) (uint32, error) { + if v < 0 || v > math.MaxUint32 { + return 0, ErrOverflow + } + + return uint32(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, ErrOverflow + } + + return uint64(v), nil +} + +// SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32. +func SignExtendByteToUint32(b byte) uint32 { + if b&0x80 == 0 { + return uint32(b) + } + + return 0xFFFFFF00 | uint32(b) +} + +// Uint32ToInt converts v to int, returning an error if it overflows. +func Uint32ToInt(v uint32) (int, error) { + if uint64(v) > uint64(math.MaxInt) { + return 0, ErrOverflow + } + + return int(v), nil +} + +// Uint32ToUint8 converts v to uint8, returning an error if it overflows. +func Uint32ToUint8(v uint32) (uint8, error) { + if v > math.MaxUint8 { + return 0, ErrOverflow + } + + return uint8(v), nil +} + +// Uint64ToInt converts v to int, returning an error if it overflows. +func Uint64ToInt(v uint64) (int, error) { + if v > uint64(math.MaxInt) { + return 0, ErrOverflow + } + + return int(v), nil +} + +// Uint64ToInt64 converts v to int64, returning an error if it overflows. +func Uint64ToInt64(v uint64) (int64, error) { + if v > math.MaxInt64 { + return 0, ErrOverflow + } + + return int64(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, ErrOverflow + } + + return int(v), nil +} diff --git a/internal/intconv/se_u8_u32.go b/internal/intconv/se_u8_u32.go deleted file mode 100644 index bef34268..00000000 --- a/internal/intconv/se_u8_u32.go +++ /dev/null @@ -1,10 +0,0 @@ -package intconv - -// SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32. -func SignExtendByteToUint32(b byte) uint32 { - if b&0x80 == 0 { - return uint32(b) - } - - return 0xFFFFFF00 | uint32(b) -} diff --git a/internal/intconv/u32_i.go b/internal/intconv/u32_i.go deleted file mode 100644 index 24147c8b..00000000 --- a/internal/intconv/u32_i.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// Uint32ToInt converts v to int, returning an error if it overflows. -func Uint32ToInt(v uint32) (int, error) { - if uint64(v) > uint64(math.MaxInt) { - return 0, ErrOverflow - } - - return int(v), nil -} diff --git a/internal/intconv/u32_u8.go b/internal/intconv/u32_u8.go deleted file mode 100644 index 40744051..00000000 --- a/internal/intconv/u32_u8.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// Uint32ToUint8 converts v to uint8, returning an error if it overflows. -func Uint32ToUint8(v uint32) (uint8, error) { - if v > math.MaxUint8 { - return 0, ErrOverflow - } - - return uint8(v), nil -} diff --git a/internal/intconv/u64_i.go b/internal/intconv/u64_i.go deleted file mode 100644 index 1e9acacd..00000000 --- a/internal/intconv/u64_i.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "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, ErrOverflow - } - - return int(v), nil -} diff --git a/internal/intconv/u64_i64.go b/internal/intconv/u64_i64.go deleted file mode 100644 index 2285142c..00000000 --- a/internal/intconv/u64_i64.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// Uint64ToInt64 converts v to int64, returning an error if it overflows. -func Uint64ToInt64(v uint64) (int64, error) { - if v > math.MaxInt64 { - return 0, ErrOverflow - } - - return int64(v), nil -} diff --git a/internal/intconv/uptr_int.go b/internal/intconv/uptr_int.go deleted file mode 100644 index 1ca4c674..00000000 --- a/internal/intconv/uptr_int.go +++ /dev/null @@ -1,14 +0,0 @@ -package intconv - -import ( - "math" -) - -// UintptrToInt converts v to int, returning an error if it overflows. -func UintptrToInt(v uintptr) (int, error) { - if v > uintptr(math.MaxInt) { - return 0, ErrOverflow - } - - return int(v), nil -} |
