18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

fix(scripting): a %b counts the way it walks along the subject

A %bxy at the end of a pattern (or before what always matches) was costed
as one pass over the subject, but matchbalance walks to the end of the
subject from every opening character nothing closes: ("("):rep(1e6):find("%b()")
was let through and ran for hours in C, past the instruction hook.

A %b now multiplies wherever it stands. With the subject at hand it counts
the longest way from an opening character to its closing one, or, when it is
tried once from each position, the ways from all of them over the positions:
a long text of short balanced pairs stays cheap, one that doesn't balance is
refused.
This commit is contained in:
jerrrrycho
2026-11-18 19:29:25 +01:00
parent 178f5b63fe
commit f6e9075a73
4 changed files with 144 additions and 1 deletions

View File

@@ -1171,6 +1171,11 @@ local slow = refused(string.find, long, ".-b")
and refused(function() for x in long:gmatch("a-b") do end end)
and refused(string.find, ("a"):rep(1000), "a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")
and refused(string.find, long, ("a"):rep(1000) .. "b", 1, true)
-- %b walks to the end of the subject from each opening character that nothing closes
local open = ("("):rep(1000000)
slow = slow and refused(string.find, open, '%b()')
and refused(string.gsub, open, '%b()', "")
and refused(function() for x in open:gmatch('%b()') do end end)
-- what a screen does: lines of a file, fields of a line, a plain search, short subjects
local text = ("key=value\n"):rep(10000)
local lines = 0
@@ -1178,7 +1183,8 @@ for line in text:gmatch("[^\n]+") do lines = lines + 1 end
local key, value = ("speed=120"):match("^(%w+)=(%d+)$")
local spaced = select(2, (("a b c "):rep(100)):gsub("%s+", " "))
local found = long:find("aaab", 1, true) == nil and (long .. "b"):find("ab", 1, true) == 1000000
local fast = lines == 10000 and key == "speed" and value == "120" and spaced == 300 and found
local calls = select(2, (("call(a, b) "):rep(10000)):gsub('%b()', ""))
local fast = lines == 10000 and key == "speed" and value == "120" and spaced == 300 and found and calls == 10000
and ("x = 1"):find("=") == 3 and ("abc"):gsub("b", "%0%0") == "abbc"
return {
render = function(state)