Bladeren bron

separate StringBuf per-platform, default implementation use no-overhead String concat

Nicolas Cannasse 13 jaren geleden
bovenliggende
commit
2c631b45e2
4 gewijzigde bestanden met toevoegingen van 163 en 82 verwijderingen
  1. 8 82
      std/StringBuf.hx
  2. 51 0
      std/cpp/_std/StringBuf.hx
  3. 52 0
      std/cs/_std/StringBuf.hx
  4. 52 0
      std/java/_std/StringBuf.hx

+ 8 - 82
std/StringBuf.hx

@@ -27,88 +27,34 @@
 	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 ):Void {
-		untyped __this__ += x;
-	}
-	public inline function addChar( i : Int ):Void {
-		untyped __this__ += String.fromCharCode(i);
-	}
-	public inline function addSub( s : String, pos : Int, ?len : Int):Void {
-		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 cpp
-			b = new Array();
-		#elseif cs
-			b = new cs.StringBuilder();
-		#elseif java
-			b = new java.lang.StringBuilder();
-		#else
-			b = "";
-		#end
+	public function new():Void {
 	}
 
 	/**
 		Adds the representation of any value to the string buffer.
 	**/
-	public inline function add( x : Dynamic ) {
-		#if cpp
-			b[b.length] = x;
-		#elseif cs
-			b.Append(x);
-		#elseif java
-			b.append(x);
-		#else
-			b += x;
-		#end
+	public inline function add( x : Dynamic ) : Void {
+		untyped __this__ += x;
 	}
 
 	/**
 		Adds a part of a string to the string buffer.
 	**/
-	public inline function addSub( s : String, pos : Int, ?len : Int ) {
-		#if cpp
-			b[b.length] = s.substr(pos,len);
-		#elseif cs
-			var l:Int = (len == null) ? (s.length - pos) : len;
-			b.Append(s, pos, l);
-		#elseif java
-			var l:Int = (len == null) ? s.length : len;
-			b.append(s, pos, pos + l);
-		#else
-			b += s.substr(pos,len);
-		#end
+	public inline function addChar( c : Int ) : Void {
+		untyped __this__ += String.fromCharCode(c);
 	}
 
 	/**
 		Adds a character to the string buffer.
 	**/
-	public inline function addChar( c : Int ) untyped {
-		#if cpp
-			b[b.length] = String.fromCharCode(c);
-		#elseif cs
-			b.Append(cast(c, cs.StdTypes.Char16));
-		#elseif java
-			b.append(cast(c, java.StdTypes.Char16));
-		#else
-			b += String.fromCharCode(c);
-		#end
+	public inline function addSub( s : String, pos : Int, ?len : Int) : Void {
+		untyped __this__ += s.substr(pos, len);
 	}
 
 	/**
@@ -116,27 +62,7 @@ class StringBuf {
 		The buffer is not emptied by this operation.
 	**/
 	public inline function toString() : String {
-		#if cpp
-			return b.join("");
-		#elseif cs
-			return b.ToString();
-		#elseif java
-			return b.toString();
-		#else
-			return b;
-		#end
+		return untyped __this__;
 	}
 
-	private var b :
-	#if cpp
-		Array<Dynamic>
-	#elseif cs 
-		cs.StringBuilder
-	#elseif java
-		java.lang.StringBuilder
-	#else
-		String
-	#end;
-
 }
-#end

+ 51 - 0
std/cpp/_std/StringBuf.hx

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2005-2012, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api
+class StringBuf {
+
+	private var b : Array<Dynamic>;
+
+	public function new() : Void {
+		b = new Array();
+	}
+
+	public inline function add( x : Dynamic ) : Void {
+		b[b.length] = x;
+	}
+
+	public inline function addSub( s : String, pos : Int, ?len : Int ) : Void {
+		b[b.length] = s.substr(pos,len);
+	}
+
+	public inline function addChar( c : Int ) : Void untyped {
+		b[b.length] = String.fromCharCode(c);
+	}
+
+	public inline function toString() : String {
+		return b.join("");
+	}
+
+}

+ 52 - 0
std/cs/_std/StringBuf.hx

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2005-2012, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api
+class StringBuf {
+
+	private var b : cs.StringBuilder;
+
+	public function new() : Void {
+		b = new cs.StringBuilder();
+	}
+
+	public function add( x : Dynamic ) : Void {
+		b.Append(x);
+	}
+
+	public function addSub( s : String, pos : Int, ?len : Int ) : Void {
+		var l:Int = (len == null) ? (s.length - pos) : len;
+		b.Append(s, pos, l);
+	}
+
+	public function addChar( c : Int ) : Void untyped {
+		b.Append(cast(c, cs.StdTypes.Char16));
+	}
+
+	public function toString() : String {
+		return b.ToString();
+	}
+
+}

+ 52 - 0
std/java/_std/StringBuf.hx

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2005-2012, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api
+class StringBuf {
+
+	private var b : java.lang.StringBuilder;
+
+	public function new() : Void {
+		b = new java.lang.StringBuilder();
+	}
+
+	public function add( x : Dynamic ) : Void {
+		b.append(x);
+	}
+
+	public function addSub( s : String, pos : Int, ?len : Int ) : Void {
+		var l:Int = (len == null) ? s.length : len;
+		b.append(s, pos, pos + l);
+	}
+
+	public function addChar( c : Int ) : Void untyped {
+		b.append(cast(c, java.StdTypes.Char16));
+	}
+
+	public function toString() : String {
+		return b.toString();
+	}
+
+}