mirror of https://github.com/rusefi/lua.git
"read" more efficient when reading lines and whole files ('.*')
This commit is contained in:
parent
02a6891939
commit
bdb1db4d37
121
liolib.c
121
liolib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: liolib.c,v 1.17 1998/03/24 20:14:25 roberto Exp roberto $
|
** $Id: liolib.c,v 1.18 1998/05/20 22:21:35 roberto Exp roberto $
|
||||||
** Standard I/O (and system) library
|
** Standard I/O (and system) library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -180,60 +180,81 @@ static void io_appendto (void)
|
||||||
|
|
||||||
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
||||||
|
|
||||||
static void io_read (void)
|
|
||||||
{
|
static void read_until (FILE *f, int lim) {
|
||||||
|
int l = 0;
|
||||||
|
int c;
|
||||||
|
for (c = getc(f); c != EOF && c != lim; c = getc(f)) {
|
||||||
|
luaL_addchar(c);
|
||||||
|
l++;
|
||||||
|
}
|
||||||
|
if (l > 0 || c == lim) /* read anything? */
|
||||||
|
lua_pushlstring(luaL_buffer(), l);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void io_read (void) {
|
||||||
int arg = FIRSTARG;
|
int arg = FIRSTARG;
|
||||||
FILE *f = getfileparam(FINPUT, &arg);
|
FILE *f = getfileparam(FINPUT, &arg);
|
||||||
int l;
|
char *p = luaL_opt_string(arg, NULL);
|
||||||
char *p = luaL_opt_string(arg, "[^\n]*{\n}");
|
|
||||||
int inskip = 0; /* to control {skips} */
|
|
||||||
int c = NEED_OTHER;
|
|
||||||
luaL_resetbuffer();
|
luaL_resetbuffer();
|
||||||
while (*p) {
|
if (p == NULL) /* default: read a line */
|
||||||
if (*p == '{') {
|
read_until(f, '\n');
|
||||||
inskip++;
|
else if (p[0] == '.' && p[1] == '*' && p[2] == 0) /* p = ".*" */
|
||||||
p++;
|
read_until(f, EOF);
|
||||||
}
|
else {
|
||||||
else if (*p == '}') {
|
int l = 0; /* number of chars read in buffer */
|
||||||
if (inskip == 0)
|
int inskip = 0; /* to control {skips} */
|
||||||
lua_error("unbalanced braces in read pattern");
|
int c = NEED_OTHER;
|
||||||
inskip--;
|
while (*p) {
|
||||||
p++;
|
switch (*p) {
|
||||||
}
|
case '{':
|
||||||
else {
|
inskip++;
|
||||||
char *ep; /* get what is next */
|
p++;
|
||||||
int m; /* match result */
|
continue;
|
||||||
if (c == NEED_OTHER) c = getc(f);
|
case '}':
|
||||||
if (c == EOF) {
|
if (inskip == 0)
|
||||||
luaI_singlematch(0, p, &ep); /* to set "ep" */
|
lua_error("unbalanced braces in read pattern");
|
||||||
m = 0;
|
inskip--;
|
||||||
}
|
p++;
|
||||||
else {
|
continue;
|
||||||
m = luaI_singlematch(c, p, &ep);
|
default: {
|
||||||
if (m) {
|
char *ep; /* get what is next */
|
||||||
if (inskip == 0) luaL_addchar(c);
|
int m; /* match result */
|
||||||
c = NEED_OTHER;
|
if (c == NEED_OTHER) c = getc(f);
|
||||||
|
if (c == EOF) {
|
||||||
|
luaI_singlematch(0, p, &ep); /* to set "ep" */
|
||||||
|
m = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
m = luaI_singlematch(c, p, &ep);
|
||||||
|
if (m) {
|
||||||
|
if (inskip == 0) {
|
||||||
|
luaL_addchar(c);
|
||||||
|
l++;
|
||||||
|
}
|
||||||
|
c = NEED_OTHER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (*ep) {
|
||||||
|
case '*': /* repetition */
|
||||||
|
if (!m) p = ep+1; /* else stay in (repeat) the same item */
|
||||||
|
continue;
|
||||||
|
case '?': /* optional */
|
||||||
|
p = ep+1; /* continues reading the pattern */
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
if (m) p = ep; /* continues reading the pattern */
|
||||||
|
else
|
||||||
|
goto break_while; /* pattern fails */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (*ep) {
|
} break_while:
|
||||||
case '*': /* repetition */
|
if (c >= 0) /* not EOF nor NEED_OTHER? */
|
||||||
if (!m) p = ep+1; /* else stay in (repeat) the same item */
|
ungetc(c, f);
|
||||||
break;
|
if (l > 0 || *p == 0) /* read something or did not fail? */
|
||||||
case '?': /* optional */
|
lua_pushlstring(luaL_buffer(), l);
|
||||||
p = ep+1; /* continues reading the pattern */
|
}
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (m) p = ep; /* continues reading the pattern */
|
|
||||||
else
|
|
||||||
goto break_while; /* pattern fails */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} break_while:
|
|
||||||
if (c >= 0) /* not EOF nor NEED_OTHER? */
|
|
||||||
ungetc(c, f);
|
|
||||||
l = luaL_getsize();
|
|
||||||
if (l > 0 || *p == 0) /* read something or did not fail? */
|
|
||||||
lua_pushlstring(luaL_buffer(), l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue