Lua table转string

使用C++实现了几个不同版本的table2string
经过测试后发现:

  1. 对于数值类型转字符串, sprintf性能好于std::to_string, stringstream表现不堪入目
  2. 直接使用char*要好于std::string, 字符串连接, strcat好于sprintf
  3. 对于数值类型, 转为16进制性能略优于10进制但相当有限, 同时可读性会差很多

以下是std::to_string版本, 虽然char*版本性能更好, 但实在没必要, 差距并不大的情况下, 还是照顾下程序可读性吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 使用std::string
void table2string(lua_State * L, int index, std::string& str, std::set<const void*>& visited) {
try {
lua_pushnil(L);
str.push_back('{');
bool is_null = true;
while (lua_next(L, index)) {
int ktype = lua_type(L, -2);
str += "[";
if (ktype == LUA_TNUMBER) {
if (lua_isinteger(L, -2)) {
str += std::to_string(lua_tointeger(L, -2));
} else if (lua_isnumber(L, -2)) {
str += std::to_string(lua_tonumber(L, -2));
}
} else if (ktype == LUA_TSTRING) {
str += "\"";
str += lua_tostring(L, -2);
str += "\"";
} else if (ktype == LUA_TBOOLEAN) {
str += lua_toboolean(L, -2) ? "true" : "false";
} else {
char buffer[32] = {0};
sprintf(buffer, "\"%p\"", lua_topointer(L, -2));
str += buffer;
}
str += "]=";

int vtype = lua_type(L, -1);
if (vtype == LUA_TTABLE) {
const void * p = lua_topointer(L, -1);
if (visited.count(p)) {
char buffer[32] = {0};
sprintf(buffer, "\"%p\":", p);
str += buffer;
} else {
visited.insert(lua_topointer(L, -1));
table2string(L, lua_gettop(L), str, visited);
}
} else if (vtype == LUA_TBOOLEAN) {
bool b = lua_toboolean(L, -1);
str += b ? "true" : "false";
} else if (vtype == LUA_TSTRING) {
str.push_back('"');
str += lua_tostring(L, -1);
str.push_back('"');
} else if (vtype == LUA_TNUMBER) {
if (lua_isinteger(L, -1)) {
str += std::to_string(lua_tointeger(L, -1));
} else if (lua_isnumber(L, -1))
str += std::to_string(lua_tonumber(L, -1));
} else {
char buffer[32] = {0};
sprintf(buffer, "\"%p\"", lua_topointer(L, -1));
str += buffer;
}
str.push_back(',');
is_null = false;
lua_pop(L, 1);
}
if (!is_null)
str.pop_back();
str.push_back('}');
}
catch(const std::exception& e) {
luaL_error(L, e.what());
}
}

static int TableToString(lua_State * L)
{
int type = lua_type(L, -1);
switch (type) {
case LUA_TNIL:
lua_pushnil(L);
break;
case LUA_TBOOLEAN:
lua_pushstring(L, lua_toboolean(L, -1) ? "true" : "false");
break;
case LUA_TSTRING:
lua_pushstring(L, lua_tostring(L, -1));
break;
case LUA_TNUMBER:
if (lua_isinteger(L, -1)) {
lua_pushstring(L, std::to_string(lua_tointeger(L, -1)).c_str());
} else {
lua_pushstring(L, std::to_string(lua_tonumber(L, -1)).c_str());
}
break;
case LUA_TTABLE: {
std::set<const void*> visited;
visited.insert(lua_topointer(L, -1));
std::string str;
str.reserve(512);// 提前分配一段空间, 减少自动扩容次数, 如果大概知道字符串的长度, 可以通过lua设置, 更加灵活
table2string(L, lua_gettop(L), str, visited);
lua_pushstring(L, str.c_str());
}
break;
default: {
char buffer[32] = {0};
sprintf(buffer, "\"%p\"", lua_topointer(L, -1));
lua_pushstring(L, buffer);
}
break;
}
return 1;
}


#define ADD_VALUE(L, index, type, buffer, format) {\
char tmp[32] = {0};\
sprintf(tmp, (format), lua_to##type((L), (index)));\
luaL_addstring(&(buffer), tmp);\
}

// 使用luaL_Buffer
void concattable(lua_State * L, int index, luaL_Buffer& b, std::set<const void*>& visited) {
try {
lua_pushnil(L);
luaL_addchar(&b, '{');
while (lua_next(L, index)) {
int ktype = lua_type(L, -2);
luaL_addchar(&b, '[');
if (ktype == LUA_TNUMBER) {
if (lua_isinteger(L, -2)) {
ADD_VALUE(L, -2, integer, b, "%lld");
} else if (lua_isnumber(L, -2)) {
ADD_VALUE(L, -2, number, b, "%0.6lf");
}
} else if (ktype == LUA_TSTRING) {
luaL_addchar(&b, '"');
luaL_addstring(&b, lua_tostring(L, -2));
luaL_addchar(&b, '"');
} else if (ktype == LUA_TBOOLEAN) {
luaL_addstring(&b, lua_toboolean(L, -2) ? "true" : "false");
} else {
ADD_VALUE(L, -2, pointer, b, "\"%p\"");
}
luaL_addstring(&b, "]=");

int vtype = lua_type(L, -1);
if (vtype == LUA_TTABLE) {
const void * p = lua_topointer(L, -1);
if (visited.count(p)) {
ADD_VALUE(L, -1, pointer, b, "\"%p\"");
} else {
visited.insert(lua_topointer(L, -1));
concattable(L, lua_gettop(L), b, visited);
}
} else if (vtype == LUA_TBOOLEAN) {
luaL_addstring(&b, lua_toboolean(L, -1) ? "true" : "false");
} else if (vtype == LUA_TSTRING) {
luaL_addchar(&b, '"');
luaL_addstring(&b, lua_tostring(L, -1));
luaL_addchar(&b, '"');
} else if (vtype == LUA_TNUMBER) {
if (lua_isinteger(L, -1)) {
ADD_VALUE(L, -1, integer, b, "%lld");
} else if (lua_isnumber(L, -1)) {
ADD_VALUE(L, -1, number, b, "%0.6lf");
}
} else {
ADD_VALUE(L, -1, pointer, b, "\"%p\"");
}
luaL_addchar(&b, ',');
lua_pop(L, 1);
}
luaL_addchar(&b, '}');
}
catch(const std::exception& e) {
luaL_error(L, e.what());
}
}

static int joinstring(lua_State * L) {
int type = lua_type(L, 1);
switch (type) {
case LUA_TNIL:
lua_pushnil(L);
break;
case LUA_TBOOLEAN:
lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false");
break;
case LUA_TSTRING:
lua_pushstring(L, lua_tostring(L, 1));
break;
case LUA_TNUMBER:
if (lua_isinteger(L, -1)) {
lua_pushstring(L, std::to_string(lua_tointeger(L, 1)).c_str());
} else {
lua_pushstring(L, std::to_string(lua_tonumber(L, 1)).c_str());
}
break;
case LUA_TTABLE: {
std::set<const void*> visited;
visited.insert(lua_topointer(L, 1));
luaL_Buffer b;
luaL_buffinit(L, &b);
concattable(L, 1, b, visited);
luaL_pushresult(&b);
}
break;
default: {
char buffer[32] = {0};
sprintf(buffer, "\"%p\"", lua_topointer(L, 1));
lua_pushstring(L, buffer);
}
break;
}
return 1;
}

static const struct luaL_Reg methods[] = {
{ "tostring", TableToString },
{ "join", joinstring },
{ NULL, NULL },
};