Split vercmp into a separate module
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
d144a7f758
commit
36d3be72a7
5
go.mod
5
go.mod
@ -6,6 +6,7 @@ require (
|
||||
github.com/AlecAivazis/survey/v2 v2.3.6
|
||||
github.com/PuerkitoBio/purell v1.2.0
|
||||
github.com/alecthomas/chroma/v2 v2.4.0
|
||||
github.com/anacrolix/log v0.14.0
|
||||
github.com/anacrolix/torrent v1.51.3
|
||||
github.com/charmbracelet/bubbles v0.14.0
|
||||
github.com/charmbracelet/bubbletea v0.23.1
|
||||
@ -26,7 +27,8 @@ require (
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5
|
||||
go.elara.ws/logger v0.0.0-20230421022458-e80700db2090
|
||||
go.elara.ws/translate v0.0.0-20230421025926-32ccfcd110e6
|
||||
golang.org/x/exp v0.0.0-20220916125017-b168a2c6b86b
|
||||
go.elara.ws/vercmp v0.0.0-20230622214216-0b2b067575c4
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
|
||||
golang.org/x/sys v0.7.0
|
||||
golang.org/x/text v0.9.0
|
||||
google.golang.org/protobuf v1.28.1
|
||||
@ -53,7 +55,6 @@ require (
|
||||
github.com/anacrolix/envpprof v1.2.1 // indirect
|
||||
github.com/anacrolix/generics v0.0.0-20230428105757-683593396d68 // indirect
|
||||
github.com/anacrolix/go-libutp v1.2.0 // indirect
|
||||
github.com/anacrolix/log v0.14.0 // indirect
|
||||
github.com/anacrolix/missinggo v1.3.0 // indirect
|
||||
github.com/anacrolix/missinggo/perf v1.0.0 // indirect
|
||||
github.com/anacrolix/missinggo/v2 v2.7.0 // indirect
|
||||
|
10
go.sum
10
go.sum
@ -72,8 +72,6 @@ github.com/anacrolix/log v0.3.0/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgw
|
||||
github.com/anacrolix/log v0.6.0/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgwU9jwU=
|
||||
github.com/anacrolix/log v0.10.0/go.mod h1:s5yBP/j046fm9odtUTbHOfDUq/zh1W8OkPpJtnX0oQI=
|
||||
github.com/anacrolix/log v0.10.1-0.20220123034749-3920702c17f8/go.mod h1:GmnE2c0nvz8pOIPUSC9Rawgefy1sDXqposC2wgtBZE4=
|
||||
github.com/anacrolix/log v0.13.2-0.20230518105052-6aef2c4c91f1 h1:Yo4XQhmdmrkB4RGP7RWvl8U+og2rCBsNqoJFTew0plk=
|
||||
github.com/anacrolix/log v0.13.2-0.20230518105052-6aef2c4c91f1/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
|
||||
github.com/anacrolix/log v0.14.0 h1:mYhTSemILe/Z8tIxbGdTIWWpPspI8W/fhZHpoFbDaL0=
|
||||
github.com/anacrolix/log v0.14.0/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
|
||||
github.com/anacrolix/lsan v0.0.0-20211126052245-807000409a62 h1:P04VG6Td13FHMgS5ZBcJX23NPC/fiC4cp9bXwYujdYM=
|
||||
@ -106,8 +104,6 @@ github.com/anacrolix/sync v0.4.0/go.mod h1:BbecHL6jDSExojhNtgTFSBcdGerzNc64tz3DC
|
||||
github.com/anacrolix/tagflag v0.0.0-20180109131632-2146c8d41bf0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw=
|
||||
github.com/anacrolix/tagflag v1.0.0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw=
|
||||
github.com/anacrolix/tagflag v1.1.0/go.mod h1:Scxs9CV10NQatSmbyjqmqmeQNwGzlNe0CMUMIxqHIG8=
|
||||
github.com/anacrolix/torrent v1.51.2 h1:7UWUElt7rCVZlFF/VMaBOoZfpNtaMDj1zfvgsoX17LA=
|
||||
github.com/anacrolix/torrent v1.51.2/go.mod h1:yCbMnw1IpltDstasqpZploAMnJrkXNjZFjo7XdDYUDY=
|
||||
github.com/anacrolix/torrent v1.51.3 h1:Rj5LNfT2/IucClxyskD5klaepNQorSeWHChP+y/xYU8=
|
||||
github.com/anacrolix/torrent v1.51.3/go.mod h1:t9v92CO5xOCvmg+Qfn3XcBbXVhN9Xg6xID2d565IhVo=
|
||||
github.com/anacrolix/upnp v0.1.3-0.20220123035249-922794e51c96 h1:QAVZ3pN/J4/UziniAhJR2OZ9Ox5kOY2053tBbbqUPYA=
|
||||
@ -560,6 +556,8 @@ go.elara.ws/logger v0.0.0-20230421022458-e80700db2090 h1:RVC8XvWo6Yw4HUshqx4TSzu
|
||||
go.elara.ws/logger v0.0.0-20230421022458-e80700db2090/go.mod h1:qng49owViqsW5Aey93lwBXONw20oGbJIoLVscB16mPM=
|
||||
go.elara.ws/translate v0.0.0-20230421025926-32ccfcd110e6 h1:4xCBxLPBn3Y2DuIcj8zQ1tQOFLrpu6tEIGUWn/Q6zPM=
|
||||
go.elara.ws/translate v0.0.0-20230421025926-32ccfcd110e6/go.mod h1:NmfCFqwq7X/aqa/ZVkIysj17JyMEY4Bb5E921kMswNo=
|
||||
go.elara.ws/vercmp v0.0.0-20230622214216-0b2b067575c4 h1:Ep54XceQlKhcCHl9awG+wWP4kz4kIP3c3Lzw/Gc/zwY=
|
||||
go.elara.ws/vercmp v0.0.0-20230622214216-0b2b067575c4/go.mod h1:/7PNW7nFnDR5W7UXZVc04gdVLR/wBNgkm33KgIz0OBk=
|
||||
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
|
||||
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
|
||||
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||
@ -584,8 +582,8 @@ golang.org/x/crypto v0.0.0-20220516162934-403b01795ae8/go.mod h1:IxCIyHEi3zRg3s0
|
||||
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
|
||||
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20220916125017-b168a2c6b86b h1:SCE/18RnFsLrjydh/R/s5EVvHoZprqEQUuoxK8q2Pc4=
|
||||
golang.org/x/exp v0.0.0-20220916125017-b168a2c6b86b/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
|
@ -42,7 +42,7 @@ import (
|
||||
"go.elara.ws/lure/internal/shutils"
|
||||
"go.elara.ws/lure/internal/shutils/decoder"
|
||||
"go.elara.ws/lure/internal/types"
|
||||
"go.elara.ws/lure/vercmp"
|
||||
"go.elara.ws/vercmp"
|
||||
"mvdan.cc/sh/v3/expand"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
|
@ -28,7 +28,7 @@ import (
|
||||
"go.elara.ws/lure/internal/db"
|
||||
"go.elara.ws/lure/internal/repos"
|
||||
"go.elara.ws/lure/manager"
|
||||
"go.elara.ws/lure/vercmp"
|
||||
"go.elara.ws/vercmp"
|
||||
"golang.org/x/exp/maps"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
165
vercmp/vercmp.go
165
vercmp/vercmp.go
@ -1,165 +0,0 @@
|
||||
/*
|
||||
* LURE - Linux User REpository
|
||||
* Copyright (C) 2023 Arsen Musayelyan
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package vercmp
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
// Compare compares two version strings.
|
||||
// It returns 1 if v1 is greater,
|
||||
// 0 if the versions are equal,
|
||||
// and -1 if v2 is greater
|
||||
func Compare(v1, v2 string) int {
|
||||
if v1 == v2 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return sepVerCmp(sepLabel(v1), sepLabel(v2))
|
||||
}
|
||||
|
||||
func sepVerCmp(e1, e2 []string) int {
|
||||
if slices.Equal(e1, e2) {
|
||||
return 0
|
||||
}
|
||||
|
||||
// proc stores the amount of elements processed
|
||||
proc := 0
|
||||
|
||||
for i := 0; i < len(e1); i++ {
|
||||
proc++
|
||||
|
||||
if i >= len(e2) {
|
||||
return 1
|
||||
}
|
||||
|
||||
elem1 := e1[i]
|
||||
elem2 := e2[i]
|
||||
|
||||
if elem1 == elem2 {
|
||||
continue
|
||||
}
|
||||
|
||||
if isNumElem(elem1) && isNumElem(elem2) {
|
||||
elem1v, err := strconv.ParseInt(elem1, 10, 64)
|
||||
if err != nil {
|
||||
// error should never happen due to isNumElem()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
elem2v, err := strconv.ParseInt(elem2, 10, 64)
|
||||
if err != nil {
|
||||
// error should never happen due to isNumElem()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if elem1v > elem2v {
|
||||
return 1
|
||||
} else if elem1v < elem2v {
|
||||
return -1
|
||||
}
|
||||
} else if isNumElem(elem1) && isAlphaElem(elem2) {
|
||||
return 1
|
||||
} else if isAlphaElem(elem1) && isNumElem(elem2) {
|
||||
return -1
|
||||
} else if isAlphaElem(elem1) && isAlphaElem(elem2) {
|
||||
if elem1 > elem2 {
|
||||
return 1
|
||||
} else if elem1 < elem2 {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if proc < len(e2) {
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func sepLabel(label string) []string {
|
||||
const (
|
||||
other = iota
|
||||
alpha
|
||||
num
|
||||
)
|
||||
|
||||
var (
|
||||
curType uint8
|
||||
out []string
|
||||
sb strings.Builder
|
||||
)
|
||||
|
||||
for _, char := range label {
|
||||
if isNum(char) {
|
||||
if curType != num && curType != other {
|
||||
out = append(out, sb.String())
|
||||
sb.Reset()
|
||||
}
|
||||
|
||||
sb.WriteRune(char)
|
||||
curType = num
|
||||
} else if isAlpha(char) {
|
||||
if curType != alpha && curType != other {
|
||||
out = append(out, sb.String())
|
||||
sb.Reset()
|
||||
}
|
||||
|
||||
sb.WriteRune(char)
|
||||
curType = alpha
|
||||
} else {
|
||||
if curType != other {
|
||||
out = append(out, sb.String())
|
||||
sb.Reset()
|
||||
}
|
||||
curType = other
|
||||
}
|
||||
}
|
||||
|
||||
if sb.Len() != 0 {
|
||||
out = append(out, sb.String())
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func isNumElem(s string) bool {
|
||||
// Check only the first rune as all elements
|
||||
// should consist of the same type of rune
|
||||
return isNum([]rune(s[:1])[0])
|
||||
}
|
||||
|
||||
func isNum(r rune) bool {
|
||||
return r >= '0' && r <= '9'
|
||||
}
|
||||
|
||||
func isAlphaElem(s string) bool {
|
||||
// Check only the first rune as all elements
|
||||
// should consist of the same type of rune
|
||||
return isAlpha([]rune(s[:1])[0])
|
||||
}
|
||||
|
||||
func isAlpha(r rune) bool {
|
||||
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* LURE - Linux User REpository
|
||||
* Copyright (C) 2023 Arsen Musayelyan
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package vercmp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func TestSepLabel(t *testing.T) {
|
||||
type item struct {
|
||||
label string
|
||||
expected []string
|
||||
}
|
||||
|
||||
table := []item{
|
||||
{"2.0.1", []string{"2", "0", "1"}},
|
||||
{"v0.0.1", []string{"v", "0", "0", "1"}},
|
||||
{"2xFg33.+f.5", []string{"2", "xFg", "33", "f", "5"}},
|
||||
}
|
||||
|
||||
for _, it := range table {
|
||||
t.Run(it.label, func(t *testing.T) {
|
||||
s := sepLabel(it.label)
|
||||
if !slices.Equal(s, it.expected) {
|
||||
t.Errorf("Expected %v, got %v", it.expected, s)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerCmp(t *testing.T) {
|
||||
type item struct {
|
||||
v1, v2 string
|
||||
expected int
|
||||
}
|
||||
|
||||
table := []item{
|
||||
{"1.0010", "1.9", 1},
|
||||
{"1.05", "1.5", 0},
|
||||
{"1.0", "1", 1},
|
||||
{"1", "1.0", -1},
|
||||
{"2.50", "2.5", 1},
|
||||
{"FC5", "fc4", -1},
|
||||
{"2a", "2.0", -1},
|
||||
{"1.0", "1.fc4", 1},
|
||||
{"3.0.0_fc", "3.0.0.fc", 0},
|
||||
{"4.1__", "4.1+", 0},
|
||||
}
|
||||
|
||||
for _, it := range table {
|
||||
t.Run(it.v1+"/"+it.v2, func(t *testing.T) {
|
||||
c := Compare(it.v1, it.v2)
|
||||
if c != it.expected {
|
||||
t.Errorf("Expected %d, got %d", it.expected, c)
|
||||
}
|
||||
|
||||
// Ensure opposite comparison gives opposite value
|
||||
c = -Compare(it.v2, it.v1)
|
||||
if c != it.expected {
|
||||
t.Errorf("Expected %d, got %d (opposite)", it.expected, c)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user