Print this page
9083 replace regex implementation with tre


1627 
1628 /* handle overflow of dst. we need "i" more bytes */
1629 #ifdef OVERFLOW
1630 #undef OVERFLOW
1631 #define OVERFLOW(i) { \
1632                 int pos = op - dst; \
1633                 dst = (wchar_t *)realloc(odst = dst, \
1634                         (len += len + i) * sizeof (wchar_t)); \
1635                 if (dst == NULL) \
1636                         goto nospace; \
1637                 op = dst + pos; \
1638                 end = dst + len; \
1639         }
1640 #endif
1641 
1642         *dstp = dst = (wchar_t *)malloc(len * sizeof (wchar_t));
1643         if (dst == NULL)
1644                 return (REG_ESPACE);
1645 
1646         if (rp == NULL || rpl == NULL || src == NULL || dst ==  NULL)
1647                 return (REG_EFATAL);
1648 
1649         glob = 0;       /* match count */
1650         ip = src;       /* source position */
1651         op = dst;       /* destination position */
1652         end = dst + len;
1653 
1654         flags = 0;
1655         while ((regerr = int_regwexec(rp, ip, NSUB, rm, flags)) == REG_OK) {
1656                 /* Copy text preceding match */
1657                 if (op + (i = rm[0].rm_sp - ip) >= end)
1658                         OVERFLOW(i)
1659                 while (i--)
1660                         *op++ = *ip++;
1661 
1662                 if (iglob == 0 || ++glob == iglob) {
1663                         oglob++;
1664                         xp = rpl;               /* do substitute */
1665                 } else
1666                         xp = L"&";          /* preserve text */
1667 




1627 
1628 /* handle overflow of dst. we need "i" more bytes */
1629 #ifdef OVERFLOW
1630 #undef OVERFLOW
1631 #define OVERFLOW(i) { \
1632                 int pos = op - dst; \
1633                 dst = (wchar_t *)realloc(odst = dst, \
1634                         (len += len + i) * sizeof (wchar_t)); \
1635                 if (dst == NULL) \
1636                         goto nospace; \
1637                 op = dst + pos; \
1638                 end = dst + len; \
1639         }
1640 #endif
1641 
1642         *dstp = dst = (wchar_t *)malloc(len * sizeof (wchar_t));
1643         if (dst == NULL)
1644                 return (REG_ESPACE);
1645 
1646         if (rp == NULL || rpl == NULL || src == NULL || dst ==  NULL)
1647                 return (REG_BADPAT);
1648 
1649         glob = 0;       /* match count */
1650         ip = src;       /* source position */
1651         op = dst;       /* destination position */
1652         end = dst + len;
1653 
1654         flags = 0;
1655         while ((regerr = int_regwexec(rp, ip, NSUB, rm, flags)) == REG_OK) {
1656                 /* Copy text preceding match */
1657                 if (op + (i = rm[0].rm_sp - ip) >= end)
1658                         OVERFLOW(i)
1659                 while (i--)
1660                         *op++ = *ip++;
1661 
1662                 if (iglob == 0 || ++glob == iglob) {
1663                         oglob++;
1664                         xp = rpl;               /* do substitute */
1665                 } else
1666                         xp = L"&";          /* preserve text */
1667