small improvement

This commit is contained in:
Roberto Ierusalimschy
2005-06-03 17:16:16 -03:00
parent e33d7bae45
commit eca9fa02d2
4 changed files with 16 additions and 20 deletions

22
lzio.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lzio.c,v 1.29 2004/04/30 20:13:38 roberto Exp roberto $
** $Id: lzio.c,v 1.30 2005/05/17 19:49:15 roberto Exp roberto $
** a generic input stream interface
** See Copyright Notice in lua.h
*/
@@ -34,10 +34,12 @@ int luaZ_fill (ZIO *z) {
int luaZ_lookahead (ZIO *z) {
if (z->n == 0) {
int c = luaZ_fill(z);
if (c == EOZ) return c;
z->n++;
z->p--;
if (luaZ_fill(z) == EOZ)
return EOZ;
else {
z->n++; /* luaZ_fill removed first byte; put back it */
z->p--;
}
}
return char2int(*z->p);
}
@@ -56,14 +58,8 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
size_t luaZ_read (ZIO *z, void *b, size_t n) {
while (n) {
size_t m;
if (z->n == 0) {
if (luaZ_fill(z) == EOZ)
return n; /* return number of missing bytes */
else {
++z->n; /* filbuf removed first byte; put back it */
--z->p;
}
}
if (luaZ_lookahead(z) == EOZ)
return n; /* return number of missing bytes */
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m);
z->n -= m;