aboutsummaryrefslogtreecommitdiff
path: root/Lua/lbp.c
diff options
context:
space:
mode:
Diffstat (limited to 'Lua/lbp.c')
-rw-r--r--Lua/lbp.c55
1 files changed, 17 insertions, 38 deletions
diff --git a/Lua/lbp.c b/Lua/lbp.c
index b062973..c1c3f61 100644
--- a/Lua/lbp.c
+++ b/Lua/lbp.c
@@ -33,8 +33,10 @@ static void push_match(lua_State *L, match_t *m, const char *start);
lua_State *cur_state = NULL;
-static void match_error(const char *msg)
+static void match_error(pat_t *pat, const char *msg)
{
+ (void)pat;
+ recycle_all_matches();
lua_pushstring(cur_state, msg);
lua_error(cur_state);
}
@@ -139,19 +141,6 @@ static void push_match(lua_State *L, match_t *m, const char *start)
lua_setfield(L, -2, "after");
}
-typedef struct {
- lua_State *L;
- const char *text;
-} lua_match_context_t;
-
-static bp_match_behavior handle_match(match_t *m, int matchnum, void *data)
-{
- (void)matchnum;
- lua_match_context_t *ctx = data;
- push_match(ctx->L, m, ctx->text);
- return BP_STOP;
-}
-
static int Lmatch(lua_State *L)
{
if (lua_isstring(L, 1)) {
@@ -178,29 +167,17 @@ static int Lmatch(lua_State *L)
if (index > (lua_Integer)strlen(text)+1)
return 0;
+ match_t *m = NULL;
+ int ret = 0;
cur_state = L;
- lua_match_context_t ctx = {.L=L, .text=text};
- bp_errhand_t prev_handler = set_match_error_handler(match_error);
- int ret = each_match(handle_match, &ctx, text+index-1, &text[textlen], pat, builtins, NULL, false);
- (void)set_match_error_handler(prev_handler);
+ if (next_match_safe(&m, text+index-1, &text[textlen], pat, builtins, NULL, false, match_error)) {
+ push_match(L, m, text);
+ stop_matching(&m);
+ ret = 1;
+ }
return ret;
}
-typedef struct {
- FILE *out;
- const char *prev, *text;
-} lua_replace_context_t;
-
-static bp_match_behavior handle_replacement(match_t *m, int matchnum, void *data)
-{
- (void)matchnum;
- lua_replace_context_t *ctx = data;
- fwrite(ctx->prev, sizeof(char), (size_t)(m->start - ctx->prev), ctx->out);
- fprint_match(ctx->out, ctx->text, m, NULL);
- ctx->prev = m->end;
- return BP_CONTINUE;
-}
-
static int Lreplace(lua_State *L)
{
if (lua_isstring(L, 1)) {
@@ -228,14 +205,16 @@ static int Lreplace(lua_State *L)
char *buf = NULL;
size_t size = 0;
FILE *out = open_memstream(&buf, &size);
+ int replacements = 0;
const char *prev = text;
pat_t *rep_pat = maybe_replacement.value.pat;
cur_state = L;
-
- lua_replace_context_t ctx = {.out=out, .prev=text, .text=text};
- bp_errhand_t prev_handler = set_match_error_handler(match_error);
- int replacements = each_match(handle_replacement, &ctx, text, &text[textlen], rep_pat, builtins, NULL, false);
- (void)set_match_error_handler(prev_handler);
+ for (match_t *m = NULL; next_match_safe(&m, text, &text[textlen], rep_pat, builtins, NULL, false, match_error); ) {
+ fwrite(prev, sizeof(char), (size_t)(m->start - prev), out);
+ fprint_match(out, text, m, NULL);
+ prev = m->end;
+ ++replacements;
+ }
fwrite(prev, sizeof(char), (size_t)(&text[textlen] - prev), out);
fflush(out);
lua_pushlstring(L, buf, size);