Ver código fonte

[php] optimized increasing array size on writing at out-of-bounds index

Alexander Kuzmenko 7 anos atrás
pai
commit
6554f1a138
2 arquivos alterados com 7 adições e 2 exclusões
  1. 5 0
      std/php/Global.hx
  2. 2 2
      std/php/_std/Array.hx

+ 5 - 0
std/php/Global.hx

@@ -225,6 +225,11 @@ extern class Global {
 	**/
 	static function array_fill( start_index:Int, num:Int, value:Dynamic ) : NativeArray ;
 
+	/**
+		@see http://php.net/manual/en/function.array-pad.php
+	**/
+	static function array_pad( array:NativeArray, size:Int, value:Dynamic ) : NativeArray ;
+
 	/**
 		@see http://php.net/manual/en/function.in-array.php
 	**/

+ 2 - 2
std/php/_std/Array.hx

@@ -192,8 +192,8 @@ class Array<T> implements ArrayAccess<Int,T> {
 
 	@:noCompletion
 	function offsetSet( offset:Int, value:T ) : Void {
-		if (length <= offset) {
-			arr = Global.array_merge(arr, Global.array_fill(0, offset + 1 - length, null));
+		if (length < offset) {
+			arr = Global.array_pad(arr, offset + 1, null);
 			length = offset + 1;
 		}
 		arr[offset] = value;