Skip to main content

tmove

Function tmove 

Source
pub fn tmove(state: &mut LuaState) -> Result<usize, LuaError>
Expand description

Copies elements a1[f..e] into a2[t..] (or a1[t..] if a2 is absent). Copies in increasing order when safe, decreasing when ranges overlap.

static int tmove (lua_State *L) {
  lua_Integer f = luaL_checkinteger(L, 2);
  lua_Integer e = luaL_checkinteger(L, 3);
  lua_Integer t = luaL_checkinteger(L, 4);
  int tt = !lua_isnoneornil(L, 5) ? 5 : 1;
  checktab(L, 1, TAB_R);
  checktab(L, tt, TAB_W);
  if (e >= f) {
    lua_Integer n, i;
    luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, "too many elements to move");
    n = e - f + 1;
    luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around");
    if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
      for (i = 0; i < n; i++) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); }
    } else {
      for (i = n - 1; i >= 0; i--) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); }
    }
  }
  lua_pushvalue(L, tt);
  return 1;
}