Browse Source

[cpp] Use hybrid code for adding Strings or Chars to StringBuf. Closes https://github.com/HaxeFoundation/hxcpp/issues/239

Hugh 9 years ago
parent
commit
9a3c076118
2 changed files with 20 additions and 5 deletions
  1. 4 0
      std/cpp/NativeString.hx
  2. 16 5
      std/cpp/_std/StringBuf.hx

+ 4 - 0
std/cpp/NativeString.hx

@@ -32,6 +32,10 @@ extern class NativeString {
 	public static inline function fromPointer(inPtr:ConstPointer<Char> ) : String {
 	public static inline function fromPointer(inPtr:ConstPointer<Char> ) : String {
       return untyped __global__.String(inPtr.ptr);
       return untyped __global__.String(inPtr.ptr);
    }
    }
+	public static inline function fromGcPointer(inPtr:ConstPointer<Char>, inLen:Int ) : String {
+      return untyped __global__.String(inPtr.ptr,inLen);
+   }
+
 
 
    @:native("_hx_string_compare")
    @:native("_hx_string_compare")
    public static function compare(inString0:String, inString1:String) : Int return 0;
    public static function compare(inString0:String, inString1:String) : Int return 0;

+ 16 - 5
std/cpp/_std/StringBuf.hx

@@ -19,37 +19,48 @@
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
+import cpp.NativeString;
+using cpp.NativeArray;
+
 @:coreApi
 @:coreApi
 class StringBuf {
 class StringBuf {
-
 	private var b : Array<String>;
 	private var b : Array<String>;
-
 	public var length(get,never) : Int;
 	public var length(get,never) : Int;
+	var charBuf:Array<cpp.Char>;
 
 
 	public function new() : Void {
 	public function new() : Void {
 		b = new Array();
 		b = new Array();
 	}
 	}
 
 
+   private function flush() : Void{
+      b.push( NativeString.fromGcPointer( charBuf.address(0), charBuf.length ) );
+      charBuf = null;
+   }
 	function get_length() : Int {
 	function get_length() : Int {
+		if (charBuf!=null) flush();
 		var len = 0;
 		var len = 0;
 		for(s in b)
 		for(s in b)
 			len += s==null ? 4 : s.length;
 			len += s==null ? 4 : s.length;
 		return len;
 		return len;
 	}
 	}
 
 
-	public function add<T>( x : T ) : Void {
+	public inline function add<T>( x : T ) : Void {
+		if (charBuf!=null) flush();
 		b.push(Std.string(x));
 		b.push(Std.string(x));
 	}
 	}
 
 
 	public inline function addSub( s : String, pos : Int, ?len : Int ) : Void {
 	public inline function addSub( s : String, pos : Int, ?len : Int ) : Void {
+		if (charBuf!=null) flush();
 		b.push(s.substr(pos,len));
 		b.push(s.substr(pos,len));
 	}
 	}
 
 
-	public inline function addChar( c : Int ) : Void untyped {
-		b.push(String.fromCharCode(c));
+	public inline function addChar( c : Int ) : Void {
+		if (charBuf==null) charBuf = new Array<cpp.Char>();
+		charBuf.push(c);
 	}
 	}
 
 
 	public inline function toString() : String {
 	public inline function toString() : String {
+		if (charBuf!=null) flush();
 		return b.join("");
 		return b.join("");
 	}
 	}