7#define REX_VERSION "Lrexlib " VERSION
10static void gmatch_pushsubject (lua_State *L,
TArgExec *argE);
19static int findmatch_exec (TUserdata *ud,
TArgExec *argE);
29static int split_exec (TUserdata *ud,
TArgExec *argE,
int offset);
39static int gsub_exec (TUserdata *ud,
TArgExec *argE,
int offset);
48static int gmatch_exec (TUserdata *ud,
TArgExec *argE);
58static int compile_regex (lua_State *L,
const TArgComp *argC, TUserdata **pud);
68static int generate_error (lua_State *L,
const TUserdata *ud,
int errcode);
70#if LUA_VERSION_NUM == 501
71# define ALG_ENVIRONINDEX LUA_ENVIRONINDEX
73# define ALG_ENVIRONINDEX lua_upvalueindex(1)
77# define ALG_CHARSIZE 1
80#ifndef BUFFERZ_PUTREPSTRING
81# define BUFFERZ_PUTREPSTRING bufferZ_putrepstring
85# define ALG_GETCARGS(a,b,c)
88#ifndef DO_NAMED_SUBPATTERNS
89#define DO_NAMED_SUBPATTERNS(a,b,c)
110static int OptLimit (lua_State *L,
int pos) {
111 if (lua_isnoneornil (L, pos))
112 return GSUB_UNLIMITED;
113 if (lua_isfunction (L, pos))
114 return GSUB_CONDITIONAL;
115 if (lua_isnumber (L, pos)) {
116 int a = lua_tointeger (L, pos);
117 return a < 0 ? 0 : a;
119 return luaL_typerror (L, pos,
"number or function");
135static int get_startoffset(lua_State *L,
int stackpos,
size_t len) {
136 int startoffset = (int)luaL_optinteger(L, stackpos, 1);
139 else if(startoffset < 0) {
140 startoffset += len/ALG_CHARSIZE;
144 return startoffset*ALG_CHARSIZE;
158static TUserdata* test_ud (lua_State *L,
int pos)
161 if (lua_getmetatable(L, pos) &&
162 lua_rawequal(L, -1, ALG_ENVIRONINDEX) &&
163 (ud = (TUserdata *)lua_touserdata(L, pos)) != NULL) {
179static TUserdata* check_ud (lua_State *L)
181 TUserdata *ud = test_ud(L, 1);
182 if (ud == NULL) luaL_typerror(L, 1, REX_TYPENAME);
194static void check_subject (lua_State *L,
int pos,
TArgExec *argE)
197 argE->text = lua_tolstring (L, pos, &argE->textlen);
198 stype = lua_type (L, pos);
199 if (stype != LUA_TSTRING && stype != LUA_TTABLE && stype != LUA_TUSERDATA) {
200 luaL_typerror (L, pos,
"string, table or userdata");
201 }
else if (argE->text == NULL) {
203 lua_getfield (L, pos,
"topointer");
204 if (lua_type (L, -1) != LUA_TFUNCTION)
205 luaL_error (L,
"subject has no topointer method");
206 lua_pushvalue (L, pos);
208 type = lua_type (L, -1);
209 if (type != LUA_TLIGHTUSERDATA)
210 luaL_error (L,
"subject's topointer method returned %s (expected lightuserdata)",
211 lua_typename (L, type));
212 argE->text = (
const char*) lua_touserdata (L, -1);
214#if LUA_VERSION_NUM == 501
215 if (luaL_callmeta (L, pos,
"__len")) {
216 if (lua_type (L, -1) != LUA_TNUMBER)
217 luaL_argerror (L, pos,
"subject's length is not a number");
218 argE->textlen = lua_tointeger (L, -1);
222 argE->textlen = lua_objlen (L, pos);
224 argE->textlen = luaL_len (L, pos);
240static void check_pattern (lua_State *L,
int pos,
TArgComp *argC)
242 if (lua_isstring (L, pos)) {
243 argC->pattern = lua_tolstring (L, pos, &argC->patlen);
246 else if ((argC->ud = test_ud (L, pos)) == NULL)
247 luaL_typerror(L, pos,
"string or " REX_TYPENAME);
258static void checkarg_new (lua_State *L,
TArgComp *argC) {
259 argC->pattern = luaL_checklstring (L, 1, &argC->patlen);
260 argC->cflags = ALG_GETCFLAGS (L, 2);
261 ALG_GETCARGS (L, 3, argC);
277 check_subject (L, 1, argE);
278 check_pattern (L, 2, argC);
280 argE->reptype = lua_type (L, 3);
281 if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE &&
282 argE->reptype != LUA_TFUNCTION) {
283 luaL_typerror (L, 3,
"string, table or function");
287 argE->maxmatch = OptLimit (L, 4);
288 argC->cflags = ALG_GETCFLAGS (L, 5);
289 argE->eflags = (int)luaL_optinteger (L, 6, ALG_EFLAGS_DFLT);
290 ALG_GETCARGS (L, 7, argC);
307 check_subject (L, 1, argE);
308 check_pattern (L, 2, argC);
309 argC->cflags = ALG_GETCFLAGS (L, 3);
310 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
311 ALG_GETCARGS (L, 5, argC);
327static void checkarg_find_func (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
328 check_subject (L, 1, argE);
329 check_pattern (L, 2, argC);
330 argE->startoffset = get_startoffset (L, 3, argE->textlen);
331 argC->cflags = ALG_GETCFLAGS (L, 4);
332 argE->eflags = (int)luaL_optinteger (L, 5, ALG_EFLAGS_DFLT);
333 ALG_GETCARGS (L, 6, argC);
349static void checkarg_gmatch_split (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
350 check_subject (L, 1, argE);
351 check_pattern (L, 2, argC);
352 argC->cflags = ALG_GETCFLAGS (L, 3);
353 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
354 ALG_GETCARGS (L, 5, argC);
372static void checkarg_find_method (lua_State *L,
TArgExec *argE, TUserdata **ud) {
374 check_subject (L, 2, argE);
375 argE->startoffset = get_startoffset (L, 3, argE->textlen);
376 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
388static int algf_new (lua_State *L) {
390 checkarg_new (L, &argC);
391 return compile_regex (L, &argC, NULL);
406static void push_substrings (lua_State *L, TUserdata *ud,
const char *text,
409 if (lua_checkstack (L, ALG_NSUB(ud)) == 0) {
411 freelist_free (freelist);
412 luaL_error (L,
"cannot add %d stack slots", ALG_NSUB(ud));
414 for (i = 1; i <= ALG_NSUB(ud); i++) {
415 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
428static int algf_gsub (lua_State *L) {
432 int n_match = 0, n_subst = 0, st = 0, last_to = -1;
433 TBuffer BufOut, BufRep, BufTemp, *pBuf = &BufOut;
436 checkarg_gsub (L, &argC, &argE);
438 ud = (TUserdata*) argC.ud;
439 lua_pushvalue (L, 2);
441 else compile_regex (L, &argC, &ud);
442 freelist_init (&freelist);
444 if (argE.reptype == LUA_TSTRING) {
445 buffer_init (&BufRep, 256, L, &freelist);
446 BUFFERZ_PUTREPSTRING (&BufRep, argE.funcpos, ALG_NSUB(ud));
449 if (argE.maxmatch == GSUB_CONDITIONAL) {
450 buffer_init (&BufTemp, 1024, L, &freelist);
454 buffer_init (&BufOut, 1024, L, &freelist);
455 while ((argE.maxmatch < 0 || n_match < argE.maxmatch) && st <= (
int)argE.textlen) {
458 res = gsub_exec (ud, &argE, st);
459 if (ALG_NOMATCH (res)) {
462 else if (!ALG_ISMATCH (res)) {
463 freelist_free (&freelist);
464 return generate_error (L, ud, res);
466 from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
467 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
469 if (st < (
int)argE.textlen) {
470 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
479 buffer_addlstring (&BufOut, argE.text + st, from - st);
485 if (argE.reptype == LUA_TSTRING) {
486 size_t iter = 0, num;
488 while (bufferZ_next (&BufRep, &iter, &num, &str)) {
490 buffer_addlstring (pBuf, str, num);
491 else if (num == 0 || ALG_SUBVALID (ud,num))
492 buffer_addlstring (pBuf, argE.text + ALG_BASE(st) + ALG_SUBBEG(ud,num), ALG_SUBLEN(ud,num));
497 else if (argE.reptype == LUA_TTABLE) {
498 if (ALG_NSUB(ud) > 0)
499 ALG_PUSHSUB_OR_FALSE (L, ud, argE.text + ALG_BASE(st), 1);
501 lua_pushlstring (L, argE.text + from, to - from);
502 lua_gettable (L, argE.funcpos);
505 else if (argE.reptype == LUA_TFUNCTION) {
507 lua_pushvalue (L, argE.funcpos);
508 if (ALG_NSUB(ud) > 0) {
509 push_substrings (L, ud, argE.text + ALG_BASE(st), &freelist);
513 lua_pushlstring (L, argE.text + from, to - from);
516 if (0 != lua_pcall (L, narg, 1, 0)) {
517 freelist_free (&freelist);
518 return lua_error (L);
522 if (argE.reptype == LUA_TTABLE || argE.reptype == LUA_TFUNCTION) {
523 if (lua_tostring (L, -1)) {
524 buffer_addvalue (pBuf, -1);
527 else if (!lua_toboolean (L, -1))
528 buffer_addlstring (pBuf, argE.text + from, to - from);
530 freelist_free (&freelist);
531 luaL_error (L,
"invalid replacement value (a %s)", luaL_typename (L, -1));
533 if (argE.maxmatch != GSUB_CONDITIONAL)
537 if (argE.maxmatch == GSUB_CONDITIONAL) {
539 lua_pushvalue (L, argE.funcpos2);
540 lua_pushinteger (L, from/ALG_CHARSIZE + 1);
541 lua_pushinteger (L, to/ALG_CHARSIZE);
542 if (argE.reptype == LUA_TSTRING)
543 buffer_pushresult (&BufTemp);
545 lua_pushvalue (L, -4);
548 if (0 != lua_pcall (L, 3, 2, 0)) {
549 freelist_free (&freelist);
553 if (lua_isstring (L, -2)) {
554 buffer_addvalue (&BufOut, -2);
557 else if (lua_toboolean (L, -2))
558 buffer_addbuffer (&BufOut, &BufTemp);
560 buffer_addlstring (&BufOut, argE.text + from, to - from);
564 if (lua_type (L, -1) == LUA_TNUMBER) {
565 int n = lua_tointeger (L, -1);
568 argE.maxmatch = n_match + n;
570 else if (lua_toboolean (L, -1))
571 argE.maxmatch = GSUB_UNLIMITED;
573 buffer_clear (&BufTemp);
576 if (argE.maxmatch != GSUB_CONDITIONAL)
580 n_subst += curr_subst;
584 else if (st < (
int)argE.textlen) {
586 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
592 buffer_addlstring (&BufOut, argE.text + st, argE.textlen - st);
593 buffer_pushresult (&BufOut);
594 lua_pushinteger (L, n_match);
595 lua_pushinteger (L, n_subst);
596 freelist_free (&freelist);
609static int algf_count (lua_State *L) {
613 int n_match = 0, st = 0, last_to = -1;
615 checkarg_count (L, &argC, &argE);
617 ud = (TUserdata*) argC.ud;
618 lua_pushvalue (L, 2);
620 else compile_regex (L, &argC, &ud);
622 while (st <= (
int)argE.textlen) {
624 res = gsub_exec (ud, &argE, st);
625 if (ALG_NOMATCH (res)) {
628 else if (!ALG_ISMATCH (res)) {
629 return generate_error (L, ud, res);
631 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
633 if (st < (
int)argE.textlen) {
643 int from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
652 else if (st < (
int)argE.textlen) {
659 lua_pushinteger (L, n_match);
677static int finish_generic_find (lua_State *L, TUserdata *ud,
TArgExec *argE,
680 if (ALG_ISMATCH (res)) {
681 if (method == METHOD_FIND)
682 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE->startoffset), 0);
684 push_substrings (L, ud, argE->text, NULL);
685 else if (method != METHOD_FIND) {
686 ALG_PUSHSUB (L, ud, argE->text, 0);
689 return (method == METHOD_FIND) ? ALG_NSUB(ud) + 2 : ALG_NSUB(ud);
691 else if (ALG_NOMATCH (res))
692 return lua_pushnil (L), 1;
694 return generate_error (L, ud, res);
707static int generic_find_func (lua_State *L,
int method) {
713 checkarg_find_func (L, &argC, &argE);
714 if (argE.startoffset > (
int)argE.textlen)
715 return lua_pushnil (L), 1;
718 ud = (TUserdata*) argC.ud;
719 lua_pushvalue (L, 2);
721 else compile_regex (L, &argC, &ud);
722 res = findmatch_exec (ud, &argE);
723 return finish_generic_find (L, ud, &argE, method, res);
733static int algf_find (lua_State *L) {
734 return generic_find_func (L, METHOD_FIND);
746static int algf_match (lua_State *L) {
747 return generic_find_func (L, METHOD_MATCH);
759static int gmatch_iter (lua_State *L) {
762 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
763 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
764 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
765 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
766 last_end = lua_tointeger (L, lua_upvalueindex (5));
769 if (argE.startoffset > (
int)argE.textlen)
771 res = gmatch_exec (ud, &argE);
772 if (ALG_ISMATCH (res)) {
774 if (!ALG_SUBLEN(ud,0)) {
775 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
776 argE.startoffset += ALG_CHARSIZE;
781 last_end = ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0);
782 lua_pushinteger(L, last_end + incr);
783 lua_replace (L, lua_upvalueindex (4));
784 lua_pushinteger(L, last_end);
785 lua_replace (L, lua_upvalueindex (5));
788 push_substrings (L, ud, argE.text, NULL);
792 ALG_PUSHSUB (L, ud, argE.text, 0);
796 else if (ALG_NOMATCH (res))
799 return generate_error (L, ud, res);
814static int split_iter (lua_State *L) {
815 int incr, last_end, newoffset, res;
817 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
818 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
819 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
820 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
821 incr = lua_tointeger (L, lua_upvalueindex (5));
822 last_end = lua_tointeger (L, lua_upvalueindex (6));
828 if ((newoffset = argE.startoffset + incr) > (
int)argE.textlen)
830 res = split_exec (ud, &argE, newoffset);
831 if (ALG_ISMATCH (res)) {
832 if (!ALG_SUBLEN(ud,0)) {
833 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
834 incr += ALG_CHARSIZE;
838 lua_pushinteger(L, ALG_BASE(newoffset) + ALG_SUBEND(ud,0));
839 lua_pushvalue (L, -1);
840 lua_replace (L, lua_upvalueindex (4));
841 lua_replace (L, lua_upvalueindex (6));
842 lua_pushinteger (L, ALG_SUBLEN(ud,0) ? 0 : ALG_CHARSIZE);
843 lua_replace (L, lua_upvalueindex (5));
845 lua_pushlstring (L, argE.text + argE.startoffset,
846 ALG_SUBBEG(ud,0) + ALG_BASE(newoffset) - argE.startoffset);
849 push_substrings (L, ud, argE.text + ALG_BASE(newoffset), NULL);
850 return 1 + ALG_NSUB(ud);
853 ALG_PUSHSUB (L, ud, argE.text + ALG_BASE(newoffset), 0);
857 else if (ALG_NOMATCH (res))
860 return generate_error (L, ud, res);
862 lua_pushinteger (L, -1);
863 lua_replace (L, lua_upvalueindex (5));
864 lua_pushlstring (L, argE.text+argE.startoffset, argE.textlen-argE.startoffset);
877static int algf_gmatch (lua_State *L)
881 checkarg_gmatch_split (L, &argC, &argE);
883 lua_pushvalue (L, 2);
885 compile_regex (L, &argC, NULL);
886 gmatch_pushsubject (L, &argE);
887 lua_pushinteger (L, argE.eflags);
888 lua_pushinteger (L, 0);
889 lua_pushinteger (L, -1);
890 lua_pushcclosure (L, gmatch_iter, 5);
902static int algf_split (lua_State *L)
906 checkarg_gmatch_split (L, &argC, &argE);
908 lua_pushvalue (L, 2);
910 compile_regex (L, &argC, NULL);
911 gmatch_pushsubject (L, &argE);
912 lua_pushinteger (L, argE.eflags);
913 lua_pushinteger (L, 0);
914 lua_pushinteger (L, 0);
915 lua_pushinteger (L, -1);
916 lua_pushcclosure (L, split_iter, 6);
931static void push_substring_table (lua_State *L, TUserdata *ud,
const char *text) {
934 for (i = 1; i <= ALG_NSUB(ud); i++) {
935 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
936 lua_rawseti (L, -2, i);
950static void push_offset_table (lua_State *L, TUserdata *ud,
int startoffset) {
953 for (i=1, j=1; i <= ALG_NSUB(ud); i++) {
954 if (ALG_SUBVALID (ud,i)) {
955 ALG_PUSHSTART (L, ud, startoffset, i);
956 lua_rawseti (L, -2, j++);
957 ALG_PUSHEND (L, ud, startoffset, i);
958 lua_rawseti (L, -2, j++);
961 lua_pushboolean (L, 0);
962 lua_rawseti (L, -2, j++);
963 lua_pushboolean (L, 0);
964 lua_rawseti (L, -2, j++);
981static int generic_find_method (lua_State *L,
int method) {
986 checkarg_find_method (L, &argE, &ud);
987 if (argE.startoffset > (
int)argE.textlen)
988 return lua_pushnil(L), 1;
990 res = findmatch_exec (ud, &argE);
991 if (ALG_ISMATCH (res)) {
994 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
995 push_offset_table (L, ud, ALG_BASE(argE.startoffset));
996 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
999 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
1000 push_substring_table (L, ud, argE.text);
1001 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
1005 return finish_generic_find (L, ud, &argE, method, res);
1009 else if (ALG_NOMATCH (res))
1010 return lua_pushnil (L), 1;
1012 return generate_error(L, ud, res);
1024static int algm_find (lua_State *L) {
1025 return generic_find_method (L, METHOD_FIND);
1028static int algm_match (lua_State *L) {
1029 return generic_find_method (L, METHOD_MATCH);
1032static int algm_tfind (lua_State *L) {
1033 return generic_find_method (L, METHOD_TFIND);
1044static int algm_exec (lua_State *L) {
1045 return generic_find_method (L, METHOD_EXEC);
1060static void alg_register (lua_State *L,
const luaL_Reg *r_methods,
1061 const luaL_Reg *r_functions,
const char *name) {
1063#if LUA_VERSION_NUM == 501
1065 lua_pushvalue (L, -1);
1066 lua_replace (L, LUA_ENVIRONINDEX);
1067 luaL_register (L, NULL, r_methods);
1069 luaL_newmetatable(L, REX_TYPENAME);
1070 lua_pushvalue(L, -1);
1071 luaL_setfuncs (L, r_methods, 1);
1073 lua_pushvalue(L, -1);
1074 lua_setfield(L, -2,
"__index");
1077 lua_createtable(L, 0, 8);
1078#if LUA_VERSION_NUM == 501
1079 luaL_register (L, NULL, r_functions);
1081 lua_pushvalue(L, -2);
1082 luaL_setfuncs (L, r_functions, 1);
1084#ifdef REX_CREATEGLOBALVAR
1085 lua_pushvalue(L, -1);
1086 lua_setglobal(L, REX_LIBNAME);
1088 lua_pushfstring (L, REX_VERSION
" (for %s)", name);
1089 lua_setfield (L, -2,
"_VERSION");
1090#ifndef REX_NOEMBEDDEDTEST
1091 lua_pushcfunction (L, newmembuffer);
1092 lua_setfield (L, -2,
"_newmembuffer");