Lua解包bug? [英] Lua unpack bug?

查看:20
本文介绍了Lua解包bug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Lua 解包函数中偶然发现了一个奇怪的行为

table1 = {true, nil, true, false, nil, true, nil}table2 = {真,假,零,假,零,真,零}a1,b1,c1,d1,e1,f1,g1 = unpack( table1 )打印 ("table1:",a1,b1,c1,d1,e1,f1,g1)a2,b2,c2,d2,e2,f2,g2 = unpack( table2 )打印 ("table2:",a2,b2,c2,d2,e2,f2,g2)

输出:

table1: true nil true false nil nil niltable2: true false nil nil nil nil nil

第二次解包将参数传递到第一个 nil 值.我可以忍受.第一张桌子提供 4?中间有一个参数为零.它有 4 个不是 nil 的参数,但它们不是显示的那个.

谁能解释一下?这是用 codepad.org 和 lua 5.1 尝试的

解决方案

这个问题可以简单地通过将开始和结束索引指定为 unpack() 并使用 table.maxn 来解决() 作为结束索引:

<前>table1 = {真,零,真,假,零,真,零}a1,b1,c1,d1,e1,f1,g1 = unpack( table1, 1, table.maxn(table1) )打印 ("table1:",a1,b1,c1,d1,e1,f1,g1)-->table1: true nil true false nil true nil

两个表处理方式不一致的真正原因在于确定表数组部分长度的逻辑.

luaB_unpack() 函数使用了 luaL_getn(),它是根据 lua_objlen() 定义的,它调用了 luaH_getn() 用于表格.luaH_getn() 查看数组的最后一个位置,如果是 nil 则对表中的边界执行二分查找(使得 t[i]非零且 t[i+1] 为零").对数组末尾的二分查找是 table1table2 的处理方式不同的原因.

只有当数组中的最后一个条目是 nil 时,这才应该是一个问题.

来自 Lua 编程(第 16 页)(您应该买这本书.):当一个数组有洞——里面有 nil 元素时——长度操作符可以假设这些 nil 元素中的任何一个作为结束标记.因此,您应该避免在可能包含空洞的数组上使用长度运算符.

unpack() 使用长度运算符 lua_objlen(),它可以假设任何 [the] nil 元素作为数组的结尾".

I Have stumbled on a weird behavior in Lua unpack function

table1 = {true, nil, true, false, nil, true, nil}
table2 = {true, false, nil, false, nil, true, nil}

a1,b1,c1,d1,e1,f1,g1 = unpack( table1 )
print ("table1:",a1,b1,c1,d1,e1,f1,g1)

a2,b2,c2,d2,e2,f2,g2 = unpack( table2 )
print ("table2:",a2,b2,c2,d2,e2,f2,g2)

Output:

table1: true    nil true    false   nil nil nil
table2: true    false   nil nil nil nil nil

The second unpack delivers parameters up to the first nil value. I could live with that. The first table delivers 4? parameters with one being nil in the middle. It has 4 parameters that are not nil, but they aren't the one that are shown.

Could anyone explain this? This was tried with codepad.org and lua 5.1

解决方案

The problem can be resolved simply by specifying the beginning and ending indexes to unpack() and using the table.maxn() as the ending index:

table1 = {true, nil, true, false, nil, true, nil}

a1,b1,c1,d1,e1,f1,g1 = unpack( table1, 1, table.maxn(table1) )
print ("table1:",a1,b1,c1,d1,e1,f1,g1)
-->table1: true    nil     true    false   nil     true    nil

The true reason for the discrepancy on how the two tables are handled is in the logic of determining the length of the array portion of the table.

The luaB_unpack() function uses luaL_getn() which is defined in terms of lua_objlen() which calls luaH_getn() for tables. The luaH_getn() looks at the last position of the array, and if it is nil performs a binary search for a boundary in the table ("such that t[i] is non-nil and t[i+1] is nil"). The binary search for the end of the array is the reason that table1 is handled differently then table2.

This should only be an issue if the last entry in the array is nil.

From Programming in Lua (pg.16) (You should buy this book.): When an array has holes--nil elements inside it--the length operator may assume any of these nil elements as the end marker. Therefore, you should avoid using the length operator on arrays that may contain holes.

The unpack() is using the length operator lua_objlen(), which "may assume any of [the] nil elements as the end" of the array.

这篇关于Lua解包bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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