Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #362 from sdarnell/ArrayClear
Browse files Browse the repository at this point in the history
Implement Array.Clear().
  • Loading branch information
erik-kallen committed Dec 3, 2014
2 parents ade57ec + 9c8b99e commit 69a54d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Runtime/CoreLib.Script/Array.js
Expand Up @@ -194,4 +194,16 @@ ss.repeat = function#? DEBUG ss$repeat##(value, count) {
for (var i = 0; i < count; i++)
result.push(value);
return result;
};
};

ss.arrayFill = function#? DEBUG ss$arrayFill##(dst, val, index, count) {
if (index < 0 || count < 0 || (index + count) > dst.length)
throw new ss_ArgumentException();
if (Array.prototype.fill) {
dst.fill(val, index, index + count);
}
else {
while (--count >= 0)
dst[index + count] = val;
}
};
16 changes: 16 additions & 0 deletions Runtime/CoreLib.TestScript/ArrayTests.cs
Expand Up @@ -378,5 +378,21 @@ private class C {
Assert.AreEqual(Array.Repeat(42, 3), new[] { 42, 42, 42 });
Assert.AreEqual(Array.Repeat("X", 5), new[] { "X", "X", "X", "X", "X" });
}

[Test]
public void ClearWorks() {
var arr1 = new byte[] { 10, 11, 12, 13 };
Array.Clear(arr1, 2, 2);
Assert.AreEqual(arr1, new byte[] { 10, 11, 0, 0 });

var arr2 = new int[] { 10, 11, 12, 13 };
Array.Clear(arr2, 0, 4);
Assert.AreEqual(arr2, new int[] { 0, 0, 0, 0 });

var arr3 = new string[] { "A", "B", "C", "D" };
Array.Clear(arr3, 3, 1);
Assert.AreEqual(arr3, new string[] { "A", "B", "C", null });
}

}
}
4 changes: 4 additions & 0 deletions Runtime/CoreLib/Array.cs
Expand Up @@ -82,5 +82,9 @@ public sealed class Array : IEnumerable {
public static T[] Repeat<T>(T value, int count) {
return null;
}

[InlineCode("{$System.Script}.arrayFill({dst}, {$System.Script}.getDefaultValue({T}), {index}, {count})")]
public static void Clear<T>(T[] dst, int index, int count) {
}
}
}

0 comments on commit 69a54d2

Please sign in to comment.