summaryrefslogtreecommitdiff
path: root/listbox.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-03-09 10:51:01 -0400
committerPietro Gagliardi <[email protected]>2014-03-09 10:51:01 -0400
commitf4506277b94bc2bff4790b6e7cee543b91c44fce (patch)
treec470abac6e0b399ff01393f7033d997ff9b5ae7d /listbox.go
parent3e47b00eda59d3365bc189e5dc23fd0644e2d84a (diff)
Added bounds checks for Listbox.InsertBefore(). This stupid 32-bit Mac bug keeps Listbox.Delete() unchecked for now...
Diffstat (limited to 'listbox.go')
-rw-r--r--listbox.go15
1 files changed, 12 insertions, 3 deletions
diff --git a/listbox.go b/listbox.go
index 66ff398..54c698b 100644
--- a/listbox.go
+++ b/listbox.go
@@ -45,20 +45,29 @@ func (l *Listbox) Append(what ...string) (err error) {
return nil
}
-// InsertBefore inserts a new item in the Listbox before the item at the given position.
-// (TODO action on invalid index)
+// InsertBefore inserts a new item in the Listbox before the item at the given position. It panics if the given index is out of bounds.
func (l *Listbox) InsertBefore(what string, before int) (err error) {
l.lock.Lock()
defer l.lock.Unlock()
+ var m []string
+
if l.created {
+ if before < 0 || before >= l.sysData.len() {
+ goto badrange
+ }
return l.sysData.insertBefore(what, before)
}
- m := make([]string, 0, len(l.initItems) + 1)
+ if before < 0 || before >= len(l.initItems) {
+ goto badrange
+ }
+ m = make([]string, 0, len(l.initItems) + 1)
m = append(m, l.initItems[:before]...)
m = append(m, what)
l.initItems = append(m, l.initItems[before:]...)
return nil
+badrange:
+ panic(fmt.Errorf("index %d out of range in Listbox.InsertBefore()", before))
}
// Delete removes the given item from the Listbox.