ruby: better message on out of bounds array access

develop
jj 2012-07-06 13:32:39 +02:00
parent bef5079d43
commit d645d6b046
1 changed files with 19 additions and 7 deletions

@ -380,12 +380,18 @@ module DFHack
def [](i) def [](i)
i = _indexenum.int(i) if _indexenum i = _indexenum.int(i) if _indexenum
i += @_length if i < 0 i += @_length if i < 0
_tgat(i)._get if t = _tgat(i)
t._get
end
end end
def []=(i, v) def []=(i, v)
i = _indexenum.int(i) if _indexenum i = _indexenum.int(i) if _indexenum
i += @_length if i < 0 i += @_length if i < 0
_tgat(i)._set(v) if t = _tgat(i)
t._set(v)
else
raise 'index out of bounds'
end
end end
include Enumerable include Enumerable
@ -444,7 +450,7 @@ module DFHack
if idx >= length if idx >= length
insert_at(idx, 0) insert_at(idx, 0)
elsif idx < 0 elsif idx < 0
raise 'invalid idx' raise 'index out of bounds'
end end
@_tg._at(valueptr_at(idx))._set(v) @_tg._at(valueptr_at(idx))._set(v)
end end
@ -530,7 +536,7 @@ module DFHack
if idx >= length if idx >= length
insert_at(idx, v) insert_at(idx, v)
elsif idx < 0 elsif idx < 0
raise 'invalid idx' raise 'index out of bounds'
else else
DFHack.memory_vectorbool_setat(@_memaddr, idx, v) DFHack.memory_vectorbool_setat(@_memaddr, idx, v)
end end
@ -582,7 +588,7 @@ module DFHack
idx = _indexenum.int(idx) if _indexenum idx = _indexenum.int(idx) if _indexenum
idx += length if idx < 0 idx += length if idx < 0
if idx >= length or idx < 0 if idx >= length or idx < 0
raise 'invalid idx' raise 'index out of bounds'
else else
DFHack.memory_bitarray_set(@_memaddr, idx, v) DFHack.memory_bitarray_set(@_memaddr, idx, v)
end end
@ -608,11 +614,17 @@ module DFHack
end end
def [](i) def [](i)
i += _length if i < 0 i += _length if i < 0
_tgat(i)._get if t = _tgat(i)
t._get
end
end end
def []=(i, v) def []=(i, v)
i += _length if i < 0 i += _length if i < 0
_tgat(i)._set(v) if t = _tgat(i)
t._set(v)
else
raise 'index out of bounds'
end
end end
def _set(a) def _set(a)
a.each_with_index { |v, i| self[i] = v } a.each_with_index { |v, i| self[i] = v }