"read" more efficient when reading lines and whole files ('.*')

This commit is contained in:
Roberto Ierusalimschy
1998-06-02 18:20:54 -03:00
parent 02a6891939
commit bdb1db4d37

View File

@@ -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,27 +180,44 @@ 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}"); luaL_resetbuffer();
if (p == NULL) /* default: read a line */
read_until(f, '\n');
else if (p[0] == '.' && p[1] == '*' && p[2] == 0) /* p = ".*" */
read_until(f, EOF);
else {
int l = 0; /* number of chars read in buffer */
int inskip = 0; /* to control {skips} */ int inskip = 0; /* to control {skips} */
int c = NEED_OTHER; int c = NEED_OTHER;
luaL_resetbuffer();
while (*p) { while (*p) {
if (*p == '{') { switch (*p) {
case '{':
inskip++; inskip++;
p++; p++;
} continue;
else if (*p == '}') { case '}':
if (inskip == 0) if (inskip == 0)
lua_error("unbalanced braces in read pattern"); lua_error("unbalanced braces in read pattern");
inskip--; inskip--;
p++; p++;
} continue;
else { default: {
char *ep; /* get what is next */ char *ep; /* get what is next */
int m; /* match result */ int m; /* match result */
if (c == NEED_OTHER) c = getc(f); if (c == NEED_OTHER) c = getc(f);
@@ -211,30 +228,34 @@ static void io_read (void)
else { else {
m = luaI_singlematch(c, p, &ep); m = luaI_singlematch(c, p, &ep);
if (m) { if (m) {
if (inskip == 0) luaL_addchar(c); if (inskip == 0) {
luaL_addchar(c);
l++;
}
c = NEED_OTHER; c = NEED_OTHER;
} }
} }
switch (*ep) { switch (*ep) {
case '*': /* repetition */ case '*': /* repetition */
if (!m) p = ep+1; /* else stay in (repeat) the same item */ if (!m) p = ep+1; /* else stay in (repeat) the same item */
break; continue;
case '?': /* optional */ case '?': /* optional */
p = ep+1; /* continues reading the pattern */ p = ep+1; /* continues reading the pattern */
break; continue;
default: default:
if (m) p = ep; /* continues reading the pattern */ if (m) p = ep; /* continues reading the pattern */
else else
goto break_while; /* pattern fails */ goto break_while; /* pattern fails */
} }
} }
}
} break_while: } break_while:
if (c >= 0) /* not EOF nor NEED_OTHER? */ if (c >= 0) /* not EOF nor NEED_OTHER? */
ungetc(c, f); ungetc(c, f);
l = luaL_getsize();
if (l > 0 || *p == 0) /* read something or did not fail? */ if (l > 0 || *p == 0) /* read something or did not fail? */
lua_pushlstring(luaL_buffer(), l); lua_pushlstring(luaL_buffer(), l);
} }
}
static void io_write (void) static void io_write (void)