Browse Source

[php] use interpolation for BytesData and inline everything

frabbit 10 years ago
parent
commit
ca5436e05b
1 changed files with 40 additions and 31 deletions
  1. 40 31
      std/php/BytesData.hx

+ 40 - 31
std/php/BytesData.hx

@@ -1,64 +1,73 @@
 package php;
 
-class BytesDataWrapper {
-	public var s : php.NativeString;
-	public inline function new (s:php.NativeString) { this.s = s;}
-}
 
-abstract BytesData(BytesDataWrapper) {
 
-	inline function new (x:BytesDataWrapper) this = x;
+private class Wrapper {
+	public var s : NativeString;
+	public inline function new (s:NativeString) {
+		this.s = s;
+	}
+}
+
+abstract BytesData(Wrapper) {
 
-	inline function str () return this.s;
-	inline function raw () return this;
+	inline function new (x:Wrapper) this = x;
 
-	public var length(get, never):Int;
+	inline function str ():php.NativeString return this.s;
 
-	public inline function compare (other:BytesData):Int {
-		var x = str();
-		var y = other.raw().s;
-		return untyped __php__("$x < $y ? -1 : ($x == $y ? 0 : 1)");
+	inline function setNativeString (val:NativeString):Void {
+		this.s = val;
 	}
 
-	public inline function get_length ():Int {
+	inline function get_length ():Int {
 		return untyped __call__("strlen", str());
 	}
 
+	static inline function wrap (s:NativeString):Wrapper {
+		return new Wrapper(s);
+	}
+
+	static inline function ofNativeString (s:NativeString) {
+		return new BytesData( wrap(s));
+	}
+
+	public inline function set (index:Int, val:Int):Void {
+		untyped __php__("{0}->s[{1}] = chr({2})", this, index, val);
+	}
+
+	public var length(get, never):Int;
+
+	public inline function compare (other:BytesData):Int {
+		return untyped __php__("{0} < {1} ? -1 : ({0} == {1} ? 0 : 1)", str(), other.str());
+	}
+
 	public inline function get (pos:Int):Int {
 		return untyped __call__("ord", str()[pos]);
 	}
 
 	public inline function copy ():BytesData {
-		return new BytesData(new BytesDataWrapper(this.s));
+		return ofNativeString(str());
 	}
 
-	public  function getString (pos:Int, len:Int):String {
+	public inline function getString (pos:Int, len:Int):String {
 		return untyped __call__("substr", str(), pos, len);
 	}
 
-	public  function sub (pos:Int, len:Int):BytesData {
+	public inline function sub (pos:Int, len:Int):BytesData {
 		return ofString(untyped __call__("substr", str(), pos, len));
 	}
 
-	public function blit (pos : Int, src : BytesData, srcpos : Int, len : Int) {
-		var x = str();
-		var y = src.str();
-		this.s = untyped __php__("substr($x, 0, $pos) . substr($y, $srcpos, $len) . substr($x, $pos+$len)");
+	public inline function blit (pos : Int, src : BytesData, srcpos : Int, len : Int):Void {
+		setNativeString(untyped __php__("substr({0}, 0, {2}) . substr({1}, {3}, {4}) . substr({0}, {2}+{4})", str(), src.str(), pos, srcpos, len));
 	}
 
-	public inline function toString():String return cast this.s;
-
-	public function set (index:Int, val:Int):Void {
-		var x = this;
-		untyped __php__("$x->s[$index] = chr($val)");
-	}
-
-
+	public inline function toString():String return cast str();
 
 	public static inline function ofString (s:String) {
-		return new BytesData( new BytesDataWrapper(cast s));
+		return ofNativeString(cast s);
 	}
+
 	public static inline function alloc (length:Int) {
-		return new BytesData(new BytesDataWrapper(untyped __call__("str_repeat", __call__("chr", 0), length)));
+		return ofNativeString(untyped __call__("str_repeat", __call__("chr", 0), length));
 	}
 }