-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.c
191 lines (179 loc) · 6.05 KB
/
text.c
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
#include <lauxlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "buffer.h"
static const char *char2escape[256] = {
"\\x00", "\\x01", "\\x02", "\\x03",
"\\x04", "\\x05", "\\x06", "\\x07",
"\\b", "\\t", "\\n", "\\x0b",
"\\f", "\\r", "\\x0e", "\\x0f",
"\\x10", "\\x11", "\\x12", "\\x13",
"\\x14", "\\x15", "\\x16", "\\x17",
"\\x18", "\\x19", "\\x1a", "\\x1b",
"\\x1c", "\\x1d", "\\x1e", "\\x1f",
NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\x7f",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};
inline static bool
is_name(const char *str, size_t len) {
if (len == 0) return false;
char c = *str;
if (c != '_' && !isalpha(c))
return false;
for (size_t i = 1; i < len; ++i) {
c = str[i];
if (c != '_' && !isalpha(c) && !isdigit(c))
return false;
}
return true;
}
inline static void
append_escape_string(struct buffer *bf, const char *str, size_t len) {
for (size_t i = 0; i < len; ++i) {
const char *esc = char2escape[(unsigned char)str[i]];
if (esc)
buffer_append_str(bf, esc);
else
buffer_append_char(bf, str[i]);
}
}
static void
_serialize(lua_State *L, int idx, struct buffer *bf, bool is_key, int depth) {
if (depth > MAX_DEPTH) {
buffer_free(bf);
luaL_error(L, "serialize can't pack too depth table");
}
int type = lua_type(L, idx);
char numbuff[64] = {0};
switch(type) {
case LUA_TNIL:
buffer_append_lstr(bf, "nil", 3);
break;
case LUA_TNUMBER: {
if (is_key) buffer_append_char(bf, '[');
#if LUA_VERSION_NUM < 503
int len = lua_number2str(numbuff, lua_tonumber(L, idx));
buffer_append_lstr(bf, numbuff, len);
#else
if (lua_isinteger(L, idx)) {
int len = lua_integer2str(numbuff, sizeof(numbuff), lua_tointeger(L, idx));
buffer_append_lstr(bf, numbuff, len);
} else {
int len = lua_number2str(numbuff, sizeof(numbuff), lua_tonumber(L, idx));
buffer_append_lstr(bf, numbuff, len);
}
#endif
if (is_key) buffer_append_lstr(bf, "]=", 2);
break;
}
case LUA_TBOOLEAN:
if (is_key) buffer_append_char(bf, '[');
if (lua_toboolean(L, idx))
buffer_append_lstr(bf, "true", 4);
else
buffer_append_lstr(bf, "false", 5);
if (is_key) buffer_append_lstr(bf, "]=", 2);
break;
case LUA_TSTRING: {
size_t len;
const char *str = lua_tolstring(L, idx, &len);
if (is_key) {
if (is_name(str, len)) {
buffer_append_lstr(bf, str, len);
buffer_append_char(bf, '=');
} else {
buffer_append_lstr(bf, "[\"", 2);
append_escape_string(bf, str, len);
buffer_append_lstr(bf, "\"]=", 3);
}
} else {
buffer_append_char(bf, '"');
append_escape_string(bf, str, len);
buffer_append_char(bf, '"');
}
break;
}
case LUA_TTABLE: {
luaL_checkstack(L, LUA_MINSTACK, NULL);
if (is_key) buffer_append_char(bf, '[');
buffer_append_char(bf, '{');
bool first = 1;
int len = lua_rawlen(L, idx);
for (int i = 1; i <= len; ++i) {
if (first)
first = 0;
else
buffer_append_char(bf, ',');
lua_rawgeti(L, idx, i);
int top = lua_gettop(L);
_serialize(L, top, bf, false, depth + 1);
lua_pop(L, 1);
}
lua_pushnil(L);
while (lua_next(L, idx)) {
if (lua_type(L, -2) == LUA_TNUMBER && lua_isinteger(L, -2)) {
lua_Integer i = lua_tointeger(L, -2);
if (i > 0 && i <= len) {
lua_pop(L, 1);
continue;
}
}
if (first)
first = 0;
else
buffer_append_char(bf, ',');
int top = lua_gettop(L);
_serialize(L, top - 1, bf, true, depth + 1);
_serialize(L, top, bf, false, depth + 1);
lua_pop(L, 1);
}
buffer_append_char(bf, '}');
if (is_key) buffer_append_lstr(bf, "]=", 2);
break;
}
default:
buffer_free(bf);
luaL_error(L, "Bad type: %s", lua_typename(L, type));
}
}
int to_txt(lua_State *L) {
struct buffer bf;
buffer_initialize(&bf, L);
for (int i = 1; i <= lua_gettop(L); ++i) {
if (i != 1)
buffer_append_char(&bf, ',');
_serialize(L, i, &bf, false, 0);
}
buffer_push_string(&bf);
buffer_free(&bf);
return 1;
}