Fast implementation of Git in pure Go

internal/intconv: Split

runxiyu.tngl.sh 9053c854 cf02f295

verified
+141 -97
+2
internal/intconv/doc.go
··· 1 + // Package intconv provides checked integer conversion helpers. 2 + package intconv
+15
internal/intconv/i64_i32.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // Int64ToInt32 converts v to int32, returning an error if it overflows. 9 + func Int64ToInt32(v int64) (int32, error) { 10 + if v < math.MinInt32 || v > math.MaxInt32 { 11 + return 0, fmt.Errorf("intconv: int64 %d overflows int32", v) 12 + } 13 + 14 + return int32(v), nil 15 + }
+12
internal/intconv/i64_u64.go
··· 1 + package intconv 2 + 3 + import "fmt" 4 + 5 + // Int64ToUint64 converts v to uint64, returning an error if v is negative. 6 + func Int64ToUint64(v int64) (uint64, error) { 7 + if v < 0 { 8 + return 0, fmt.Errorf("intconv: int64 %d is negative", v) 9 + } 10 + 11 + return uint64(v), nil 12 + }
+15
internal/intconv/i_u32.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // IntToUint32 converts v to uint32, returning an error if it overflows. 9 + func IntToUint32(v int) (uint32, error) { 10 + if v < 0 || v > math.MaxUint32 { 11 + return 0, fmt.Errorf("intconv: int %d overflows uint32", v) 12 + } 13 + 14 + return uint32(v), nil 15 + }
+12
internal/intconv/i_u64.go
··· 1 + package intconv 2 + 3 + import "fmt" 4 + 5 + // IntToUint64 converts v to uint64, returning an error if v is negative. 6 + func IntToUint64(v int) (uint64, error) { 7 + if v < 0 { 8 + return 0, fmt.Errorf("intconv: int %d is negative", v) 9 + } 10 + 11 + return uint64(v), nil 12 + }
-97
internal/intconv/intconv.go
··· 1 - // Package intconv provides checked integer conversion helpers. 2 - package intconv 3 - 4 - import ( 5 - "fmt" 6 - "math" 7 - ) 8 - 9 - // Uint64ToInt converts v to int, returning an error if it overflows. 10 - func Uint64ToInt(v uint64) (int, error) { 11 - if v > uint64(math.MaxInt) { 12 - return 0, fmt.Errorf("intconv: uint64 %d overflows int", v) 13 - } 14 - 15 - return int(v), nil 16 - } 17 - 18 - // UintptrToInt converts v to int, returning an error if it overflows. 19 - func UintptrToInt(v uintptr) (int, error) { 20 - if v > uintptr(math.MaxInt) { 21 - return 0, fmt.Errorf("intconv: uintptr %d overflows int", v) 22 - } 23 - 24 - return int(v), nil 25 - } 26 - 27 - // IntToUint64 converts v to uint64, returning an error if v is negative. 28 - func IntToUint64(v int) (uint64, error) { 29 - if v < 0 { 30 - return 0, fmt.Errorf("intconv: int %d is negative", v) 31 - } 32 - 33 - return uint64(v), nil 34 - } 35 - 36 - // IntToUint32 converts v to uint32, returning an error if it overflows. 37 - func IntToUint32(v int) (uint32, error) { 38 - if v < 0 || v > math.MaxUint32 { 39 - return 0, fmt.Errorf("intconv: int %d overflows uint32", v) 40 - } 41 - 42 - return uint32(v), nil 43 - } 44 - 45 - // Uint64ToInt64 converts v to int64, returning an error if it overflows. 46 - func Uint64ToInt64(v uint64) (int64, error) { 47 - if v > math.MaxInt64 { 48 - return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v) 49 - } 50 - 51 - return int64(v), nil 52 - } 53 - 54 - // Int64ToUint64 converts v to uint64, returning an error if v is negative. 55 - func Int64ToUint64(v int64) (uint64, error) { 56 - if v < 0 { 57 - return 0, fmt.Errorf("intconv: int64 %d is negative", v) 58 - } 59 - 60 - return uint64(v), nil 61 - } 62 - 63 - // Int64ToInt32 converts v to int32, returning an error if it overflows. 64 - func Int64ToInt32(v int64) (int32, error) { 65 - if v < math.MinInt32 || v > math.MaxInt32 { 66 - return 0, fmt.Errorf("intconv: int64 %d overflows int32", v) 67 - } 68 - 69 - return int32(v), nil 70 - } 71 - 72 - // SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32. 73 - func SignExtendByteToUint32(b byte) uint32 { 74 - if b&0x80 == 0 { 75 - return uint32(b) 76 - } 77 - 78 - return 0xFFFFFF00 | uint32(b) 79 - } 80 - 81 - // Uint32ToUint8 converts v to uint8, returning an error if it overflows. 82 - func Uint32ToUint8(v uint32) (uint8, error) { 83 - if v > math.MaxUint8 { 84 - return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v) 85 - } 86 - 87 - return uint8(v), nil 88 - } 89 - 90 - // Uint32ToInt converts v to int, returning an error if it overflows. 91 - func Uint32ToInt(v uint32) (int, error) { 92 - if uint64(v) > uint64(math.MaxInt) { 93 - return 0, fmt.Errorf("intconv: uint32 %d overflows int", v) 94 - } 95 - 96 - return int(v), nil 97 - }
+10
internal/intconv/se_u8_u32.go
··· 1 + package intconv 2 + 3 + // SignExtendByteToUint32 sign-extends b as a signed 8-bit integer into uint32. 4 + func SignExtendByteToUint32(b byte) uint32 { 5 + if b&0x80 == 0 { 6 + return uint32(b) 7 + } 8 + 9 + return 0xFFFFFF00 | uint32(b) 10 + }
+15
internal/intconv/u32_i.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // Uint32ToInt converts v to int, returning an error if it overflows. 9 + func Uint32ToInt(v uint32) (int, error) { 10 + if uint64(v) > uint64(math.MaxInt) { 11 + return 0, fmt.Errorf("intconv: uint32 %d overflows int", v) 12 + } 13 + 14 + return int(v), nil 15 + }
+15
internal/intconv/u32_u8.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // Uint32ToUint8 converts v to uint8, returning an error if it overflows. 9 + func Uint32ToUint8(v uint32) (uint8, error) { 10 + if v > math.MaxUint8 { 11 + return 0, fmt.Errorf("intconv: uint32 %d overflows uint8", v) 12 + } 13 + 14 + return uint8(v), nil 15 + }
+15
internal/intconv/u64_i.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // Uint64ToInt converts v to int, returning an error if it overflows. 9 + func Uint64ToInt(v uint64) (int, error) { 10 + if v > uint64(math.MaxInt) { 11 + return 0, fmt.Errorf("intconv: uint64 %d overflows int", v) 12 + } 13 + 14 + return int(v), nil 15 + }
+15
internal/intconv/u64_i64.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // Uint64ToInt64 converts v to int64, returning an error if it overflows. 9 + func Uint64ToInt64(v uint64) (int64, error) { 10 + if v > math.MaxInt64 { 11 + return 0, fmt.Errorf("intconv: uint64 %d overflows int64", v) 12 + } 13 + 14 + return int64(v), nil 15 + }
+15
internal/intconv/uptr_int.go
··· 1 + package intconv 2 + 3 + import ( 4 + "fmt" 5 + "math" 6 + ) 7 + 8 + // UintptrToInt converts v to int, returning an error if it overflows. 9 + func UintptrToInt(v uintptr) (int, error) { 10 + if v > uintptr(math.MaxInt) { 11 + return 0, fmt.Errorf("intconv: uintptr %d overflows int", v) 12 + } 13 + 14 + return int(v), nil 15 + }