Skip to content

Commit

Permalink
sort: fix crash when sorting string list fields
Browse files Browse the repository at this point in the history
  • Loading branch information
flwyd committed Aug 19, 2024
1 parent 53fcbb4 commit 5d907f5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
34 changes: 19 additions & 15 deletions adif/spec/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func ComparatorForField(f Field, locale language.Tag) FieldComparator {
}
case IOTARefNoDataType.Name, POTARefDataType.Name, SOTARefDataType.Name, WWFFRefDataType.Name:
c = compareStringsBasic
case AwardListDataType.Name, CreditListDataType.Name, GridSquareListDataType.Name, POTARefListDataType.Name, SecondarySubdivisionListDataType.Name, SponsoredAwardListDataType.Name:
c = compareStringLists
case AwardListDataType.Name, CreditListDataType.Name, GridSquareListDataType.Name, POTARefListDataType.Name, SponsoredAwardListDataType.Name:
c = compareStringLists(",")
case SecondarySubdivisionListDataType.Name:
c = compareStringLists(":")
default:
c = compareStringsBasic
}
Expand Down Expand Up @@ -294,19 +296,21 @@ func compareBands(a, b string) (int, error) {
return 1, nil
}

func compareStringLists(a, b string) (int, error) {
alist := strings.Split(strings.ToUpper(a), ",")
blist := strings.Split(strings.ToUpper(b), ",")
sort.Strings(alist) // treat foo,bar as equal to bar,foo
sort.Strings(blist)
min := len(alist)
if len(blist) > len(alist) {
min = len(b)
}
for i := 0; i < min; i++ {
if c, err := compareStringsBasic(alist[i], blist[i]); c != 0 || err != nil {
return c, err
func compareStringLists(sep string) FieldComparator {
return func(a, b string) (int, error) {
alist := strings.Split(strings.ToUpper(a), sep)
blist := strings.Split(strings.ToUpper(b), sep)
sort.Strings(alist) // treat foo,bar as equal to bar,foo
sort.Strings(blist)
min := len(alist)
if len(blist) < len(alist) {
min = len(blist)
}
for i := 0; i < min; i++ {
if c, err := compareStringsBasic(alist[i], blist[i]); c != 0 || err != nil {
return c, err
}
}
return len(alist) - len(blist), nil
}
return len(alist) - len(blist), nil
}
53 changes: 53 additions & 0 deletions adif/spec/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,59 @@ func TestCompareStringsBasic(t *testing.T) {
}
}

func TestCompareStringLists(t *testing.T) {
tests := []struct {
name string
field Field
want comparisonCheck
vals []string
}{
{
name: "empty string equal", field: AwardGrantedField, want: shouldCompareEqual,
vals: []string{"", ""},
},
{
name: "empty string less", field: CreditGrantedField, want: shouldCompareLess,
vals: []string{"", "CQDX", "DXCC,IOTA"},
},
{
name: "sublist less different order", field: CreditGrantedField, want: shouldCompareLess,
vals: []string{"", "CQDX", "DXCC,CQDX", "CQDX,IOTA,DXCC"},
},
{
name: "identical lists", field: VuccGridsField, want: shouldCompareEqual,
vals: []string{"AB34,AB44", "AB34,AB44"},
},
{
name: "same list different order", field: VuccGridsField, want: shouldCompareEqual,
vals: []string{"AB34,AB44", "AB44,AB34"},
},
{
name: "sublist less", field: CreditGrantedField, want: shouldCompareLess,
vals: []string{"", "CQDX", "CQDX,DXCC", "CQDX,DXCC,IOTA"},
},
{
name: "different case", field: PotaRefField, want: shouldCompareEqual,
vals: []string{"US-4567,US-0791", "us-4567,us-0791", "Us-4567,uS-0791"},
},
{
name: "single secondary subdivision syntax equal", field: UsacaCountiesField, want: shouldCompareEqual,
vals: []string{"CA,Los Angeles", "CA,LOS ANGELES"},
},
{
name: "multiple secondary subdivision syntax equal", field: MyUsacaCountiesField, want: shouldCompareEqual,
vals: []string{"KS,Wyandotte:MO,Jackson", "MO,Jackson:KS,Wyandotte"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := ComparatorForField(tc.field, language.Greek)
tc.want(t, c, tc.vals...)
})
}
}

func TestCompareStringsLocale(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 5d907f5

Please sign in to comment.