void insert(ILuaTable table, ILuaValue pos, ILuaValue value = null) { CheckNotNull("table.insert", table); CheckNotNull("table.insert", pos); double i; double len = table.Length().AsDouble() ?? 0; if (value == null) { value = pos; i = len + 1; } else { i = pos.AsDouble() ?? 0; } if (i > len + 1 || i < 1 || i % 1 != 0) { throw new ArgumentException( "Position given to function 'table.insert' is outside valid range."); } for (double d = len; d >= i; d--) { var temp = table.GetItemRaw(E.Runtime.CreateValue(d)); table.SetItemRaw(E.Runtime.CreateValue(d + 1), temp); } table.SetItemRaw(pos, value); }
string concat(ILuaTable table, string sep = null, int i = 1, int j = -1) { CheckNotNull("table.concat", table); int len = (int)(table.Length().AsDouble() ?? 0); if (i >= len) { return(""); } i = normalizeIndex_(len, i); j = normalizeIndex_(len, j); StringBuilder str = new StringBuilder(); for (; i <= j; i++) { ILuaValue temp = table.GetItemRaw(E.Runtime.CreateValue(i)); if (temp.ValueType != LuaValueType.String && temp.ValueType != LuaValueType.Number) { throw new ArgumentException( "Invalid '" + temp.ValueType + "' value for function 'table.concat'."); } if (str.Length > 0) { str.Append(sep); } str.Append(temp); } return(str.ToString()); }
IEnumerable <ILuaValue> unpack(ILuaTable table, int i = 1, int?jOrNull = null) { CheckNotNull("table.unpack", table); int len = (int)(table.Length().AsDouble() ?? 0); int j = jOrNull ?? len; for (; i <= j; i++) { yield return(table.GetItemRaw(E.Runtime.CreateValue(i))); } }
ILuaValue remove(ILuaTable table, int?pos = null) { CheckNotNull("table.remove", table); double len = table.Length().AsDouble() ?? 0; pos = pos ?? (int)len; if (pos > len + 1 || pos < 1) { throw new ArgumentException( "Position given to function 'table.remove' is outside valid range."); } ILuaValue prev = LuaNil.Nil; for (double d = len; d >= pos; d--) { ILuaValue ind = E.Runtime.CreateValue(d); ILuaValue temp = table.GetItemRaw(ind); table.SetItemRaw(ind, prev); prev = temp; } return(prev); }