1
1
mirror of https://github.com/wader/fq.git synced 2024-12-22 21:01:37 +03:00

ranges,decode: Correctly skip empty ranges when adding unknown fields

Before an empty range could cause gaps to be devided instead of continous.
This commit is contained in:
Mattias Wadman 2022-09-25 15:58:01 +02:00
parent 783b20ece5
commit 0f35fe48fd
2 changed files with 20 additions and 0 deletions

View File

@ -73,6 +73,14 @@ func Gaps(total Range, ranges []Range) []Range {
for i := 0; i < len(ranges); {
madded = false
m = ranges[i]
// skip empty ranges
if m.Len == 0 {
i++
madded = true
continue
}
j := i + 1
for ; j < len(ranges); j++ {
if m.Start <= ranges[j].Start && m.Stop()+1 >= ranges[j].Start {
@ -91,10 +99,15 @@ func Gaps(total Range, ranges []Range) []Range {
break
}
}
if !madded {
merged = append(merged, m)
}
if len(merged) == 0 {
return []Range{total}
}
gaps := make([]Range, 0, len(merged))
if merged[0].Start != total.Start {
gaps = append(gaps, Range{Start: 0, Len: merged[0].Start})

View File

@ -27,6 +27,13 @@ func TestRangeGaps(t *testing.T) {
{"0:10", "1:1 2:5 8:1", "0:1 9:1"},
{"0:10", "1:1 2:8 8:2", "0:1"},
{"0:10", "0:4 2:8 8:2", ""},
// handle empty ranges
{"0:12", "4:4 8:0", "0:4 8:4"},
{"0:12", "0:0 4:4", "0:4 8:4"},
{"0:12", "0:0 4:4 8:0", "0:4 8:4"},
{"0:12", "0:0 0:0 4:4 8:0 8:0", "0:4 8:4"},
{"0:12", "8:0", "0:12"},
}
for _, tC := range testCases {
t.Run(fmt.Sprintf("%v_%v_%v", tC.total, tC.ranges, tC.expected), func(t *testing.T) {