Re: Proposal: Constant Tables
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Proposal: Constant Tables
- From: Gregory Bonik <gregory@...>
- Date: 2011年8月13日 17:10:24 +0400
> int table_to_string(lua_State* L)
> {
> std::ostringstream o;
> lua_pushnil(L);
> while (lua_next(L, 1) != 0) {
> encode_lua_value(L, -2, o);
> encode_lua_value(L, -1, o);
> lua_pop(L, 1);
> }
> std::string s = o.str();
> lua_pushlstring(L, s.data(), s.length());
> return 1;
> }
>
Just realized that this implementation is incorrect because the order of
elements can be different in two different (buf logically identical)
tables. One has to sort the keys in some fixed order to ensure
corectness. Nevertheless, the general idea still holds.
I'll try in Lua this time:
local function table_to_string(t)
local elems = {}
for k, v in pairs(t) do
local s = encode_lua_value(k) .. encode_lua_value(v)
table.insert(elems, s)
end
table.sort(elems)
return table.concat(elems)
end