Przeglądaj źródła

Add test for constructing a utf8 string with a (reserved) size. Add native array reserve function for cpp. Closes #5211

Hugh 9 lat temu
rodzic
commit
c1f7197d8f

+ 3 - 0
std/cpp/NativeArray.hx

@@ -33,6 +33,9 @@ extern class NativeArray {
       return untyped inArray;
    }
 
+   @:nativeStaticExtension
+	public static function reserve<T>( inArray:Array<T>,inElements:Int ) : Void { }
+
 	public static inline function address<T>( inArray:Array<T>,inIndex:Int ) : Pointer<T> {
       return Pointer.arrayElem(inArray,inIndex);
    }

+ 1 - 1
std/cpp/_std/haxe/Utf8.hx

@@ -31,7 +31,7 @@ class Utf8
 	public function new( ?size : Null<Int> ) : Void {
       __s = new Array<Int>();
       if (size!=null && size>0)
-         __s[size-1] = 0;
+         cpp.NativeArray.reserve(__s,size);
 	}
 
 	public function addChar( c : Int ) : Void {

+ 2 - 0
tests/unit/src/unitstd/haxe/Utf8.unit.hx

@@ -30,6 +30,8 @@ haxe.Utf8.length(str) == 3;
 haxe.Utf8.charCodeAt(str, 0) == 0x3042;
 haxe.Utf8.charCodeAt(str, 1) == 0xE9;
 haxe.Utf8.charCodeAt(str, 2) == 0x3044;
+var big = new haxe.Utf8(10);
+big.toString().length == 0;
 var buf = new haxe.Utf8();
 buf.addChar(0x3042);
 buf.addChar(0xE9);