fix Bytearray.insert, Bytearray.remove
This commit is contained in:
parent
fb07c86ea6
commit
1ade910fa1
@ -13,3 +13,12 @@ end
|
|||||||
|
|
||||||
Bytearray.remove(arr, 2)
|
Bytearray.remove(arr, 2)
|
||||||
assert(#arr == 9)
|
assert(#arr == 9)
|
||||||
|
|
||||||
|
Bytearray.insert(arr, {5, 3, 6})
|
||||||
|
|
||||||
|
assert(#arr == 12)
|
||||||
|
Bytearray.insert(arr, 2, 8)
|
||||||
|
assert(#arr == 13)
|
||||||
|
for i=1,10 do
|
||||||
|
assert(arr[i] == 10 - i)
|
||||||
|
end
|
||||||
|
|||||||
@ -48,7 +48,8 @@ local function append(self, b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function insert(self, index, b)
|
local function insert(self, index, b)
|
||||||
if index == nil then
|
if b == nil then
|
||||||
|
b = index
|
||||||
index = self.size + 1
|
index = self.size + 1
|
||||||
end
|
end
|
||||||
if index <= 0 or index > self.size + 1 then
|
if index <= 0 or index > self.size + 1 then
|
||||||
@ -81,7 +82,7 @@ local function remove(self, index, elems)
|
|||||||
if index + elems > self.size then
|
if index + elems > self.size then
|
||||||
elems = self.size - index + 1
|
elems = self.size - index + 1
|
||||||
end
|
end
|
||||||
for i=index, self.size - elems do
|
for i=index - 1, self.size - elems - 1 do
|
||||||
self.bytes[i] = self.bytes[i + elems]
|
self.bytes[i] = self.bytes[i + elems]
|
||||||
end
|
end
|
||||||
self.size = self.size - elems
|
self.size = self.size - elems
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user