Procházet zdrojové kódy

StringBuf, faster length, cleanup

frabbit před 11 roky
rodič
revize
a7c7de90ae

+ 12 - 7
std/python/_std/StringBuf.hx

@@ -21,6 +21,7 @@
  */
 
 import python.lib.Builtin;
+import python.lib.io.IOBase.SeekSet;
 import python.lib.io.StringIO;
 
 
@@ -47,15 +48,19 @@ class StringBuf {
 
 	private var b : StringIO;
 
-	public function new():Void {
+	public inline function new():Void {
 		this.b = new StringIO();
 	}
 
 	public var length(get, never):Int;
 
-	public inline function get_length ():Int {
-		// TODO improve implementation
-		return toString().length;
+	public function get_length ():Int {
+		// TODO improve implementation like f.seek(0, 2) size = f.tell() f.seek(0)
+		var pos = b.tell();
+		b.seek(0, SeekSet.SeekEnd);
+		var len = b.tell();
+		b.seek(pos, SeekSet.SeekSet);
+		return len;
 	}
 
 	/**
@@ -75,7 +80,7 @@ class StringBuf {
 		b.write(s);
 	}
 
-/**
+	/**
 		Appends the character identified by [c] to [this] StringBuf.
 
 		If [c] is negative or has another invalid value, the result is
@@ -85,7 +90,7 @@ class StringBuf {
 		add1(String.fromCharCode(c));
 	}
 
-/**
+	/**
 		Appends a substring of [s] to [this] StringBuf.
 
 		This function expects [pos] and [len] to describe a valid substring of
@@ -101,7 +106,7 @@ class StringBuf {
 		add1((len == null ? s.substr(pos) : s.substr(pos, len)));
 	}
 
-/**
+	/**
 		Returns the content of [this] StringBuf as String.
 
 		The buffer is not emptied by this operation.

+ 1 - 1
std/python/io/NativeInput.hx

@@ -49,7 +49,7 @@ class NativeInput extends Input{
 	{
 		var pos = switch(pos)
 		{
-			case SeekBegin: SeekSet.SeekStart;
+			case SeekBegin: SeekSet.SeekSet;
 			case SeekCur: SeekSet.SeekCur;
 			case SeekEnd: SeekSet.SeekEnd;
 		};

+ 18 - 1
std/python/io/NativeOutput.hx

@@ -39,7 +39,7 @@ class NativeOutput extends Output{
 	{
 		var pos = switch(pos)
 		{
-			case SeekBegin: SeekSet.SeekStart;
+			case SeekBegin: SeekSet.SeekSet;
 			case SeekCur: SeekSet.SeekCur;
 			case SeekEnd: SeekSet.SeekEnd;
 		};
@@ -60,4 +60,21 @@ class NativeOutput extends Output{
 	{
 		stream.write(Builtin.bytearray([c]));
 	}
+
+	/*
+	** TODO not sure if this implementation is working
+
+	override public function writeBytes( s : haxe.io.Bytes, pos : Int, len : Int ) : Int
+	{
+		if( pos < 0 || len < 0 || pos + len > s.length )
+			throw haxe.io.Error.OutsideBounds;
+
+		var ba = s.sub(pos, len).getData();
+
+		var ret = stream.write(ba);
+
+		return ret;
+	}
+	*/
+
 }

+ 1 - 1
std/python/lib/io/IOBase.hx

@@ -2,7 +2,7 @@
 package python.lib.io;
 
 @:enum abstract SeekSet(Int) {
-	var SeekStart = 0;
+	var SeekSet = 0;
 	var SeekCur = 1;
 	var SeekEnd = 2;
 }