Added bounds checking to buffer

This commit is contained in:
Sasha Koshka 2022-11-06 15:25:55 -05:00
parent ff7240d553
commit f98b551ba2

View File

@ -49,6 +49,15 @@ type Buffer struct {
}
}
func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) {
outOfBounds =
x < 0 ||
y < 0 ||
x >= buffer.width ||
y >= buffer.height
return
}
func (buffer *Buffer) Size () (width, height int) {
width = buffer.width
height = buffer.height
@ -62,19 +71,23 @@ func (buffer *Buffer) SetSize (width, height int) {
}
func (buffer *Buffer) Cell (x, y int) (cell Cell) {
if buffer.isOutOfBounds(x, y) { return }
cell = buffer.content[x + y * buffer.width]
return
}
func (buffer *Buffer) SetColor (x, y int, color Color) {
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].color = color
}
func (buffer *Buffer) SetStyle (x, y int, style Style) {
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].style = style
}
func (buffer *Buffer) SetRune (x, y int, content rune) {
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].content = content
}
@ -110,18 +123,24 @@ func (buffer *DamageBuffer) SetSize (width, height int) {
}
func (buffer *DamageBuffer) SetColor (x, y int, color Color) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].color == color
buffer.Buffer.SetColor(x, y, color)
buffer.clean[x + y * buffer.width] = false
}
func (buffer *DamageBuffer) SetStyle (x, y int, style Style) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].style == style
buffer.Buffer.SetStyle(x, y, style)
buffer.clean[x + y * buffer.width] = false
}
func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].content == content
buffer.Buffer.SetRune(x, y, content)
buffer.clean[x + y * buffer.width] = false
}
func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
@ -141,10 +160,12 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
}
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
if buffer.isOutOfBounds(x, y) { return }
clean = buffer.clean[x + y * buffer.width]
return
}
func (buffer *DamageBuffer) MarkClean (x, y int) {
if buffer.isOutOfBounds(x, y) { return }
buffer.clean[x + y * buffer.width] = true
}