mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 13:22:40 +00:00
[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:
@@ -76,6 +76,17 @@ function mt:Vector(off)
|
|||||||
return off + self:Get(N.UOffsetT, off) + 4
|
return off + self:Get(N.UOffsetT, off) + 4
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mt:VectorAsString(off, start, stop)
|
||||||
|
local o = self:Offset(off)
|
||||||
|
if o ~= 0 then
|
||||||
|
start = start or 1
|
||||||
|
stop = stop or self:VectorLen(o)
|
||||||
|
local a = self:Vector(o) + start - 1
|
||||||
|
return self.bytes:Slice(a, a + stop - start + 1)
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
function mt:Union(t2, off)
|
function mt:Union(t2, off)
|
||||||
assert(getmetatable(t2) == mt_name)
|
assert(getmetatable(t2) == mt_name)
|
||||||
enforceOffset(off)
|
enforceOffset(off)
|
||||||
|
|||||||
@@ -339,6 +339,18 @@ class LuaGenerator : public BaseGenerator {
|
|||||||
code += EndFunc;
|
code += EndFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Access a byte/ubyte vector as a string
|
||||||
|
void AccessByteVectorAsString(const StructDef &struct_def,
|
||||||
|
const FieldDef &field, std::string *code_ptr) {
|
||||||
|
std::string &code = *code_ptr;
|
||||||
|
GenReceiver(struct_def, code_ptr);
|
||||||
|
code += MakeCamel(NormalizedName(field));
|
||||||
|
code += "AsString(start, stop)\n";
|
||||||
|
code += std::string(Indent) + "return " + SelfData + ":VectorAsString(" +
|
||||||
|
NumToString(field.value.offset) + ", start, stop)\n";
|
||||||
|
code += EndFunc;
|
||||||
|
}
|
||||||
|
|
||||||
// Begin the creator function signature.
|
// Begin the creator function signature.
|
||||||
void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) {
|
void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) {
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
@@ -499,6 +511,10 @@ class LuaGenerator : public BaseGenerator {
|
|||||||
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
|
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
|
||||||
} else {
|
} else {
|
||||||
GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
|
GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
|
||||||
|
if (vectortype.base_type == BASE_TYPE_CHAR ||
|
||||||
|
vectortype.base_type == BASE_TYPE_UCHAR) {
|
||||||
|
AccessByteVectorAsString(struct_def, field, code_ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ function Monster_mt:Inventory(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function Monster_mt:InventoryAsString(start, stop)
|
||||||
|
return self.view:VectorAsString(14, start, stop)
|
||||||
|
end
|
||||||
function Monster_mt:InventoryLength()
|
function Monster_mt:InventoryLength()
|
||||||
local o = self.view:Offset(14)
|
local o = self.view:Offset(14)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
@@ -160,6 +163,9 @@ function Monster_mt:Testnestedflatbuffer(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function Monster_mt:TestnestedflatbufferAsString(start, stop)
|
||||||
|
return self.view:VectorAsString(30, start, stop)
|
||||||
|
end
|
||||||
function Monster_mt:TestnestedflatbufferLength()
|
function Monster_mt:TestnestedflatbufferLength()
|
||||||
local o = self.view:Offset(30)
|
local o = self.view:Offset(30)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
@@ -315,6 +321,9 @@ function Monster_mt:Flex(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function Monster_mt:FlexAsString(start, stop)
|
||||||
|
return self.view:VectorAsString(64, start, stop)
|
||||||
|
end
|
||||||
function Monster_mt:FlexLength()
|
function Monster_mt:FlexLength()
|
||||||
local o = self.view:Offset(64)
|
local o = self.view:Offset(64)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
@@ -518,6 +527,9 @@ function Monster_mt:VectorOfEnums(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function Monster_mt:VectorOfEnumsAsString(start, stop)
|
||||||
|
return self.view:VectorAsString(98, start, stop)
|
||||||
|
end
|
||||||
function Monster_mt:VectorOfEnumsLength()
|
function Monster_mt:VectorOfEnumsLength()
|
||||||
local o = self.view:Offset(98)
|
local o = self.view:Offset(98)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
@@ -540,6 +552,9 @@ function Monster_mt:Testrequirednestedflatbuffer(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function Monster_mt:TestrequirednestedflatbufferAsString(start, stop)
|
||||||
|
return self.view:VectorAsString(102, start, stop)
|
||||||
|
end
|
||||||
function Monster_mt:TestrequirednestedflatbufferLength()
|
function Monster_mt:TestrequirednestedflatbufferLength()
|
||||||
local o = self.view:Offset(102)
|
local o = self.view:Offset(102)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ function TypeAliases_mt:V8(j)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
function TypeAliases_mt:V8AsString(start, stop)
|
||||||
|
return self.view:VectorAsString(24, start, stop)
|
||||||
|
end
|
||||||
function TypeAliases_mt:V8Length()
|
function TypeAliases_mt:V8Length()
|
||||||
local o = self.view:Offset(24)
|
local o = self.view:Offset(24)
|
||||||
if o ~= 0 then
|
if o ~= 0 then
|
||||||
|
|||||||
@@ -282,6 +282,41 @@ local function getRootAs_canAcceptString()
|
|||||||
assert(mon:Hp() == 80, "Monster Hp is not 80")
|
assert(mon:Hp() == 80, "Monster Hp is not 80")
|
||||||
end
|
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 =
|
local tests =
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@@ -305,6 +340,10 @@ local tests =
|
|||||||
f = getRootAs_canAcceptString,
|
f = getRootAs_canAcceptString,
|
||||||
d = "Tests that GetRootAs<type>() generated methods accept strings"
|
d = "Tests that GetRootAs<type>() generated methods accept strings"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
f = testAccessByteVectorAsString,
|
||||||
|
d = "Access byte vector as string"
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local benchmarks =
|
local benchmarks =
|
||||||
|
|||||||
Reference in New Issue
Block a user