···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// Int64ToInt32 converts v to int32, returning an error if it overflows.
99+func Int64ToInt32(v int64) (int32, error) {
1010+ if v < math.MinInt32 || v > math.MaxInt32 {
1111+ return 0, fmt.Errorf("intconv: int64 %d overflows int32", v)
1212+ }
1313+1414+ return int32(v), nil
1515+}
+12
internal/intconv/i64_u64.go
···11+package intconv
22+33+import "fmt"
44+55+// Int64ToUint64 converts v to uint64, returning an error if v is negative.
66+func Int64ToUint64(v int64) (uint64, error) {
77+ if v < 0 {
88+ return 0, fmt.Errorf("intconv: int64 %d is negative", v)
99+ }
1010+1111+ return uint64(v), nil
1212+}
+15
internal/intconv/i_u32.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// IntToUint32 converts v to uint32, returning an error if it overflows.
99+func IntToUint32(v int) (uint32, error) {
1010+ if v < 0 || v > math.MaxUint32 {
1111+ return 0, fmt.Errorf("intconv: int %d overflows uint32", v)
1212+ }
1313+1414+ return uint32(v), nil
1515+}
+12
internal/intconv/i_u64.go
···11+package intconv
22+33+import "fmt"
44+55+// IntToUint64 converts v to uint64, returning an error if v is negative.
66+func IntToUint64(v int) (uint64, error) {
77+ if v < 0 {
88+ return 0, fmt.Errorf("intconv: int %d is negative", v)
99+ }
1010+1111+ return uint64(v), nil
1212+}
-97
internal/intconv/intconv.go
···11-// Package intconv provides checked integer conversion helpers.
22-package intconv
33-44-import (
55- "fmt"
66- "math"
77-)
88-99-// Uint64ToInt converts v to int, returning an error if it overflows.
1010-func Uint64ToInt(v uint64) (int, error) {
1111- if v > uint64(math.MaxInt) {
1212- return 0, fmt.Errorf("intconv: uint64 %d overflows int", v)
1313- }
1414-1515- return int(v), nil
1616-}
1717-1818-// UintptrToInt converts v to int, returning an error if it overflows.
1919-func UintptrToInt(v uintptr) (int, error) {
2020- if v > uintptr(math.MaxInt) {
2121- return 0, fmt.Errorf("intconv: uintptr %d overflows int", v)
2222- }
2323-2424- return int(v), nil
2525-}
2626-2727-// IntToUint64 converts v to uint64, returning an error if v is negative.
2828-func IntToUint64(v int) (uint64, error) {
2929- if v < 0 {
3030- return 0, fmt.Errorf("intconv: int %d is negative", v)
3131- }
3232-3333- return uint64(v), nil
3434-}
3535-3636-// IntToUint32 converts v to uint32, returning an error if it overflows.
3737-func IntToUint32(v int) (uint32, error) {
3838- if v < 0 || v > math.MaxUint32 {
3939- return 0, fmt.Errorf("intconv: int %d overflows uint32", v)
4040- }
4141-4242- return uint32(v), nil
4343-}
4444-4545-// Uint64ToInt64 converts v to int64, returning an error if it overflows.
4646-func Uint64ToInt64(v uint64) (int64, error) {
4747- if v > math.MaxInt64 {
4848- return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v)
4949- }
5050-5151- return int64(v), nil
5252-}
5353-5454-// Int64ToUint64 converts v to uint64, returning an error if v is negative.
5555-func Int64ToUint64(v int64) (uint64, error) {
5656- if v < 0 {
5757- return 0, fmt.Errorf("intconv: int64 %d is negative", v)
5858- }
5959-6060- return uint64(v), nil
6161-}
6262-6363-// Int64ToInt32 converts v to int32, returning an error if it overflows.
6464-func Int64ToInt32(v int64) (int32, error) {
6565- if v < math.MinInt32 || v > math.MaxInt32 {
6666- return 0, fmt.Errorf("intconv: int64 %d overflows int32", v)
6767- }
6868-6969- return int32(v), nil
7070-}
7171-7272-// SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32.
7373-func SignExtendByteToUint32(b byte) uint32 {
7474- if b&0x80 == 0 {
7575- return uint32(b)
7676- }
7777-7878- return 0xFFFFFF00 | uint32(b)
7979-}
8080-8181-// Uint32ToUint8 converts v to uint8, returning an error if it overflows.
8282-func Uint32ToUint8(v uint32) (uint8, error) {
8383- if v > math.MaxUint8 {
8484- return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v)
8585- }
8686-8787- return uint8(v), nil
8888-}
8989-9090-// Uint32ToInt converts v to int, returning an error if it overflows.
9191-func Uint32ToInt(v uint32) (int, error) {
9292- if uint64(v) > uint64(math.MaxInt) {
9393- return 0, fmt.Errorf("intconv: uint32 %d overflows int", v)
9494- }
9595-9696- return int(v), nil
9797-}
+10
internal/intconv/se_u8_u32.go
···11+package intconv
22+33+// SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32.
44+func SignExtendByteToUint32(b byte) uint32 {
55+ if b&0x80 == 0 {
66+ return uint32(b)
77+ }
88+99+ return 0xFFFFFF00 | uint32(b)
1010+}
+15
internal/intconv/u32_i.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// Uint32ToInt converts v to int, returning an error if it overflows.
99+func Uint32ToInt(v uint32) (int, error) {
1010+ if uint64(v) > uint64(math.MaxInt) {
1111+ return 0, fmt.Errorf("intconv: uint32 %d overflows int", v)
1212+ }
1313+1414+ return int(v), nil
1515+}
+15
internal/intconv/u32_u8.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// Uint32ToUint8 converts v to uint8, returning an error if it overflows.
99+func Uint32ToUint8(v uint32) (uint8, error) {
1010+ if v > math.MaxUint8 {
1111+ return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v)
1212+ }
1313+1414+ return uint8(v), nil
1515+}
+15
internal/intconv/u64_i.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// Uint64ToInt converts v to int, returning an error if it overflows.
99+func Uint64ToInt(v uint64) (int, error) {
1010+ if v > uint64(math.MaxInt) {
1111+ return 0, fmt.Errorf("intconv: uint64 %d overflows int", v)
1212+ }
1313+1414+ return int(v), nil
1515+}
+15
internal/intconv/u64_i64.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// Uint64ToInt64 converts v to int64, returning an error if it overflows.
99+func Uint64ToInt64(v uint64) (int64, error) {
1010+ if v > math.MaxInt64 {
1111+ return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v)
1212+ }
1313+1414+ return int64(v), nil
1515+}
+15
internal/intconv/uptr_int.go
···11+package intconv
22+33+import (
44+ "fmt"
55+ "math"
66+)
77+88+// UintptrToInt converts v to int, returning an error if it overflows.
99+func UintptrToInt(v uintptr) (int, error) {
1010+ if v > uintptr(math.MaxInt) {
1111+ return 0, fmt.Errorf("intconv: uintptr %d overflows int", v)
1212+ }
1313+1414+ return int(v), nil
1515+}