fix(package): [stream] avoid overflowing range arithmetic (#393)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-22 10:40:12 +08:00
1 parent 6ff2966d50
commit 8225b6d7d9
2 files changed
+21 -5

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
package stream
import "testing"
func TestFromRangeSmallIntegerSpan(t *testing.T) {
got := FromRange[int8](-120, 120, 10).ToSlice()
if len(got) != 25 || got[0] != -120 || got[len(got)-1] != 120 {
t.Errorf("unexpected range: %v", got)
}
if got := FromRange[uint8](250, 255, 5).ToSlice(); len(got) != 2 || got[1] != 255 {
t.Errorf("unexpected unsigned range: %v", got)
}
}
+8 -5
View File
@@ -101,11 +101,14 @@ func FromRange[T constraints.Integer | constraints.Float](start, end, step T) St
panic("stream.FromRange: param step should be positive") panic("stream.FromRange: param step should be positive")
} }
l := int((end-start)/step) + 1 source := make([]T, 0)
source := make([]T, l) for value := start; value <= end; {
source = append(source, value)
for i := 0; i < l; i++ { next := value + step
source[i] = start + (T(i) * step) if next <= value {
break
}
value = next
} }
return FromSlice(source) return FromSlice(source)