fix: allow str_utfindex second argument to be an explicit nil (#16448)

* str_utfindex checks number of arguments only, but ignores the case in
  which the second argument is an explicit nil. Previously this required
  dropping the second argument entirely.
* Modify the C binding to explicitly check if the second argument is nil
This commit is contained in:
Michael Lingelbach 2021-11-27 10:07:58 -05:00 committed by GitHub
parent afbf89dc01
commit 512ec4632f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -175,13 +175,13 @@ int nlua_str_utfindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
size_t s1_len; size_t s1_len;
const char *s1 = luaL_checklstring(lstate, 1, &s1_len); const char *s1 = luaL_checklstring(lstate, 1, &s1_len);
intptr_t idx; intptr_t idx;
if (lua_gettop(lstate) >= 2) { if (lua_isnoneornil(lstate, 2)) {
idx = (intptr_t)s1_len;
} else {
idx = luaL_checkinteger(lstate, 2); idx = luaL_checkinteger(lstate, 2);
if (idx < 0 || idx > (intptr_t)s1_len) { if (idx < 0 || idx > (intptr_t)s1_len) {
return luaL_error(lstate, "index out of range"); return luaL_error(lstate, "index out of range");
} }
} else {
idx = (intptr_t)s1_len;
} }
size_t codepoints = 0, codeunits = 0; size_t codepoints = 0, codeunits = 0;