From 0f35fe48fd351fc89f0632cb12953a55acee3ab1 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Sun, 25 Sep 2022 15:58:01 +0200 Subject: [PATCH] ranges,decode: Correctly skip empty ranges when adding unknown fields Before an empty range could cause gaps to be devided instead of continous. --- pkg/ranges/ranges.go | 13 +++++++++++++ pkg/ranges/ranges_test.go | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/ranges/ranges.go b/pkg/ranges/ranges.go index 5601f2c7..a26513e7 100644 --- a/pkg/ranges/ranges.go +++ b/pkg/ranges/ranges.go @@ -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}) diff --git a/pkg/ranges/ranges_test.go b/pkg/ranges/ranges_test.go index d8d596e6..eb653a8e 100644 --- a/pkg/ranges/ranges_test.go +++ b/pkg/ranges/ranges_test.go @@ -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) {