Skip to content

Commit d4166a4

Browse files
committed
Add: LuaTable merge (#122)
1 parent c60d3a8 commit d4166a4

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

NeoLua/LuaTable.cs

+31-5
Original file line numberDiff line numberDiff line change
@@ -3613,9 +3613,7 @@ public static void insert(LuaTable t, object pos, object value)
36133613
/// <param name="e"></param>
36143614
/// <param name="t"></param>
36153615
public static void move(LuaTable t1, int f, int e, int t)
3616-
{
3617-
move(t1, f, e, t, t1);
3618-
} // proc move
3616+
=> move(t1, f, e, t, t1);
36193617

36203618
/// <summary></summary>
36213619
/// <param name="t1"></param>
@@ -3626,9 +3624,9 @@ public static void move(LuaTable t1, int f, int e, int t)
36263624
public static void move(LuaTable t1, int f, int e, int t, LuaTable t2)
36273625
{
36283626
if (f < 0)
3629-
throw new ArgumentOutOfRangeException("f");
3627+
throw new ArgumentOutOfRangeException(nameof(f));
36303628
if (t < 0)
3631-
throw new ArgumentOutOfRangeException("t");
3629+
throw new ArgumentOutOfRangeException(nameof(t));
36323630
if (f > e)
36333631
return;
36343632

@@ -3638,6 +3636,34 @@ public static void move(LuaTable t1, int f, int e, int t, LuaTable t2)
36383636

36393637
#endregion
36403638

3639+
#region -- merge --
3640+
3641+
/// <summary>merge the second table into the first one.</summary>
3642+
/// <param name="targetTable"></param>
3643+
/// <param name="mergeTable"></param>
3644+
/// <param name="overwrite"></param>
3645+
/// <returns></returns>
3646+
public static LuaTable merge(LuaTable targetTable, LuaTable mergeTable, bool overwrite = true)
3647+
{
3648+
foreach (var kv in mergeTable)
3649+
{
3650+
if (kv.Value is LuaTable m)
3651+
{
3652+
var v = targetTable[kv.Key];
3653+
if (v is LuaTable t)
3654+
merge(t, m, overwrite);
3655+
else if (v == null || (v != null && overwrite))
3656+
targetTable[kv.Key] = merge(new LuaTable(), m, true);
3657+
}
3658+
else if (overwrite || kv.Value == null)
3659+
targetTable[kv.Key] = kv.Value;
3660+
}
3661+
3662+
return targetTable;
3663+
} // proc merge
3664+
3665+
#endregion
3666+
36413667
#region -- pack --
36423668

36433669
/// <summary>Returns a new table with all parameters stored into keys 1, 2, etc. and with a field &quot;n&quot;

0 commit comments

Comments
 (0)