16
16
You should have received a copy of the GNU Lesser General Public
17
17
License along with the GNU C Library; if not, write to the Free
18
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
21
static void re_string_construct_common (const char *str, int len,
26
26
static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx,
27
27
wint_t *last_wc) internal_function;
28
28
#endif /* RE_ENABLE_I18N */
29
static re_dfastate_t *create_newstate_common (re_dfa_t *dfa,
30
const re_node_set *nodes,
31
unsigned int hash) internal_function;
32
29
static reg_errcode_t register_state (re_dfa_t *dfa, re_dfastate_t *newstate,
33
30
unsigned int hash) internal_function;
34
31
static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
619
612
memmove (pstr->wcs, pstr->wcs + offset,
620
613
(pstr->valid_len - offset) * sizeof (wint_t));
621
614
#endif /* RE_ENABLE_I18N */
622
if (pstr->mbs_allocated)
615
if (BE (pstr->mbs_allocated, 0))
623
616
memmove (pstr->mbs, pstr->mbs + offset,
624
617
pstr->valid_len - offset);
625
618
pstr->valid_len -= offset;
849
if (idx < 0 || idx == input->len)
852
/* In this case, we use the value stored in input->tip_context,
853
since we can't know the character in input->mbs[-1] here. */
854
return input->tip_context;
855
else /* (idx == input->len) */
856
return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
857
: CONTEXT_NEWLINE | CONTEXT_ENDBUF);
843
/* In this case, we use the value stored in input->tip_context,
844
since we can't know the character in input->mbs[-1] here. */
845
return input->tip_context;
846
if (BE (idx == input->len, 0))
847
return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
848
: CONTEXT_NEWLINE | CONTEXT_ENDBUF);
859
849
#ifdef RE_ENABLE_I18N
860
850
if (input->mb_cur_max > 1)
1261
/* Insert the new element ELEM to the re_node_set* SET.
1262
SET should not already have any element greater than or equal to ELEM.
1263
Return -1 if an error is occured, return 1 otherwise. */
1266
re_node_set_insert_last (set, elem)
1270
/* Realloc if we need. */
1271
if (set->alloc == set->nelem)
1274
set->alloc = (set->alloc + 1) * 2;
1275
new_array = re_realloc (set->elems, int, set->alloc);
1276
if (BE (new_array == NULL, 0))
1278
set->elems = new_array;
1281
/* Insert the new element. */
1282
set->elems[set->nelem++] = elem;
1261
1286
/* Compare two node sets SET1 and SET2.
1262
1287
return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
1316
1341
Or return -1, if an error will be occured. */
1319
re_dfa_add_node (dfa, token, mode)
1344
re_dfa_add_node (dfa, token)
1321
1346
re_token_t token;
1324
1348
if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
1326
1350
int new_nodes_alloc = dfa->nodes_alloc * 2;
1351
int *new_nexts, *new_indices;
1352
re_node_set *new_edests, *new_eclosures;
1327
1354
re_token_t *new_array = re_realloc (dfa->nodes, re_token_t,
1328
1355
new_nodes_alloc);
1329
1356
if (BE (new_array == NULL, 0))
1331
1358
dfa->nodes = new_array;
1334
int *new_nexts, *new_indices;
1335
re_node_set *new_edests, *new_eclosures, *new_inveclosures;
1337
new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
1338
new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
1339
new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
1340
new_eclosures = re_realloc (dfa->eclosures, re_node_set,
1342
new_inveclosures = re_realloc (dfa->inveclosures, re_node_set,
1344
if (BE (new_nexts == NULL || new_indices == NULL
1345
|| new_edests == NULL || new_eclosures == NULL
1346
|| new_inveclosures == NULL, 0))
1348
dfa->nexts = new_nexts;
1349
dfa->org_indices = new_indices;
1350
dfa->edests = new_edests;
1351
dfa->eclosures = new_eclosures;
1352
dfa->inveclosures = new_inveclosures;
1359
new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
1360
new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
1361
new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
1362
new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
1363
if (BE (new_nexts == NULL || new_indices == NULL
1364
|| new_edests == NULL || new_eclosures == NULL, 0))
1366
dfa->nexts = new_nexts;
1367
dfa->org_indices = new_indices;
1368
dfa->edests = new_edests;
1369
dfa->eclosures = new_eclosures;
1354
1370
dfa->nodes_alloc = new_nodes_alloc;
1356
1372
dfa->nodes[dfa->nodes_len] = token;
1357
dfa->nodes[dfa->nodes_len].opt_subexp = 0;
1358
dfa->nodes[dfa->nodes_len].duplicated = 0;
1359
1373
dfa->nodes[dfa->nodes_len].constraint = 0;
1374
#ifdef RE_ENABLE_I18N
1375
dfa->nodes[dfa->nodes_len].accept_mb =
1376
(token.type == OP_PERIOD && dfa->mb_cur_max > 1) || token.type == COMPLEX_BRACKET;
1378
dfa->nexts[dfa->nodes_len] = -1;
1379
re_node_set_init_empty (dfa->edests + dfa->nodes_len);
1380
re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
1360
1381
return dfa->nodes_len++;
1470
/* Allocate memory for DFA state and initialize common properties.
1471
Return the new state if succeeded, otherwise return NULL. */
1473
static re_dfastate_t *
1474
create_newstate_common (dfa, nodes, hash)
1476
const re_node_set *nodes;
1479
re_dfastate_t *newstate;
1481
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1482
if (BE (newstate == NULL, 0))
1484
err = re_node_set_init_copy (&newstate->nodes, nodes);
1485
if (BE (err != REG_NOERROR, 0))
1490
newstate->trtable = NULL;
1491
newstate->hash = hash;
1495
/* Store the new state NEWSTATE whose hash value is HASH in appropriate
1496
position. Return value indicate the error code if failed. */
1491
/* Finish initialization of the new state NEWSTATE, and using its hash value
1492
HASH put in the appropriate bucket of DFA's state table. Return value
1493
indicates the error code if failed. */
1498
1495
static reg_errcode_t
1499
1496
register_state (dfa, newstate, hash)
1502
1499
unsigned int hash;
1504
1501
struct re_state_table_entry *spot;
1505
newstate->hash = hash;
1506
err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
1507
if (BE (err != REG_NOERROR, 0))
1509
for (i = 0; i < newstate->nodes.nelem; i++)
1511
int elem = newstate->nodes.elems[i];
1512
if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
1513
re_node_set_insert_last (&newstate->non_eps_nodes, elem);
1505
1516
spot = dfa->state_table + (hash & dfa->state_hash_mask);
1507
1517
if (BE (spot->alloc <= spot->num, 0))
1509
1519
int new_alloc = 2 * spot->num + 2;
1531
1541
reg_errcode_t err;
1532
1542
re_dfastate_t *newstate;
1533
newstate = create_newstate_common (dfa, nodes, hash);
1544
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1534
1545
if (BE (newstate == NULL, 0))
1547
err = re_node_set_init_copy (&newstate->nodes, nodes);
1548
if (BE (err != REG_NOERROR, 0))
1536
1554
newstate->entrance_nodes = &newstate->nodes;
1538
1555
for (i = 0 ; i < nodes->nelem ; i++)
1540
1557
re_token_t *node = dfa->nodes + nodes->elems[i];
1541
1558
re_token_type_t type = node->type;
1542
1559
if (type == CHARACTER && !node->constraint)
1561
#ifdef RE_ENABLE_I18N
1562
newstate->accept_mb |= node->accept_mb;
1563
#endif /* RE_ENABLE_I18N */
1545
1565
/* If the state has the halt node, the state is a halt state. */
1546
else if (type == END_OF_RE)
1566
if (type == END_OF_RE)
1547
1567
newstate->halt = 1;
1548
#ifdef RE_ENABLE_I18N
1549
else if (type == COMPLEX_BRACKET
1550
|| type == OP_UTF8_PERIOD
1551
|| (type == OP_PERIOD && dfa->mb_cur_max > 1))
1552
newstate->accept_mb = 1;
1553
#endif /* RE_ENABLE_I18N */
1554
1568
else if (type == OP_BACK_REF)
1555
1569
newstate->has_backref = 1;
1556
1570
else if (type == ANCHOR || node->constraint)
1578
1592
reg_errcode_t err;
1579
1593
re_dfastate_t *newstate;
1581
newstate = create_newstate_common (dfa, nodes, hash);
1595
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1582
1596
if (BE (newstate == NULL, 0))
1598
err = re_node_set_init_copy (&newstate->nodes, nodes);
1599
if (BE (err != REG_NOERROR, 0))
1584
1605
newstate->context = context;
1585
1606
newstate->entrance_nodes = &newstate->nodes;
1595
1616
if (type == CHARACTER && !constraint)
1618
#ifdef RE_ENABLE_I18N
1619
newstate->accept_mb |= node->accept_mb;
1620
#endif /* RE_ENABLE_I18N */
1597
1622
/* If the state has the halt node, the state is a halt state. */
1598
else if (type == END_OF_RE)
1623
if (type == END_OF_RE)
1599
1624
newstate->halt = 1;
1600
#ifdef RE_ENABLE_I18N
1601
else if (type == COMPLEX_BRACKET
1602
|| type == OP_UTF8_PERIOD
1603
|| (type == OP_PERIOD && dfa->mb_cur_max > 1))
1604
newstate->accept_mb = 1;
1605
#endif /* RE_ENABLE_I18N */
1606
1625
else if (type == OP_BACK_REF)
1607
1626
newstate->has_backref = 1;
1608
1627
else if (type == ANCHOR)