summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReuben Thomas <rrt@sc3d.org>2010-07-31 23:11:42 +0100
committerReuben Thomas <rrt@sc3d.org>2010-07-31 23:11:42 +0100
commit9c8f7c9708386917bd73e2ceeb01caf9f39a4a7e (patch)
tree73b6da98c91517586209d22192d8e4f662fda54d
parentc37e2f1f4098d15302343459150da3a4f9e1df4e (diff)
downloadlrexlib-9c8f7c9708386917bd73e2ceeb01caf9f39a4a7e.tar.gz
Simplify collection of extra compilation arguments.
-rwxr-xr-xsrc/algo.h28
-rwxr-xr-xsrc/gnu/lgnu.c44
-rwxr-xr-xsrc/oniguruma/lonig.c22
-rwxr-xr-xsrc/pcre/lpcre.c6
4 files changed, 42 insertions, 58 deletions
diff --git a/src/algo.h b/src/algo.h
index 212933e..b914312 100755
--- a/src/algo.h
+++ b/src/algo.h
@@ -12,16 +12,8 @@ static int split_exec (TUserdata *ud, TArgExec *argE, int offset);
static int compile_regex (lua_State *L, const TArgComp *argC, TUserdata **pud);
static int generate_error (lua_State *L, const TUserdata *ud, int errcode);
-#ifndef ALG_OPTLOCALE
-# define ALG_OPTLOCALE(a,b,c)
-#endif
-
-#ifndef ALG_OPTSYNTAX
-# define ALG_OPTSYNTAX(a,b,c)
-#endif
-
-#ifndef ALG_OPTTRANSLATE
-# define ALG_OPTTRANSLATE(a,b,c)
+#ifndef ALG_GETCARGS
+# define ALG_GETCARGS(a,b,c)
#endif
#ifndef DO_NAMED_SUBPATTERNS
@@ -121,9 +113,7 @@ static void check_pattern (lua_State *L, int pos, TArgComp *argC)
static void checkarg_new (lua_State *L, TArgComp *argC) {
argC->pattern = luaL_checklstring (L, 1, &argC->patlen);
argC->cflags = ALG_GETCFLAGS (L, 2);
- ALG_OPTLOCALE (argC, L, 3);
- ALG_OPTTRANSLATE (argC, L, 3);
- ALG_OPTSYNTAX (argC, L, 4);
+ ALG_GETCARGS (L, 3, argC);
}
@@ -142,9 +132,7 @@ static void checkarg_gsub (lua_State *L, TArgComp *argC, TArgExec *argE) {
argE->maxmatch = OptLimit (L, 4);
argC->cflags = ALG_GETCFLAGS (L, 5);
argE->eflags = luaL_optint (L, 6, ALG_EFLAGS_DFLT);
- ALG_OPTLOCALE (argC, L, 7);
- ALG_OPTTRANSLATE (argC, L, 7);
- ALG_OPTSYNTAX (argC, L, 8);
+ ALG_GETCARGS (L, 7, argC);
}
@@ -156,9 +144,7 @@ static void checkarg_find_func (lua_State *L, TArgComp *argC, TArgExec *argE) {
argE->startoffset = get_startoffset (L, 3, argE->textlen);
argC->cflags = ALG_GETCFLAGS (L, 4);
argE->eflags = luaL_optint (L, 5, ALG_EFLAGS_DFLT);
- ALG_OPTLOCALE (argC, L, 6);
- ALG_OPTTRANSLATE (argC, L, 6);
- ALG_OPTSYNTAX (argC, L, 7);
+ ALG_GETCARGS (L, 6, argC);
}
@@ -169,9 +155,7 @@ static void checkarg_gmatch_split (lua_State *L, TArgComp *argC, TArgExec *argE)
check_pattern (L, 2, argC);
argC->cflags = ALG_GETCFLAGS (L, 3);
argE->eflags = luaL_optint (L, 4, ALG_EFLAGS_DFLT);
- ALG_OPTLOCALE (argC, L, 5);
- ALG_OPTTRANSLATE (argC, L, 5);
- ALG_OPTSYNTAX (argC, L, 6);
+ ALG_GETCARGS (L, 5, argC);
}
diff --git a/src/gnu/lgnu.c b/src/gnu/lgnu.c
index 0cc4b8c..502ef66 100755
--- a/src/gnu/lgnu.c
+++ b/src/gnu/lgnu.c
@@ -32,11 +32,8 @@
#define ALG_GETCFLAGS(L,pos) ALG_CFLAGS_DFLT
-static void opttranslate (TArgComp *argC, lua_State *L, int pos);
-#define ALG_OPTTRANSLATE(a,b,c) opttranslate(a,b,c)
-
-static void optsyntax (TArgComp *argC, lua_State *L, int pos);
-#define ALG_OPTSYNTAX(a,b,c) optsyntax(a,b,c)
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC);
+#define ALG_GETCARGS(a,b,c) checkarg_compile(a,b,c)
#define ALG_NOMATCH(res) ((res) == -1 || (res) == -2)
#define ALG_ISMATCH(res) ((res) >= 0)
@@ -98,21 +95,23 @@ static int generate_error (lua_State *L, const TUserdata *ud, int errcode) {
}
#define ALG_TRANSLATE_SIZE (UCHAR_MAX + 1)
-static void opttranslate (TArgComp *argC, lua_State *L, int pos) {
- if (!lua_isnoneornil (L, pos)) {
- unsigned i;
-
- argC->translate = (const unsigned char *) Lmalloc (L, ALG_TRANSLATE_SIZE);
- memset ((unsigned char *) argC->translate, 0, ALG_TRANSLATE_SIZE); /* initialize all members to 0 */
- for (i = 0; i <= UCHAR_MAX; i++) {
- lua_pushinteger (L, i);
- lua_gettable (L, pos);
- if (lua_tostring (L, -1))
- ((unsigned char *) argC->translate)[i] = *lua_tostring (L, -1);
- lua_pop (L, 1);
- }
- } else
- argC->translate = NULL;
+static const unsigned char *gettranslate (lua_State *L, int pos) {
+ unsigned i;
+ const unsigned char *translate;
+
+ if (lua_isnoneornil (L, pos))
+ return NULL;
+
+ translate = (const unsigned char *) Lmalloc (L, ALG_TRANSLATE_SIZE);
+ memset ((unsigned char *) translate, 0, ALG_TRANSLATE_SIZE); /* initialize all members to 0 */
+ for (i = 0; i <= UCHAR_MAX; i++) {
+ lua_pushinteger (L, i);
+ lua_gettable (L, pos);
+ if (lua_tostring (L, -1))
+ ((unsigned char *) translate)[i] = *lua_tostring (L, -1);
+ lua_pop (L, 1);
+ }
+ return translate;
}
typedef struct {
@@ -155,8 +154,9 @@ static int getsyntax (lua_State *L, int pos) {
return found->value;
}
-static void optsyntax (TArgComp *argC, lua_State *L, int pos) {
- argC->gnusyn = getsyntax (L, pos);
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC) {
+ argC->translate = gettranslate (L, pos);
+ argC->gnusyn = getsyntax (L, pos + 1);
}
static void seteflags (TGnu *ud, TArgExec *argE) {
diff --git a/src/oniguruma/lonig.c b/src/oniguruma/lonig.c
index 72e5ae0..eb09f73 100755
--- a/src/oniguruma/lonig.c
+++ b/src/oniguruma/lonig.c
@@ -31,11 +31,8 @@ extern int LOnig_get_flags (lua_State *L);
static int getcflags (lua_State *L, int pos);
#define ALG_GETCFLAGS(L,pos) getcflags(L, pos)
-static void optlocale (TArgComp *argC, lua_State *L, int pos);
-#define ALG_OPTLOCALE(a,b,c) optlocale(a,b,c)
-
-static void optsyntax (TArgComp *argC, lua_State *L, int pos);
-#define ALG_OPTSYNTAX(a,b,c) optsyntax(a,b,c)
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC);
+#define ALG_GETCARGS(a,b,c) checkarg_compile(a,b,c)
#define ALG_NOMATCH(res) ((res) == ONIG_MISMATCH)
#define ALG_ISMATCH(res) ((res) >= 0)
@@ -176,17 +173,19 @@ static int fcmp(const void *p1, const void *p2) {
return strcmp(((EncPair*)p1)->name, ((EncPair*)p2)->name);
}
-static void optlocale (TArgComp *argC, lua_State *L, int pos) {
+static const char *getlocale (lua_State *L, int pos) {
EncPair key;
if ((key.name = luaL_optstring(L, pos, NULL)) == NULL)
- argC->locale = (const char*)ONIG_ENCODING_ASCII;
+ return (const char*)ONIG_ENCODING_ASCII;
else {
EncPair *pair = (EncPair*) bsearch(&key, Encodings,
sizeof(Encodings)/sizeof(EncPair), sizeof(EncPair), fcmp);
if (pair != NULL)
- argC->locale = (const char*)pair->value;
- else
+ return (const char*)pair->value;
+ else {
luaL_argerror(L, pos, "invalid or unsupported encoding string");
+ return NULL;
+ }
}
}
@@ -201,8 +200,9 @@ static void *getsyntax (lua_State *L, int pos) {
return found->value;
}
-static void optsyntax (TArgComp *argC, lua_State *L, int pos) {
- argC->syntax = getsyntax(L, pos);
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC) {
+ argC->locale = getlocale (L, pos);
+ argC->syntax = getsyntax (L, pos + 1);
}
/*
diff --git a/src/pcre/lpcre.c b/src/pcre/lpcre.c
index 199cf42..bf72f13 100755
--- a/src/pcre/lpcre.c
+++ b/src/pcre/lpcre.c
@@ -33,8 +33,8 @@ extern flag_pair pcre_error_flags[];
static int getcflags (lua_State *L, int pos);
#define ALG_GETCFLAGS(L,pos) getcflags(L, pos)
-static void optlocale (TArgComp *argC, lua_State *L, int pos);
-#define ALG_OPTLOCALE(a,b,c) optlocale(a,b,c)
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC);
+#define ALG_GETCARGS(a,b,c) checkarg_compile(a,b,c)
#define ALG_NOMATCH(res) ((res) == PCRE_ERROR_NOMATCH)
#define ALG_ISMATCH(res) ((res) >= 0)
@@ -169,7 +169,7 @@ static int chartables_gc (lua_State *L) {
return 0;
}
-static void optlocale (TArgComp *argC, lua_State *L, int pos) {
+static void checkarg_compile (lua_State *L, int pos, TArgComp *argC) {
argC->locale = NULL;
argC->tables = NULL;
if (!lua_isnoneornil (L, pos)) {