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:
morganamilo 2019-10-16 22:02:50 +01:00
parent 543d3afaa7
commit 0856edcf04
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
6 changed files with 34 additions and 70 deletions

3
dep.go
View file

@ -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) {

View file

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

View file

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

View file

@ -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":

View file

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

View file

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