diff --git a/format/apple/loop_detector.go b/format/apple/loop_detector.go index ca58e6522..c8dfff316 100644 --- a/format/apple/loop_detector.go +++ b/format/apple/loop_detector.go @@ -1,12 +1,10 @@ package apple -import ( - "golang.org/x/exp/constraints" -) +import "github.com/wader/fq/internal/mathx" // PosLoopDetector is used for detecting loops when writing decoders, and can // short-circuit infinite recursion that can cause stack overflows. -type PosLoopDetector[T constraints.Integer] []T +type PosLoopDetector[T mathx.Integer] []T // Push adds the current offset to the stack and executes the supplied // detection function diff --git a/go.mod b/go.mod index 2e3f86bc6..1752c5edc 100644 --- a/go.mod +++ b/go.mod @@ -52,10 +52,6 @@ require ( // bump: gomod-golang-x-crypto link "Tags" https://github.com/golang/crypto/tags golang.org/x/crypto v0.31.0 - // has no tags - // go get golang.org/x/exp@master && go mod tidy - golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa - // bump: gomod-golang-x-net /golang\.org\/x\/net v(.*)/ https://github.com/golang/net.git|^0 // bump: gomod-golang-x-net command go get golang.org/x/net@v$LATEST && go mod tidy // bump: gomod-golang-x-net link "Tags" https://github.com/golang/net/tags diff --git a/go.sum b/go.sum index 4a883f438..d3cb4b30c 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,6 @@ github.com/wader/gojq v0.12.1-0.20240822064856-a7688e3344e7 h1:zrUiUpIX/C5QzEkR/ github.com/wader/gojq v0.12.1-0.20240822064856-a7688e3344e7/go.mod h1:EPKZhJLM6ILU40HkgFbhrsV7MHf5flxQDS5fSf/KNpE= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= -golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI= -golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= diff --git a/internal/mathx/constraints.go b/internal/mathx/constraints.go new file mode 100644 index 000000000..813204d92 --- /dev/null +++ b/internal/mathx/constraints.go @@ -0,0 +1,17 @@ +// Constraints type from https://github.com/golang/exp/blob/1829a127f884df39fc2eaf7e0dfc760648098768/constraints/constraints.go. +package mathx + +// Signed is a constraint that permits any signed integer type. +type Signed interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 +} + +// Unsigned is a constraint that permits any unsigned integer type. +type Unsigned interface { + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +} + +// Integer is a constraint that permits any integer type. +type Integer interface { + Signed | Unsigned +} diff --git a/internal/mathx/num.go b/internal/mathx/num.go index 343334d36..50296cbb4 100644 --- a/internal/mathx/num.go +++ b/internal/mathx/num.go @@ -1,6 +1,7 @@ package mathx import ( + "cmp" "fmt" "math" "math/big" @@ -8,7 +9,6 @@ import ( "strings" "github.com/wader/fq/pkg/ranges" - "golang.org/x/exp/constraints" ) var BasePrefixMap = map[int]string{ @@ -17,7 +17,7 @@ var BasePrefixMap = map[int]string{ 16: "0x", } -func DigitsInBase[T constraints.Integer](n T, basePrefix bool, base int) int { +func DigitsInBase[T Integer](n T, basePrefix bool, base int) int { prefixLen := 0 if basePrefix { prefixLen = len(BasePrefixMap[base]) @@ -42,11 +42,11 @@ func padFormatNumber(s string, base int, basePrefix bool, width int) string { return prefixStr + padStr + s } -func PadFormatInt[T constraints.Signed](i T, base int, basePrefix bool, width int) string { +func PadFormatInt[T Signed](i T, base int, basePrefix bool, width int) string { return padFormatNumber(strconv.FormatInt(int64(i), base), base, basePrefix, width) } -func PadFormatUint[T constraints.Unsigned](i T, base int, basePrefix bool, width int) string { +func PadFormatUint[T Unsigned](i T, base int, basePrefix bool, width int) string { return padFormatNumber(strconv.FormatUint(uint64(i), base), base, basePrefix, width) } @@ -54,7 +54,7 @@ func PadFormatBigInt(i *big.Int, base int, basePrefix bool, width int) string { return padFormatNumber(i.Text(base), base, basePrefix, width) } -func Clamp[T constraints.Ordered](a, b, v T) T { +func Clamp[T cmp.Ordered](a, b, v T) T { return max(a, min(b, v)) } @@ -83,6 +83,6 @@ func TwosComplement(nBits int, n uint64) int64 { // decode zigzag encoded integer // https://developers.google.com/protocol-buffers/docs/encoding -func ZigZag[U constraints.Unsigned, S constraints.Signed](n U) S { +func ZigZag[U Unsigned, S Signed](n U) S { return S(n>>1 ^ -(n & 1)) }