Pārlūkot izejas kodu

added stringInput, use neko.Lib.string/bytesReference

Nicolas Cannasse 17 gadi atpakaļ
vecāks
revīzija
f68d795004

+ 3 - 3
std/haxe/Http.hx

@@ -235,7 +235,7 @@ class Http {
 		}
 		customRequest(post,output);
 		if( !err )
-			me.onData(new String(cast output.getBytes().getData()));
+			me.onData(neko.Lib.stringReference(output.getBytes()));
 	#end
 	}
 
@@ -450,7 +450,7 @@ class Http {
 				}
 			}
 		}
-		var headers = new String(cast b.getBytes().getData()).split("\r\n");
+		var headers = neko.Lib.stringReference(b.getBytes()).split("\r\n");
 		var response = headers.shift();
 		var rp = response.split(" ");
 		var status = Std.parseInt(rp[1]);
@@ -527,7 +527,7 @@ class Http {
 				len += chunk_buf.length;
 				chunk_buf = null;
 			}
-			if( chunk_re.match(new String(cast buf.getData())) ) {
+			if( chunk_re.match(neko.Lib.stringReference(buf)) ) {
 				var p = chunk_re.matchedPos();
 				if( p.len <= len ) {
 					var cstr = chunk_re.matched(1);

+ 1 - 0
std/haxe/ImportAll.hx

@@ -70,6 +70,7 @@ import haxe.io.Eof;
 import haxe.io.Error;
 import haxe.io.Input;
 import haxe.io.Output;
+import haxe.io.StringInput;
 
 import haxe.remoting.Connection;
 import haxe.remoting.AsyncConnection;

+ 1 - 1
std/haxe/Serializer.hx

@@ -246,7 +246,7 @@ class Serializer {
 			case cast haxe.io.Bytes:
 				var v : haxe.io.Bytes = v;
 				#if neko
-				var chars = StringTools.baseEncode(new String(cast v.getData()),BASE64);
+				var chars = StringTools.baseEncode(neko.Lib.stringReference(v),BASE64);
 				#else
 				var i = 0;
 				var max = v.length - 2;

+ 2 - 2
std/haxe/Unserializer.hx

@@ -282,8 +282,8 @@ class Unserializer {
  			if( buf.charAt(pos++) != ":" || length - pos < len )
 				throw "Invalid bytes length";
 			#if neko
-			var str =  StringTools.baseDecode(buf.substr(pos,len),BASE64);
-			var bytes = untyped new haxe.io.Bytes(str.length,str.__s);
+			var str = StringTools.baseDecode(buf.substr(pos,len),BASE64);
+			var bytes = neko.Lib.bytesReference(str);
 			#else
 			var codes = CODES;
 			if( codes == null ) {

+ 1 - 1
std/haxe/io/Input.hx

@@ -220,7 +220,7 @@ class Input {
 		var b = Bytes.alloc(len);
 		readFullBytes(b,0,len);
 		#if neko
-		return new String(cast b.getData());
+		return neko.Lib.stringReference(b);
 		#else
 		return b.toString();
 		#end

+ 38 - 0
std/haxe/io/StringInput.hx

@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2005-2008, 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.
+ */
+package haxe.io;
+
+class StringInput extends BytesInput {
+
+	public function new( s : String ) {
+		#if neko
+		// don't copy the string
+		super( neko.Lib.bytesReference(s) );
+		#else
+		super( haxe.io.Bytes.ofString(s) );
+		#end
+	}
+
+}

+ 14 - 0
std/neko/Lib.hx

@@ -166,6 +166,20 @@ class Lib {
 		return untyped neko.Boot.__classes;
 	}
 
+	/**
+		Returns a string referencing the data contains in bytes.
+	**/
+	public inline static function stringReference( b : haxe.io.Bytes ) {
+		return new String( cast b.getData() );
+	}
+
+	/**
+		Returns bytes referencing the content of a string.
+	**/
+	public inline static function bytesReference( s : String ) : haxe.io.Bytes {
+		return untyped new haxe.io.Bytes( s.length, s.__s );
+	}
+
 	static var __serialize = load("std","serialize",1);
 	static var __unserialize = load("std","unserialize",2);
 

+ 2 - 2
std/neko/Web.hx

@@ -243,7 +243,7 @@ class Web {
 		var curname = null;
 		parseMultipart(function(p,_) {
 			if( curname != null )
-				h.set(curname,new String(cast buf.getBytes().getData()));
+				h.set(curname,neko.Lib.stringReference(buf.getBytes()));
 			curname = p;
 			buf = new haxe.io.BytesBuffer();
 			maxSize -= p.length;
@@ -256,7 +256,7 @@ class Web {
 			buf.addBytes(str,pos,len);
 		});
 		if( curname != null )
-			h.set(curname,new String(cast buf.getBytes().getData()));
+			h.set(curname,neko.Lib.stringReference(buf.getBytes()));
 		return h;
 	}
 

+ 1 - 1
std/neko/io/File.hx

@@ -43,7 +43,7 @@ class File {
 	}
 
 	public static function getBytes( path : String ) {
-		return haxe.io.Bytes.ofString(getContent(path));
+		return neko.Lib.bytesReference(getContent(path));
 	}
 
 	public static function read( path : String, binary : Bool ) {