/* Gets an optional ending string position from argument 'arg', with default value 'def'. Negative means back from end: clip result to   */
static size_t getendpos(lua_State *L, int arg, lua_Integer def, size_t len)
/* Gets an optional ending string position from argument 'arg', with default value 'def'. Negative means back from end: clip result to   */
static size_t getendpos(lua_State *L, int arg, lua_Integer def, size_t len)
{
  lua_Integer pos = luaL_optinteger(L, arg, def);
  if (pos > (lua_Integer)len)
    return len;
  else if (pos >= 0)
    return (size_t)pos;
  else if (pos < -(lua_Integer)len)
    return 0;
  else return len + (size_t)pos + 1;
}