Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 8 additions & 4 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
31 changes: 16 additions & 15 deletions v2/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hostutils
import (
"bytes"
"fmt"
"golang.org/x/exp/slices"
"slices"
"strconv"
)

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