3 Commits

Author SHA1 Message Date
e553da2c61 fixed panic when writing to zero-sized buffer 2022-05-04 09:49:22 +02:00
3e2be025b3 added support for zero-sized buffers 2022-04-02 17:33:52 +02:00
157e4f302d ForEachLine added 2022-04-02 11:33:59 +02:00
3 changed files with 17 additions and 3 deletions

View File

@ -42,7 +42,9 @@ func (b *Buffer[T]) y(y int) int {
// Set sets the value at position (x,y) to c
func (b *Buffer[T]) Set(x, y int, v T) {
b.data[b.y(y)][b.x(x)] = v
if b.width >= 0 && b.height >= 0 {
b.data[b.y(y)][b.x(x)] = v
}
}
// Get returns the value at position (x,y)
@ -65,6 +67,13 @@ func (b *Buffer[T]) Height() int {
return b.height
}
// ForEach calls f for every value in this buffer
func (b *Buffer[T]) ForEachLine(f func(line int, content []T)) {
for line, content := range b.data {
f(line, content)
}
}
// ForEach calls f for every value in this buffer
func (b *Buffer[T]) ForEach(f func(x, y int, v T)) {
for y, col := range b.data {
@ -95,8 +104,8 @@ func (b *Buffer[T]) Sub(x, y, w, h int) *Buffer[T] {
// sanitize inputs
x = limit(x, 0, b.width-1)
y = limit(y, 0, b.height-1)
w = limit(w, 1, b.width-x)
h = limit(h, 1, b.height-y)
w = limit(w, 0, b.width-x)
h = limit(h, 0, b.height-y)
// make slice references
data := make([][]T, h)

View File

@ -17,3 +17,8 @@ func TestSub(t *testing.T) {
fmt.Println(strings.Repeat("-", 10))
fmt.Println(s)
}
func TestSet(t *testing.T) {
b := NewBuffer(10, 10, ' ')
b.Set(11, 0, 'a')
}

0
go.sum Normal file
View File