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).
This commit is contained in:
Roberto Ierusalimschy
2019-04-11 11:29:16 -03:00
parent a93e014447
commit b0810c51c3
3 changed files with 115 additions and 47 deletions

View File

@@ -113,7 +113,7 @@ do
contCreate = 0
while contCreate <= limit do
a = contCreate .. "b";
a = string.gsub(a, '(%d%d*)', string.upper)
a = string.gsub(a, '(%d%d*)', "%1 %1")
a = "a"
contCreate = contCreate+1
end