lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

On Feb 19, 2014, at 16:48 , Luiz Henrique de Figueiredo wrote:

Actually the C function behaves properly, just that Lua makes no attempt to
determine how much was actually read.

If the data read contains \0 then there is no way to determine how much
was actually read. fgets relies on strlen to determine that.

A patch might be about as simple as scanning for newline as you suggested.

The performance will be the same as the code was already scanning for the
0-terminator.

To avoid scanning for \n and \0 we force terminate, scan for \n, and swap the
last char back. Passes my tests. Performance indistinguishable on this turbo
boosting Intel Core CPU.

Inclusion of something like this would be highly appreciated:

--- lua-5.2.3/src/liolib.c 2013-04-12 18:48:47.000000000 +0000
+++ lua-5.2.3-patched/src/liolib.c 2014-01-22 05:47:35.331037586 +0000
@@ -378,7 +378,13 @@
       luaL_pushresult(&b);  /* close buffer */
       return (lua_rawlen(L, -1) > 0);  /* check whether read something */
     }
-    l = strlen(p);
+    {
+      char t = p[LUAL_BUFFERSIZE - 1]; /* scan for line end */
+      p[LUAL_BUFFERSIZE - 1] = '\n';
+      l = (char*)memchr(p, '\n', LUAL_BUFFERSIZE) - p + 1;
+      p[LUAL_BUFFERSIZE - 1] = t;
+      if (l == LUAL_BUFFERSIZE && t == 0) --l; /* was 0-terminated */
+    }
     if (l == 0 || p[l-1] != '\n')
       luaL_addsize(&b, l);
     else {

René

-- 
 ExactCODE GmbH, Jaegerstr. 67, DE-10117 Berlin
 http://exactcode.com | http://exactscan.com | http://ocrkit.com | http://t2-project.org | http://rene.rebe.de