7#define REX_VERSION "Lrexlib " VERSION
16static void gmatch_pushsubject (lua_State *L,
TArgExec *argE);
25static int findmatch_exec (TUserdata *ud,
TArgExec *argE);
35static int split_exec (TUserdata *ud,
TArgExec *argE,
int offset);
45static int gsub_exec (TUserdata *ud,
TArgExec *argE,
int offset);
54static int gmatch_exec (TUserdata *ud,
TArgExec *argE);
64static int compile_regex (lua_State *L,
const TArgComp *argC, TUserdata **pud);
74static int generate_error (lua_State *L,
const TUserdata *ud,
int errcode);
76#if LUA_VERSION_NUM == 501
77# define ALG_ENVIRONINDEX LUA_ENVIRONINDEX
79# define ALG_ENVIRONINDEX lua_upvalueindex(1)
83# define ALG_CHARSIZE 1
86#ifndef BUFFERZ_PUTREPSTRING
87# define BUFFERZ_PUTREPSTRING bufferZ_putrepstring
91# define ALG_GETCARGS(a,b,c)
94#ifndef DO_NAMED_SUBPATTERNS
95#define DO_NAMED_SUBPATTERNS(a,b,c)
101#define METHOD_TFIND 3
116static int OptLimit (lua_State *L,
int pos) {
117 if (lua_isnoneornil (L, pos))
118 return GSUB_UNLIMITED;
119 if (lua_isfunction (L, pos))
120 return GSUB_CONDITIONAL;
121 if (lua_isnumber (L, pos)) {
122 int a = lua_tointeger (L, pos);
123 return a < 0 ? 0 : a;
125 return luaL_typerror (L, pos,
"number or function");
141static int get_startoffset(lua_State *L,
int stackpos,
size_t len) {
142 int startoffset = (int)luaL_optinteger(L, stackpos, 1);
145 else if(startoffset < 0) {
146 startoffset += len/ALG_CHARSIZE;
150 return startoffset*ALG_CHARSIZE;
164static TUserdata* test_ud (lua_State *L,
int pos)
167 if (lua_getmetatable(L, pos) &&
168 lua_rawequal(L, -1, ALG_ENVIRONINDEX) &&
169 (ud = (TUserdata *)lua_touserdata(L, pos)) != NULL) {
185static TUserdata* check_ud (lua_State *L)
187 TUserdata *ud = test_ud(L, 1);
188 if (ud == NULL) luaL_typerror(L, 1, REX_TYPENAME);
200static void check_subject (lua_State *L,
int pos,
TArgExec *argE)
203 argE->text = lua_tolstring (L, pos, &argE->textlen);
204 stype = lua_type (L, pos);
205 if (stype != LUA_TSTRING && stype != LUA_TTABLE && stype != LUA_TUSERDATA) {
206 luaL_typerror (L, pos,
"string, table or userdata");
207 }
else if (argE->text == NULL) {
209 lua_getfield (L, pos,
"topointer");
210 if (lua_type (L, -1) != LUA_TFUNCTION)
211 luaL_error (L,
"subject has no topointer method");
212 lua_pushvalue (L, pos);
214 type = lua_type (L, -1);
215 if (type != LUA_TLIGHTUSERDATA)
216 luaL_error (L,
"subject's topointer method returned %s (expected lightuserdata)",
217 lua_typename (L, type));
218 argE->text = (
const char*) lua_touserdata (L, -1);
220#if LUA_VERSION_NUM == 501
221 if (luaL_callmeta (L, pos,
"__len")) {
222 if (lua_type (L, -1) != LUA_TNUMBER)
223 luaL_argerror (L, pos,
"subject's length is not a number");
224 argE->textlen = lua_tointeger (L, -1);
228 argE->textlen = lua_objlen (L, pos);
230 argE->textlen = luaL_len (L, pos);
246static void check_pattern (lua_State *L,
int pos,
TArgComp *argC)
248 if (lua_isstring (L, pos)) {
249 argC->pattern = lua_tolstring (L, pos, &argC->patlen);
252 else if ((argC->ud = test_ud (L, pos)) == NULL)
253 luaL_typerror(L, pos,
"string or " REX_TYPENAME);
264static void checkarg_new (lua_State *L,
TArgComp *argC) {
265 argC->pattern = luaL_checklstring (L, 1, &argC->patlen);
266 argC->cflags = ALG_GETCFLAGS (L, 2);
267 ALG_GETCARGS (L, 3, argC);
283 check_subject (L, 1, argE);
284 check_pattern (L, 2, argC);
286 argE->reptype = lua_type (L, 3);
287 if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE &&
288 argE->reptype != LUA_TFUNCTION) {
289 luaL_typerror (L, 3,
"string, table or function");
293 argE->maxmatch = OptLimit (L, 4);
294 argC->cflags = ALG_GETCFLAGS (L, 5);
295 argE->eflags = (int)luaL_optinteger (L, 6, ALG_EFLAGS_DFLT);
296 ALG_GETCARGS (L, 7, argC);
313 check_subject (L, 1, argE);
314 check_pattern (L, 2, argC);
315 argC->cflags = ALG_GETCFLAGS (L, 3);
316 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
317 ALG_GETCARGS (L, 5, argC);
333static void checkarg_find_func (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
334 check_subject (L, 1, argE);
335 check_pattern (L, 2, argC);
336 argE->startoffset = get_startoffset (L, 3, argE->textlen);
337 argC->cflags = ALG_GETCFLAGS (L, 4);
338 argE->eflags = (int)luaL_optinteger (L, 5, ALG_EFLAGS_DFLT);
339 ALG_GETCARGS (L, 6, argC);
355static void checkarg_gmatch_split (lua_State *L,
TArgComp *argC,
TArgExec *argE) {
356 check_subject (L, 1, argE);
357 check_pattern (L, 2, argC);
358 argC->cflags = ALG_GETCFLAGS (L, 3);
359 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
360 ALG_GETCARGS (L, 5, argC);
378static void checkarg_find_method (lua_State *L,
TArgExec *argE, TUserdata **ud) {
380 check_subject (L, 2, argE);
381 argE->startoffset = get_startoffset (L, 3, argE->textlen);
382 argE->eflags = (int)luaL_optinteger (L, 4, ALG_EFLAGS_DFLT);
394static int algf_new (lua_State *L) {
396 checkarg_new (L, &argC);
397 return compile_regex (L, &argC, NULL);
412static void push_substrings (lua_State *L, TUserdata *ud,
const char *text,
415 if (lua_checkstack (L, ALG_NSUB(ud)) == 0) {
417 freelist_free (freelist);
418 luaL_error (L,
"cannot add %d stack slots", ALG_NSUB(ud));
420 for (i = 1; i <= ALG_NSUB(ud); i++) {
421 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
434static int algf_gsub (lua_State *L) {
438 int n_match = 0, n_subst = 0, st = 0, last_to = -1;
439 TBuffer BufOut, BufRep, BufTemp, *pBuf = &BufOut;
442 checkarg_gsub (L, &argC, &argE);
444 ud = (TUserdata*) argC.ud;
445 lua_pushvalue (L, 2);
447 else compile_regex (L, &argC, &ud);
448 freelist_init (&freelist);
450 if (argE.reptype == LUA_TSTRING) {
451 buffer_init (&BufRep, 256, L, &freelist);
452 BUFFERZ_PUTREPSTRING (&BufRep, argE.funcpos, ALG_NSUB(ud));
455 if (argE.maxmatch == GSUB_CONDITIONAL) {
456 buffer_init (&BufTemp, 1024, L, &freelist);
460 buffer_init (&BufOut, 1024, L, &freelist);
461 while ((argE.maxmatch < 0 || n_match < argE.maxmatch) && st <= (
int)argE.textlen) {
464 res = gsub_exec (ud, &argE, st);
465 if (ALG_NOMATCH (res)) {
468 else if (!ALG_ISMATCH (res)) {
469 freelist_free (&freelist);
470 return generate_error (L, ud, res);
472 from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
473 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
475 if (st < (
int)argE.textlen) {
476 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
485 buffer_addlstring (&BufOut, argE.text + st, from - st);
491 if (argE.reptype == LUA_TSTRING) {
492 size_t iter = 0, num;
494 while (bufferZ_next (&BufRep, &iter, &num, &str)) {
496 buffer_addlstring (pBuf, str, num);
497 else if (num == 0 || ALG_SUBVALID (ud,num))
498 buffer_addlstring (pBuf, argE.text + ALG_BASE(st) + ALG_SUBBEG(ud,num), ALG_SUBLEN(ud,num));
503 else if (argE.reptype == LUA_TTABLE) {
504 if (ALG_NSUB(ud) > 0)
505 ALG_PUSHSUB_OR_FALSE (L, ud, argE.text + ALG_BASE(st), 1);
507 lua_pushlstring (L, argE.text + from, to - from);
508 lua_gettable (L, argE.funcpos);
511 else if (argE.reptype == LUA_TFUNCTION) {
513 lua_pushvalue (L, argE.funcpos);
514 if (ALG_NSUB(ud) > 0) {
515 push_substrings (L, ud, argE.text + ALG_BASE(st), &freelist);
519 lua_pushlstring (L, argE.text + from, to - from);
522 if (0 != lua_pcall (L, narg, 1, 0)) {
523 freelist_free (&freelist);
524 return lua_error (L);
528 if (argE.reptype == LUA_TTABLE || argE.reptype == LUA_TFUNCTION) {
529 if (lua_tostring (L, -1)) {
530 buffer_addvalue (pBuf, -1);
533 else if (!lua_toboolean (L, -1))
534 buffer_addlstring (pBuf, argE.text + from, to - from);
536 freelist_free (&freelist);
537 luaL_error (L,
"invalid replacement value (a %s)", luaL_typename (L, -1));
539 if (argE.maxmatch != GSUB_CONDITIONAL)
543 if (argE.maxmatch == GSUB_CONDITIONAL) {
545 lua_pushvalue (L, argE.funcpos2);
546 lua_pushinteger (L, from/ALG_CHARSIZE + 1);
547 lua_pushinteger (L, to/ALG_CHARSIZE);
548 if (argE.reptype == LUA_TSTRING)
549 buffer_pushresult (&BufTemp);
551 lua_pushvalue (L, -4);
554 if (0 != lua_pcall (L, 3, 2, 0)) {
555 freelist_free (&freelist);
559 if (lua_isstring (L, -2)) {
560 buffer_addvalue (&BufOut, -2);
563 else if (lua_toboolean (L, -2))
564 buffer_addbuffer (&BufOut, &BufTemp);
566 buffer_addlstring (&BufOut, argE.text + from, to - from);
570 if (lua_type (L, -1) == LUA_TNUMBER) {
571 int n = lua_tointeger (L, -1);
574 argE.maxmatch = n_match + n;
576 else if (lua_toboolean (L, -1))
577 argE.maxmatch = GSUB_UNLIMITED;
579 buffer_clear (&BufTemp);
582 if (argE.maxmatch != GSUB_CONDITIONAL)
586 n_subst += curr_subst;
590 else if (st < (
int)argE.textlen) {
592 buffer_addlstring (&BufOut, argE.text + st, ALG_CHARSIZE);
598 buffer_addlstring (&BufOut, argE.text + st, argE.textlen - st);
599 buffer_pushresult (&BufOut);
600 lua_pushinteger (L, n_match);
601 lua_pushinteger (L, n_subst);
602 freelist_free (&freelist);
615static int algf_count (lua_State *L) {
619 int n_match = 0, st = 0, last_to = -1;
621 checkarg_count (L, &argC, &argE);
623 ud = (TUserdata*) argC.ud;
624 lua_pushvalue (L, 2);
626 else compile_regex (L, &argC, &ud);
628 while (st <= (
int)argE.textlen) {
630 res = gsub_exec (ud, &argE, st);
631 if (ALG_NOMATCH (res)) {
634 else if (!ALG_ISMATCH (res)) {
635 return generate_error (L, ud, res);
637 to = ALG_BASE(st) + ALG_SUBEND(ud,0);
639 if (st < (
int)argE.textlen) {
649 int from = ALG_BASE(st) + ALG_SUBBEG(ud,0);
658 else if (st < (
int)argE.textlen) {
665 lua_pushinteger (L, n_match);
683static int finish_generic_find (lua_State *L, TUserdata *ud,
TArgExec *argE,
686 if (ALG_ISMATCH (res)) {
687 if (method == METHOD_FIND)
688 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE->startoffset), 0);
690 push_substrings (L, ud, argE->text, NULL);
691 else if (method != METHOD_FIND) {
692 ALG_PUSHSUB (L, ud, argE->text, 0);
695 return (method == METHOD_FIND) ? ALG_NSUB(ud) + 2 : ALG_NSUB(ud);
697 else if (ALG_NOMATCH (res))
698 return lua_pushnil (L), 1;
700 return generate_error (L, ud, res);
713static int generic_find_func (lua_State *L,
int method) {
719 checkarg_find_func (L, &argC, &argE);
720 if (argE.startoffset > (
int)argE.textlen)
721 return lua_pushnil (L), 1;
724 ud = (TUserdata*) argC.ud;
725 lua_pushvalue (L, 2);
727 else compile_regex (L, &argC, &ud);
728 res = findmatch_exec (ud, &argE);
729 return finish_generic_find (L, ud, &argE, method, res);
739static int algf_find (lua_State *L) {
740 return generic_find_func (L, METHOD_FIND);
752static int algf_match (lua_State *L) {
753 return generic_find_func (L, METHOD_MATCH);
765static int gmatch_iter (lua_State *L) {
768 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
769 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
770 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
771 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
772 last_end = lua_tointeger (L, lua_upvalueindex (5));
775 if (argE.startoffset > (
int)argE.textlen)
777 res = gmatch_exec (ud, &argE);
778 if (ALG_ISMATCH (res)) {
780 if (!ALG_SUBLEN(ud,0)) {
781 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
782 argE.startoffset += ALG_CHARSIZE;
787 last_end = ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0);
788 lua_pushinteger(L, last_end + incr);
789 lua_replace (L, lua_upvalueindex (4));
790 lua_pushinteger(L, last_end);
791 lua_replace (L, lua_upvalueindex (5));
794 push_substrings (L, ud, argE.text, NULL);
798 ALG_PUSHSUB (L, ud, argE.text, 0);
802 else if (ALG_NOMATCH (res))
805 return generate_error (L, ud, res);
820static int split_iter (lua_State *L) {
821 int incr, last_end, newoffset, res;
823 TUserdata *ud = (TUserdata*) lua_touserdata (L, lua_upvalueindex (1));
824 argE.text = lua_tolstring (L, lua_upvalueindex (2), &argE.textlen);
825 argE.eflags = lua_tointeger (L, lua_upvalueindex (3));
826 argE.startoffset = lua_tointeger (L, lua_upvalueindex (4));
827 incr = lua_tointeger (L, lua_upvalueindex (5));
828 last_end = lua_tointeger (L, lua_upvalueindex (6));
834 if ((newoffset = argE.startoffset + incr) > (
int)argE.textlen)
836 res = split_exec (ud, &argE, newoffset);
837 if (ALG_ISMATCH (res)) {
838 if (!ALG_SUBLEN(ud,0)) {
839 if (last_end == ALG_BASE(argE.startoffset) + ALG_SUBEND(ud,0)) {
840 incr += ALG_CHARSIZE;
844 lua_pushinteger(L, ALG_BASE(newoffset) + ALG_SUBEND(ud,0));
845 lua_pushvalue (L, -1);
846 lua_replace (L, lua_upvalueindex (4));
847 lua_replace (L, lua_upvalueindex (6));
848 lua_pushinteger (L, ALG_SUBLEN(ud,0) ? 0 : ALG_CHARSIZE);
849 lua_replace (L, lua_upvalueindex (5));
851 lua_pushlstring (L, argE.text + argE.startoffset,
852 ALG_SUBBEG(ud,0) + ALG_BASE(newoffset) - argE.startoffset);
855 push_substrings (L, ud, argE.text + ALG_BASE(newoffset), NULL);
856 return 1 + ALG_NSUB(ud);
859 ALG_PUSHSUB (L, ud, argE.text + ALG_BASE(newoffset), 0);
863 else if (ALG_NOMATCH (res))
866 return generate_error (L, ud, res);
868 lua_pushinteger (L, -1);
869 lua_replace (L, lua_upvalueindex (5));
870 lua_pushlstring (L, argE.text+argE.startoffset, argE.textlen-argE.startoffset);
883static int algf_gmatch (lua_State *L)
887 checkarg_gmatch_split (L, &argC, &argE);
889 lua_pushvalue (L, 2);
891 compile_regex (L, &argC, NULL);
892 gmatch_pushsubject (L, &argE);
893 lua_pushinteger (L, argE.eflags);
894 lua_pushinteger (L, 0);
895 lua_pushinteger (L, -1);
896 lua_pushcclosure (L, gmatch_iter, 5);
908static int algf_split (lua_State *L)
912 checkarg_gmatch_split (L, &argC, &argE);
914 lua_pushvalue (L, 2);
916 compile_regex (L, &argC, NULL);
917 gmatch_pushsubject (L, &argE);
918 lua_pushinteger (L, argE.eflags);
919 lua_pushinteger (L, 0);
920 lua_pushinteger (L, 0);
921 lua_pushinteger (L, -1);
922 lua_pushcclosure (L, split_iter, 6);
937static void push_substring_table (lua_State *L, TUserdata *ud,
const char *text) {
940 for (i = 1; i <= ALG_NSUB(ud); i++) {
941 ALG_PUSHSUB_OR_FALSE (L, ud, text, i);
942 lua_rawseti (L, -2, i);
956static void push_offset_table (lua_State *L, TUserdata *ud,
int startoffset) {
959 for (i=1, j=1; i <= ALG_NSUB(ud); i++) {
960 if (ALG_SUBVALID (ud,i)) {
961 ALG_PUSHSTART (L, ud, startoffset, i);
962 lua_rawseti (L, -2, j++);
963 ALG_PUSHEND (L, ud, startoffset, i);
964 lua_rawseti (L, -2, j++);
967 lua_pushboolean (L, 0);
968 lua_rawseti (L, -2, j++);
969 lua_pushboolean (L, 0);
970 lua_rawseti (L, -2, j++);
987static int generic_find_method (lua_State *L,
int method) {
992 checkarg_find_method (L, &argE, &ud);
993 if (argE.startoffset > (
int)argE.textlen)
994 return lua_pushnil(L), 1;
996 res = findmatch_exec (ud, &argE);
997 if (ALG_ISMATCH (res)) {
1000 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
1001 push_offset_table (L, ud, ALG_BASE(argE.startoffset));
1002 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
1005 ALG_PUSHOFFSETS (L, ud, ALG_BASE(argE.startoffset), 0);
1006 push_substring_table (L, ud, argE.text);
1007 DO_NAMED_SUBPATTERNS (L, ud, argE.text);
1011 return finish_generic_find (L, ud, &argE, method, res);
1015 else if (ALG_NOMATCH (res))
1016 return lua_pushnil (L), 1;
1018 return generate_error(L, ud, res);
1030static int algm_find (lua_State *L) {
1031 return generic_find_method (L, METHOD_FIND);
1042static int algm_match (lua_State *L) {
1043 return generic_find_method (L, METHOD_MATCH);
1052static int algm_tfind (lua_State *L) {
1053 return generic_find_method (L, METHOD_TFIND);
1064static int algm_exec (lua_State *L) {
1065 return generic_find_method (L, METHOD_EXEC);
1080static void alg_register (lua_State *L,
const luaL_Reg *r_methods,
1081 const luaL_Reg *r_functions,
const char *name) {
1083#if LUA_VERSION_NUM == 501
1085 lua_pushvalue (L, -1);
1086 lua_replace (L, LUA_ENVIRONINDEX);
1087 luaL_register (L, NULL, r_methods);
1089 luaL_newmetatable(L, REX_TYPENAME);
1090 lua_pushvalue(L, -1);
1091 luaL_setfuncs (L, r_methods, 1);
1093 lua_pushvalue(L, -1);
1094 lua_setfield(L, -2,
"__index");
1097 lua_createtable(L, 0, 8);
1098#if LUA_VERSION_NUM == 501
1099 luaL_register (L, NULL, r_functions);
1101 lua_pushvalue(L, -2);
1102 luaL_setfuncs (L, r_functions, 1);
1104#ifdef REX_CREATEGLOBALVAR
1105 lua_pushvalue(L, -1);
1106 lua_setglobal(L, REX_LIBNAME);
1108 lua_pushfstring (L, REX_VERSION
" (for %s)", name);
1109 lua_setfield (L, -2,
"_VERSION");
1110#ifndef REX_NOEMBEDDEDTEST
1111 lua_pushcfunction (L, newmembuffer);
1112 lua_setfield (L, -2,
"_newmembuffer");