浏览代码

Add an optional parameter to read_line to tell the expected line size as a hint to do memory allocations

mingodad 10 年之前
父节点
当前提交
f2753d185f
共有 1 个文件被更改,包括 5 次插入2 次删除
  1. 5 2
      SquiLu/sqstdlib/sqstdstream.cpp

+ 5 - 2
SquiLu/sqstdlib/sqstdstream.cpp

@@ -23,7 +23,10 @@ SQInteger _stream_read_line(HSQUIRRELVM v) {
     SQChar c = _SC('\0'), nl = _SC('\n');
     SQChar c = _SC('\0'), nl = _SC('\n');
     int m = 0, len = sizeof(SQChar);
     int m = 0, len = sizeof(SQChar);
     SQInteger start_pos = self->Tell();
     SQInteger start_pos = self->Tell();
-    SQInteger read_size = 0, size = 2048;
+    SQInteger read_size = 0, size;
+    //optional expected line size
+    if(sq_gettop(v) > 1) sq_getinteger(v,2,&size);
+    else size = 2048;
     SQChar *buf = sq_getscratchpad(v, size);
     SQChar *buf = sq_getscratchpad(v, size);
     while(!self->EOS() && c != nl){
     while(!self->EOS() && c != nl){
         read_size = self->Read(buf, size);
         read_size = self->Read(buf, size);
@@ -344,7 +347,7 @@ SQInteger _stream_eos(HSQUIRRELVM v)
  }
  }
 
 
 static SQRegFunction _stream_methods[] = {
 static SQRegFunction _stream_methods[] = {
-	_DECL_STREAM_FUNC(read_line,1,_SC("x")),
+	_DECL_STREAM_FUNC(read_line,-1,_SC("xi")),
 	_DECL_STREAM_FUNC(read,2,_SC("xn")),
 	_DECL_STREAM_FUNC(read,2,_SC("xn")),
 	_DECL_STREAM_FUNC(readblob,2,_SC("xn")),
 	_DECL_STREAM_FUNC(readblob,2,_SC("xn")),
 	_DECL_STREAM_FUNC(readn,2,_SC("xn")),
 	_DECL_STREAM_FUNC(readn,2,_SC("xn")),