Browse Source

haxe.io.BytesData now uses strings instead of arrays for PHP

Franco Ponticelli 17 years ago
parent
commit
ed2e6f36ac

+ 1 - 0
doc/CHANGES.txt

@@ -12,6 +12,7 @@
 	switch/for/while/do/try/if are no longer using parse_next (parenthesises requ. instead)
 	switch/for/while/do/try/if are no longer using parse_next (parenthesises requ. instead)
 	fixed Type.typeof and Std.is in case of too much large integers for Flash6-8/JS
 	fixed Type.typeof and Std.is in case of too much large integers for Flash6-8/JS
 	haxe.xml.Check : treat comments the same as PCDATA spaces
 	haxe.xml.Check : treat comments the same as PCDATA spaces
+	haxe.io.BytesData now uses strings instead of arrays for PHP
 
 
 2008-10-04: 2.01
 2008-10-04: 2.01
 	fixed php.Sys
 	fixed php.Sys

+ 25 - 4
std/haxe/io/Bytes.hx

@@ -39,6 +39,8 @@ class Bytes {
 		return untyped __dollar__sget(b,pos);
 		return untyped __dollar__sget(b,pos);
 		#elseif flash9
 		#elseif flash9
 		return b[pos];
 		return b[pos];
+		#elseif php
+		return untyped __call__("ord", b[pos]);
 		#else
 		#else
 		return b[pos];
 		return b[pos];
 		#end
 		#end
@@ -49,6 +51,8 @@ class Bytes {
 		untyped __dollar__sset(b,pos,v);
 		untyped __dollar__sset(b,pos,v);
 		#elseif flash9
 		#elseif flash9
 		b[pos] = v;
 		b[pos] = v;
+		#elseif php
+		b[pos] = untyped __call__("chr", v);
 		#else
 		#else
 		b[pos] = v;
 		b[pos] = v;
 		#end
 		#end
@@ -60,6 +64,9 @@ class Bytes {
 		#end
 		#end
 		#if neko
 		#if neko
 		try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 		try untyped __dollar__sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
+		#elseif php
+		// TODO: test me
+		b = untyped __php__("substr($this->b, 0, $pos) . substr($src->b, $srcpos, $len) . substr($this->b, $pos+$len)"); //__call__("substr", b, 0, pos)+__call__("substr", src.b, srcpos, len)+__call__("substr", b, pos+len);
 		#elseif flash9
 		#elseif flash9
 		b.position = pos;
 		b.position = pos;
 		b.writeBytes(src.b,srcpos,len);
 		b.writeBytes(src.b,srcpos,len);
@@ -91,7 +98,8 @@ class Bytes {
 		b.readBytes(b2,0,len);
 		b.readBytes(b2,0,len);
 		return new Bytes(len,b2);
 		return new Bytes(len,b2);
 		#elseif php
 		#elseif php
-		return new Bytes(len,untyped __call__("new _hx_array", __call__("array_slice", b.__a, pos, len)));
+		// TODO: test me
+		return new Bytes(len, untyped __call__("substr", b, pos, len));
 		#else
 		#else
 		return new Bytes(len,b.slice(pos,pos+len));
 		return new Bytes(len,b.slice(pos,pos+len));
 		#end
 		#end
@@ -116,6 +124,8 @@ class Bytes {
 			if( b1.readUnsignedByte() != b2.readUnsignedByte() )
 			if( b1.readUnsignedByte() != b2.readUnsignedByte() )
 				return b1[b1.position-1] - b2[b2.position-1];
 				return b1[b1.position-1] - b2[b2.position-1];
 		return length - other.length;
 		return length - other.length;
+		#elseif php
+		return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
 		#else
 		#else
 		var b1 = b;
 		var b1 = b;
 		var b2 = other.b;
 		var b2 = other.b;
@@ -137,7 +147,9 @@ class Bytes {
 		b.position = pos;
 		b.position = pos;
 		return b.readUTFBytes(len);
 		return b.readUTFBytes(len);
 		#elseif php
 		#elseif php
-		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.__a, pos, len)));
+		// TODO: test me
+		return untyped __call__("substr", b, pos, len);
+//		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.__a, pos, len)));
 		#else
 		#else
 		var s = "";
 		var s = "";
 		var b = b;
 		var b = b;
@@ -172,7 +184,9 @@ class Bytes {
 		b.position = 0;
 		b.position = 0;
 		return b.readUTFBytes(length);
 		return b.readUTFBytes(length);
 		#elseif php
 		#elseif php
-		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.__a));
+		// TODO: test me
+		return cast b;
+//		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.__a));
 		#else
 		#else
 		return readString(0,length);
 		return readString(0,length);
 		#end
 		#end
@@ -190,10 +204,14 @@ class Bytes {
 		b.length = length;
 		b.length = length;
 		return new Bytes(length,b);
 		return new Bytes(length,b);
 		#elseif php
 		#elseif php
+		// TODO: test me
+		return new Bytes(length, untyped __call__("str_repeat", __call__("chr", 0), length));
+		/*
 		if(length > 0)
 		if(length > 0)
 			return new Bytes(length, untyped __call__("new _hx_array", __call__("array_fill", 0, length, 0)));
 			return new Bytes(length, untyped __call__("new _hx_array", __call__("array_fill", 0, length, 0)));
 		else
 		else
 			return new Bytes(0, untyped __call__("new _hx_array", __call__("array")));
 			return new Bytes(0, untyped __call__("new _hx_array", __call__("array")));
+		*/
 		#else
 		#else
 		var a = new Array();
 		var a = new Array();
 		for( i in 0...length )
 		for( i in 0...length )
@@ -210,7 +228,8 @@ class Bytes {
 		b.writeUTFBytes(s);
 		b.writeUTFBytes(s);
 		return new Bytes(b.length,b);
 		return new Bytes(b.length,b);
 		#elseif php
 		#elseif php
-		return ofData(untyped __call__("new _hx_array", __call__("array_values", __call__("unpack", "C*",  s))));
+		return new Bytes(untyped __call__("strlen", s), cast s);
+//		return ofData(untyped __call__("new _hx_array", __call__("array_values", __call__("unpack", "C*",  s))));
 		#else
 		#else
 		var a = new Array();
 		var a = new Array();
 		// utf8-decode
 		// utf8-decode
@@ -241,6 +260,8 @@ class Bytes {
 		return new Bytes(b.length,b);
 		return new Bytes(b.length,b);
 		#elseif neko
 		#elseif neko
 		return new Bytes(untyped __dollar__ssize(b),b);
 		return new Bytes(untyped __dollar__ssize(b),b);
+		#elseif php
+		return new Bytes(untyped __call__("strlen", b), b);
 		#else
 		#else
 		return new Bytes(b.length,b);
 		return new Bytes(b.length,b);
 		#end
 		#end

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

@@ -30,6 +30,8 @@ class BytesBuffer {
 	var b : Void; // neko string buffer
 	var b : Void; // neko string buffer
 	#elseif flash9
 	#elseif flash9
 	var b : flash.utils.ByteArray;
 	var b : flash.utils.ByteArray;
+	#elseif php
+	var b : String;
 	#else
 	#else
 	var b : Array<Int>;
 	var b : Array<Int>;
 	#end
 	#end
@@ -39,6 +41,8 @@ class BytesBuffer {
 		b = untyped StringBuf.__make();
 		b = untyped StringBuf.__make();
 		#elseif flash9
 		#elseif flash9
 		b = new flash.utils.ByteArray();
 		b = new flash.utils.ByteArray();
+		#elseif php
+		b = "";
 		#else
 		#else
 		b = new Array();
 		b = new Array();
 		#end
 		#end
@@ -49,6 +53,8 @@ class BytesBuffer {
 		untyped StringBuf.__add_char(b,byte);
 		untyped StringBuf.__add_char(b,byte);
 		#elseif flash9
 		#elseif flash9
 		b.writeByte(byte);
 		b.writeByte(byte);
+		#elseif php
+		b += untyped __call__("chr", byte);
 		#else
 		#else
 		b.push(byte);
 		b.push(byte);
 		#end
 		#end
@@ -60,7 +66,8 @@ class BytesBuffer {
 		#elseif flash9
 		#elseif flash9
 		b.writeBytes(src.getData());
 		b.writeBytes(src.getData());
 		#elseif php
 		#elseif php
-		b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, src.getData().__a));
+		b += cast src.getData();
+//		b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, src.getData().__a));
 		#else
 		#else
 		var b1 = b;
 		var b1 = b;
 		var b2 = src.getData();
 		var b2 = src.getData();
@@ -78,11 +85,14 @@ class BytesBuffer {
 		#elseif flash9
 		#elseif flash9
 		b.writeBytes(src.getData(),pos,len);
 		b.writeBytes(src.getData(),pos,len);
 		#elseif php
 		#elseif php
+		b += untyped __call__("substr", src.b, pos, len);
+/*
 		try {
 		try {
 			b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, __call__("array_slice", src.getData().__a, pos, len)));
 			b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, __call__("array_slice", src.getData().__a, pos, len)));
 		} catch(e : Dynamic) {
 		} catch(e : Dynamic) {
 			throw Error.OutsideBounds;
 			throw Error.OutsideBounds;
 		}
 		}
+*/
 		#else
 		#else
 		var b1 = b;
 		var b1 = b;
 		var b2 = src.getData();
 		var b2 = src.getData();
@@ -103,7 +113,7 @@ class BytesBuffer {
 		var bytes = new Bytes(b.length,b);
 		var bytes = new Bytes(b.length,b);
 		b.position = 0;
 		b.position = 0;
 		#elseif php
 		#elseif php
-		var bytes = Bytes.ofData(b);
+		var bytes = new Bytes(b.length, cast b);
 		#else
 		#else
 		var bytes = new Bytes(b.length,b);
 		var bytes = new Bytes(b.length,b);
 		#end
 		#end

+ 2 - 0
std/haxe/io/BytesData.hx

@@ -28,6 +28,8 @@ package haxe.io;
 	typedef BytesData =	neko.NativeString;
 	typedef BytesData =	neko.NativeString;
 #elseif flash9
 #elseif flash9
 	typedef BytesData =	flash.utils.ByteArray;
 	typedef BytesData =	flash.utils.ByteArray;
+#elseif php
+	typedef BytesData =	php.NativeString;
 #else
 #else
 	typedef BytesData = Array<Int>;
 	typedef BytesData = Array<Int>;
 #end
 #end

+ 11 - 1
std/haxe/io/BytesInput.hx

@@ -62,6 +62,8 @@ class BytesInput extends Input {
 			len--;
 			len--;
 			#if neko
 			#if neko
 			return untyped __dollar__sget(b,pos++);
 			return untyped __dollar__sget(b,pos++);
+			#elseif php
+			return untyped __call__("ord", b[pos++]);
 			#else
 			#else
 			return b[pos++];
 			return b[pos++];
 			#end
 			#end
@@ -69,7 +71,10 @@ class BytesInput extends Input {
 	}
 	}
 
 
 	public override function readBytes( buf : Bytes, pos, len ) : Int {
 	public override function readBytes( buf : Bytes, pos, len ) : Int {
-		#if !neko
+		#if php
+			if( pos < 0 || len < 0 || pos + len > untyped __call__("strlen", b))
+				throw Error.OutsideBounds;
+		#elseif !neko
 			if( pos < 0 || len < 0 || pos + len > b.length )
 			if( pos < 0 || len < 0 || pos + len > b.length )
 				throw Error.OutsideBounds;
 				throw Error.OutsideBounds;
 		#end
 		#end
@@ -82,6 +87,11 @@ class BytesInput extends Input {
 				len = this.len;
 				len = this.len;
 			#if neko
 			#if neko
 			try untyped __dollar__sblit(buf.getData(),pos,b,this.pos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 			try untyped __dollar__sblit(buf.getData(),pos,b,this.pos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
+			#elseif php
+			// TODO: test me
+			untyped __php__("$buf->b = substr($buf->b, 0, $pos) . substr($this->b, $this->pos, $len) . substr($buf->b, $pos+$len)"); //__call__("substr", b, 0, pos)+__call__("substr", src.b, srcpos, len)+__call__("substr", b, pos+len);
+//			var b2 = untyped __php__("& $buf->b");
+//			b2 = untyped __call__("substr", b2, 0, pos)+__call__("substr", b, 0, len)+__call__("substr", b2, pos+len);
 			#else
 			#else
 			var b1 = b;
 			var b1 = b;
 			var b2 = buf.getData();
 			var b2 = buf.getData();

+ 2 - 0
std/haxe/io/Input.hx

@@ -44,6 +44,8 @@ class Input {
 		while( k > 0 ) {
 		while( k > 0 ) {
 			#if neko
 			#if neko
 				untyped __dollar__sset(b,pos,readByte());
 				untyped __dollar__sset(b,pos,readByte());
+			#elseif php
+				b[pos] = untyped __call__("chr", readByte());
 			#else
 			#else
 				b[pos] = readByte();
 				b[pos] = readByte();
 			#end
 			#end

+ 2 - 0
std/haxe/io/Output.hx

@@ -48,6 +48,8 @@ class Output {
 		while( k > 0 ) {
 		while( k > 0 ) {
 			#if neko
 			#if neko
 				writeByte(untyped __dollar__sget(b,pos));
 				writeByte(untyped __dollar__sget(b,pos));
+			#elseif php
+				writeByte(untyped __call__("ord", b[pos]));
 			#else
 			#else
 				writeByte(b[pos]);
 				writeByte(b[pos]);
 			#end
 			#end

+ 1 - 1
std/php/NativeArray.hx

@@ -1,5 +1,5 @@
 package php;
 package php;
 
 
-extern class NativeArray {
+extern class NativeArray implements ArrayAccess<Int> {
 
 
 }
 }

+ 5 - 0
std/php/NativeString.hx

@@ -0,0 +1,5 @@
+package php;
+
+extern class NativeString implements ArrayAccess<Int> {
+
+}