From b14609032cf328dea48b0803f3e585e223283b3d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 May 2019 10:14:25 -0300 Subject: Avoid the creation of too many strings in 'package' Both when setting a path and searching for a file ('searchpath'), this commit reduces the number of intermediate strings created in Lua. (For setting a path the change is not relevant, because this is done only twice when loading the module. Anyway, it is a nice example of how to use auxlib buffers to manipulate strings in the C API.) --- testes/main.lua | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'testes') diff --git a/testes/main.lua b/testes/main.lua index b9dcab1c..aab490c8 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -142,12 +142,18 @@ do prepfile("print(package.path, package.cpath)") RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', prog, out) + local output = getoutput() + defaultpath = string.match(output, "^(.-)\t") + defaultCpath = string.match(output, "\t(.-)$") + + -- running with an empty environment + RUN('env -i lua %s > %s', prog, out) local out = getoutput() - defaultpath = string.match(out, "^(.-)\t") - defaultCpath = string.match(out, "\t(.-)$") + assert(defaultpath == string.match(output, "^(.-)\t")) + assert(defaultCpath == string.match(output, "\t(.-)$")) end --- paths did not changed +-- paths did not change assert(not string.find(defaultpath, "xxx") and string.find(defaultpath, "lua") and not string.find(defaultCpath, "xxx") and @@ -160,15 +166,20 @@ local function convert (p) RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) local expected = getoutput() expected = string.sub(expected, 1, -2) -- cut final end of line - assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) + if string.find(p, ";;") then + p = string.gsub(p, ";;", ";"..defaultpath..";") + p = string.gsub(p, "^;", "") -- remove ';' at the beginning + p = string.gsub(p, ";$", "") -- remove ';' at the end + end + assert(p == expected) end convert(";") convert(";;") -convert(";;;") -convert(";;;;") -convert(";;;;;") -convert(";;a;;;bc") +convert("a;;b") +convert(";;b") +convert("a;;") +convert("a;b;;c") -- test -l over multiple libraries -- cgit v1.2.3-55-g6feb