lua-users home
lua-l archive

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


Hi,

On Feb 21, 2014, at 10:27 , Tom N Harris wrote:

On Friday, February 21, 2014 09:20:35 AM René Rebe wrote:
There is no corner case left. At EOF without EOL the string is 0 terminated.
My last patch checks for if the stream reached EOF and removes the
superfluous \0\n.

I realised after I sent the mail that it was less hairy than I had assumed.
But the way to do it is much simpler than your patch. Just use memrchr instead
of memchr to find the terminating NULL. The read_line function then becomes:

## SNIP ##
   memset(p, '\n', LUAL_BUFFERSIZE);  /* could be any non-NULL character */
   if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) {  /* eof? */
     luaL_pushresult(&b);  /* close buffer */
     return (lua_rawlen(L, -1) > 0);  /* check whether read something */
   }
   l = (char*)memrchr(p, '\0', LUAL_BUFFERSIZE) - p;
## SNIP ##

1 insertion and 1 changed line. Though I get a warning that I think must be a
bug in GCC. (It's trying to tell me memrchr returns an int.)

Yes, but beside not being C89 I guess, this has worse performance for average sized lines as it scans more data for small lines. Which is also why I decreased the buffer for my patch to 82 bytes.

This could however be combined with a open coded reverse loop and the smaller buffer size to keep the code structure more like it currently is.

On Friday, February 21, 2014 10:48:00 AM Dirk Laurie wrote:
2. Anybody, even the OP, thinking that the Lua developers
will do this?

I don't recall anyone from PUC-Rio saying they *wouldn't* make the change.

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