Skip to content

Commit

Permalink
Remove golang.org/x/exp dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Dec 12, 2024
1 parent 6fe9193 commit 9f26ed8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
6 changes: 2 additions & 4 deletions format/apple/loop_detector.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
17 changes: 17 additions & 0 deletions internal/mathx/constraints.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions internal/mathx/num.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package mathx

import (
"cmp"
"fmt"
"math"
"math/big"
"strconv"
"strings"

"github.com/wader/fq/pkg/ranges"
"golang.org/x/exp/constraints"
)

var BasePrefixMap = map[int]string{
Expand All @@ -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])
Expand All @@ -42,19 +42,19 @@ 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)
}

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))
}

Expand Down Expand Up @@ -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))
}

0 comments on commit 9f26ed8

Please sign in to comment.