[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

@@ -339,6 +339,18 @@ class LuaGenerator : public BaseGenerator {
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.
void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -499,6 +511,10 @@ class LuaGenerator : public BaseGenerator {
GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
} else {
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;
}