"C like struct"在Lua中进行长缓冲操作? [英] "C like struct" in Lua for LONG BUFFER manipulation?

查看:66
本文介绍了"C like struct"在Lua中进行长缓冲操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Lua而不是用C编写映射",以求Lua的简单和美观;-)

I am wanting to code "mappings" in Lua rather than in C for the simplicity and beauty of Lua ;-)

所以可以说在C中我可能有以下情况:

So lets say in C I could have had the following:

typedef struct my_struct{
    char field_1[10];                              
    char field_2[250];                                
    char field_3[2000];                              
}my_struct;

my_struct   *pmy_struct;

pmy_struct = (my_struct *) some_buffer;

所以我想在Lua中使用一种方法在结构"内部的字段上具有固定长度,以便在到达目标系统时整体缓冲区偏移保持原样....

So I am wanting a way in Lua to have FIXED lengths on fields inside a "structure" so that the overall BUFFER OFFSETS stay in tact when it reaches the target system....

因此,在上述结构中,我想将"field_3"设置为文本"apple"……我仍然希望该结构字段的总长度为2000字节...

推荐答案

Lua不能那样工作. Lua没有结构".它没有具有大小"的字段".

Lua doesn't work that way. Lua does not have "structures". It does not have "fields" which have "sizes".

它具有表,该表可以具有几种不同类型的值.这些值被映射到键(它们本身就是值).尽管您可以使用元表来防止添加新键,但是您不能强制值的大小"必须特别大.

It has tables, which can have values of several different types. These values are mapped to keys (which are themselves values). While you can use metatables to prevent the addition of new keys, you can't force the "size" of a value to be anything in particular.

或者,换句话说,停止尝试像C一样编程Lua.它们是不同的语言,因此您应该以自己的方式使用每种语言.在C语言中,您要关心字段的大小,结构的布局等.

Or, to put it another way, stop trying to program Lua like it's C. They are different languages, and you should approach each language in its own way. In C, you want to care about the size of fields, the layout of structs, etc.

使用Lua(以及大多数脚本语言)的原因是,您不想想要关心那些事情.您不需要担心字符串"apple"是否恰好存储在大小为2000字节的字节数组中.而且,如果您要关心它,那么您就不想使用Lua.

The reason to use Lua (and most scripting languages) is because you don't want to care about those things. You don't want to care if the string "apple" happens to be stored in a byte array 2000 bytes in size. And if you want to care about that, then you don't want to use Lua.

这篇关于"C like struct"在Lua中进行长缓冲操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆