From d0d83ed699a6f84fe0f905174216e183e73f99cd Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Sat, 3 Jun 2023 21:59:17 +0200 Subject: [PATCH] fix string.gmatch for Lua 5.4 --- changelog.md | 1 + .../assets/opencomputers/lua/machine.lua | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 9e7a9b01b..3432f6355 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ * [#3533] Added support for observing the contents of fluid container items. * [#3620] Fixed OC 1.8.0+ regression involving API arguments and numbers. * [#3013] Fixed rare server-side deadlock when sending disk activity update packets. +* Fixed string.gmatch not supporting the "init" argument on Lua 5.4. * Update GNU Unifont to 15.0.04. ## OpenOS fixes/improvements diff --git a/src/main/resources/assets/opencomputers/lua/machine.lua b/src/main/resources/assets/opencomputers/lua/machine.lua index f4b94321a..df2cd8cf8 100644 --- a/src/main/resources/assets/opencomputers/lua/machine.lua +++ b/src/main/resources/assets/opencomputers/lua/machine.lua @@ -62,6 +62,9 @@ end ------------------------------------------------------------------------------- +local isLuaOver54 = _VERSION:match("5.4") +local isLuaOver53 = isLuaOver54 or _VERSION:match("5.3") + local function checkArg(n, have, ...) have = type(have) local function check(want, ...) @@ -548,17 +551,27 @@ do return str_find_aux(s, pattern, init, false, false) end - local function str_gmatch(s, pattern) + local function str_gmatch(s, pattern, init) checkArg(1, s, "string") checkArg(2, pattern, "string") if #s < SHORT_STRING then - return string_gmatch(s, pattern, repl, n) + return string_gmatch(s, pattern, init) + end + + local start = 0 + if isLuaOver54 then + checkArg(3, init, "number", "nil") + if init ~= nil then + start = posrelat(init, #s) + if start < 1 then start = 0 + elseif start > #s + 1 then start = #s + 1 + else start = start - 1 end + end end local s = strptr(s) local p = strptr(pattern) - local start = 0 return function() ms = { src_init = s,