Make empty groups in `regex.gmatch` return their offset (#1325)

This makes `regex.gmatch` behave like `string.gmatch`.
This commit is contained in:
Guldoman 2023-01-13 19:34:09 +01:00 committed by takase1121
parent 38fa9f976c
commit a9d8f12cb7
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
1 changed files with 4 additions and 1 deletions

View File

@ -91,7 +91,10 @@ static int regex_gmatch_iterator(lua_State *L) {
int total_results = ovector_count * 2; int total_results = ovector_count * 2;
size_t last_offset = 0; size_t last_offset = 0;
for (int i = index; i < total_results; i+=2) { 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]; last_offset = ovector[i+1];
total++; total++;
} }