some cleaning

This commit is contained in:
Roberto Ierusalimschy 2000-11-23 11:49:35 -02:00
parent 22914afab3
commit 35d6b15057
3 changed files with 164 additions and 211 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.90 2000/10/27 16:15:53 roberto Exp roberto $ ** $Id: liolib.c,v 1.91 2000/10/31 13:10:24 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
*/ */
@ -51,6 +51,8 @@ int pclose(); */
#define INFILE 0 #define INFILE 0
#define OUTFILE 1 #define OUTFILE 1
#define NOFILE 2
typedef struct IOCtrl { typedef struct IOCtrl {
int ref[2]; /* ref for strings _INPUT/_OUTPUT */ int ref[2]; /* ref for strings _INPUT/_OUTPUT */
@ -132,6 +134,7 @@ static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
if (f == NULL) if (f == NULL)
return pushresult(L, 0); return pushresult(L, 0);
else { else {
if (inout != NOFILE)
setfile(L, ctrl, f, inout); setfile(L, ctrl, f, inout);
lua_pushusertag(L, f, ctrl->iotag); lua_pushusertag(L, f, ctrl->iotag);
return 1; return 1;
@ -171,12 +174,13 @@ static int io_open (lua_State *L) {
FILE *f; FILE *f;
lua_pop(L, 1); /* remove upvalue */ lua_pop(L, 1); /* remove upvalue */
f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
if (f) { return setreturn(L, ctrl, f, NOFILE);
lua_pushusertag(L, f, ctrl->iotag); }
return 1;
}
else static int io_tmpfile (lua_State *L) {
return pushresult(L, 0); IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
return setreturn(L, ctrl, tmpfile(), NOFILE);
} }
@ -227,71 +231,9 @@ static int io_appendto (lua_State *L) {
#ifdef LUA_COMPAT_READPATTERN
/*
** We cannot lookahead without need, because this can lock stdin.
** This flag signals when we need to read a next char.
*/
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
static int read_pattern (lua_State *L, FILE *f, const char *p) {
int inskip = 0; /* {skip} level */
int c = NEED_OTHER;
luaL_Buffer b;
luaL_buffinit(L, &b);
while (*p != '\0') {
switch (*p) {
case '{':
inskip++;
p++;
continue;
case '}':
if (!inskip) lua_error(L, "unbalanced braces in read pattern");
inskip--;
p++;
continue;
default: {
const char *ep = luaI_classend(L, p); /* get what is next */
int m; /* match result */
if (c == NEED_OTHER) c = getc(f);
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
if (m) {
if (!inskip) luaL_putchar(&b, c);
c = NEED_OTHER;
}
switch (*ep) {
case '+': /* repetition (1 or more) */
if (!m) goto break_while; /* pattern fails? */
/* else go through */
case '*': /* repetition (0 or more) */
while (m) { /* reads the same item until it fails */
c = getc(f);
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
if (m && !inskip) luaL_putchar(&b, c);
}
/* go through to continue reading the pattern */
case '?': /* optional */
p = ep+1; /* continues reading the pattern */
continue;
default:
if (!m) goto break_while; /* pattern fails? */
p = ep; /* else continues reading the pattern */
}
}
}
} break_while:
if (c != NEED_OTHER) ungetc(c, f);
luaL_pushresult(&b); /* close buffer */
return (*p == '\0');
}
#else
#define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0) #define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0)
#endif
static int read_number (lua_State *L, FILE *f) { static int read_number (lua_State *L, FILE *f) {
@ -400,8 +342,10 @@ static int io_read (lua_State *L) {
success = read_chars(L, f, (size_t)lua_tonumber(L, n)); success = read_chars(L, f, (size_t)lua_tonumber(L, n));
else { else {
const char *p = luaL_check_string(L, n); const char *p = luaL_check_string(L, n);
if (p[0] != '*') if (p[0] != '*') {
success = read_pattern(L, f, p); /* deprecated! */ lua_error(L, "read patterns are deprecated");
success = 0; /* to avoid warnings */
}
else { else {
switch (p[1]) { switch (p[1]) {
case 'n': /* number */ case 'n': /* number */
@ -516,7 +460,10 @@ static int io_rename (lua_State *L) {
static int io_tmpname (lua_State *L) { static int io_tmpname (lua_State *L) {
lua_pushstring(L, tmpnam(NULL)); char buff[L_tmpnam];
if (tmpnam(buff) != buff)
lua_error(L, "unable to generate a unique filename");
lua_pushstring(L, buff);
return 1; return 1;
} }
@ -548,7 +495,7 @@ static int io_date (lua_State *L) {
} }
static int setloc (lua_State *L) { static int io_setloc (lua_State *L) {
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
LC_NUMERIC, LC_TIME}; LC_NUMERIC, LC_TIME};
static const char *const catnames[] = {"all", "collate", "ctype", "monetary", static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
@ -665,7 +612,7 @@ static const struct luaL_reg iolib[] = {
{"getenv", io_getenv}, {"getenv", io_getenv},
{"remove", io_remove}, {"remove", io_remove},
{"rename", io_rename}, {"rename", io_rename},
{"setlocale", setloc}, {"setlocale", io_setloc},
{"tmpname", io_tmpname} {"tmpname", io_tmpname}
}; };
@ -678,6 +625,7 @@ static const struct luaL_reg iolibtag[] = {
{"read", io_read}, {"read", io_read},
{"readfrom", io_readfrom}, {"readfrom", io_readfrom},
{"seek", io_seek}, {"seek", io_seek},
{"tmpfile", io_tmpfile},
{"write", io_write}, {"write", io_write},
{"writeto", io_writeto} {"writeto", io_writeto}
}; };

239
lstrlib.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.55 2000/10/20 16:39:03 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.56 2000/10/27 16:15:53 roberto Exp roberto $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -121,46 +121,47 @@ static int str_char (lua_State *L) {
#endif #endif
struct Capture { typedef struct MatchState {
const char *src_end; /* end ('\0') of source string */ const char *src_end; /* end ('\0') of source string */
int level; /* total number of captures (finished or unfinished) */ int level; /* total number of captures (finished or unfinished) */
struct { struct {
const char *init; const char *init;
long len; /* -1 signals unfinished capture */ long len; /* -1 signals unfinished capture */
} capture[MAX_CAPTURES]; } capture[MAX_CAPTURES];
}; lua_State *L;
} MatchState;
#define ESC '%' #define ESC '%'
#define SPECIALS "^$*+?.([%-" #define SPECIALS "^$*+?.([%-"
static int check_capture (lua_State *L, int l, struct Capture *cap) { static int check_capture (MatchState *ms, int l) {
l -= '1'; l -= '1';
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1)) if (!(0 <= l && l < ms->level && ms->capture[l].len != -1))
lua_error(L, "invalid capture index"); lua_error(ms->L, "invalid capture index");
return l; return l;
} }
static int capture_to_close (lua_State *L, struct Capture *cap) { static int capture_to_close (MatchState *ms) {
int level = cap->level; int level = ms->level;
for (level--; level>=0; level--) for (level--; level>=0; level--)
if (cap->capture[level].len == -1) return level; if (ms->capture[level].len == -1) return level;
lua_error(L, "invalid pattern capture"); lua_error(ms->L, "invalid pattern capture");
return 0; /* to avoid warnings */ return 0; /* to avoid warnings */
} }
const char *luaI_classend (lua_State *L, const char *p) { static const char *luaI_classend (MatchState *ms, const char *p) {
switch (*p++) { switch (*p++) {
case ESC: case ESC:
if (*p == '\0') lua_error(L, "malformed pattern (ends with `%')"); if (*p == '\0') lua_error(ms->L, "malformed pattern (ends with `%')");
return p+1; return p+1;
case '[': case '[':
if (*p == '^') p++; if (*p == '^') p++;
do { /* look for a ']' */ do { /* look for a ']' */
if (*p == '\0') lua_error(L, "malformed pattern (missing `]')"); if (*p == '\0') lua_error(ms->L, "malformed pattern (missing `]')");
if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */ if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
} while (*p != ']'); } while (*p != ']');
return p+1; return p+1;
@ -189,7 +190,6 @@ static int match_class (int c, int cl) {
} }
static int matchbracketclass (int c, const char *p, const char *endclass) { static int matchbracketclass (int c, const char *p, const char *endclass) {
int sig = 1; int sig = 1;
if (*(p+1) == '^') { if (*(p+1) == '^') {
@ -213,8 +213,7 @@ static int matchbracketclass (int c, const char *p, const char *endclass) {
} }
static int luaI_singlematch (int c, const char *p, const char *ep) {
int luaI_singlematch (int c, const char *p, const char *ep) {
switch (*p) { switch (*p) {
case '.': /* matches any char */ case '.': /* matches any char */
return 1; return 1;
@ -228,20 +227,18 @@ int luaI_singlematch (int c, const char *p, const char *ep) {
} }
static const char *match (lua_State *L, const char *s, const char *p, static const char *match (MatchState *ms, const char *s, const char *p);
struct Capture *cap);
static const char *matchbalance (lua_State *L, const char *s, const char *p, static const char *matchbalance (MatchState *ms, const char *s, const char *p) {
struct Capture *cap) {
if (*p == 0 || *(p+1) == 0) if (*p == 0 || *(p+1) == 0)
lua_error(L, "unbalanced pattern"); lua_error(ms->L, "unbalanced pattern");
if (*s != *p) return NULL; if (*s != *p) return NULL;
else { else {
int b = *p; int b = *p;
int e = *(p+1); int e = *(p+1);
int cont = 1; int cont = 1;
while (++s < cap->src_end) { while (++s < ms->src_end) {
if (*s == e) { if (*s == e) {
if (--cont == 0) return s+1; if (--cont == 0) return s+1;
} }
@ -252,14 +249,14 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
} }
static const char *max_expand (lua_State *L, const char *s, const char *p, static const char *max_expand (MatchState *ms, const char *s, const char *p,
const char *ep, struct Capture *cap) { const char *ep) {
long i = 0; /* counts maximum expand for item */ long i = 0; /* counts maximum expand for item */
while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) while ((s+i)<ms->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
i++; i++;
/* keeps trying to match with the maximum repetitions */ /* keeps trying to match with the maximum repetitions */
while (i>=0) { while (i>=0) {
const char *res = match(L, (s+i), ep+1, cap); const char *res = match(ms, (s+i), ep+1);
if (res) return res; if (res) return res;
i--; /* else didn't match; reduce 1 repetition to try again */ i--; /* else didn't match; reduce 1 repetition to try again */
} }
@ -267,100 +264,96 @@ static const char *max_expand (lua_State *L, const char *s, const char *p,
} }
static const char *min_expand (lua_State *L, const char *s, const char *p, static const char *min_expand (MatchState *ms, const char *s, const char *p,
const char *ep, struct Capture *cap) { const char *ep) {
for (;;) { for (;;) {
const char *res = match(L, s, ep+1, cap); const char *res = match(ms, s, ep+1);
if (res != NULL) if (res != NULL)
return res; return res;
else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep)) else if (s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep))
s++; /* try with one more repetition */ s++; /* try with one more repetition */
else return NULL; else return NULL;
} }
} }
static const char *start_capture (lua_State *L, const char *s, const char *p, static const char *start_capture (MatchState *ms, const char *s, const char *p){
struct Capture *cap) {
const char *res; const char *res;
int level = cap->level; int level = ms->level;
if (level >= MAX_CAPTURES) lua_error(L, "too many captures"); if (level >= MAX_CAPTURES) lua_error(ms->L, "too many captures");
cap->capture[level].init = s; ms->capture[level].init = s;
cap->capture[level].len = -1; ms->capture[level].len = -1;
cap->level = level+1; ms->level = level+1;
if ((res=match(L, s, p+1, cap)) == NULL) /* match failed? */ if ((res=match(ms, s, p+1)) == NULL) /* match failed? */
cap->level--; /* undo capture */ ms->level--; /* undo capture */
return res; return res;
} }
static const char *end_capture (lua_State *L, const char *s, const char *p, static const char *end_capture (MatchState *ms, const char *s, const char *p) {
struct Capture *cap) { int l = capture_to_close(ms);
int l = capture_to_close(L, cap);
const char *res; const char *res;
cap->capture[l].len = s - cap->capture[l].init; /* close capture */ ms->capture[l].len = s - ms->capture[l].init; /* close capture */
if ((res = match(L, s, p+1, cap)) == NULL) /* match failed? */ if ((res = match(ms, s, p+1)) == NULL) /* match failed? */
cap->capture[l].len = -1; /* undo capture */ ms->capture[l].len = -1; /* undo capture */
return res; return res;
} }
static const char *match_capture (lua_State *L, const char *s, int level, static const char *match_capture (MatchState *ms, const char *s, int level) {
struct Capture *cap) { int l = check_capture(ms, level);
int l = check_capture(L, level, cap); size_t len = ms->capture[l].len;
size_t len = cap->capture[l].len; if ((size_t)(ms->src_end-s) >= len &&
if ((size_t)(cap->src_end-s) >= len && memcmp(ms->capture[l].init, s, len) == 0)
memcmp(cap->capture[l].init, s, len) == 0)
return s+len; return s+len;
else return NULL; else return NULL;
} }
static const char *match (lua_State *L, const char *s, const char *p, static const char *match (MatchState *ms, const char *s, const char *p) {
struct Capture *cap) {
init: /* using goto's to optimize tail recursion */ init: /* using goto's to optimize tail recursion */
switch (*p) { switch (*p) {
case '(': /* start capture */ case '(': /* start capture */
return start_capture(L, s, p, cap); return start_capture(ms, s, p);
case ')': /* end capture */ case ')': /* end capture */
return end_capture(L, s, p, cap); return end_capture(ms, s, p);
case ESC: /* may be %[0-9] or %b */ case ESC: /* may be %[0-9] or %b */
if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
s = match_capture(L, s, *(p+1), cap); s = match_capture(ms, s, *(p+1));
if (s == NULL) return NULL; if (s == NULL) return NULL;
p+=2; goto init; /* else return match(L, s, p+2, cap) */ p+=2; goto init; /* else return match(ms, s, p+2) */
} }
else if (*(p+1) == 'b') { /* balanced string? */ else if (*(p+1) == 'b') { /* balanced string? */
s = matchbalance(L, s, p+2, cap); s = matchbalance(ms, s, p+2);
if (s == NULL) return NULL; if (s == NULL) return NULL;
p+=4; goto init; /* else return match(L, s, p+4, cap); */ p+=4; goto init; /* else return match(ms, s, p+4); */
} }
else goto dflt; /* case default */ else goto dflt; /* case default */
case '\0': /* end of pattern */ case '\0': /* end of pattern */
return s; /* match succeeded */ return s; /* match succeeded */
case '$': case '$':
if (*(p+1) == '\0') /* is the '$' the last char in pattern? */ if (*(p+1) == '\0') /* is the '$' the last char in pattern? */
return (s == cap->src_end) ? s : NULL; /* check end of string */ return (s == ms->src_end) ? s : NULL; /* check end of string */
else goto dflt; else goto dflt;
default: dflt: { /* it is a pattern item */ default: dflt: { /* it is a pattern item */
const char *ep = luaI_classend(L, p); /* points to what is next */ const char *ep = luaI_classend(ms, p); /* points to what is next */
int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep); int m = s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep);
switch (*ep) { switch (*ep) {
case '?': { /* optional */ case '?': { /* optional */
const char *res; const char *res;
if (m && ((res=match(L, s+1, ep+1, cap)) != NULL)) if (m && ((res=match(ms, s+1, ep+1)) != NULL))
return res; return res;
p=ep+1; goto init; /* else return match(L, s, ep+1, cap); */ p=ep+1; goto init; /* else return match(ms, s, ep+1); */
} }
case '*': /* 0 or more repetitions */ case '*': /* 0 or more repetitions */
return max_expand(L, s, p, ep, cap); return max_expand(ms, s, p, ep);
case '+': /* 1 or more repetitions */ case '+': /* 1 or more repetitions */
return (m ? max_expand(L, s+1, p, ep, cap) : NULL); return (m ? max_expand(ms, s+1, p, ep) : NULL);
case '-': /* 0 or more repetitions (minimum) */ case '-': /* 0 or more repetitions (minimum) */
return min_expand(L, s, p, ep, cap); return min_expand(ms, s, p, ep);
default: default:
if (!m) return NULL; if (!m) return NULL;
s++; p=ep; goto init; /* else return match(L, s+1, ep, cap); */ s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
} }
} }
} }
@ -390,15 +383,15 @@ static const char *lmemfind (const char *s1, size_t l1,
} }
static int push_captures (lua_State *L, struct Capture *cap) { static int push_captures (MatchState *ms) {
int i; int i;
luaL_checkstack(L, cap->level, "too many captures"); luaL_checkstack(ms->L, ms->level, "too many captures");
for (i=0; i<cap->level; i++) { for (i=0; i<ms->level; i++) {
int l = cap->capture[i].len; int l = ms->capture[i].len;
if (l == -1) lua_error(L, "unfinished capture"); if (l == -1) lua_error(ms->L, "unfinished capture");
lua_pushlstring(L, cap->capture[i].init, l); lua_pushlstring(ms->L, ms->capture[i].init, l);
} }
return cap->level; /* number of strings pushed */ return ms->level; /* number of strings pushed */
} }
@ -407,7 +400,6 @@ static int str_find (lua_State *L) {
const char *s = luaL_check_lstr(L, 1, &l1); const char *s = luaL_check_lstr(L, 1, &l1);
const char *p = luaL_check_lstr(L, 2, &l2); const char *p = luaL_check_lstr(L, 2, &l2);
long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
struct Capture cap;
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range"); luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
if (lua_gettop(L) > 3 || /* extra argument? */ if (lua_gettop(L) > 3 || /* extra argument? */
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
@ -419,25 +411,28 @@ static int str_find (lua_State *L) {
} }
} }
else { else {
MatchState ms;
int anchor = (*p == '^') ? (p++, 1) : 0; int anchor = (*p == '^') ? (p++, 1) : 0;
const char *s1=s+init; const char *s1=s+init;
cap.src_end = s+l1; ms.L = L;
ms.src_end = s+l1;
do { do {
const char *res; const char *res;
cap.level = 0; ms.level = 0;
if ((res=match(L, s1, p, &cap)) != NULL) { if ((res=match(&ms, s1, p)) != NULL) {
lua_pushnumber(L, s1-s+1); /* start */ lua_pushnumber(L, s1-s+1); /* start */
lua_pushnumber(L, res-s); /* end */ lua_pushnumber(L, res-s); /* end */
return push_captures(L, &cap) + 2; return push_captures(&ms) + 2;
} }
} while (s1++<cap.src_end && !anchor); } while (s1++<ms.src_end && !anchor);
} }
lua_pushnil(L); /* not found */ lua_pushnil(L); /* not found */
return 1; return 1;
} }
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) { static void add_s (MatchState *ms, luaL_Buffer *b) {
lua_State *L = ms->L;
if (lua_isstring(L, 3)) { if (lua_isstring(L, 3)) {
const char *news = lua_tostring(L, 3); const char *news = lua_tostring(L, 3);
size_t l = lua_strlen(L, 3); size_t l = lua_strlen(L, 3);
@ -450,8 +445,8 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
if (!isdigit((unsigned char)news[i])) if (!isdigit((unsigned char)news[i]))
luaL_putchar(b, news[i]); luaL_putchar(b, news[i]);
else { else {
int level = check_capture(L, news[i], cap); int level = check_capture(ms, news[i]);
luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len); luaL_addlstring(b, ms->capture[level].init, ms->capture[level].len);
} }
} }
} }
@ -459,7 +454,7 @@ static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
else { /* is a function */ else { /* is a function */
int n; int n;
lua_pushvalue(L, 3); lua_pushvalue(L, 3);
n = push_captures(L, cap); n = push_captures(ms);
lua_rawcall(L, n, 1); lua_rawcall(L, n, 1);
if (lua_isstring(L, -1)) if (lua_isstring(L, -1))
luaL_addvalue(b); /* add return to accumulated result */ luaL_addvalue(b); /* add return to accumulated result */
@ -476,29 +471,30 @@ static int str_gsub (lua_State *L) {
int max_s = luaL_opt_int(L, 4, srcl+1); int max_s = luaL_opt_int(L, 4, srcl+1);
int anchor = (*p == '^') ? (p++, 1) : 0; int anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0; int n = 0;
struct Capture cap; MatchState ms;
luaL_Buffer b; luaL_Buffer b;
luaL_arg_check(L, luaL_arg_check(L,
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
3, "string or function expected"); 3, "string or function expected");
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
cap.src_end = src+srcl; ms.L = L;
ms.src_end = src+srcl;
while (n < max_s) { while (n < max_s) {
const char *e; const char *e;
cap.level = 0; ms.level = 0;
e = match(L, src, p, &cap); e = match(&ms, src, p);
if (e) { if (e) {
n++; n++;
add_s(L, &b, &cap); add_s(&ms, &b);
} }
if (e && e>src) /* non empty match? */ if (e && e>src) /* non empty match? */
src = e; /* skip it */ src = e; /* skip it */
else if (src < cap.src_end) else if (src < ms.src_end)
luaL_putchar(&b, *src++); luaL_putchar(&b, *src++);
else break; else break;
if (anchor) break; if (anchor) break;
} }
luaL_addlstring(&b, src, cap.src_end-src); luaL_addlstring(&b, src, ms.src_end-src);
luaL_pushresult(&b); luaL_pushresult(&b);
lua_pushnumber(L, n); /* number of substitutions */ lua_pushnumber(L, n); /* number of substitutions */
return 2; return 2;
@ -507,6 +503,12 @@ static int str_gsub (lua_State *L) {
/* }====================================================== */ /* }====================================================== */
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
#define MAX_ITEM 512
/* maximum size of each format specification (such as '%-099.99d') */
#define MAX_FORMAT 20
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
size_t l; size_t l;
const char *s = luaL_check_lstr(L, arg, &l); const char *s = luaL_check_lstr(L, arg, &l);
@ -525,10 +527,29 @@ static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
luaL_putchar(b, '"'); luaL_putchar(b, '"');
} }
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
#define MAX_ITEM 512 static const char *scanformat (lua_State *L, const char *strfrmt, char *form,
/* maximum size of each format specification (such as '%-099.99d') */ int *hasprecision) {
#define MAX_FORMAT 20 const char *p = strfrmt;
while (strchr("-+ #0", *p)) p++; /* skip flags */
if (isdigit((unsigned char)*p)) p++; /* skip width */
if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */
if (*p == '.') {
p++;
*hasprecision = 1;
if (isdigit((unsigned char)*p)) p++; /* skip precision */
if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */
}
if (isdigit((unsigned char)*p))
lua_error(L, "invalid format (width or precision too long)");
if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
lua_error(L, "invalid format (too long)");
form[0] = '%';
strncpy(form+1, strfrmt, p-strfrmt+1);
form[p-strfrmt+2] = 0;
return p;
}
static int str_format (lua_State *L) { static int str_format (lua_State *L) {
int arg = 1; int arg = 1;
@ -541,24 +562,15 @@ static int str_format (lua_State *L) {
else if (*++strfrmt == '%') else if (*++strfrmt == '%')
luaL_putchar(&b, *strfrmt++); /* %% */ luaL_putchar(&b, *strfrmt++); /* %% */
else { /* format item */ else { /* format item */
struct Capture cap; char form[MAX_FORMAT]; /* to store the format (`%...') */
char form[MAX_FORMAT]; /* to store the format ('%...') */
char buff[MAX_ITEM]; /* to store the formatted item */ char buff[MAX_ITEM]; /* to store the formatted item */
const char *initf = strfrmt; int hasprecision = 0;
form[0] = '%'; if (isdigit((unsigned char)*strfrmt) && *(strfrmt+1) == '$') {
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') { arg = *strfrmt - '0';
arg = *initf - '0'; strfrmt += 2; /* skip the `n$' */
initf += 2; /* skip the 'n$' */
} }
arg++; arg++;
cap.src_end = strfrmt+strlen(strfrmt)+1; strfrmt = scanformat(L, strfrmt, form, &hasprecision);
cap.level = 0;
strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
strfrmt-initf > MAX_FORMAT-2)
lua_error(L, "invalid format (width or precision too long)");
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
form[strfrmt-initf+2] = 0;
switch (*strfrmt++) { switch (*strfrmt++) {
case 'c': case 'd': case 'i': case 'c': case 'd': case 'i':
sprintf(buff, form, luaL_check_int(L, arg)); sprintf(buff, form, luaL_check_int(L, arg));
@ -575,7 +587,7 @@ static int str_format (lua_State *L) {
case 's': { case 's': {
size_t l; size_t l;
const char *s = luaL_check_lstr(L, arg, &l); const char *s = luaL_check_lstr(L, arg, &l);
if (cap.capture[1].len == 0 && l >= 100) { if (!hasprecision && l >= 100) {
/* no precision and string is too long to be formatted; /* no precision and string is too long to be formatted;
keep original string */ keep original string */
lua_pushvalue(L, arg); lua_pushvalue(L, arg);
@ -587,7 +599,7 @@ static int str_format (lua_State *L) {
break; break;
} }
} }
default: /* also treat cases 'pnLlh' */ default: /* also treat cases `pnLlh' */
lua_error(L, "invalid option in `format'"); lua_error(L, "invalid option in `format'");
} }
luaL_addlstring(&b, buff, strlen(buff)); luaL_addlstring(&b, buff, strlen(buff));
@ -605,7 +617,6 @@ static const struct luaL_reg strlib[] = {
{"strupper", str_upper}, {"strupper", str_upper},
{"strchar", str_char}, {"strchar", str_char},
{"strrep", str_rep}, {"strrep", str_rep},
{"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
{"strbyte", str_byte}, {"strbyte", str_byte},
{"format", str_format}, {"format", str_format},
{"strfind", str_find}, {"strfind", str_find},

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lualib.h,v 1.13 2000/10/20 16:39:03 roberto Exp roberto $ ** $Id: lualib.h,v 1.14 2000/10/27 16:15:53 roberto Exp roberto $
** Lua standard libraries ** Lua standard libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -25,10 +25,4 @@ LUALIB_API void lua_mathlibopen (lua_State *L);
LUALIB_API void lua_dblibopen (lua_State *L); LUALIB_API void lua_dblibopen (lua_State *L);
/* Auxiliary functions (private) */
const char *luaI_classend (lua_State *L, const char *p);
int luaI_singlematch (int c, const char *p, const char *ep);
#endif #endif