Skip to content

Commit 4d95089

Browse files
use array.resize for setLength
1 parent 4da7a3e commit 4d95089

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

flixel/util/FlxArrayUtil.hx

+4-18
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,11 @@ class FlxArrayUtil
1111
* @param array The array.
1212
* @param newLength The length you want the array to have.
1313
*/
14-
@:generic
15-
public static function setLength<T>(array:Array<T>, newLength:Int):Array<T>
14+
public static inline function setLength<T>(array:Array<T>, newLength:Int):Array<T>
1615
{
17-
if (newLength < 0)
18-
return array;
19-
20-
var oldLength:Int = array.length;
21-
var diff:Int = newLength - oldLength;
22-
if (diff >= 0)
23-
return array;
24-
25-
#if flash
26-
untyped array.length = newLength;
27-
#else
28-
diff = -diff;
29-
for (i in 0...diff)
30-
array.pop();
31-
#end
32-
16+
if (newLength > 0)
17+
array.resize(newLength);
18+
3319
return array;
3420
}
3521

tests/unit/src/flixel/util/FlxArrayUtilTest.hx

+7
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,11 @@ class FlxArrayUtilTest
102102
Assert.isTrue([0, 1, 2, 3, 4, 5].safeSwap(0, 2).equals([2, 1, 0, 3, 4, 5]));
103103
Assert.isTrue([0, 1, 2, 3, 4, 5].safeSwap(1, 6).equals([0, 1, 2, 3, 4, 5]));
104104
}
105+
106+
@Test
107+
function testSetLength()
108+
{
109+
Assert.isTrue([0, 1, 2, 3, 4, 5].setLength(3).equals([0, 1, 2]));
110+
Assert.isTrue([0, 1, 2].setLength(5).equals([0, 1, 2, 0, 0]));
111+
}
105112
}

0 commit comments

Comments
 (0)