static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { int res; TPosix *ud; ud = (TPosix *)lua_newuserdata (L, sizeof (TPosix)); memset (ud, 0, sizeof (TPosix)); /* initialize all members to 0 */ #ifdef REX_POSIX_EXT if (argC->cflags & REG_PEND) ud->r.re_endp = argC->pattern + argC->patlen; #endif res = regcomp (&ud->r, argC->pattern, argC->cflags); if (res != 0) return generate_error (L, ud, res); if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); if (!ud->match) luaL_error (L, "malloc failed"); lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; return 1; }
static int gmatch_exec (TUserdata *ud, TArgExec *argE) { if (argE->startoffset > 0) argE->eflags |= REG_NOTBOL; argE->text += argE->startoffset; return tre_regwnexec (&ud->r, (const wchar_t*)argE->text, (argE->textlen - argE->startoffset)/ALG_CHARSIZE, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int gmatch_exec (TUserdata *ud, TArgExec *argE) { if (argE->startoffset > 0) argE->eflags |= REG_NOTBOL; argE->text += argE->startoffset; return tre_regnexec (&ud->r, argE->text, argE->textlen - argE->startoffset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int findmatch_exec (TPosix *ud, TArgExec *argE) { #ifdef REX_POSIX_EXT CheckStartEnd (argE, ud); #else argE->text += argE->startoffset; #endif return regexec (&ud->r, argE->text, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int Posix_gc (lua_State *L) { TPosix *ud = check_ud (L); if (ud->freed == 0) { /* precaution against "manual" __gc calling */ ud->freed = 1; regfree (&ud->r); Lfree (L, ud->match, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); } return 0; }
static int gsub_exec (TPosix *ud, TArgExec *argE, int st) { #ifdef REG_STARTEND if(argE->eflags & REG_STARTEND) { ALG_SUBBEG(ud,0) = 0; ALG_SUBEND(ud,0) = argE->textlen - st; } #endif if (st > 0) argE->eflags |= REG_NOTBOL; return regexec (&ud->r, argE->text+st, ALG_NSUB(ud)+1, ud->match, argE->eflags); }
static int findmatch_exec (TPosix *ud, TArgExec *argE) { #ifdef REG_STARTEND if (argE->eflags & REG_STARTEND) { ud->match[0].rm_so = argE->startoffset; ud->match[0].rm_eo = argE->textlen; argE->startoffset = 0; } else #endif argE->text += argE->startoffset; return regexec (&ud->r, argE->text, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int split_exec (TPosix *ud, TArgExec *argE, int offset) { #ifdef REX_POSIX_EXT if (argE->eflags & REG_STARTEND) { ALG_SUBBEG(ud,0) = 0; ALG_SUBEND(ud,0) = argE->textlen - offset; } #endif if (offset > 0) argE->eflags |= REG_NOTBOL; return regexec (&ud->r, argE->text + offset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int gmatch_exec (TUserdata *ud, TArgExec *argE) { if (argE->startoffset > 0) argE->eflags |= REG_NOTBOL; #ifdef REG_STARTEND if (argE->eflags & REG_STARTEND) { ALG_SUBBEG(ud,0) = 0; ALG_SUBEND(ud,0) = argE->textlen - argE->startoffset; } #endif argE->text += argE->startoffset; return regexec (&ud->r, argE->text, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int compile_regex (lua_State *L, const TArgComp *argC, TPcre **pud) { const char *error; int erroffset; TPcre *ud; const unsigned char *tables = NULL; ud = (TPcre*)lua_newuserdata (L, sizeof (TPcre)); memset (ud, 0, sizeof (TPcre)); /* initialize all members to 0 */ lua_pushvalue (L, LUA_ENVIRONINDEX); lua_setmetatable (L, -2); if (argC->locale) { char old_locale[256]; strcpy (old_locale, setlocale (LC_CTYPE, NULL)); /* store the locale */ if (NULL == setlocale (LC_CTYPE, argC->locale)) /* set new locale */ return luaL_error (L, "cannot set locale"); ud->tables = tables = pcre_maketables (); /* make tables with new locale */ setlocale (LC_CTYPE, old_locale); /* restore the old locale */ } else if (argC->tables) { tables = argC->tables; lua_pushinteger (L, INDEX_CHARTABLES_LINK); lua_rawget (L, LUA_ENVIRONINDEX); lua_pushvalue (L, -2); lua_pushvalue (L, argC->tablespos); lua_rawset (L, -3); lua_pop (L, 1); } ud->pr = pcre_compile (argC->pattern, argC->cflags, &error, &erroffset, tables); if (!ud->pr) return luaL_error (L, "%s (pattern offset: %d)", error, erroffset + 1); ud->extra = pcre_study (ud->pr, 0, &error); if (error) return luaL_error (L, "%s", error); pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt); /* need (2 ints per capture, plus one for substring match) * 3/2 */ ud->match = (int *) Lmalloc (L, (ALG_NSUB(ud) + 1) * 3 * sizeof (int)); if (pud) *pud = ud; return 1; }
static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { int res; TPosix *ud; ud = (TPosix *)lua_newuserdata (L, sizeof (TPosix)); memset (ud, 0, sizeof (TPosix)); /* initialize all members to 0 */ res = tre_regncomp (&ud->r, argC->pattern, argC->patlen, argC->cflags); if (res != 0) return generate_error (L, ud, res); if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); lua_pushvalue (L, LUA_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; return 1; }
/* the target table must be on lua stack top */ static void do_named_subpatterns (lua_State *L, TPcre *ud, const char *text) { int i, namecount, name_entry_size; unsigned char *name_table, *tabptr; /* do named subpatterns - NJG */ pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_NAMECOUNT, &namecount); if (namecount <= 0) return; pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_NAMETABLE, &name_table); pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size); tabptr = name_table; for (i = 0; i < namecount; i++) { int n = (tabptr[0] << 8) | tabptr[1]; /* number of the capturing parenthesis */ if (n > 0 && n <= ALG_NSUB(ud)) { /* check range */ lua_pushstring (L, (char *)tabptr + 2); /* name of the capture, zero terminated */ ALG_PUSHSUB_OR_FALSE (L, ud, text, n); lua_rawset (L, -3); } tabptr += name_entry_size; } }
static int generic_atfind (lua_State *L, int tfind) { int res; TArgExec argE; TPosix *ud; regaparams_t argP; regamatch_t res_match; checkarg_atfind (L, &argE, &ud, &argP); if (argE.startoffset > (int)argE.textlen) return lua_pushnil(L), 1; argE.text += argE.startoffset; res_match.nmatch = ALG_NSUB(ud) + 1; res_match.pmatch = ud->match; /* execute the search */ res = tre_reganexec (&ud->r, argE.text, argE.textlen - argE.startoffset, &res_match, argP, argE.eflags); if (ALG_ISMATCH (res)) { ALG_PUSHOFFSETS (L, ud, argE.startoffset, 0); if (tfind) push_substring_table (L, ud, argE.text); else push_offset_table (L, ud, argE.startoffset); /* set values in the dictionary part of the table */ set_int_field (L, "cost", res_match.cost); set_int_field (L, "num_ins", res_match.num_ins); set_int_field (L, "num_del", res_match.num_del); set_int_field (L, "num_subst", res_match.num_subst); return 3; } else if (ALG_NOMATCH (res)) return lua_pushnil (L), 1; else return generate_error (L, ud, res); }
static int split_exec (TPosix *ud, TArgExec *argE, int offset) { if (offset > 0) argE->eflags |= REG_NOTBOL; return tre_regwnexec (&ud->r, (const wchar_t*)(argE->text + offset), (argE->textlen - offset)/ALG_CHARSIZE, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int findmatch_exec (TPosix *ud, TArgExec *argE) { argE->text += argE->startoffset; return tre_regnexec (&ud->r, argE->text, argE->textlen - argE->startoffset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int split_exec (TPosix *ud, TArgExec *argE, int offset) { if (offset > 0) argE->eflags |= REG_NOTBOL; return tre_regnexec (&ud->r, argE->text + offset, argE->textlen - offset, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }
static int gsub_exec (TPosix *ud, TArgExec *argE, int st) { if (st > 0) argE->eflags |= REG_NOTBOL; return tre_regnexec (&ud->r, argE->text+st, argE->textlen-st, ALG_NSUB(ud)+1, ud->match, argE->eflags); }
static int split_exec (TPcre *ud, TArgExec *argE, int offset) { return pcre_exec (ud->pr, ud->extra, argE->text, argE->textlen, offset, argE->eflags, ud->match, (ALG_NSUB(ud) + 1) * 3); }
static int gsub_exec (TPcre *ud, TArgExec *argE, int st, int retry) { int eflags = retry ? (argE->eflags|PCRE_NOTEMPTY|PCRE_ANCHORED) : argE->eflags; return pcre_exec (ud->pr, ud->extra, argE->text, argE->textlen, st, eflags, ud->match, (ALG_NSUB(ud) + 1) * 3); }
static int findmatch_exec (TPcre *ud, TArgExec *argE) { return pcre_exec (ud->pr, ud->extra, argE->text, argE->textlen, argE->startoffset, argE->eflags, ud->match, (ALG_NSUB(ud) + 1) * 3); }
static int gmatch_exec (TUserdata *ud, TArgExec *argE, int retry) { int eflags = retry ? (argE->eflags|PCRE_NOTEMPTY|PCRE_ANCHORED) : argE->eflags; return pcre_exec (ud->pr, ud->extra, argE->text, argE->textlen, argE->startoffset, eflags, ud->match, (ALG_NSUB(ud) + 1) * 3); }
static int findmatch_exec (TPosix *ud, TArgExec *argE) { argE->text += argE->startoffset; return tre_regwnexec (&ud->r, (const wchar_t*)argE->text, (argE->textlen - argE->startoffset)/ALG_CHARSIZE, ALG_NSUB(ud) + 1, ud->match, argE->eflags); }