diff --git a/CHANGELOG.md b/CHANGELOG.md index 7425abf..1542572 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 2.0.3 (2024-06-05) + +* Bump up `golang.org/x/exp` to `v0.0.0-20240604190554-fc45aab8b7f8` + ## 2.0.2 (2023-04-18) * Fix a bug on sorting, which cause `Pack` don't work as expected diff --git a/v2/go.mod b/v2/go.mod index 4e5b3d7..3823596 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -3,11 +3,12 @@ module github.com/Wing924/hostutils/v2 go 1.20 require ( - github.com/stretchr/testify v1.2.2 - golang.org/x/exp v0.0.0-20230321023759-10a507213a29 + github.com/stretchr/testify v1.9.0 + golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/v2/go.sum b/v2/go.sum index 73b746c..2c692e7 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -2,7 +2,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM= +golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/v2/pack.go b/v2/pack.go index 1f0dd7e..f75233c 100644 --- a/v2/pack.go +++ b/v2/pack.go @@ -3,7 +3,7 @@ package hostutils import ( "bytes" "fmt" - "golang.org/x/exp/slices" + "slices" "strconv" ) @@ -47,8 +47,8 @@ func Pack(hosts []string) []string { for _, host := range regHosts { uniqHosts = append(uniqHosts, parseHost(host)) } - slices.SortFunc(uniqHosts, func(a, b *host) bool { - return a.Less(b) + slices.SortFunc(uniqHosts, func(a, b *host) int { + return a.CompareTo(b) }) result := make([]string, 0, len(uniqHosts)) @@ -224,41 +224,42 @@ func (h *host) appendToken(token string, digitMode bool) { } func (h *host) Less(rhs *host) bool { + return h.CompareTo(rhs) < 0 +} + +func (h *host) CompareTo(rhs *host) int { m := min(len(h.NonDigits), len(rhs.NonDigits)) for i := 0; i < m; i++ { if h.NonDigits[i] < rhs.NonDigits[i] { - return true + return -1 } if h.NonDigits[i] > rhs.NonDigits[i] { - return false + return 1 } } if len(h.NonDigits) < len(rhs.NonDigits) { - return true + return -1 } if len(h.NonDigits) > len(rhs.NonDigits) { - return false + return 1 } m = min(len(h.Digits), len(rhs.Digits)) for i := m - 1; i >= 0; i-- { if h.Digits[i].Digit < rhs.Digits[i].Digit { - return true + return -1 } if h.Digits[i].Digit > rhs.Digits[i].Digit { - return false + return 1 } if h.Digits[i].Value < rhs.Digits[i].Value { - return true + return -1 } if h.Digits[i].Value > rhs.Digits[i].Value { - return false + return 1 } } - if len(h.Digits) < len(rhs.Digits) { - return true - } - return false + return len(h.Digits) - len(rhs.Digits) } func isDigit(c rune) bool {