diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-03 10:14:25 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-05-03 10:14:25 -0300 |
| commit | b14609032cf328dea48b0803f3e585e223283b3d (patch) | |
| tree | 6f59256fb668d15a17ec41feef0d01304c2a1ed6 /testes | |
| parent | b36e26f51b117df98f0f5376f352c2381df3025f (diff) | |
| download | lua-b14609032cf328dea48b0803f3e585e223283b3d.tar.gz lua-b14609032cf328dea48b0803f3e585e223283b3d.tar.bz2 lua-b14609032cf328dea48b0803f3e585e223283b3d.zip | |
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.)
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/main.lua | 27 |
1 files changed, 19 insertions, 8 deletions
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 | |||
| 142 | prepfile("print(package.path, package.cpath)") | 142 | prepfile("print(package.path, package.cpath)") |
| 143 | RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', | 143 | RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', |
| 144 | prog, out) | 144 | prog, out) |
| 145 | local output = getoutput() | ||
| 146 | defaultpath = string.match(output, "^(.-)\t") | ||
| 147 | defaultCpath = string.match(output, "\t(.-)$") | ||
| 148 | |||
| 149 | -- running with an empty environment | ||
| 150 | RUN('env -i lua %s > %s', prog, out) | ||
| 145 | local out = getoutput() | 151 | local out = getoutput() |
| 146 | defaultpath = string.match(out, "^(.-)\t") | 152 | assert(defaultpath == string.match(output, "^(.-)\t")) |
| 147 | defaultCpath = string.match(out, "\t(.-)$") | 153 | assert(defaultCpath == string.match(output, "\t(.-)$")) |
| 148 | end | 154 | end |
| 149 | 155 | ||
| 150 | -- paths did not changed | 156 | -- paths did not change |
| 151 | assert(not string.find(defaultpath, "xxx") and | 157 | assert(not string.find(defaultpath, "xxx") and |
| 152 | string.find(defaultpath, "lua") and | 158 | string.find(defaultpath, "lua") and |
| 153 | not string.find(defaultCpath, "xxx") and | 159 | not string.find(defaultCpath, "xxx") and |
| @@ -160,15 +166,20 @@ local function convert (p) | |||
| 160 | RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) | 166 | RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) |
| 161 | local expected = getoutput() | 167 | local expected = getoutput() |
| 162 | expected = string.sub(expected, 1, -2) -- cut final end of line | 168 | expected = string.sub(expected, 1, -2) -- cut final end of line |
| 163 | assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) | 169 | if string.find(p, ";;") then |
| 170 | p = string.gsub(p, ";;", ";"..defaultpath..";") | ||
| 171 | p = string.gsub(p, "^;", "") -- remove ';' at the beginning | ||
| 172 | p = string.gsub(p, ";$", "") -- remove ';' at the end | ||
| 173 | end | ||
| 174 | assert(p == expected) | ||
| 164 | end | 175 | end |
| 165 | 176 | ||
| 166 | convert(";") | 177 | convert(";") |
| 167 | convert(";;") | 178 | convert(";;") |
| 168 | convert(";;;") | 179 | convert("a;;b") |
| 169 | convert(";;;;") | 180 | convert(";;b") |
| 170 | convert(";;;;;") | 181 | convert("a;;") |
| 171 | convert(";;a;;;bc") | 182 | convert("a;b;;c") |
| 172 | 183 | ||
| 173 | 184 | ||
| 174 | -- test -l over multiple libraries | 185 | -- test -l over multiple libraries |
