Skip to content

Commit

Permalink
lregex: introduce a helper function for getting the match location fr…
Browse files Browse the repository at this point in the history
…om the window

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed May 16, 2021
1 parent 77bd99b commit cf6b3bb
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions main/lregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ static void scriptEvalHook (OptVM *vm, struct lregexControlBlock *lcb, enum hook
static void scriptTeardown (OptVM *vm, struct lregexControlBlock *lcb);

static char* make_match_string (scriptWindow *window, int group);
static matchLoc *make_mloc (scriptWindow *window, int group, bool start);

static void deleteTable (void *ptrn)
{
Expand Down Expand Up @@ -3275,28 +3276,10 @@ static EsObject* lrop_get_match_loc (OptVM *vm, EsObject *name)
struct lregexControlBlock *lcb = opt_vm_get_app_data (vm);
scriptWindow *window = lcb->window;

if (window == NULL
|| window->nmatch <= g
|| window->pmatch [g].rm_so == -1)
matchLoc *mloc = make_mloc (window, g, start);
if (mloc == NULL)
return OPT_ERR_RANGECHECK;

matchLoc *mloc = xMalloc (1, matchLoc);
if (window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
{
mloc->delta = 0;
mloc->line = getInputLineNumber ();
mloc->pos = getInputFilePosition ();
}
else
{
mloc->delta = (start
? window->pmatch [g].rm_so
: window->pmatch [g].rm_eo);
off_t offset = (window->line + mloc->delta) - window->start;
mloc->line = getInputLineNumberForFileOffset (offset);
mloc->pos = getInputFilePositionForLine (mloc->line);
}

EsObject * mlocobj = es_pointer_new (OPT_TYPE_MATCHLOC, mloc);
if (es_error_p (mlocobj))
{
Expand Down Expand Up @@ -3417,6 +3400,33 @@ static char* make_match_string (scriptWindow *window, int group)
return eStrndup (start, len);
}

static matchLoc *make_mloc (scriptWindow *window, int group, bool start)
{
if (window == NULL
|| 0 > group
|| window->nmatch <= group
|| window->pmatch [group].rm_so == -1)
return NULL;

matchLoc *mloc = xMalloc (1, matchLoc);
if (window->patbuf->regptype == REG_PARSER_SINGLE_LINE)
{
mloc->delta = 0;
mloc->line = getInputLineNumber ();
mloc->pos = getInputFilePosition ();
}
else
{
mloc->delta = (start
? window->pmatch [group].rm_so
: window->pmatch [group].rm_eo);
off_t offset = (window->line + mloc->delta) - window->start;
mloc->line = getInputLineNumberForFileOffset (offset);
mloc->pos = getInputFilePositionForLine (mloc->line);
}
return mloc;
}

static EsObject* lrop_set_scope (OptVM *vm, EsObject *name)
{
EsObject *corkIndex = opt_vm_ostack_top (vm);
Expand Down

0 comments on commit cf6b3bb

Please sign in to comment.