1
1
-- Minetest: builtin/misc_helpers.lua
2
2
3
+ ---- ----------------------------------------------------------------------------
4
+ -- Localize functions to avoid table lookups (better performance).
5
+ local table_insert = table.insert
6
+ local string_sub , string_sub = string.sub , string.find
7
+
3
8
---- ----------------------------------------------------------------------------
4
9
function basic_dump (o )
5
10
local tp = type (o )
@@ -89,13 +94,13 @@ function dump2(o, name, dumped)
89
94
-- the form _G["table: 0xFFFFFFF"]
90
95
keyStr = string.format (" _G[%q]" , tostring (k ))
91
96
-- Dump key table
92
- table.insert (t , dump2 (k , keyStr , dumped ))
97
+ table_insert (t , dump2 (k , keyStr , dumped ))
93
98
end
94
99
else
95
100
keyStr = basic_dump (k )
96
101
end
97
102
local vname = string.format (" %s[%s]" , name , keyStr )
98
- table.insert (t , dump2 (v , vname , dumped ))
103
+ table_insert (t , dump2 (v , vname , dumped ))
99
104
end
100
105
return string.format (" %s = {}\n %s" , name , table.concat (t ))
101
106
end
@@ -130,7 +135,7 @@ function dump(o, indent, nested, level)
130
135
local t = {}
131
136
local dumped_indexes = {}
132
137
for i , v in ipairs (o ) do
133
- table.insert (t , dump (v , indent , nested , level + 1 ))
138
+ table_insert (t , dump (v , indent , nested , level + 1 ))
134
139
dumped_indexes [i ] = true
135
140
end
136
141
for k , v in pairs (o ) do
@@ -139,7 +144,7 @@ function dump(o, indent, nested, level)
139
144
k = " [" .. dump (k , indent , nested , level + 1 ).. " ]"
140
145
end
141
146
v = dump (v , indent , nested , level + 1 )
142
- table.insert (t , k .. " = " .. v )
147
+ table_insert (t , k .. " = " .. v )
143
148
end
144
149
end
145
150
nested [o ] = nil
@@ -154,10 +159,6 @@ function dump(o, indent, nested, level)
154
159
return " {" .. table.concat (t , " , " ).. " }"
155
160
end
156
161
157
- ---- ----------------------------------------------------------------------------
158
- -- Localize functions to avoid table lookups (better performance).
159
- local table_insert = table.insert
160
- local str_sub , str_find = string.sub , string.find
161
162
function string .split (str , delim , include_empty , max_splits , sep_is_pattern )
162
163
delim = delim or " ,"
163
164
max_splits = max_splits or - 1
@@ -166,13 +167,13 @@ function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
166
167
local plain = not sep_is_pattern
167
168
max_splits = max_splits + 1
168
169
repeat
169
- local np , npe = str_find (str , delim , pos , plain )
170
+ local np , npe = string_sub (str , delim , pos , plain )
170
171
np , npe = (np or (len + 1 )), (npe or (len + 1 ))
171
172
if (not np ) or (max_splits == 1 ) then
172
173
np = len + 1
173
174
npe = np
174
175
end
175
- local s = str_sub (str , pos , np - 1 )
176
+ local s = string_sub (str , pos , np - 1 )
176
177
if include_empty or (s ~= " " ) then
177
178
max_splits = max_splits - 1
178
179
table_insert (items , s )
@@ -298,8 +299,8 @@ function core.splittext(text,charlimit)
298
299
299
300
local current_idx = 1
300
301
301
- local start ,stop = string.find (text ," " ,current_idx )
302
- local nl_start ,nl_stop = string.find (text ," \n " ,current_idx )
302
+ local start ,stop = string_find (text , " " , current_idx )
303
+ local nl_start ,nl_stop = string_find (text , " \n " , current_idx )
303
304
local gotnewline = false
304
305
if nl_start ~= nil and (start == nil or nl_start < start ) then
305
306
start = nl_start
@@ -309,25 +310,25 @@ function core.splittext(text,charlimit)
309
310
local last_line = " "
310
311
while start ~= nil do
311
312
if string.len (last_line ) + (stop - start ) > charlimit then
312
- table.insert (retval ,last_line )
313
+ table_insert (retval , last_line )
313
314
last_line = " "
314
315
end
315
316
316
317
if last_line ~= " " then
317
318
last_line = last_line .. " "
318
319
end
319
320
320
- last_line = last_line .. string.sub (text ,current_idx ,stop - 1 )
321
+ last_line = last_line .. string_sub (text , current_idx , stop - 1 )
321
322
322
323
if gotnewline then
323
- table.insert (retval ,last_line )
324
+ table_insert (retval , last_line )
324
325
last_line = " "
325
326
gotnewline = false
326
327
end
327
328
current_idx = stop + 1
328
329
329
- start ,stop = string.find (text ," " ,current_idx )
330
- nl_start ,nl_stop = string.find (text ," \n " ,current_idx )
330
+ start ,stop = string_find (text , " " , current_idx )
331
+ nl_start ,nl_stop = string_find (text , " \n " , current_idx )
331
332
332
333
if nl_start ~= nil and (start == nil or nl_start < start ) then
333
334
start = nl_start
@@ -338,11 +339,11 @@ function core.splittext(text,charlimit)
338
339
339
340
-- add last part of text
340
341
if string.len (last_line ) + (string.len (text ) - current_idx ) > charlimit then
341
- table.insert (retval ,last_line )
342
- table.insert (retval ,string.sub (text ,current_idx ))
342
+ table_insert (retval , last_line )
343
+ table_insert (retval , string_sub (text , current_idx ))
343
344
else
344
- last_line = last_line .. " " .. string.sub (text ,current_idx )
345
- table.insert (retval ,last_line )
345
+ last_line = last_line .. " " .. string_sub (text , current_idx )
346
+ table_insert (retval , last_line )
346
347
end
347
348
348
349
return retval
0 commit comments