[Lua] manipulate byte array as string (#6624)

* [Lua] manipulate byte array as string

Sometimes it would be more effective than reading byte by byte

* change according to the review

* update
This commit is contained in:
罗泽轩
2021-05-08 13:57:13 +08:00
committed by GitHub
parent 47361baf61
commit 4525cd9c56
5 changed files with 84 additions and 0 deletions

View File

@@ -282,6 +282,41 @@ local function getRootAs_canAcceptString()
assert(mon:Hp() == 80, "Monster Hp is not 80")
end
local function testAccessByteVectorAsString()
local f = assert(io.open('monsterdata_test.mon', 'rb'))
local wireData = f:read("*a")
f:close()
local mon = monster.GetRootAsMonster(wireData, 0)
-- the data of byte array Inventory is [0, 1, 2, 3, 4]
local s = mon:InventoryAsString(1, 3)
assert(#s == 3)
for i = 1, #s do
assert(string.byte(s, i) == i - 1)
end
local s = mon:InventoryAsString(2, 5)
assert(#s == 4)
for i = 1, #s do
assert(string.byte(s, i) == i)
end
local s = mon:InventoryAsString(5, 5)
assert(#s == 1)
assert(string.byte(s, 1) == 4)
local s = mon:InventoryAsString(2)
assert(#s == 4)
for i = 1, #s do
assert(string.byte(s, i) == i)
end
local s = mon:InventoryAsString()
assert(#s == 5)
for i = 1, #s do
assert(string.byte(s, i) == i - 1)
end
end
local tests =
{
{
@@ -305,6 +340,10 @@ local tests =
f = getRootAs_canAcceptString,
d = "Tests that GetRootAs<type>() generated methods accept strings"
},
{
f = testAccessByteVectorAsString,
d = "Access byte vector as string"
},
}
local benchmarks =