1
0
mirror of https://github.com/pbnjay/grate.git synced 2024-12-13 13:58:27 +02:00

Fix index out of bounds panic

Sheet.Resize did not resize all old rows.
This commit is contained in:
Tamás Gulácsi 2021-02-25 08:31:53 +01:00 committed by Jeremy Jay
parent ebe430dc09
commit a459175619

View File

@ -21,6 +21,17 @@ type Sheet struct {
// Resize the sheet for the number of rows and cols given.
// Newly added cells default to blank.
func (s *Sheet) Resize(rows, cols int) {
for i := range s.Rows {
if i > rows {
break
}
n := cols - len(s.Rows[i])
if n <= 0 {
continue
}
s.Rows[i] = append(s.Rows[i], make([]Cell, n)...)
}
if rows <= 0 {
rows = 1
}
@ -34,11 +45,6 @@ func (s *Sheet) Resize(rows, cols int) {
for rows >= len(s.Rows) {
s.Rows = append(s.Rows, make([]Cell, cols))
}
for i := 0; i < s.NumRows && len(s.Rows[i]) < cols; i++ {
r2 := make([]Cell, cols-len(s.Rows[i]))
s.Rows[i] = append(s.Rows[i], r2...)
}
}
// Put the value at the cell location given.