lua-users home
lua-l archive

Re: io:lines() and 0円

[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.c2013年04月12日 18:48:47.000000000 +0000
+++ lua-5.2.3-patched/src/liolib.c2014年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é


AltStyle によって変換されたページ (->オリジナル) /