Skip to content

Commit

Permalink
Implement list.Splice() (#2201)
Browse files Browse the repository at this point in the history
Co-authored-by: ike709 <[email protected]>
  • Loading branch information
ike709 and ike709 authored Feb 19, 2025
1 parent b1134c3 commit 5175290
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Content.Tests/DMProject/Tests/List/ListSplice.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/proc/RunTest()
var/list/L = list("foo", "bar", "test", "value")
L.Splice(2, 4, "lorem", "ipsum", "word", "another word")
ASSERT(L ~= list("foo","lorem","ipsum","word","another word","value"))

// Again with list() as the arg
var/list/L2 = list("foo", "bar", "test", "value")
L2.Splice(2, 4, list("lorem", "ipsum", "word", "another word"))
ASSERT(L2 ~= list("foo","lorem","ipsum","word","another word","value"))
4 changes: 1 addition & 3 deletions DMCompiler/DMStandard/Types/List.dm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@
proc/Remove(Item1)
proc/RemoveAll(Item1)
proc/Swap(Index1, Index2)

proc/Splice(Start=1,End=0, ...)
set opendream_unimplemented = TRUE
proc/Splice(Start = 1 as num, End = 0 as num, Item1, ...) as null
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Join);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Remove);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_RemoveAll);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Splice);
objectTree.SetNativeProc(objectTree.List, DreamProcNativeList.NativeProc_Swap);

objectTree.SetNativeProc(objectTree.Matrix, DreamProcNativeMatrix.NativeProc_Add);
Expand Down
30 changes: 30 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ private static int ListRemove(DreamList list, ReadOnlySpan<DreamValue> args) {
return itemRemoved;
}

[DreamProc("Splice")]
[DreamProcParameter("Start", Type = DreamValueTypeFlag.Float, DefaultValue = 1)]
[DreamProcParameter("End", Type = DreamValueTypeFlag.Float, DefaultValue = 0)]
[DreamProcParameter("Item1")]
public static DreamValue NativeProc_Splice(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
int startIndex = bundle.GetArgument(0, "Start").MustGetValueAsInteger(); //1-indexed
int end = bundle.GetArgument(1, "End").MustGetValueAsInteger(); //1-indexed
DreamList list = (DreamList)src!;

list.Cut(startIndex, end);

if (startIndex <= 0) startIndex = list.GetLength() + 1;
if (bundle.Arguments.Length < 3) return DreamValue.Null;

// i = 2 is Item1
for (var i = 2; i < bundle.Arguments.Length; i++) {
var item = bundle.Arguments[i];

if (item.TryGetValueAsDreamList(out var valueList)) {
foreach (DreamValue value in valueList.GetValues()) {
list.Insert(startIndex++, value);
}
} else {
list.Insert(startIndex++, item);
}
}

return DreamValue.Null;
}

[DreamProc("Swap")]
[DreamProcParameter("Index1", Type = DreamValueTypeFlag.Float)]
[DreamProcParameter("Index2", Type = DreamValueTypeFlag.Float)]
Expand Down

0 comments on commit 5175290

Please sign in to comment.