Parcourir la source

[python] use bytearray for haxe.io.BytesBuffer

Dan Korostelev il y a 6 ans
Parent
commit
f486ce7885
2 fichiers modifiés avec 18 ajouts et 3 suppressions
  1. 13 2
      std/haxe/io/BytesBuffer.hx
  2. 5 1
      std/python/Bytearray.hx

+ 13 - 2
std/haxe/io/BytesBuffer.hx

@@ -33,6 +33,8 @@ class BytesBuffer {
 	var b : cs.system.io.MemoryStream;
 	#elseif java
 	var b : java.io.ByteArrayOutputStream;
+	#elseif python
+	var b : python.Bytearray;
 	#else
 	var b : Array<Int>;
 	#end
@@ -52,6 +54,8 @@ class BytesBuffer {
 		b = new cs.system.io.MemoryStream();
 		#elseif java
 		b = new java.io.ByteArrayOutputStream();
+		#elseif python
+		b = new python.Bytearray();
 		#else
 		b = new Array();
 		#end
@@ -80,6 +84,8 @@ class BytesBuffer {
 		b.WriteByte(cast byte);
 		#elseif java
 		b.write(byte);
+		#elseif python
+		b.append(byte);
 		#else
 		b.push(byte);
 		#end
@@ -99,6 +105,8 @@ class BytesBuffer {
 		var b2 = @:privateAccess src.b;
 		for( i in 0...src.length )
 			b.push(b2[i]);
+		#elseif python
+		b.extend(src.getData());
 		#else
 		var b1 = b;
 		var b2 = src.getData();
@@ -112,6 +120,8 @@ class BytesBuffer {
 		untyped StringBuf.__add(b, v.__s);
 		#elseif flash
 		if( encoding == RawNative ) b.writeMultiByte(v, "unicode") else b.writeUTFBytes(v);
+		#elseif python
+		b.extend(new python.Bytearray(v, "UTF-8"));
 		#else
 		add(Bytes.ofString(v,encoding));
 		#end
@@ -166,6 +176,8 @@ class BytesBuffer {
 		var b2 = @:privateAccess src.b;
 		for( i in pos...pos+len )
 			b.push(b2[i]);
+		#elseif python
+		b.extend(python.Syntax.code("{0}[{1}:{2}]", src.getData(), pos, pos + len));
 		#else
 		var b1 = b;
 		var b2 = src.getData();
@@ -192,8 +204,7 @@ class BytesBuffer {
 		var buf = b.toByteArray();
 		var bytes = new Bytes(buf.length, buf);
 		#elseif python
-		var buf = new python.Bytearray(b);
-		var bytes = new Bytes(buf.length, buf);
+		var bytes = new Bytes(b.length, b);
 		#elseif js
 		var bytes = new Bytes(new js.lib.Uint8Array(b).buffer);
 		#else

+ 5 - 1
std/python/Bytearray.hx

@@ -28,15 +28,19 @@ extern class Bytearray implements ArrayAccess<Int> {
 
 	public var length(get,never):Int;
 
+	@:overload(function ():Void {})
 	@:overload(function (it:Array<Int>):Void {})
 	@:overload(function (it:NativeIterable<Int>):Void {})
 	@:overload(function (size:Int):Void {})
 	public function new (source:String,encoding:String,?errors:Dynamic):Void;
 
-	inline function get_length ():Int {
+	private inline function get_length ():Int {
 		return python.internal.UBuiltins.len(this);
 	}
 
+	function append(x:Int):Void;
+	function extend(t:Bytearray):Void;
+
 	public inline function get(i:Int):Int {
 		return Syntax.arrayAccess(this, i);
 	}