Browse Source

StringBuf optimization for js and flash targets

Simon Krajewski 13 years ago
parent
commit
c79632d627
1 changed files with 29 additions and 17 deletions
  1. 29 17
      std/StringBuf.hx

+ 29 - 17
std/StringBuf.hx

@@ -27,13 +27,33 @@
 	A String buffer is an efficient way to build a big string by
 	appending small elements together.
 **/
+#if (js || flash)
+@:native("String")
+extern class StringBuf {
+	public function new():Void {
+		
+	}
+	public inline function add( x : Dynamic ):String {
+		return untyped __this__ += x;
+	}
+	public inline function addChar( i : Int ):String {
+		return untyped __this__ += String.fromCharCode(i);
+	}
+	public inline function addSub( s : String, pos : Int, ?len : Int):String {
+		return untyped __this__ += s.substr(pos, len);
+	}
+	public inline function toString():String {
+		return untyped __this__;
+	}
+}
+#else
 class StringBuf {
 
 	/**
 		Creates a new string buffer.
 	**/
 	public function new() {
-		#if (js || cpp)
+		#if cpp
 			b = new Array();
 		#elseif cs
 			b = new cs.StringBuilder();
@@ -48,9 +68,7 @@ class StringBuf {
 		Adds the representation of any value to the string buffer.
 	**/
 	public inline function add( x : Dynamic ) {
-		#if js
-			b[b.length] = x == null ? 'null' : x;
-		#elseif cpp
+		#if cpp
 			b[b.length] = x;
 		#elseif cs
 			b.Append(x);
@@ -65,12 +83,7 @@ class StringBuf {
 		Adds a part of a string to the string buffer.
 	**/
 	public inline function addSub( s : String, pos : Int, ?len : Int ) {
-		#if flash9
-			if( len == null )
-				b += s.substr(pos);
-			else
-				b += s.substr(pos,len);
-		#elseif (js || cpp)
+		#if cpp
 			b[b.length] = s.substr(pos,len);
 		#elseif cs
 			var l:Int = (len == null) ? (s.length - pos) : len;
@@ -87,10 +100,8 @@ class StringBuf {
 		Adds a character to the string buffer.
 	**/
 	public inline function addChar( c : Int ) untyped {
-		#if (js || cpp)
+		#if cpp
 			b[b.length] = String.fromCharCode(c);
-		#elseif (flash && !flash9)
-			b += String["fromCharCode"](c);
 		#elseif cs
 			b.Append(cast(c, cs.StdTypes.Char16));
 		#elseif java
@@ -105,7 +116,7 @@ class StringBuf {
 		The buffer is not emptied by this operation.
 	**/
 	public inline function toString() : String {
-		#if (js || cpp)
+		#if cpp
 			return b.join("");
 		#elseif cs
 			return b.ToString();
@@ -117,14 +128,15 @@ class StringBuf {
 	}
 
 	private var b :
-	#if (js || cpp)
+	#if cpp
 		Array<Dynamic>
 	#elseif cs 
-	cs.StringBuilder
+		cs.StringBuilder
 	#elseif java
 		java.lang.StringBuilder
 	#else
 		String
 	#end;
 
-}
+}
+#end