tre-match-parallel.c [plain text]
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#undef TRE_USE_ALLOCA
#ifdef TRE_USE_ALLOCA
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca
char *alloca ();
# endif
# endif
# endif
#endif
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
#ifdef HAVE_WCTYPE_H
#include <wctype.h>
#endif
#ifndef TRE_WCHAR
#include <ctype.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include "tre-internal.h"
#include "tre-match-utils.h"
#include "tre.h"
#include "xmalloc.h"
typedef struct {
tre_tnfa_transition_t *state;
tre_tag_t *tags;
} tre_tnfa_reach_t;
typedef struct {
int pos;
tre_tag_t **tags;
} tre_reach_pos_t;
#ifdef TRE_DEBUG
static void
tre_print_reach1(tre_tnfa_transition_t *state, tre_tag_t *tags, int num_tags)
{
DPRINT((" %p", (void *)state));
if (num_tags > 0)
{
DPRINT(("/"));
tre_print_tags(tags, num_tags);
}
}
static void
tre_print_reach(const tre_tnfa_t *tnfa, tre_tnfa_reach_t *reach, int num_tags)
{
while (reach->state != NULL)
{
tre_print_reach1(reach->state, reach->tags, num_tags);
reach++;
}
DPRINT(("\n"));
}
#endif
reg_errcode_t
tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,
tre_str_type_t type, tre_tag_t *match_tags, int eflags,
int *match_end_ofs)
{
tre_char_t prev_c = 0, next_c = 0;
const char *str_byte = string;
int pos = -1;
unsigned int pos_add_next = 1;
#ifdef TRE_WCHAR
const wchar_t *str_wide = string;
#ifdef TRE_MBSTATE
mbstate_t mbstate;
#endif
#endif
int reg_notbol = eflags & REG_NOTBOL;
int reg_noteol = eflags & REG_NOTEOL;
int reg_newline = tnfa->cflags & REG_NEWLINE;
#ifdef TRE_STR_USER
int str_user_end = 0;
#endif
char *buf;
tre_tnfa_transition_t *trans_i;
tre_tnfa_reach_t *reach, *reach_next, *reach_i, *reach_next_i;
tre_reach_pos_t *reach_pos;
int *tag_i;
int num_tags, i;
int match_eo = -1;
#ifdef TRE_DEBUG
int once;
#endif
tre_tag_t *tmp_tags = NULL;
tre_tag_t *tmp_iptr;
size_t tbytes;
int touch = 1;
#ifdef TRE_MBSTATE
memset(&mbstate, '\0', sizeof(mbstate));
#endif
DPRINT(("tre_tnfa_run_parallel, input type %d\n", type));
if (!match_tags)
num_tags = 0;
else
num_tags = tnfa->num_tags;
{
size_t rbytes, pbytes, total_bytes;
char *tmp_buf;
tbytes = sizeof(*tmp_tags) * num_tags;
rbytes = sizeof(*reach_next) * (tnfa->num_states + 1);
pbytes = sizeof(*reach_pos) * tnfa->num_states;
total_bytes =
(sizeof(long) - 1) * 4
+ (rbytes + tbytes * tnfa->num_states) * 2 + tbytes + pbytes;
DPRINT(("tre_tnfa_run_parallel, allocate %d bytes\n", total_bytes));
#ifdef TRE_USE_ALLOCA
buf = alloca(total_bytes);
#else
buf = xmalloc(total_bytes);
#endif
if (buf == NULL)
return REG_ESPACE;
memset(buf, 0, total_bytes);
tmp_tags = (void *)buf;
tmp_buf = buf + tbytes;
tmp_buf += ALIGN(tmp_buf, long);
reach_next = (void *)tmp_buf;
tmp_buf += rbytes;
tmp_buf += ALIGN(tmp_buf, long);
reach = (void *)tmp_buf;
tmp_buf += rbytes;
tmp_buf += ALIGN(tmp_buf, long);
reach_pos = (void *)tmp_buf;
tmp_buf += pbytes;
tmp_buf += ALIGN(tmp_buf, long);
for (i = 0; i < tnfa->num_states; i++)
{
reach[i].tags = (void *)tmp_buf;
tmp_buf += tbytes;
reach_next[i].tags = (void *)tmp_buf;
tmp_buf += tbytes;
}
}
for (i = 0; i < tnfa->num_states; i++)
reach_pos[i].pos = -1;
if (tnfa->first_char >= 0 && str_byte)
{
const char *orig_str = str_byte;
int first = tnfa->first_char;
int found_high_bit = 0;
if (type == STR_BYTE)
{
if (len >= 0)
str_byte = memchr(orig_str, first, (size_t)len);
else
str_byte = strchr(orig_str, first);
}
else if (type == STR_MBS)
{
if (first < 0x80)
{
if (len >= 0)
{
int i;
for (i = 0; ; str_byte++, i++)
{
if (i >= len)
{
str_byte = NULL;
break;
}
if (*str_byte == first)
break;
if (*str_byte & 0x80)
{
found_high_bit = 1;
break;
}
}
}
else
{
for (; ; str_byte++)
{
if (!*str_byte)
{
str_byte = NULL;
break;
}
if (*str_byte == first)
break;
if (*str_byte & 0x80)
{
found_high_bit = 1;
break;
}
}
}
}
else
{
if (len >= 0)
{
int i;
for (i = 0; ; str_byte++, i++)
{
if (i >= len)
{
str_byte = NULL;
break;
}
if (*str_byte & 0x80)
{
found_high_bit = 1;
break;
}
}
}
else
{
for (; ; str_byte++)
{
if (!*str_byte)
{
str_byte = NULL;
break;
}
if (*str_byte & 0x80)
{
found_high_bit = 1;
break;
}
}
}
}
}
if (str_byte == NULL)
{
#ifndef TRE_USE_ALLOCA
if (buf)
xfree(buf);
#endif
return REG_NOMATCH;
}
DPRINT(("skipped %lu chars\n", (unsigned long)(str_byte - orig_str)));
if (!found_high_bit)
{
if (str_byte >= orig_str + 1)
prev_c = (unsigned char)*(str_byte - 1);
next_c = (unsigned char)*str_byte;
pos = str_byte - orig_str;
if (len < 0 || pos < len)
str_byte++;
}
else
{
if (str_byte == orig_str)
goto no_first_optimization;
next_c = (unsigned char)*(str_byte - 1);
pos = (str_byte - 1) - orig_str;
GET_NEXT_WCHAR();
}
}
else
{
no_first_optimization:
GET_NEXT_WCHAR();
pos = 0;
}
#ifdef USE_FIRSTPOS_CHARS
if (tnfa->firstpos_chars != NULL)
{
char *chars = tnfa->firstpos_chars;
if (len < 0)
{
const char *orig_str = str_byte;
while (next_c != L'\0' && !chars[next_c])
{
next_c = *str_byte++;
}
prev_c = *(str_byte - 2);
pos += str_byte - orig_str;
DPRINT(("skipped %d chars\n", str_byte - orig_str));
}
else
{
while (pos <= len && !chars[next_c])
{
prev_c = next_c;
next_c = (unsigned char)(*str_byte++);
pos++;
}
}
}
#endif
DPRINT(("length: %d\n", len));
DPRINT(("pos:chr/code | states and tags\n"));
DPRINT(("-------------+------------------------------------------------\n"));
reach_next_i = reach_next;
while (1)
{
if (match_eo < 0)
{
DPRINT((" init >"));
trans_i = tnfa->initial;
while (trans_i->state != NULL)
{
if (reach_pos[trans_i->state_id].pos < pos)
{
if (trans_i->assertions
&& CHECK_ASSERTIONS(trans_i->assertions))
{
DPRINT(("assertion failed\n"));
trans_i++;
continue;
}
DPRINT((" %p", (void *)trans_i->state));
reach_next_i->state = trans_i->state;
memset(reach_next_i->tags, 0, tbytes);
tag_i = trans_i->tags;
if (tag_i)
{
while (*tag_i >= 0)
{
if (*tag_i < num_tags)
tre_tag_set(reach_next_i->tags, *tag_i, pos, touch);
tag_i++;
}
touch++;
}
if (reach_next_i->state == tnfa->final)
{
DPRINT((" found empty match\n"));
match_eo = pos;
memcpy(match_tags, reach_next_i->tags, tbytes);
}
reach_pos[trans_i->state_id].pos = pos;
reach_pos[trans_i->state_id].tags = &reach_next_i->tags;
reach_next_i++;
}
trans_i++;
}
DPRINT(("\n"));
reach_next_i->state = NULL;
}
else
{
if (num_tags == 0 || reach_next_i == reach_next)
break;
}
if (len < 0)
{
#ifdef TRE_STR_USER
if (type == STR_USER)
{
if (str_user_end)
break;
}
else
#endif
if (next_c == L'\0')
break;
}
else
{
if (pos >= len)
break;
}
GET_NEXT_WCHAR();
#ifdef TRE_DEBUG
DPRINT(("%3d:%2lc/%05d |", pos - 1, (tre_cint_t)prev_c, (int)prev_c));
tre_print_reach(tnfa, reach_next, num_tags);
#endif
reach_i = reach;
reach = reach_next;
reach_next = reach_i;
#ifdef TRE_DEBUG
once = 0;
#endif
reach_next_i = reach_next;
for (reach_i = reach; reach_i->state; reach_i++)
{
for (trans_i = reach_i->state; trans_i->state; trans_i++)
{
if (trans_i->code_min <= (tre_cint_t)prev_c &&
trans_i->code_max >= (tre_cint_t)prev_c)
{
if (trans_i->assertions
&& (CHECK_ASSERTIONS(trans_i->assertions)
|| CHECK_CHAR_CLASSES(trans_i, tnfa, eflags)))
{
DPRINT(("assertion failed\n"));
continue;
}
memcpy(tmp_tags, reach_i->tags, tbytes);
tag_i = trans_i->tags;
if (tag_i != NULL)
{
while (*tag_i >= 0)
{
if (*tag_i < num_tags)
tre_tag_set(tmp_tags, *tag_i, pos, touch);
tag_i++;
}
touch++;
}
if (tnfa->num_minimals && match_eo >= 0)
{
int skip = 0;
#ifdef TRE_DEBUG
if (!once)
{
DPRINT(("Checking minimal conditions: match_eo=%d "
"match_tags=", match_eo));
tre_print_tags(match_tags, num_tags);
DPRINT(("\n"));
once++;
}
#endif
for (i = 0; tnfa->minimal_tags[i] >= 0; i += 2)
{
int end = tnfa->minimal_tags[i];
int start = tnfa->minimal_tags[i + 1];
DPRINT((" Minimal start %d, end %d\n", start, end));
if (tre_minimal_tag_order(start, end, match_tags,
tmp_tags) > 0)
{
skip = 1;
break;
}
}
if (skip)
{
#ifdef TRE_DEBUG
DPRINT((" Throwing out"));
tre_print_reach1(reach_i->state, tmp_tags,
num_tags);
DPRINT(("\n"));
#endif
continue;
}
}
if (reach_pos[trans_i->state_id].pos < pos)
{
reach_next_i->state = trans_i->state;
tmp_iptr = reach_next_i->tags;
reach_next_i->tags = tmp_tags;
tmp_tags = tmp_iptr;
reach_pos[trans_i->state_id].pos = pos;
reach_pos[trans_i->state_id].tags = &reach_next_i->tags;
if (reach_next_i->state == tnfa->final
&& (match_eo == -1
|| (num_tags > 0
&& tre_tag_get(reach_next_i->tags, 0) <=
tre_tag_get(match_tags, 0))))
{
#ifdef TRE_DEBUG
DPRINT((" found match"));
tre_print_reach1(trans_i->state, reach_next_i->tags, num_tags);
DPRINT(("\n"));
#endif
match_eo = pos;
memcpy(match_tags, reach_next_i->tags, tbytes);
}
reach_next_i++;
}
else
{
assert(reach_pos[trans_i->state_id].pos == pos);
if (tre_tag_order(num_tags, tnfa->tag_directions,
tmp_tags,
*reach_pos[trans_i->state_id].tags))
{
tmp_iptr = *reach_pos[trans_i->state_id].tags;
*reach_pos[trans_i->state_id].tags = tmp_tags;
if (trans_i->state == tnfa->final)
{
#ifdef TRE_DEBUG
DPRINT((" found better match"));
tre_print_reach1(trans_i->state, tmp_tags, num_tags);
DPRINT(("\n"));
#endif
match_eo = pos;
memcpy(match_tags, tmp_tags, tbytes);
}
tmp_tags = tmp_iptr;
}
}
}
}
}
reach_next_i->state = NULL;
}
DPRINT(("match end offset = %d\n", match_eo));
*match_end_ofs = match_eo;
#ifdef TRE_DEBUG
if (match_tags)
{
DPRINT(("Winning tags="));
tre_print_tags_all(match_tags, num_tags);
DPRINT((" touch=%d\n", touch));
}
#endif
#ifndef TRE_USE_ALLOCA
if (buf)
xfree(buf);
#endif
return match_eo >= 0 ? REG_OK : REG_NOMATCH;
}