From 58a035d98de1676cf229db7b71cbaf407757c948 Mon Sep 17 00:00:00 2001 From: Eldo Loguzzo Date: Wed, 7 Oct 2026 15:09:20 -0600 Subject: [PATCH] Issue #543 : string.match returns nil when there is no match strMatch pushed LNil but returned 0, so no value reached the caller and tonumber(s:match(...)) failed with "value expected". Co-Authored-By: Claude Opus 5.5 (1M context) --- _glua-tests/strings.lua | 6 ++++++ stringlib.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/_glua-tests/strings.lua b/_glua-tests/strings.lua index 273226d9..4f52fe78 100644 --- a/_glua-tests/strings.lua +++ b/_glua-tests/strings.lua @@ -11,3 +11,9 @@ assert(ret1 == 1) assert(ret2 == 3) assert(ret3 == "aaa") assert(ret4 == 4) + +-- issue #543: string.match returns nil when there is no match +assert(select("#", ("abc"):match("(%d+)")) == 1) +assert(("abc"):match("(%d+)") == nil) +assert(tonumber(("abc"):match("(%d+)")) == nil) +assert(select("#", string.match("abc", "%d")) == 1) diff --git a/stringlib.go b/stringlib.go index f484c2b3..5271f06e 100644 --- a/stringlib.go +++ b/stringlib.go @@ -369,7 +369,7 @@ func strMatch(L *LState) int { } if len(mds) == 0 { L.Push(LNil) - return 0 + return 1 } md := mds[0] nsubs := md.CaptureLength() / 2