-
Notifications
You must be signed in to change notification settings - Fork 1
/
advanced-string-pack.lua
379 lines (298 loc) · 8.6 KB
/
advanced-string-pack.lua
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
local old_pack, old_unpack = string.pack, string.unpack
local pack, unpack
local function skip_node(fmt, pos)
if fmt:find("^%}", pos) then
error("skip fail")
end
local deep = 1
while (pos <= #fmt and deep > 0) do
local _, new_pos = fmt:find("[{,}()[%]]", pos)
if new_pos then
if fmt:find("^[})%]]", new_pos) then
deep = deep - 1
elseif fmt:find("^[{[(]", new_pos) then
deep = deep + 1
elseif fmt:find("^,", new_pos) and deep == 1 then
return new_pos + 1
end
pos = new_pos + 1
else
return #fmt + 1
end
end
return pos
end
local function skip_spaces(fmt, pos)
if fmt:find("^%s", pos) then
local _, pos = fmt:find("^%s+", pos)
return pos + 1
else
return pos
end
end
local function check_key(data_key, fmt_key, index)
if not data_key then
return true, index + 1
elseif not fmt_key then
return data_key == index, index + 1
elseif fmt_key == "*" then
return true, index + 1
elseif data_key == fmt_key then
return true, index + 1
elseif type(data_key) == "number" and tonumber(fmt_key) then
index = tonumber(fmt_key)
return data_key == index, index + 1
end
return false, index + 1
end
local function unpack_node(fmt, data, i, fmt_index, stack, data_key, count, tree)
local index = 1
local skip_rest = (count == 0)
local key_found = false
local value
repeat
if not skip_rest then
local fmt_key = fmt:match("^([^:,(){}[%]]+):", fmt_index)
if fmt_key then
fmt_index = fmt_index + #fmt_key + 1
end
key_found, index = check_key(data_key, fmt_key, index)
end
local new_fmt_index
if skip_rest or not key_found then
new_fmt_index = skip_node(fmt, fmt_index)
else
if tree then
if not count then
local new_stack
new_fmt_index, new_stack, i, value = unpack(fmt, data, i, fmt_index, tree)
table.insert(stack, new_stack)
else
local stack_array = {}
for _ = 1, count do
local new_stack
new_fmt_index, new_stack, i, value = unpack(fmt, data, i, fmt_index, tree)
table.insert(stack_array, new_stack)
end
table.insert(stack, stack_array)
end
else
for _ = 1, (count or 1) do
local new_stack
new_fmt_index, new_stack, i, value = unpack(fmt, data, i, fmt_index, tree)
table.move(new_stack, 1, #new_stack, #stack + 1, stack)
end
end
skip_rest = true
end
fmt_index = new_fmt_index
until not fmt:find("^,", fmt_index - 1)
if tree and not key_found then
table.insert(stack, {})
end
return i, fmt_index, value
end
local pack_reg = "^[!0-9<>=xX bBhHlLjJTiIfdnczs]+"
local values_reg = "[bBhHlLjJTiIfdnczs]"
function unpack(fmt, data, i, fmt_index, tree)
fmt_index = fmt_index or 1
if fmt_index > #fmt then
error("out of range")
end
local stack = {}
local keys = {}
local counts = {}
local value
local last_value
local return_next_value
while (#fmt >= fmt_index) do
fmt_index = skip_spaces(fmt, fmt_index)
if (#fmt < fmt_index) then
break
end
local new_value
if fmt:find("^[([]", fmt_index) then
local it_is_key = fmt:find("^%(", fmt_index)
local new_stack
local debug_pos = fmt_index
fmt_index, new_stack, i, new_value = unpack(fmt, data, i, fmt_index + 1, tree)
if it_is_key then
table.insert(keys, new_value)
else
table.insert(counts, new_value)
end
table.move(new_stack, 1, #new_stack, #stack + 1, stack)
elseif fmt:find("^%{c%}", fmt_index) then
table.remove(keys, 1)
local count = table.remove(counts, 1)
new_value = data:sub(i, i + count - 1)
i = i + #new_value
fmt_index = fmt_index + 3
table.insert(stack, new_value)
elseif fmt:find("^%{", fmt_index) then
i, fmt_index, new_value = unpack_node(fmt, data, i, fmt_index + 1, stack, table.remove(keys, 1), table.remove(counts, 1), tree)
elseif fmt:find("^[)}%],]", fmt_index) then
fmt_index = fmt_index + 1
break
elseif fmt:find("^%*", fmt_index) then
return_next_value = true
fmt_index = fmt_index + 1
elseif fmt:find(pack_reg, fmt_index) then
local command = fmt:match(pack_reg, fmt_index)
local new_stack = {old_unpack(command, data, i)}
i = table.remove(new_stack)
fmt_index = fmt_index + #command
if #new_stack > 0 then
table.move(new_stack, 1, #new_stack, #stack + 1, stack)
if return_next_value then
new_value = new_stack[1]
else
new_value = new_stack[#new_stack]
end
end
else
error(("strange: %d %q"):format(fmt_index, fmt:sub(fmt_index)))
end
if new_value then
if return_next_value and not value then
value = new_value
return_next_value = false
end
last_value = new_value
end
end
return fmt_index, stack, i, value or last_value
end
local function count_args(fmt)
local find_pos = 1
local count = 0
local ok
repeat
ok, find_pos = fmt:find(values_reg, find_pos)
if ok then
find_pos = find_pos + 1
count = count + 1
end
until not ok
find_pos = 1
repeat
ok, find_pos = fmt:find("X", find_pos, true)
if ok then
find_pos = find_pos + 1
count = count - 1
end
until not ok
return count
end
function pack(fmt, fmt_index, args, arg_index)
fmt_index = fmt_index or 1
arg_index = arg_index or 1
if fmt_index > #fmt then
error("out of range")
end
local keys = {}
local counts = {}
local buffer = {}
local value
local last_value
local return_next_value
while (#fmt >= fmt_index) do
local new_value
fmt_index = skip_spaces(fmt, fmt_index)
if (#fmt < fmt_index) then
break
end
if fmt:find("^[)}%],]", fmt_index) then
fmt_index = fmt_index + 1
break
elseif fmt:find("^[([]", fmt_index) then
local in_tbl = counts
if fmt:find("^%(", fmt_index) then
in_tbl = keys
end
fmt_index, arg_index, new_value, data = pack(fmt, fmt_index + 1, args, arg_index)
table.insert(in_tbl, new_value)
table.insert(buffer, data)
elseif fmt:find("^%*", fmt_index) then
return_next_value = true
fmt_index = fmt_index + 1
elseif fmt:find("^%{c%}", fmt_index) then
table.remove(keys, 1)
local count = table.remove(counts, 1)
table.insert(buffer, old_pack(("c%d"):format(count), args[arg_index]))
arg_index = arg_index + 1
fmt_index = fmt_index + 3
elseif fmt:find("^%{", fmt_index) then
local data_key = table.remove(keys, 1)
local count = table.remove(counts, 1)
local index = 1
local skip_rest = (count == 0)
local key_found
fmt_index = fmt_index + 1
repeat
if not skip_rest then
local fmt_key = fmt:match("^([^:,(){}[%]]+):", fmt_index)
if fmt_key then
fmt_index = fmt_index + #fmt_key + 1
end
key_found, index = check_key(data_key, fmt_key, index)
end
local new_fmt_index
if skip_rest or not key_found then
new_fmt_index = skip_node(fmt, fmt_index)
else
for _ = 1, count or 1 do
new_fmt_index, arg_index, new_value, data = pack(fmt, fmt_index, args, arg_index)
table.insert(buffer, data)
end
skip_rest = true
end
fmt_index = new_fmt_index
until not fmt:find("^,", fmt_index - 1)
elseif fmt:find(pack_reg, fmt_index) then
local fmt = fmt:match(pack_reg, fmt_index)
local next_index = arg_index + count_args(fmt)
table.insert(buffer, old_pack(fmt, table.unpack(args, arg_index, next_index - 1)))
if return_next_value then
new_value = args[arg_index]
end
arg_index = next_index
fmt_index = fmt_index + #fmt
if not return_next_value then
new_value = args[arg_index - 1]
end
else
error(("strange: %d %q"):format(fmt_index, fmt:sub(fmt_index)))
end
if new_value then
if return_next_value and not value then
value = new_value
return_next_value = false
end
last_value = new_value
end
end
return fmt_index, arg_index, value or last_value, table.concat(buffer)
end
local function flat(tbl, stack)
stack = stack or {}
for _, v in ipairs(tbl) do
if type(v) == "table" then
flat(v, stack)
else
table.insert(stack, v)
end
end
return stack
end
function string.unpack(fmt, data, i, tree)
local pos, stack, i = unpack(fmt, data, i, 1, tree)
if tree then
return stack, i
end
table.insert(stack, i)
return table.unpack(stack)
end
function string.pack(fmt, ...)
return select(4, pack(fmt, 1, flat({...}), 1))
end