7#define REX_VERSION "Lrexlib " VERSION
10static void gmatch_pushsubject (lua_State *L,
TArgExec *argE);
11static int findmatch_exec (TUserdata *ud,
TArgExec *argE);
12static int split_exec (TUserdata *ud,
TArgExec *argE,
int offset);
13static int gsub_exec (TUserdata *ud,
TArgExec *argE,
int offset);
14static int gmatch_exec (TUserdata *ud,
TArgExec *argE);
15static int compile_regex (lua_State *L,
const TArgComp *argC, TUserdata **pud);
16static int generate_error (lua_State *L,
const TUserdata *ud,
int errcode);
18#if LUA_VERSION_NUM == 501
19# define ALG_ENVIRONINDEX LUA_ENVIRONINDEX
21# define ALG_ENVIRONINDEX lua_upvalueindex(1)
25# define ALG_CHARSIZE 1
28#ifndef BUFFERZ_PUTREPSTRING
29# define BUFFERZ_PUTREPSTRING bufferZ_putrepstring
33# define ALG_GETCARGS(a,b,c)
36#ifndef DO_NAMED_SUBPATTERNS
37#define DO_NAMED_SUBPATTERNS(a,b,c)
46static int OptLimit (lua_State *L,
int pos) {
47 if (lua_isnoneornil (L, pos))
48 return GSUB_UNLIMITED;
49 if (lua_isfunction (L, pos))
50 return GSUB_CONDITIONAL;
51 if (lua_isnumber (L, pos)) {
52 int a = lua_tointeger (L, pos);
55 return luaL_typerror (L, pos,
"number or function");
59static int get_startoffset(lua_State *L,
int stackpos,
size_t len) {
60 int startoffset = (int)luaL_optinteger(L, stackpos, 1);
63 else if(startoffset < 0) {
64 startoffset += len/ALG_CHARSIZE;
68 return startoffset*ALG_CHARSIZE;
72static TUserdata* test_ud (lua_State *L,
int pos)
75 if (lua_getmetatable(L, pos) &&
76 lua_rawequal(L, -1, ALG_ENVIRONINDEX) &&
77 (ud = (TUserdata *)lua_touserdata(L, pos)) != NULL) {
85static TUserdata* check_ud (lua_State *L)
87 TUserdata *ud = test_ud(L, 1);
88 if (ud == NULL) luaL_typerror(L, 1, REX_TYPENAME);
93static void check_subject (lua_State *L,
int pos,
TArgExec *argE)
96 argE->text = lua_tolstring (L, pos, &argE->textlen);
97 stype = lua_type (L, pos);
98 if (stype != LUA_TSTRING && stype != LUA_TTABLE && stype != LUA_TUSERDATA) {
99 luaL_typerror (L, pos,
"string, table or userdata");
100 }
else if (argE->text == NULL) {
102 lua_getfield (L, pos,
"topointer");
103 if (lua_type (L, -1) != LUA_TFUNCTION)
104 luaL_error (L,
"subject has no topointer method");
105 lua_pushvalue (L, pos);
107 type = lua_type (L, -1);
108 if (type != LUA_TLIGHTUSERDATA)
109 luaL_error (L,
"subject's topointer method returned %s (expected lightuserdata)",
110 lua_typename (L, type));
111 argE->text = (
const char*) lua_touserdata (L, -1);
113#if LUA_VERSION_NUM == 501
114 if (luaL_callmeta (L, pos,
"__len")) {
115 if (lua_type (L, -1) != LUA_TNUMBER)
116 luaL_argerror (L, pos,
"subject's length is not a number");
117 argE->textlen = lua_tointeger (L, -1);
121 argE->textlen = lua_objlen (L, pos);
123 argE->textlen = luaL_len (L, pos);
128static void check_pattern (lua_State *L,
int pos,
TArgComp *argC)
130 if (lua_isstring (L, pos)) {
131 argC->pattern = lua_tolstring (L, pos, &argC->patlen);
134 else if ((argC->ud = test_ud (L, pos)) == NULL)
135 luaL_typerror(L, pos,
"string or " REX_TYPENAME);
138static void checkarg_new (lua_State *L,
TArgComp *argC) {
139 argC->pattern = luaL_checklstring (L, 1, &argC->patlen);
140 argC->cflags = ALG_GETCFLAGS (L, 2);
141 ALG_GETCARGS (L, 3, argC);
147 check_subject (L, 1, argE);
148 check_pattern (L, 2, argC);
150 argE->reptype = lua_type (L, 3);
151 if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE &&
152 argE->reptype != LUA_TFUNCTION) {
153 luaL_typerror (L, 3,
"string, table or function");
157 argE->maxmatch = OptLimit (L, 4);
158 argC->cflags = ALG_GETCFLAGS (L, 5);
159 argE->eflags = (int)luaL_optinteger (L, 6, ALG_EFLAGS_DFLT);
160 ALG_GETCARGS (L, 7, argC);
166 check_subject (L, 1, argE);
167 check_pattern (L, 2, argC);
168 argC->cflags = ALG_GETCFLAGS (L, 3);
169 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
170 ALG_GETCARGS (L, 5, argC);
176static void checkarg_find_func (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
177 check_subject (L, 1, argE);
178 check_pattern (L, 2, argC);
179 argE->startoffset = get_startoffset (L, 3, argE->textlen);
180 argC->cflags = ALG_GETCFLAGS (L, 4);
181 argE->eflags = (int)luaL_optinteger (L, 5, ALG_EFLAGS_DFLT);
182 ALG_GETCARGS (L, 6, argC);
188static void checkarg_gmatch_split (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
189 check_subject (L, 1, argE);
190 check_pattern (L, 2, argC);
191 argC->cflags = ALG_GETCFLAGS (L, 3);
192 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
193 ALG_GETCARGS (L, 5, argC);
201static void checkarg_find_method (lua_State *L,
TArgExec *argE, TUserdata **ud) {
203 check_subject (L, 2, argE);
204 argE->startoffset = get_startoffset (L, 3, argE->textlen);
205 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
209static int algf_new (lua_State *L) {
211 checkarg_new (L, &argC);
212 return compile_regex (L, &argC, NULL);
215static void push_substrings (lua_State *L, TUserdata *ud,
const char *text,
218 if (lua_checkstack (L, ALG_NSUB(ud)) == 0) {
220 freelist_free (freelist);
221 luaL_error (L,
"cannot add %d stack slots", ALG_NSUB(ud));
223 for (i = 1; i <= ALG_NSUB(ud); i++) {
224 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
228static int algf_gsub (lua_State *L) {
232 int n_match = 0, n_subst = 0, st = 0, last_to = -1;
233 TBuffer BufOut, BufRep, BufTemp, *pBuf = &BufOut;
236 checkarg_gsub (L, &argC, &argE);
238 ud = (TUserdata*) argC.ud;
239 lua_pushvalue (L, 2);
241 else compile_regex (L, &argC, &ud);
242 freelist_init (&freelist);
244 if (argE.reptype == LUA_TSTRING) {
245 buffer_init (&BufRep, 256, L, &freelist);
246 BUFFERZ_PUTREPSTRING (&BufRep, argE.funcpos, ALG_NSUB(ud));
249 if (argE.maxmatch == GSUB_CONDITIONAL) {
250 buffer_init (&BufTemp, 1024, L, &freelist);
254 buffer_init (&BufOut, 1024, L, &freelist);
255 while ((argE.maxmatch < 0 || n_match < argE.maxmatch) && st <= (
int)argE.textlen) {
258 res = gsub_exec (ud, &argE, st);
259 if (ALG_NOMATCH (res)) {
262 else if (!ALG_ISMATCH (res)) {
263 freelist_free (&freelist);
264 return generate_error (L, ud, res);
266 from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
267 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
269 if (st < (
int)argE.textlen) {
270 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
279 buffer_addlstring (&BufOut, argE.text + st, from - st);
285 if (argE.reptype == LUA_TSTRING) {
286 size_t iter = 0, num;
288 while (bufferZ_next (&BufRep, &iter, &num, &str)) {
290 buffer_addlstring (pBuf, str, num);
291 else if (num == 0 || ALG_SUBVALID (ud,num))
292 buffer_addlstring (pBuf, argE.text + ALG_BASE(st) + ALG_SUBBEG(ud,num), ALG_SUBLEN(ud,num));
297 else if (argE.reptype == LUA_TTABLE) {
298 if (ALG_NSUB(ud) > 0)
299 ALG_PUSHSUB_OR_FALSE (L, ud, argE.text + ALG_BASE(st), 1);
301 lua_pushlstring (L, argE.text + from, to - from);
302 lua_gettable (L, argE.funcpos);
305 else if (argE.reptype == LUA_TFUNCTION) {
307 lua_pushvalue (L, argE.funcpos);
308 if (ALG_NSUB(ud) > 0) {
309 push_substrings (L, ud, argE.text + ALG_BASE(st), &freelist);
313 lua_pushlstring (L, argE.text + from, to - from);
316 if (0 != lua_pcall (L, narg, 1, 0)) {
317 freelist_free (&freelist);
318 return lua_error (L);
322 if (argE.reptype == LUA_TTABLE || argE.reptype == LUA_TFUNCTION) {
323 if (lua_tostring (L, -1)) {
324 buffer_addvalue (pBuf, -1);
327 else if (!lua_toboolean (L, -1))
328 buffer_addlstring (pBuf, argE.text + from, to - from);
330 freelist_free (&freelist);
331 luaL_error (L,
"invalid replacement value (a %s)", luaL_typename (L, -1));
333 if (argE.maxmatch != GSUB_CONDITIONAL)
337 if (argE.maxmatch == GSUB_CONDITIONAL) {
339 lua_pushvalue (L, argE.funcpos2);
340 lua_pushinteger (L, from/ALG_CHARSIZE + 1);
341 lua_pushinteger (L, to/ALG_CHARSIZE);
342 if (argE.reptype == LUA_TSTRING)
343 buffer_pushresult (&BufTemp);
345 lua_pushvalue (L, -4);
348 if (0 != lua_pcall (L, 3, 2, 0)) {
349 freelist_free (&freelist);
353 if (lua_isstring (L, -2)) {
354 buffer_addvalue (&BufOut, -2);
357 else if (lua_toboolean (L, -2))
358 buffer_addbuffer (&BufOut, &BufTemp);
360 buffer_addlstring (&BufOut, argE.text + from, to - from);
364 if (lua_type (L, -1) == LUA_TNUMBER) {
365 int n = lua_tointeger (L, -1);
368 argE.maxmatch = n_match + n;
370 else if (lua_toboolean (L, -1))
371 argE.maxmatch = GSUB_UNLIMITED;
373 buffer_clear (&BufTemp);
376 if (argE.maxmatch != GSUB_CONDITIONAL)
380 n_subst += curr_subst;
384 else if (st < (
int)argE.textlen) {
386 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
392 buffer_addlstring (&BufOut, argE.text + st, argE.textlen - st);
393 buffer_pushresult (&BufOut);
394 lua_pushinteger (L, n_match);
395 lua_pushinteger (L, n_subst);
396 freelist_free (&freelist);
401static int algf_count (lua_State *L) {
405 int n_match = 0, st = 0, last_to = -1;
407 checkarg_count (L, &argC, &argE);
409 ud = (TUserdata*) argC.ud;
410 lua_pushvalue (L, 2);
412 else compile_regex (L, &argC, &ud);
414 while (st <= (
int)argE.textlen) {
416 res = gsub_exec (ud, &argE, st);
417 if (ALG_NOMATCH (res)) {
420 else if (!ALG_ISMATCH (res)) {
421 return generate_error (L, ud, res);
423 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
425 if (st < (
int)argE.textlen) {
435 int from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
444 else if (st < (
int)argE.textlen) {
451 lua_pushinteger (L, n_match);
456static int finish_generic_find (lua_State *L, TUserdata *ud,
TArgExec *argE,
459 if (ALG_ISMATCH (res)) {
460 if (method == METHOD_FIND)
461 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE->startoffset), 0);
463 push_substrings (L, ud, argE->text, NULL);
464 else if (method != METHOD_FIND) {
465 ALG_PUSHSUB (L, ud, argE->text, 0);
468 return (method == METHOD_FIND) ? ALG_NSUB(ud) + 2 : ALG_NSUB(ud);
470 else if (ALG_NOMATCH (res))
471 return lua_pushnil (L), 1;
473 return generate_error (L, ud, res);
477static int generic_find_func (lua_State *L,
int method) {
483 checkarg_find_func (L, &argC, &argE);
484 if (argE.startoffset > (
int)argE.textlen)
485 return lua_pushnil (L), 1;
488 ud = (TUserdata*) argC.ud;
489 lua_pushvalue (L, 2);
491 else compile_regex (L, &argC, &ud);
492 res = findmatch_exec (ud, &argE);
493 return finish_generic_find (L, ud, &argE, method, res);
497static int algf_find (lua_State *L) {
498 return generic_find_func (L, METHOD_FIND);
502static int algf_match (lua_State *L) {
503 return generic_find_func (L, METHOD_MATCH);
507static int gmatch_iter (lua_State *L) {
510 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
511 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
512 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
513 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
514 last_end = lua_tointeger (L, lua_upvalueindex (5));
517 if (argE.startoffset > (
int)argE.textlen)
519 res = gmatch_exec (ud, &argE);
520 if (ALG_ISMATCH (res)) {
522 if (!ALG_SUBLEN(ud,0)) {
523 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
524 argE.startoffset += ALG_CHARSIZE;
529 last_end = ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0);
530 lua_pushinteger(L, last_end + incr);
531 lua_replace (L, lua_upvalueindex (4));
532 lua_pushinteger(L, last_end);
533 lua_replace (L, lua_upvalueindex (5));
536 push_substrings (L, ud, argE.text, NULL);
540 ALG_PUSHSUB (L, ud, argE.text, 0);
544 else if (ALG_NOMATCH (res))
547 return generate_error (L, ud, res);
552static int split_iter (lua_State *L) {
553 int incr, last_end, newoffset, res;
555 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
556 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
557 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
558 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
559 incr = lua_tointeger (L, lua_upvalueindex (5));
560 last_end = lua_tointeger (L, lua_upvalueindex (6));
566 if ((newoffset = argE.startoffset + incr) > (
int)argE.textlen)
568 res = split_exec (ud, &argE, newoffset);
569 if (ALG_ISMATCH (res)) {
570 if (!ALG_SUBLEN(ud,0)) {
571 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
572 incr += ALG_CHARSIZE;
576 lua_pushinteger(L, ALG_BASE(newoffset) + ALG_SUBEND(ud,0));
577 lua_pushvalue (L, -1);
578 lua_replace (L, lua_upvalueindex (4));
579 lua_replace (L, lua_upvalueindex (6));
580 lua_pushinteger (L, ALG_SUBLEN(ud,0) ? 0 : ALG_CHARSIZE);
581 lua_replace (L, lua_upvalueindex (5));
583 lua_pushlstring (L, argE.text + argE.startoffset,
584 ALG_SUBBEG(ud,0) + ALG_BASE(newoffset) - argE.startoffset);
587 push_substrings (L, ud, argE.text + ALG_BASE(newoffset), NULL);
588 return 1 + ALG_NSUB(ud);
591 ALG_PUSHSUB (L, ud, argE.text + ALG_BASE(newoffset), 0);
595 else if (ALG_NOMATCH (res))
598 return generate_error (L, ud, res);
600 lua_pushinteger (L, -1);
601 lua_replace (L, lua_upvalueindex (5));
602 lua_pushlstring (L, argE.text+argE.startoffset, argE.textlen-argE.startoffset);
607static int algf_gmatch (lua_State *L)
611 checkarg_gmatch_split (L, &argC, &argE);
613 lua_pushvalue (L, 2);
615 compile_regex (L, &argC, NULL);
616 gmatch_pushsubject (L, &argE);
617 lua_pushinteger (L, argE.eflags);
618 lua_pushinteger (L, 0);
619 lua_pushinteger (L, -1);
620 lua_pushcclosure (L, gmatch_iter, 5);
624static int algf_split (lua_State *L)
628 checkarg_gmatch_split (L, &argC, &argE);
630 lua_pushvalue (L, 2);
632 compile_regex (L, &argC, NULL);
633 gmatch_pushsubject (L, &argE);
634 lua_pushinteger (L, argE.eflags);
635 lua_pushinteger (L, 0);
636 lua_pushinteger (L, 0);
637 lua_pushinteger (L, -1);
638 lua_pushcclosure (L, split_iter, 6);
643static void push_substring_table (lua_State *L, TUserdata *ud,
const char *text) {
646 for (i = 1; i <= ALG_NSUB(ud); i++) {
647 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
648 lua_rawseti (L, -2, i);
653static void push_offset_table (lua_State *L, TUserdata *ud,
int startoffset) {
656 for (i=1, j=1; i <= ALG_NSUB(ud); i++) {
657 if (ALG_SUBVALID (ud,i)) {
658 ALG_PUSHSTART (L, ud, startoffset, i);
659 lua_rawseti (L, -2, j++);
660 ALG_PUSHEND (L, ud, startoffset, i);
661 lua_rawseti (L, -2, j++);
664 lua_pushboolean (L, 0);
665 lua_rawseti (L, -2, j++);
666 lua_pushboolean (L, 0);
667 lua_rawseti (L, -2, j++);
673static int generic_find_method (lua_State *L,
int method) {
678 checkarg_find_method (L, &argE, &ud);
679 if (argE.startoffset > (
int)argE.textlen)
680 return lua_pushnil(L), 1;
682 res = findmatch_exec (ud, &argE);
683 if (ALG_ISMATCH (res)) {
686 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
687 push_offset_table (L, ud, ALG_BASE(argE.startoffset));
688 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
691 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
692 push_substring_table (L, ud, argE.text);
693 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
697 return finish_generic_find (L, ud, &argE, method, res);
701 else if (ALG_NOMATCH (res))
702 return lua_pushnil (L), 1;
704 return generate_error(L, ud, res);
708static int algm_find (lua_State *L) {
709 return generic_find_method (L, METHOD_FIND);
711static int algm_match (lua_State *L) {
712 return generic_find_method (L, METHOD_MATCH);
714static int algm_tfind (lua_State *L) {
715 return generic_find_method (L, METHOD_TFIND);
717static int algm_exec (lua_State *L) {
718 return generic_find_method (L, METHOD_EXEC);
721static void alg_register (lua_State *L,
const luaL_Reg *r_methods,
722 const luaL_Reg *r_functions,
const char *name) {
724#if LUA_VERSION_NUM == 501
726 lua_pushvalue (L, -1);
727 lua_replace (L, LUA_ENVIRONINDEX);
728 luaL_register (L, NULL, r_methods);
730 luaL_newmetatable(L, REX_TYPENAME);
731 lua_pushvalue(L, -1);
732 luaL_setfuncs (L, r_methods, 1);
734 lua_pushvalue(L, -1);
735 lua_setfield(L, -2,
"__index");
738 lua_createtable(L, 0, 8);
739#if LUA_VERSION_NUM == 501
740 luaL_register (L, NULL, r_functions);
742 lua_pushvalue(L, -2);
743 luaL_setfuncs (L, r_functions, 1);
745#ifdef REX_CREATEGLOBALVAR
746 lua_pushvalue(L, -1);
747 lua_setglobal(L, REX_LIBNAME);
749 lua_pushfstring (L, REX_VERSION
" (for %s)", name);
750 lua_setfield (L, -2,
"_VERSION");
751#ifndef REX_NOEMBEDDEDTEST
752 lua_pushcfunction (L, newmembuffer);
753 lua_setfield (L, -2,
"_newmembuffer");