lua-users home
lua-l archive

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


Doug Rogers wrote:
Doug Rogers wrote:

When loading source code the parser can treat any of CR/LF/CR-LF as
newline, but embedded long strings should not be modified.

On reading this I realized that I wasn't clear. I meant that the parser
can treat CR/LF/CR-LF as newlines for the purpose of line number
reporting. But otherwise I think it should take a hands-off approach.

luatex has a patched version liolib.c just for this issue.
The new read_line is a bit slower, but it is line-ending agnostic:

static int read_line (lua_State *L, FILE *f) {
  luaL_Buffer buf;
  int c, d;
  luaL_buffinit(L, &buf);
  while (1) {
    c = fgetc(f);
    if (c == EOF) {
      luaL_pushresult(&buf);  /* close buffer */
      return (lua_strlen(L, -1) > 0);  /* check read something */
    };
    if (c == '\n') {
      break;
    } else if (c == '\r') {
      d = fgetc(f);
      if (d != EOF && d != '\n')
        ungetc(d,f);
      break;
    } else {
      luaL_addchar(&buf,c);
    }
  }
  luaL_pushresult(&buf);  /* close buffer */
  return 1;
}

Best wishes,
Taco