aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/intconv/doc.go2
-rw-r--r--internal/intconv/i64_i32.go15
-rw-r--r--internal/intconv/i64_u64.go12
-rw-r--r--internal/intconv/i_u32.go15
-rw-r--r--internal/intconv/i_u64.go12
-rw-r--r--internal/intconv/intconv.go97
-rw-r--r--internal/intconv/se_u8_u32.go10
-rw-r--r--internal/intconv/u32_i.go15
-rw-r--r--internal/intconv/u32_u8.go15
-rw-r--r--internal/intconv/u64_i.go15
-rw-r--r--internal/intconv/u64_i64.go15
-rw-r--r--internal/intconv/uptr_int.go15
12 files changed, 141 insertions, 97 deletions
diff --git a/internal/intconv/doc.go b/internal/intconv/doc.go
new file mode 100644
index 00000000..fc1f7428
--- /dev/null
+++ b/internal/intconv/doc.go
@@ -0,0 +1,2 @@
+// Package intconv provides checked integer conversion helpers.
+package intconv
diff --git a/internal/intconv/i64_i32.go b/internal/intconv/i64_i32.go
new file mode 100644
index 00000000..485e7895
--- /dev/null
+++ b/internal/intconv/i64_i32.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "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, fmt.Errorf("intconv: int64 %d overflows int32", v)
+ }
+
+ return int32(v), nil
+}
diff --git a/internal/intconv/i64_u64.go b/internal/intconv/i64_u64.go
new file mode 100644
index 00000000..4c9b56c5
--- /dev/null
+++ b/internal/intconv/i64_u64.go
@@ -0,0 +1,12 @@
+package intconv
+
+import "fmt"
+
+// Int64ToUint64 converts v to uint64, returning an error if v is negative.
+func Int64ToUint64(v int64) (uint64, error) {
+ if v < 0 {
+ return 0, fmt.Errorf("intconv: int64 %d is negative", v)
+ }
+
+ return uint64(v), nil
+}
diff --git a/internal/intconv/i_u32.go b/internal/intconv/i_u32.go
new file mode 100644
index 00000000..5354010c
--- /dev/null
+++ b/internal/intconv/i_u32.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "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, fmt.Errorf("intconv: int %d overflows uint32", v)
+ }
+
+ return uint32(v), nil
+}
diff --git a/internal/intconv/i_u64.go b/internal/intconv/i_u64.go
new file mode 100644
index 00000000..a94a162c
--- /dev/null
+++ b/internal/intconv/i_u64.go
@@ -0,0 +1,12 @@
+package intconv
+
+import "fmt"
+
+// 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
+}
diff --git a/internal/intconv/intconv.go b/internal/intconv/intconv.go
deleted file mode 100644
index 08530265..00000000
--- a/internal/intconv/intconv.go
+++ /dev/null
@@ -1,97 +0,0 @@
-// 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
-}
-
-// 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, fmt.Errorf("intconv: int %d overflows uint32", v)
- }
-
- return uint32(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, fmt.Errorf("intconv: uint64 %d overflows int64", v)
- }
-
- return int64(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, fmt.Errorf("intconv: int64 %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
-}
-
-// 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)
-}
-
-// Uint32ToUint8 converts v to uint8, returning an error if it overflows.
-func Uint32ToUint8(v uint32) (uint8, error) {
- if v > math.MaxUint8 {
- return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v)
- }
-
- return uint8(v), nil
-}
-
-// 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, fmt.Errorf("intconv: uint32 %d overflows int", v)
- }
-
- return int(v), nil
-}
diff --git a/internal/intconv/se_u8_u32.go b/internal/intconv/se_u8_u32.go
new file mode 100644
index 00000000..bef34268
--- /dev/null
+++ b/internal/intconv/se_u8_u32.go
@@ -0,0 +1,10 @@
+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
new file mode 100644
index 00000000..a0f88724
--- /dev/null
+++ b/internal/intconv/u32_i.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "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, fmt.Errorf("intconv: uint32 %d overflows int", v)
+ }
+
+ return int(v), nil
+}
diff --git a/internal/intconv/u32_u8.go b/internal/intconv/u32_u8.go
new file mode 100644
index 00000000..13aee55b
--- /dev/null
+++ b/internal/intconv/u32_u8.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "math"
+)
+
+// Uint32ToUint8 converts v to uint8, returning an error if it overflows.
+func Uint32ToUint8(v uint32) (uint8, error) {
+ if v > math.MaxUint8 {
+ return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v)
+ }
+
+ return uint8(v), nil
+}
diff --git a/internal/intconv/u64_i.go b/internal/intconv/u64_i.go
new file mode 100644
index 00000000..45b88d53
--- /dev/null
+++ b/internal/intconv/u64_i.go
@@ -0,0 +1,15 @@
+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
+}
diff --git a/internal/intconv/u64_i64.go b/internal/intconv/u64_i64.go
new file mode 100644
index 00000000..59b26a73
--- /dev/null
+++ b/internal/intconv/u64_i64.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "math"
+)
+
+// Uint64ToInt64 converts v to int64, returning an error if it overflows.
+func Uint64ToInt64(v uint64) (int64, error) {
+ if v > math.MaxInt64 {
+ return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v)
+ }
+
+ return int64(v), nil
+}
diff --git a/internal/intconv/uptr_int.go b/internal/intconv/uptr_int.go
new file mode 100644
index 00000000..fa832147
--- /dev/null
+++ b/internal/intconv/uptr_int.go
@@ -0,0 +1,15 @@
+package intconv
+
+import (
+ "fmt"
+ "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, fmt.Errorf("intconv: uintptr %d overflows int", v)
+ }
+
+ return int(v), nil
+}