<commit_before>/*
** Table library.
** Copyright (C) 2005-2013 <NAME>. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/

#define lib_table_c
#define LUA_LIB

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_tab.h"
#include "lj_lib.h"

/* ------------------------------------------------------------------------ */

#define LJLIB_MODULE_table

LJLIB_CF(table_foreachi)
{
  GCtab *t = lj_lib_checktab(L, 1);
  GCfunc *func = lj_lib_checkfunc(L, 2);
  MSize i, n = lj_tab_len(t);
  for (i = 1; i <= n; i++) {
    cTValue *val;
    setfuncV(L, L->top, func);
    setintV(L->top+1, i);
    val = lj_tab_getint(t, (int32_t)i);
    if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); }
    L->top += 3;
    lua_call(L, 2, 1);
    if (!tvisnil(L->top-1))
      return 1;
    L->top--;
  }
  return 0;
}

LJLIB_CF(table_foreach)
{
  GCtab *t = lj_lib_checktab(L, 1);
  GCfunc *func = lj_lib_checkfunc(L, 2);
  L->top = L->base+3;
  setnilV(L->top-1);
  while (lj_tab_next(L, t, L->top-1)) {
    copyTV(L, L->top+2, L->top);
    copyTV(L, L->top+1, L->top-1);
    setfuncV(L, L->top, func);
    L->top += 3;
    lua_call(L, 2, 1);
    if (!tvisnil(L->top-1))
      return 1;
    L->top--;
  }
  return 0;
}

LJLIB_ASM(table_getn)		LJLIB_REC(.)
{
  lj_lib_checktab(L, 1);
  return FFH_UNREACHABLE;
}

LJLIB_CF(table_maxn)
{
  GCtab *t = lj_lib_checktab(L, 1);
  TValue *array = tvref(t->array);
  Node *node;
  lua_Number m = 0;
  ptrdiff_t i;
  for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--)
    if (!tvisnil(&array[i])) {
      m = (lua_Number)(int32_t)i;
      break;
    }
  node = noderef(t->node);
  for (i = (ptrdiff_t)t->hmask; i >= 0; i--)
    if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) {
      lua_Number n = numberVnum(&node[i].key);
      if (n > m) m = n;
    }
  setnumV(L->top-1, m);
  return 1;
}

LJLIB_CF(table_insert)		LJLIB_REC(.)
{
  GCtab *t = lj_lib_checktab(L, 1);
  int32_t n, i = (int32_t)lj_tab_len(t) + 1;
  int nargs = (int)((char *)L->top - (char *)L->base);
  if (nargs != 2*sizeof(TValue)) {
    if (nargs != 3*sizeof(TValue))
      lj_err_caller(L, LJ_ERR_TABINS);
    /* NOBARRIER: This just moves existing elements around. */
    for (n = lj_lib_checkint(L, 2); i > n; i--) {
      /* The set may invalidate the get pointer, so need to do it first! */
      TValue *dst = lj_tab_setint(L, t, i);
      cTValue *src = lj_tab_getint(t, i-1);
      if (src) {
	copyTV(L, dst, src);
      } else {
	setnilV(dst);
      }
    }
    i = n;
  }
  {
    TValue *dst = lj_tab_setint(L, t, i);
    copyTV(L, dst, L->top-1);  /* Set new value. */
    lj_gc_barriert(L, t, dst);
  }
  return 0;
}

LJLIB_CF(table_remove)		LJLIB_REC(.)
{
  GCtab *t = lj_lib_checktab(L, 1);
  int32_t e = (int32_t)lj_tab_len(t);
  int32_t pos = lj_lib_optint(L, 2, e);
  if (!(1 <= pos && pos <= e))  /* Nothing to remove? */
    return 0;
  lua_rawgeti(L, 1, pos);  /* Get previous value. */
  /* NOBARRIER: This just moves existing elements around. */
  for (; pos < e; pos++) {
    cTValue *src = lj_tab_getint(t, pos+1);
    TValue *dst = lj_tab_setint(L, t, pos);
    if (src) {
      copyTV(L, dst, src);
    } else {
      setnilV(dst);
    }
  }
  setnilV(lj_tab_setint(L, t, e));  /* Remove (last) value. */
  return 1;  /* Return previous value. */
}

LJLIB_CF(table_concat)
{
  luaL_Buffer b;
  GCtab *t = lj_lib_checktab(L, 1);
  GCstr *sep = lj_lib_optstr(L, 2);
  MSize seplen = sep ? sep->len : 0;
  int32_t i = lj_lib_optint(L, 3, 1);
  int32_t e = L->base+3 < L->top ? lj_lib_checkint(L, 4) :
				   (int32_t)lj_tab_len(t);
  luaL_buffinit(L, &b);
  if (i <= e) {
    for (;;) {
      cTValue *o;
      lua_rawgeti(L, 1, i);
      o = L->top-1;
      if (!(tvisstr(o) || tvisnumber(o)))
	lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i);
      luaL_addvalue(&b);<commit_msg>Fix optional argument handling in table.concat().<commit_after>/*
** Table library.
** Copyright (C) 2005-2013 <NAME>. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/

#define lib_table_c
#define LUA_LIB

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_tab.h"
#include "lj_lib.h"

/* ------------------------------------------------------------------------ */

#define LJLIB_MODULE_table

LJLIB_CF(table_foreachi)
{
  GCtab *t = lj_lib_checktab(L, 1);
  GCfunc *func = lj_lib_checkfunc(L, 2);
  MSize i, n = lj_tab_len(t);
  for (i = 1; i <= n; i++) {
    cTValue *val;
    setfuncV(L, L->top, func);
    setintV(L->top+1, i);
    val = lj_tab_getint(t, (int32_t)i);
    if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); }
    L->top += 3;
    lua_call(L, 2, 1);
    if (!tvisnil(L->top-1))
      return 1;
    L->top--;
  }
  return 0;
}

LJLIB_CF(table_foreach)
{
  GCtab *t = lj_lib_checktab(L, 1);
  GCfunc *func = lj_lib_checkfunc(L, 2);
  L->top = L->base+3;
  setnilV(L->top-1);
  while (lj_tab_next(L, t, L->top-1)) {
    copyTV(L, L->top+2, L->top);
    copyTV(L, L->top+1, L->top-1);
    setfuncV(L, L->top, func);
    L->top += 3;
    lua_call(L, 2, 1);
    if (!tvisnil(L->top-1))
      return 1;
    L->top--;
  }
  return 0;
}

LJLIB_ASM(table_getn)		LJLIB_REC(.)
{
  lj_lib_checktab(L, 1);
  return FFH_UNREACHABLE;
}

LJLIB_CF(table_maxn)
{
  GCtab *t = lj_lib_checktab(L, 1);
  TValue *array = tvref(t->array);
  Node *node;
  lua_Number m = 0;
  ptrdiff_t i;
  for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--)
    if (!tvisnil(&array[i])) {
      m = (lua_Number)(int32_t)i;
      break;
    }
  node = noderef(t->node);
  for (i = (ptrdiff_t)t->hmask; i >= 0; i--)
    if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) {
      lua_Number n = numberVnum(&node[i].key);
      if (n > m) m = n;
    }
  setnumV(L->top-1, m);
  return 1;
}

LJLIB_CF(table_insert)		LJLIB_REC(.)
{
  GCtab *t = lj_lib_checktab(L, 1);
  int32_t n, i = (int32_t)lj_tab_len(t) + 1;
  int nargs = (int)((char *)L->top - (char *)L->base);
  if (nargs != 2*sizeof(TValue)) {
    if (nargs != 3*sizeof(TValue))
      lj_err_caller(L, LJ_ERR_TABINS);
    /* NOBARRIER: This just moves existing elements around. */
    for (n = lj_lib_checkint(L, 2); i > n; i--) {
      /* The set may invalidate the get pointer, so need to do it first! */
      TValue *dst = lj_tab_setint(L, t, i);
      cTValue *src = lj_tab_getint(t, i-1);
      if (src) {
	copyTV(L, dst, src);
      } else {
	setnilV(dst);
      }
    }
    i = n;
  }
  {
    TValue *dst = lj_tab_setint(L, t, i);
    copyTV(L, dst, L->top-1);  /* Set new value. */
    lj_gc_barriert(L, t, dst);
  }
  return 0;
}

LJLIB_CF(table_remove)		LJLIB_REC(.)
{
  GCtab *t = lj_lib_checktab(L, 1);
  int32_t e = (int32_t)lj_tab_len(t);
  int32_t pos = lj_lib_optint(L, 2, e);
  if (!(1 <= pos && pos <= e))  /* Nothing to remove? */
    return 0;
  lua_rawgeti(L, 1, pos);  /* Get previous value. */
  /* NOBARRIER: This just moves existing elements around. */
  for (; pos < e; pos++) {
    cTValue *src = lj_tab_getint(t, pos+1);
    TValue *dst = lj_tab_setint(L, t, pos);
    if (src) {
      copyTV(L, dst, src);
    } else {
      setnilV(dst);
    }
  }
  setnilV(lj_tab_setint(L, t, e));  /* Remove (last) value. */
  return 1;  /* Return previous value. */
}

LJLIB_CF(table_concat)
{
  luaL_Buffer b;
  GCtab *t = lj_lib_checktab(L, 1);
  GCstr *sep = lj_lib_optstr(L, 2);
  MSize seplen = sep ? sep->len : 0;
  int32_t i = lj_lib_optint(L, 3, 1);
  int32_t e = (L->base+3 < L->top && !tvisnil(L->base+3)) ?
	      lj_lib_checkint(L, 4) : (int32_t)lj_tab_len(t);
  luaL_buffinit(L, &b);
  if (i <= e) {
    for (;;) {
      cTValue *o;
      lua_rawgeti(L, 1, i);
      o = L->top-1;
      if (!(tvisstr(o) || tvisnumber(o)))
	lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i);
      luaL_addvalue(&b);
{'fraction_non_alphanumeric': 0.20189585128647053, 'fraction_numerical': 0.023267265788501783, 'mean_word_length': 2.6977696859353664, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '58818', 'n_tokens_mistral': 3851, 'n_tokens_neox': 3688, 'n_words': 801}