mirror of
https://github.com/Jguer/yay
synced 2024-10-02 22:33:47 +00:00
Move LessRunes into main
This comparitor function could hardly be considored a type. It's also very small and probably not too useful overall so keep it in utils.
This commit is contained in:
parent
543d3afaa7
commit
0856edcf04
3
dep.go
3
dep.go
|
@ -5,7 +5,6 @@ import (
|
|||
"strings"
|
||||
|
||||
alpm "github.com/Jguer/go-alpm"
|
||||
"github.com/Jguer/yay/v9/pkg/types"
|
||||
rpc "github.com/mikkeloscar/aur"
|
||||
)
|
||||
|
||||
|
@ -34,7 +33,7 @@ func (q providers) Less(i, j int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
return types.LessRunes([]rune(q.Pkgs[i].Name), []rune(q.Pkgs[j].Name))
|
||||
return LessRunes([]rune(q.Pkgs[i].Name), []rune(q.Pkgs[j].Name))
|
||||
}
|
||||
|
||||
func (q providers) Swap(i, j int) {
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
package types
|
||||
|
||||
import "unicode"
|
||||
|
||||
// LessRunes compares two rune values, and returns true if the first argument is lexicographicaly smaller.
|
||||
func LessRunes(iRunes, jRunes []rune) bool {
|
||||
max := len(iRunes)
|
||||
if max > len(jRunes) {
|
||||
max = len(jRunes)
|
||||
}
|
||||
|
||||
for idx := 0; idx < max; idx++ {
|
||||
ir := iRunes[idx]
|
||||
jr := jRunes[idx]
|
||||
|
||||
lir := unicode.ToLower(ir)
|
||||
ljr := unicode.ToLower(jr)
|
||||
|
||||
if lir != ljr {
|
||||
return lir < ljr
|
||||
}
|
||||
|
||||
// the lowercase runes are the same, so compare the original
|
||||
if ir != jr {
|
||||
return ir < jr
|
||||
}
|
||||
}
|
||||
|
||||
return len(iRunes) < len(jRunes)
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package types
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLessRunes(t *testing.T) {
|
||||
t.Parallel()
|
||||
type args struct {
|
||||
iRunes []rune
|
||||
jRunes []rune
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{name: "nilslices", args: args{iRunes: nil, jRunes: nil}, want: false},
|
||||
{name: "emptyslices", args: args{iRunes: []rune{}, jRunes: []rune{}}, want: false},
|
||||
{name: "simpleslice a,b", args: args{iRunes: []rune{'a'}, jRunes: []rune{'b'}}, want: true},
|
||||
{name: "simpleslice b,a", args: args{iRunes: []rune{'b'}, jRunes: []rune{'a'}}, want: false},
|
||||
{name: "equalslice", args: args{iRunes: []rune{'a', 'a', 'a'}, jRunes: []rune{'a', 'a', 'a'}}, want: false},
|
||||
{name: "uppercase", args: args{iRunes: []rune{'a'}, jRunes: []rune{'A'}}, want: false},
|
||||
{name: "longerFirstArg", args: args{iRunes: []rune{'a', 'b'}, jRunes: []rune{'a'}}, want: false},
|
||||
{name: "longerSecondArg", args: args{iRunes: []rune{'a'}, jRunes: []rune{'a', 'b'}}, want: true},
|
||||
{name: "utf8 less", args: args{iRunes: []rune{'世', '2', '0'}, jRunes: []rune{'世', '界', '3'}}, want: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := LessRunes(tt.args.iRunes, tt.args.jRunes); got != tt.want {
|
||||
t.Errorf("LessRunes() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
4
query.go
4
query.go
|
@ -38,9 +38,9 @@ func (q aurQuery) Less(i, j int) bool {
|
|||
case "popularity":
|
||||
result = q[i].Popularity > q[j].Popularity
|
||||
case "name":
|
||||
result = types.LessRunes([]rune(q[i].Name), []rune(q[j].Name))
|
||||
result = LessRunes([]rune(q[i].Name), []rune(q[j].Name))
|
||||
case "base":
|
||||
result = types.LessRunes([]rune(q[i].PackageBase), []rune(q[j].PackageBase))
|
||||
result = LessRunes([]rune(q[i].PackageBase), []rune(q[j].PackageBase))
|
||||
case "submitted":
|
||||
result = q[i].FirstSubmitted < q[j].FirstSubmitted
|
||||
case "modified":
|
||||
|
|
|
@ -29,14 +29,14 @@ func (u upSlice) Less(i, j int) bool {
|
|||
if u[i].Repository == u[j].Repository {
|
||||
iRunes := []rune(u[i].Name)
|
||||
jRunes := []rune(u[j].Name)
|
||||
return types.LessRunes(iRunes, jRunes)
|
||||
return LessRunes(iRunes, jRunes)
|
||||
}
|
||||
|
||||
syncDB, err := alpmHandle.SyncDBs()
|
||||
if err != nil {
|
||||
iRunes := []rune(u[i].Repository)
|
||||
jRunes := []rune(u[j].Repository)
|
||||
return types.LessRunes(iRunes, jRunes)
|
||||
return LessRunes(iRunes, jRunes)
|
||||
}
|
||||
|
||||
less := false
|
||||
|
@ -59,7 +59,7 @@ func (u upSlice) Less(i, j int) bool {
|
|||
|
||||
iRunes := []rune(u[i].Repository)
|
||||
jRunes := []rune(u[j].Repository)
|
||||
return types.LessRunes(iRunes, jRunes)
|
||||
return LessRunes(iRunes, jRunes)
|
||||
|
||||
}
|
||||
|
||||
|
|
28
utils.go
28
utils.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const gitEmptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
||||
|
@ -49,3 +50,30 @@ func removeInvalidTargets(targets []string) []string {
|
|||
|
||||
return filteredTargets
|
||||
}
|
||||
|
||||
// LessRunes compares two rune values, and returns true if the first argument is lexicographicaly smaller.
|
||||
func LessRunes(iRunes, jRunes []rune) bool {
|
||||
max := len(iRunes)
|
||||
if max > len(jRunes) {
|
||||
max = len(jRunes)
|
||||
}
|
||||
|
||||
for idx := 0; idx < max; idx++ {
|
||||
ir := iRunes[idx]
|
||||
jr := jRunes[idx]
|
||||
|
||||
lir := unicode.ToLower(ir)
|
||||
ljr := unicode.ToLower(jr)
|
||||
|
||||
if lir != ljr {
|
||||
return lir < ljr
|
||||
}
|
||||
|
||||
// the lowercase runes are the same, so compare the original
|
||||
if ir != jr {
|
||||
return ir < jr
|
||||
}
|
||||
}
|
||||
|
||||
return len(iRunes) < len(jRunes)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue