diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-11 11:29:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-11 11:29:16 -0300 |
commit | b0810c51c3f075cc8a309bfb3c1714ac42b0f020 (patch) | |
tree | 8d9e58246f442f1fbec8bb14ffebb77bd40c3564 /testes | |
parent | a93e0144479f1eb0ac19b8c31862f4cbc2fbe1c4 (diff) | |
download | lua-b0810c51c3f075cc8a309bfb3c1714ac42b0f020.tar.gz lua-b0810c51c3f075cc8a309bfb3c1714ac42b0f020.tar.bz2 lua-b0810c51c3f075cc8a309bfb3c1714ac42b0f020.zip |
Small optimizations in 'string.gsub'
Avoid creating extra strings when possible:
- avoid creating new resulting string when subject was not modified
(instead, return the subject itself);
- avoid creating strings representing the captured substrings when
handling replacements like '%1' (instead, add the substring directly
to the buffer).
Diffstat (limited to 'testes')
-rw-r--r-- | testes/gc.lua | 2 | ||||
-rw-r--r-- | testes/pm.lua | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/testes/gc.lua b/testes/gc.lua index 91e78a48..6d24e0d8 100644 --- a/testes/gc.lua +++ b/testes/gc.lua | |||
@@ -113,7 +113,7 @@ do | |||
113 | contCreate = 0 | 113 | contCreate = 0 |
114 | while contCreate <= limit do | 114 | while contCreate <= limit do |
115 | a = contCreate .. "b"; | 115 | a = contCreate .. "b"; |
116 | a = string.gsub(a, '(%d%d*)', string.upper) | 116 | a = string.gsub(a, '(%d%d*)', "%1 %1") |
117 | a = "a" | 117 | a = "a" |
118 | contCreate = contCreate+1 | 118 | contCreate = contCreate+1 |
119 | end | 119 | end |
diff --git a/testes/pm.lua b/testes/pm.lua index 8cc8772e..4d87fad2 100644 --- a/testes/pm.lua +++ b/testes/pm.lua | |||
@@ -387,5 +387,35 @@ assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") | |||
387 | assert(string.find("abc\0\0","\0.") == 4) | 387 | assert(string.find("abc\0\0","\0.") == 4) |
388 | assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) | 388 | assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) |
389 | 389 | ||
390 | |||
391 | do -- test reuse of original string in gsub | ||
392 | local s = string.rep("a", 100) | ||
393 | local r = string.gsub(s, "b", "c") -- no match | ||
394 | assert(string.format("%p", s) == string.format("%p", r)) | ||
395 | |||
396 | r = string.gsub(s, ".", {x = "y"}) -- no substitutions | ||
397 | assert(string.format("%p", s) == string.format("%p", r)) | ||
398 | |||
399 | local count = 0 | ||
400 | r = string.gsub(s, ".", function (x) | ||
401 | assert(x == "a") | ||
402 | count = count + 1 | ||
403 | return nil -- no substitution | ||
404 | end) | ||
405 | r = string.gsub(r, ".", {b = 'x'}) -- "a" is not a key; no subst. | ||
406 | assert(count == 100) | ||
407 | assert(string.format("%p", s) == string.format("%p", r)) | ||
408 | |||
409 | count = 0 | ||
410 | r = string.gsub(s, ".", function (x) | ||
411 | assert(x == "a") | ||
412 | count = count + 1 | ||
413 | return x -- substitution... | ||
414 | end) | ||
415 | assert(count == 100) | ||
416 | -- no reuse in this case | ||
417 | assert(r == s and string.format("%p", s) ~= string.format("%p", r)) | ||
418 | end | ||
419 | |||
390 | print('OK') | 420 | print('OK') |
391 | 421 | ||