[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

@@ -62,6 +62,9 @@ function Monster_mt:Inventory(j)
end
return 0
end
function Monster_mt:InventoryAsString(start, stop)
return self.view:VectorAsString(14, start, stop)
end
function Monster_mt:InventoryLength()
local o = self.view:Offset(14)
if o ~= 0 then
@@ -160,6 +163,9 @@ function Monster_mt:Testnestedflatbuffer(j)
end
return 0
end
function Monster_mt:TestnestedflatbufferAsString(start, stop)
return self.view:VectorAsString(30, start, stop)
end
function Monster_mt:TestnestedflatbufferLength()
local o = self.view:Offset(30)
if o ~= 0 then
@@ -315,6 +321,9 @@ function Monster_mt:Flex(j)
end
return 0
end
function Monster_mt:FlexAsString(start, stop)
return self.view:VectorAsString(64, start, stop)
end
function Monster_mt:FlexLength()
local o = self.view:Offset(64)
if o ~= 0 then
@@ -518,6 +527,9 @@ function Monster_mt:VectorOfEnums(j)
end
return 0
end
function Monster_mt:VectorOfEnumsAsString(start, stop)
return self.view:VectorAsString(98, start, stop)
end
function Monster_mt:VectorOfEnumsLength()
local o = self.view:Offset(98)
if o ~= 0 then
@@ -540,6 +552,9 @@ function Monster_mt:Testrequirednestedflatbuffer(j)
end
return 0
end
function Monster_mt:TestrequirednestedflatbufferAsString(start, stop)
return self.view:VectorAsString(102, start, stop)
end
function Monster_mt:TestrequirednestedflatbufferLength()
local o = self.view:Offset(102)
if o ~= 0 then

View File

@@ -102,6 +102,9 @@ function TypeAliases_mt:V8(j)
end
return 0
end
function TypeAliases_mt:V8AsString(start, stop)
return self.view:VectorAsString(24, start, stop)
end
function TypeAliases_mt:V8Length()
local o = self.view:Offset(24)
if o ~= 0 then

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 =