From b89dedf5664fe0d05afd90ba44df8bb81f896321 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Fri, 13 Jan 2023 19:34:09 +0100 Subject: [PATCH] Make empty groups in `regex.gmatch` return their offset (#1325) This makes `regex.gmatch` behave like `string.gmatch`. --- src/api/regex.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/api/regex.c b/src/api/regex.c index f86e7ff6..bf17d424 100644 --- a/src/api/regex.c +++ b/src/api/regex.c @@ -91,7 +91,10 @@ static int regex_gmatch_iterator(lua_State *L) { int total_results = ovector_count * 2; size_t last_offset = 0; for (int i = index; i < total_results; i+=2) { - lua_pushlstring(L, state->subject+ovector[i], ovector[i+1] - ovector[i]); + if (ovector[i] == ovector[i+1]) + lua_pushinteger(L, ovector[i] + 1); + else + lua_pushlstring(L, state->subject+ovector[i], ovector[i+1] - ovector[i]); last_offset = ovector[i+1]; total++; }