Skip to content

Commit 7e9959a

Browse files
committed
Creating less tables.
1 parent c992a9a commit 7e9959a

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

preprocess.lua

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[============================================================
22
--=
3-
--= LuaPreprocess v1.20 - preprocessing library
3+
--= LuaPreprocess v1.20-dev - preprocessing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
66
--= License: MIT (see the bottom of this file)
@@ -131,7 +131,7 @@
131131

132132

133133

134-
local PP_VERSION = "1.20.0"
134+
local PP_VERSION = "1.20.0-dev"
135135

136136
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
137137

@@ -1755,23 +1755,28 @@ function metaFuncs.getOutputSoFar(bufferOrAsTable)
17551755
end
17561756
end
17571757

1758+
local lineFragments = {}
1759+
17581760
local function getOutputSoFarOnLine()
17591761
errorIfNotRunningMeta(2)
17601762

1761-
local lineFragments = {} -- @Memory
1763+
local len = 0
17621764

17631765
-- Should there be a way to get the contents of current_meta_output etc.? :GetMoreOutputFromStack
17641766
for i = #current_meta_outputStack[1], 1, -1 do
17651767
local fragment = current_meta_outputStack[1][i]
17661768

17671769
if fragment:find("\n", 1, true) then
1768-
tableInsert(lineFragments, (fragment:gsub(".*\n", "")))
1770+
len = len + 1
1771+
lineFragments[len] = fragment:gsub(".*\n", "")
17691772
break
17701773
end
1771-
tableInsert(lineFragments, fragment)
1774+
1775+
len = len + 1
1776+
lineFragments[len] = fragment
17721777
end
17731778

1774-
return table.concat(lineFragments)
1779+
return table.concat(lineFragments, 1, len)
17751780
end
17761781

17771782
-- getOutputSoFarOnLine()
@@ -2153,14 +2158,17 @@ function metaFuncs.concatTokens(tokens)
21532158
return (_concatTokens(tokens, nil, false, nil, nil))
21542159
end
21552160

2161+
local recycledArrays = {}
2162+
21562163
-- startInterceptingOutput()
21572164
-- startInterceptingOutput( )
21582165
-- Start intercepting output until stopInterceptingOutput() is called.
21592166
-- The function can be called multiple times to intercept interceptions.
21602167
function metaFuncs.startInterceptingOutput()
21612168
errorIfNotRunningMeta(2)
21622169

2163-
current_meta_output = {} -- @Memory (Especially if lots of macro calls are used!)
2170+
current_meta_output = tableRemove(recycledArrays) or {}
2171+
for i = 1, #current_meta_output do current_meta_output[i] = nil end
21642172
tableInsert(current_meta_outputStack, current_meta_output)
21652173
end
21662174

@@ -2169,6 +2177,7 @@ local function _stopInterceptingOutput(errLevel)
21692177

21702178
local interceptedLua = tableRemove(current_meta_outputStack)
21712179
current_meta_output = current_meta_outputStack[#current_meta_outputStack] or error("Called stopInterceptingOutput() before calling startInterceptingOutput().", 1+errLevel)
2180+
tableInsert(recycledArrays, interceptedLua)
21722181

21732182
return table.concat(interceptedLua)
21742183
end
@@ -2246,7 +2255,11 @@ function metaFuncs.LOG(logLevelCode, valueOrFormatCode, ...)
22462255

22472256
if ... then
22482257
tableInsert(current_meta_output, "string.format(")
2249-
tableInsert(current_meta_output, table.concat({valueOrFormatCode, ...}, ", ")) -- @Memory
2258+
tableInsert(current_meta_output, valueOrFormatCode)
2259+
for i = 1, select("#", ...) do
2260+
tableInsert(current_meta_output, ", ")
2261+
tableInsert(current_meta_output, (select(i, ...)))
2262+
end
22502263
tableInsert(current_meta_output, ")")
22512264
else
22522265
tableInsert(current_meta_output, valueOrFormatCode)

tests/suite.lua

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -467,31 +467,13 @@ doTest("Predefined macros", function()
467467
local pp = ppChunk()
468468

469469
-- @@ASSERT()
470-
local luaOut = assert(pp.processString{ code=[[
471-
@@ASSERT(foo)
472-
]]})
473-
assertCodeOutput(luaOut, [[if not (foo) then error("Assertion failed: foo") end]])
474-
475-
local luaOut = assert(pp.processString{ code=[[
476-
@@ASSERT(foo ~= "good", "Bad foo: "..foo)
477-
]]})
478-
assertCodeOutput(luaOut, [[if not (foo ~= "good") then error(("Bad foo: "..foo)) end]])
470+
assertCodeOutput(assert(pp.processString{ code=[[ @@ASSERT(foo) ]]}), [[if not (foo) then error("Assertion failed: foo") end]])
471+
assertCodeOutput(assert(pp.processString{ code=[[ @@ASSERT(foo ~= "good", "Bad foo: "..foo) ]]}), [[if not (foo ~= "good") then error(("Bad foo: "..foo)) end]])
479472

480473
-- @@LOG()
481-
local luaOut = assert(pp.processString{ logLevel="error", code=[[
482-
@@LOG("warning", "Uh oh!")
483-
]]})
484-
assertCodeOutput(luaOut, [[]])
485-
486-
local luaOut = assert(pp.processString{ logLevel="warning", code=[[
487-
@@LOG("warning", "Uh oh!")
488-
]]})
489-
assertCodeOutput(luaOut, [[print("Uh oh!")]])
490-
491-
local luaOut = assert(pp.processString{ code=[[
492-
@@LOG("warning", "Number: %d", num)
493-
]]})
494-
assertCodeOutput(luaOut, [[print(string.format("Number: %d", num))]])
474+
assertCodeOutput(assert(pp.processString{ logLevel="error", code=[[ @@LOG("warning", "Uh oh!") ]]}), [[]])
475+
assertCodeOutput(assert(pp.processString{ logLevel="warning", code=[[ @@LOG("warning", "Uh oh!") ]]}), [[print("Uh oh!")]])
476+
assertCodeOutput(assert(pp.processString{ code=[[ @@LOG("warning", "Number: %d", num) ]]}), [[print(string.format("Number: %d", num))]])
495477

496478
-- Invalid: Bad log level.
497479
assert(not pp.processString{ logLevel="bad", code=""})

0 commit comments

Comments
 (0)