new structure for closures, without static variables.

This commit is contained in:
Roberto Ierusalimschy 1997-12-15 15:58:49 -02:00
parent 9be85d1648
commit 82d09fbf0d
1 changed files with 63 additions and 51 deletions

114
lstrlib.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.2 1997/11/26 18:53:45 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.3 1997/12/09 13:35:19 roberto Exp roberto $
** Standard library for strings and pattern-matching ** Standard library for strings and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -145,45 +145,47 @@ static void str_ascii (void)
#define MAX_CAPT 9 #define MAX_CAPT 9
static struct { struct Capture {
char *init; struct {
int len; /* -1 signals unfinished capture */ char *init;
} capture[MAX_CAPT]; int len; /* -1 signals unfinished capture */
} capture[MAX_CAPT];
static int num_captures; /* only valid after a sucessful call to match */ int level; /* total number of captures (finished or unfinished) */
};
#define ESC '%' #define ESC '%'
#define SPECIALS "^$*?.([%-" #define SPECIALS "^$*?.([%-"
static void push_captures (void) static void push_captures (struct Capture *cap)
{ {
int i; int i;
for (i=0; i<num_captures; i++) { for (i=0; i<cap->level; i++) {
int l = capture[i].len; int l = cap->capture[i].len;
char *buff = openspace(l+1); char *buff = openspace(l+1);
if (l == -1) lua_error("unfinished capture"); if (l == -1) lua_error("unfinished capture");
strncpy(buff, capture[i].init, l); strncpy(buff, cap->capture[i].init, l);
buff[l] = 0; buff[l] = 0;
lua_pushstring(buff); lua_pushstring(buff);
} }
} }
static int check_cap (int l, int level) static int check_cap (int l, struct Capture *cap)
{ {
l -= '1'; l -= '1';
if (!(0 <= l && l < level && capture[l].len != -1)) if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
lua_error("invalid capture index"); lua_error("invalid capture index");
return l; return l;
} }
static int capture_to_close (int level) static int capture_to_close (struct Capture *cap)
{ {
int level = cap->level;
for (level--; level>=0; level--) for (level--; level>=0; level--)
if (capture[level].len == -1) return level; if (cap->capture[level].len == -1) return level;
lua_error("invalid pattern capture"); lua_error("invalid pattern capture");
return 0; /* to avoid warnings */ return 0; /* to avoid warnings */
} }
@ -269,15 +271,15 @@ static char *matchbalance (char *s, int b, int e)
} }
static char *matchitem (char *s, char *p, int level, char **ep) static char *matchitem (char *s, char *p, struct Capture *cap, char **ep)
{ {
if (*p == ESC) { if (*p == ESC) {
p++; p++;
if (isdigit((unsigned char)*p)) { /* capture */ if (isdigit((unsigned char)*p)) { /* capture */
int l = check_cap(*p, level); int l = check_cap(*p, cap);
*ep = p+1; *ep = p+1;
if (strncmp(capture[l].init, s, capture[l].len) == 0) if (strncmp(cap->capture[l].init, s, cap->capture[l].len) == 0)
return s+capture[l].len; return s+cap->capture[l].len;
else return NULL; else return NULL;
} }
else if (*p == 'b') { /* balanced string */ else if (*p == 'b') { /* balanced string */
@ -293,58 +295,61 @@ static char *matchitem (char *s, char *p, int level, char **ep)
} }
static char *match (char *s, char *p, int level) static char *match (char *s, 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 */
if (level >= MAX_CAPT) lua_error("too many captures");
capture[level].init = s;
capture[level].len = -1;
level++; p++; goto init; /* return match(s, p+1, level); */
case ')': { /* end capture */
int l = capture_to_close(level);
char *res; char *res;
capture[l].len = s - capture[l].init; /* close capture */ if (cap->level >= MAX_CAPT) lua_error("too many captures");
if ((res = match(s, p+1, level)) == NULL) /* match failed? */ cap->capture[cap->level].init = s;
capture[l].len = -1; /* undo capture */ cap->capture[cap->level].len = -1;
cap->level++;
if ((res=match(s, p+1, cap)) == NULL) /* match failed? */
cap->level--; /* undo capture */
return res;
}
case ')': { /* end capture */
int l = capture_to_close(cap);
char *res;
cap->capture[l].len = s - cap->capture[l].init; /* close capture */
if ((res = match(s, p+1, cap)) == NULL) /* match failed? */
cap->capture[l].len = -1; /* undo capture */
return res; return res;
} }
case '\0': case '$': /* (possibly) end of pattern */ case '\0': case '$': /* (possibly) end of pattern */
if (*p == 0 || (*(p+1) == 0 && *s == 0)) { if (*p == 0 || (*(p+1) == 0 && *s == 0))
num_captures = level;
return s; return s;
}
/* else go through */ /* else go through */
default: { /* it is a pattern item */ default: { /* it is a pattern item */
char *ep; /* get what is next */ char *ep; /* get what is next */
char *s1 = matchitem(s, p, level, &ep); char *s1 = matchitem(s, p, cap, &ep);
switch (*ep) { switch (*ep) {
case '*': { /* repetition */ case '*': { /* repetition */
char *res; char *res;
if (s1 && (res = match(s1, p, level))) if (s1 && (res = match(s1, p, cap)))
return res; return res;
p=ep+1; goto init; /* else return match(s, ep+1, level); */ p=ep+1; goto init; /* else return match(s, ep+1, cap); */
} }
case '-': { /* repetition */ case '-': { /* repetition */
char *res; char *res;
if ((res = match(s, ep+1, level)) != 0) if ((res = match(s, ep+1, cap)) != 0)
return res; return res;
else if (s1) { else if (s1) {
s = s1; s = s1;
goto init; /* return match(s1, p, level); */ goto init; /* return match(s1, p, cap); */
} }
else else
return NULL; return NULL;
} }
case '?': { /* optional */ case '?': { /* optional */
char *res; char *res;
if (s1 && (res = match(s1, ep+1, level))) if (s1 && (res = match(s1, ep+1, cap)))
return res; return res;
p=ep+1; goto init; /* else return match(s, ep+1, level); */ p=ep+1; goto init; /* else return match(s, ep+1, cap); */
} }
default: default:
if (s1) { s=s1; p=ep; goto init; } /* return match(s1, ep, level); */ if (s1) { s=s1; p=ep; goto init; } /* return match(s1, ep, cap); */
else return NULL; else return NULL;
} }
} }
@ -370,11 +375,13 @@ static void str_find (void)
int anchor = (*p == '^') ? (p++, 1) : 0; int anchor = (*p == '^') ? (p++, 1) : 0;
char *s1=s+init; char *s1=s+init;
do { do {
struct Capture cap;
char *res; char *res;
if ((res=match(s1, p, 0)) != NULL) { cap.level = 0;
if ((res=match(s1, p, &cap)) != NULL) {
lua_pushnumber(s1-s+1); /* start */ lua_pushnumber(s1-s+1); /* start */
lua_pushnumber(res-s); /* end */ lua_pushnumber(res-s); /* end */
push_captures(); push_captures(&cap);
return; return;
} }
} while (*s1++ && !anchor); } while (*s1++ && !anchor);
@ -382,7 +389,7 @@ static void str_find (void)
} }
static void add_s (lua_Object newp) static void add_s (lua_Object newp, struct Capture *cap)
{ {
if (lua_isstring(newp)) { if (lua_isstring(newp)) {
char *news = lua_getstring(newp); char *news = lua_getstring(newp);
@ -390,8 +397,8 @@ static void add_s (lua_Object newp)
if (*news != ESC || !isdigit((unsigned char)*++news)) if (*news != ESC || !isdigit((unsigned char)*++news))
luaI_addchar(*news++); luaI_addchar(*news++);
else { else {
int l = check_cap(*news++, num_captures); int l = check_cap(*news++, cap);
addnchar(capture[l].init, capture[l].len); addnchar(cap->capture[l].init, cap->capture[l].len);
} }
} }
} }
@ -400,7 +407,7 @@ static void add_s (lua_Object newp)
struct lbuff oldbuff; struct lbuff oldbuff;
int status; int status;
lua_beginblock(); lua_beginblock();
push_captures(); push_captures(cap);
/* function may use lbuffer, so save it and create a luaM_new one */ /* function may use lbuffer, so save it and create a luaM_new one */
oldbuff = lbuffer; oldbuff = lbuffer;
lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0; lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
@ -428,10 +435,13 @@ static void str_gsub (void)
int n = 0; int n = 0;
luaI_emptybuff(); luaI_emptybuff();
while (n < max_s) { while (n < max_s) {
char *e = match(src, p, 0); struct Capture cap;
char *e;
cap.level = 0;
e = match(src, p, &cap);
if (e) { if (e) {
n++; n++;
add_s(newp); add_s(newp, &cap);
} }
if (e && e>src) /* non empty match? */ if (e && e>src) /* non empty match? */
src = e; /* skip it */ src = e; /* skip it */
@ -471,11 +481,13 @@ static void str_format (void)
luaI_addchar(*strfrmt++); /* %% */ luaI_addchar(*strfrmt++); /* %% */
else { /* format item */ else { /* format item */
char form[MAX_FORMAT]; /* store the format ('%...') */ char form[MAX_FORMAT]; /* store the format ('%...') */
struct Capture cap;
char *buff; char *buff;
char *initf = strfrmt; char *initf = strfrmt;
form[0] = '%'; form[0] = '%';
strfrmt = match(strfrmt, "%d?%$?[-+ #]*(%d*)%.?(%d*)", 0); cap.level = 0;
if (capture[0].len > 3 || capture[1].len > 3) /* < 1000? */ strfrmt = match(strfrmt, "%d?%$?[-+ #]*(%d*)%.?(%d*)", &cap);
if (cap.capture[0].len > 3 || cap.capture[1].len > 3) /* < 1000? */
lua_error("invalid format (width or precision too long)"); lua_error("invalid format (width or precision too long)");
if (isdigit((unsigned char)initf[0]) && initf[1] == '$') { if (isdigit((unsigned char)initf[0]) && initf[1] == '$') {
arg = initf[0] - '0'; arg = initf[0] - '0';