summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReuben Thomas <rrt@sc3d.org>2010-07-31 20:13:55 +0100
committerReuben Thomas <rrt@sc3d.org>2010-07-31 20:13:55 +0100
commitd1152cf9502e53b4f8191cd948badf2003745f11 (patch)
treedee94da2b053eed43902a3c5dcccc5b0439f28d7
parenta896b1b439a6b7ac2650d643b40b65357962d858 (diff)
downloadlrexlib-d1152cf9502e53b4f8191cd948badf2003745f11.tar.gz
Implement reverse searching in GNU, and add a test for it.
-rwxr-xr-xsrc/gnu/lgnu.c22
-rwxr-xr-xtest/gnu_sets.lua1
2 files changed, 17 insertions, 6 deletions
diff --git a/src/gnu/lgnu.c b/src/gnu/lgnu.c
index 7b5c746..0cc4b8c 100755
--- a/src/gnu/lgnu.c
+++ b/src/gnu/lgnu.c
@@ -64,7 +64,6 @@ typedef struct {
struct re_registers match;
int freed;
const char * errmsg;
- int reverse;
} TGnu;
#define TUserdata TGnu
@@ -163,7 +162,6 @@ static void optsyntax (TArgComp *argC, lua_State *L, int pos) {
static void seteflags (TGnu *ud, TArgExec *argE) {
ud->r.not_bol = (argE->eflags & GNU_NOTBOL) != 0;
ud->r.not_eol = (argE->eflags & GNU_NOTEOL) != 0;
- ud->reverse = (argE->eflags & GNU_REVERSE) != 0;
}
/*
@@ -218,7 +216,10 @@ static int gmatch_exec (TUserdata *ud, TArgExec *argE) {
ud->r.not_bol = 1;
argE->text += argE->startoffset;
argE->textlen -= argE->startoffset;
- return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
+ if (argE->eflags & GNU_REVERSE)
+ return re_search (&ud->r, argE->text, argE->textlen, argE->textlen, -argE->textlen, &ud->match);
+ else
+ return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
}
static void gmatch_pushsubject (lua_State *L, TArgExec *argE) {
@@ -229,21 +230,30 @@ static int findmatch_exec (TGnu *ud, TArgExec *argE) {
argE->text += argE->startoffset;
argE->textlen -= argE->startoffset;
seteflags (ud, argE);
- return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
+ if (argE->eflags & GNU_REVERSE)
+ return re_search (&ud->r, argE->text, argE->textlen, argE->textlen, -argE->textlen, &ud->match);
+ else
+ return re_search (&ud->r, argE->text, argE->textlen, 0, argE->textlen, &ud->match);
}
static int gsub_exec (TGnu *ud, TArgExec *argE, int st) {
seteflags (ud, argE);
if (st > 0)
ud->r.not_bol = 1;
- return re_search (&ud->r, argE->text + st, argE->textlen - st, 0, argE->textlen - st, &ud->match);
+ if (argE->eflags & GNU_REVERSE)
+ return re_search (&ud->r, argE->text + st, argE->textlen - st, argE->textlen - st, -(argE->textlen - st), &ud->match);
+ else
+ return re_search (&ud->r, argE->text + st, argE->textlen - st, 0, argE->textlen - st, &ud->match);
}
static int split_exec (TGnu *ud, TArgExec *argE, int offset) {
seteflags (ud, argE);
if (offset > 0)
ud->r.not_bol = 1;
- return re_search (&ud->r, argE->text + offset, argE->textlen - offset, 0, argE->textlen - offset, &ud->match);
+ if (argE->eflags & GNU_REVERSE)
+ return re_search (&ud->r, argE->text + offset, argE->textlen - offset, argE->textlen - offset, -(argE->textlen - offset), &ud->match);
+ else
+ return re_search (&ud->r, argE->text + offset, argE->textlen - offset, 0, argE->textlen - offset, &ud->match);
}
static int Gnu_gc (lua_State *L) {
diff --git a/test/gnu_sets.lua b/test/gnu_sets.lua
index e88fc1c..519705a 100755
--- a/test/gnu_sets.lua
+++ b/test/gnu_sets.lua
@@ -39,6 +39,7 @@ return {
{ {"abcd", ".+", 5}, { N } }, -- failing st
{ {"abc", "^abc"}, {"abc" } }, -- anchor
{ {"abc", "^abc", N,N,flg.not_bol}, { N } }, -- anchor + ef
+ { {"cabcaab", "ca+b", N,N,flg.reverse}, {"caab" } }, -- reverse search
}
end