# Diff of /code/branches/pcre16/pcre_exec.c

revision 428 by ph10, Mon Aug 31 17:10:26 2009 UTC revision 459 by ph10, Sun Oct 4 09:21:39 2009 UTC
# Line 408  immediately. The second one is used when Line 408  immediately. The second one is used when
408  the subject. */  the subject. */
409
410  #define CHECK_PARTIAL()\  #define CHECK_PARTIAL()\
411    if (md->partial && eptr >= md->end_subject && eptr > mstart)\    if (md->partial != 0 && eptr >= md->end_subject && eptr > mstart)\
412      {\      {\
413      md->hitend = TRUE;\      md->hitend = TRUE;\
414      if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);\      if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);\
# Line 663  for (;;) Line 663  for (;;)
663    {    {
664    minimize = possessive = FALSE;    minimize = possessive = FALSE;
665    op = *ecode;    op = *ecode;
666
667    switch(op)    switch(op)
668      {      {
669      case OP_FAIL:      case OP_FAIL:
# Line 839  for (;;) Line 839  for (;;)
839
840      /* Now see what the actual condition is */      /* Now see what the actual condition is */
841
842      if (condcode == OP_RREF)         /* Recursion test */      if (condcode == OP_RREF || condcode == OP_NRREF)    /* Recursion test */
843        {        {
844        offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/        if (md->recursive == NULL)                /* Not recursing => FALSE */
845        condition = md->recursive != NULL &&          {
846          (offset == RREF_ANY || offset == md->recursive->group_num);          condition = FALSE;
847        ecode += condition? 3 : GET(ecode, 1);          ecode += GET(ecode, 1);
848        }          }
849          else
850            {
851            int recno = GET2(ecode, LINK_SIZE + 2);   /* Recursion group number*/
852            condition =  (recno == RREF_ANY || recno == md->recursive->group_num);
853
854            /* If the test is for recursion into a specific subpattern, and it is
855            false, but the test was set up by name, scan the table to see if the
856            name refers to any other numbers, and test them. The condition is true
857            if any one is set. */
858
859            if (!condition && condcode == OP_NRREF && recno != RREF_ANY)
860              {
861              uschar *slotA = md->name_table;
862              for (i = 0; i < md->name_count; i++)
863                {
864                if (GET2(slotA, 0) == recno) break;
865                slotA += md->name_entry_size;
866                }
867
868              /* Found a name for the number - there can be only one; duplicate
869              names for different numbers are allowed, but not vice versa. First
870              scan down for duplicates. */
871
872              if (i < md->name_count)
873                {
874                uschar *slotB = slotA;
875                while (slotB > md->name_table)
876                  {
877                  slotB -= md->name_entry_size;
878                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
879                    {
880                    condition = GET2(slotB, 0) == md->recursive->group_num;
881                    if (condition) break;
882                    }
883                  else break;
884                  }
885
886                /* Scan up for duplicates */
887
888                if (!condition)
889                  {
890                  slotB = slotA;
891                  for (i++; i < md->name_count; i++)
892                    {
893                    slotB += md->name_entry_size;
894                    if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
895                      {
896                      condition = GET2(slotB, 0) == md->recursive->group_num;
897                      if (condition) break;
898                      }
899                    else break;
900                    }
901                  }
902                }
903              }
904
905            /* Chose branch according to the condition */
906
907            ecode += condition? 3 : GET(ecode, 1);
908            }
909          }
910
911      else if (condcode == OP_CREF)    /* Group used test */      else if (condcode == OP_CREF || condcode == OP_NCREF)  /* Group used test */
912        {        {
913        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
914        condition = offset < offset_top && md->offset_vector[offset] >= 0;        condition = offset < offset_top && md->offset_vector[offset] >= 0;
915
916          /* If the numbered capture is unset, but the reference was by name,
917          scan the table to see if the name refers to any other numbers, and test
918          them. The condition is true if any one is set. This is tediously similar
919          to the code above, but not close enough to try to amalgamate. */
920
921          if (!condition && condcode == OP_NCREF)
922            {
923            int refno = offset >> 1;
924            uschar *slotA = md->name_table;
925
926            for (i = 0; i < md->name_count; i++)
927              {
928              if (GET2(slotA, 0) == refno) break;
929              slotA += md->name_entry_size;
930              }
931
932            /* Found a name for the number - there can be only one; duplicate names
933            for different numbers are allowed, but not vice versa. First scan down
934            for duplicates. */
935
936            if (i < md->name_count)
937              {
938              uschar *slotB = slotA;
939              while (slotB > md->name_table)
940                {
941                slotB -= md->name_entry_size;
942                if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
943                  {
944                  offset = GET2(slotB, 0) << 1;
945                  condition = offset < offset_top &&
946                    md->offset_vector[offset] >= 0;
947                  if (condition) break;
948                  }
949                else break;
950                }
951
952              /* Scan up for duplicates */
953
954              if (!condition)
955                {
956                slotB = slotA;
957                for (i++; i < md->name_count; i++)
958                  {
959                  slotB += md->name_entry_size;
960                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
961                    {
962                    offset = GET2(slotB, 0) << 1;
963                    condition = offset < offset_top &&
964                      md->offset_vector[offset] >= 0;
965                    if (condition) break;
966                    }
967                  else break;
968                  }
969                }
970              }
971            }
972
973          /* Chose branch according to the condition */
974
975        ecode += condition? 3 : GET(ecode, 1);        ecode += condition? 3 : GET(ecode, 1);
976        }        }
977
# Line 909  for (;;) Line 1030  for (;;)
1031        }        }
1032      break;      break;
1033
1034
1035        /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1036        to close any currently open capturing brackets. */
1037
1038        case OP_CLOSE:
1039        number = GET2(ecode, 1);
1040        offset = number << 1;
1041
1042    #ifdef DEBUG
1043          printf("end bracket %d at *ACCEPT", number);
1044          printf("\n");
1045    #endif
1046
1047        md->capture_last = number;
1048        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1049          {
1050          md->offset_vector[offset] =
1051            md->offset_vector[md->offset_end - number];
1052          md->offset_vector[offset+1] = eptr - md->start_subject;
1053          if (offset_top <= offset) offset_top = offset + 2;
1054          }
1055        ecode += 3;
1056        break;
1057
1058
1059      /* End of the pattern, either real or forced. If we are in a top-level      /* End of the pattern, either real or forced. If we are in a top-level
# Line 924  for (;;) Line 1069  for (;;)
1069        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
1070        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
1071          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
1072          offset_top = rec->offset_top;
1073        mstart = rec->save_start;        mstart = rec->save_start;
1074        ims = original_ims;        ims = original_ims;
1075        ecode = rec->after_call;        ecode = rec->after_call;
1076        break;        break;
1077        }        }
1078
1079      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, if we have matched an empty string, fail if PCRE_NOTEMPTY is
1080      string - backtracking will then try other alternatives, if any. */      set, or if PCRE_NOTEMPTY_ATSTART is set and we have matched at the start of
1081        the subject. In both cases, backtracking will then try other alternatives,
1082        if any. */
1083
1084        if (eptr == mstart &&
1085            (md->notempty ||
1086              (md->notempty_atstart &&
1087                mstart == md->start_subject + md->start_offset)))
1088          RRETURN(MATCH_NOMATCH);
1089
1090        /* Otherwise, we have a match. */
1091
if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);
1092      md->end_match_ptr = eptr;           /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
1093      md->end_offset_top = offset_top;    /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1094      md->start_match_ptr = mstart;       /* and the start (\K can modify) */      md->start_match_ptr = mstart;       /* and the start (\K can modify) */
# Line 1024  for (;;) Line 1179  for (;;)
1179        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1180        }        }
1181
1183
1184        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1186      break;      break;
1187
# Line 1105  for (;;) Line 1261  for (;;)
1261        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1262              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1263        new_recursive.save_start = mstart;        new_recursive.save_start = mstart;
1264          new_recursive.offset_top = offset_top;
1265        mstart = eptr;        mstart = eptr;
1266
1267        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. For each top-level alternative we
# Line 1303  for (;;) Line 1460  for (;;)
1460        {        {
1462        offset = number << 1;        offset = number << 1;
1463
1464  #ifdef DEBUG  #ifdef DEBUG
1465        printf("end bracket %d", number);        printf("end bracket %d", number);
1466        printf("\n");        printf("\n");
# Line 1329  for (;;) Line 1486  for (;;)
1486          mstart = rec->save_start;          mstart = rec->save_start;
1487          memcpy(md->offset_vector, rec->offset_save,          memcpy(md->offset_vector, rec->offset_save,
1488            rec->saved_max * sizeof(int));            rec->saved_max * sizeof(int));
1489            offset_top = rec->offset_top;
1490          ecode = rec->after_call;          ecode = rec->after_call;
1491          ims = original_ims;          ims = original_ims;
1492          break;          break;
# Line 1468  for (;;) Line 1626  for (;;)
1626
1627        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
1628        It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to        It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
1629        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
1630          partial matching. */
1631
1632  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1633        if (utf8)        if (utf8)
# Line 1477  for (;;) Line 1636  for (;;)
1636            {            {
1637            USPTR lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1638            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1639              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
1640            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1641            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1642            }            }
1643          if (eptr >= md->end_subject)          if (eptr >= md->end_subject)
1644            {            {
1645            SCHECK_PARTIAL();            SCHECK_PARTIAL();
1646            cur_is_word = FALSE;            cur_is_word = FALSE;
1647            }            }
1648          else          else
1649            {            {
# Line 1497  for (;;) Line 1657  for (;;)
1657        /* Not in UTF-8 mode */        /* Not in UTF-8 mode */
1658
1659          {          {
1660          prev_is_word = (eptr != md->start_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
1661            ((md->ctypes[eptr[-1]] & ctype_word) != 0);            {
1662          if (eptr >= md->end_subject)            if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
1663              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
1664              }
1665            if (eptr >= md->end_subject)
1666            {            {
1667            SCHECK_PARTIAL();            SCHECK_PARTIAL();
1668            cur_is_word = FALSE;            cur_is_word = FALSE;
1669            }            }
1670          else cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);          else cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
1671          }          }
# Line 1522  for (;;) Line 1685  for (;;)
1685      /* Fall through */      /* Fall through */
1686
1687      case OP_ALLANY:      case OP_ALLANY:
1688      if (eptr++ >= md->end_subject)      if (eptr++ >= md->end_subject)
1689        {        {
1690        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1691        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1692        }        }
1693      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
1694      ecode++;      ecode++;
1695      break;      break;
# Line 1535  for (;;) Line 1698  for (;;)
1698      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
1699
1700      case OP_ANYBYTE:      case OP_ANYBYTE:
1701      if (eptr++ >= md->end_subject)      if (eptr++ >= md->end_subject)
1702        {        {
1703        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1704        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1705        }        }
1706      ecode++;      ecode++;
1707      break;      break;
1708
1709      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1710      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1711        {        {
1712        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1713        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1714        }        }
1715      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1716      if (      if (
1717  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1561  for (;;) Line 1724  for (;;)
1724      break;      break;
1725
1726      case OP_DIGIT:      case OP_DIGIT:
1727      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1728        {        {
1729        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1730        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1731        }        }
1732      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1733      if (      if (
1734  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1578  for (;;) Line 1741  for (;;)
1741      break;      break;
1742
1743      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
1744      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1745        {        {
1746        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1747        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1748        }        }
1749      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1750      if (      if (
1751  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1595  for (;;) Line 1758  for (;;)
1758      break;      break;
1759
1760      case OP_WHITESPACE:      case OP_WHITESPACE:
1761      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1762        {        {
1763        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1764        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1765        }        }
1766      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1767      if (      if (
1768  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1612  for (;;) Line 1775  for (;;)
1775      break;      break;
1776
1777      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
1778      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1779        {        {
1780        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1781        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1782        }        }
1783      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1784      if (      if (
1785  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1629  for (;;) Line 1792  for (;;)
1792      break;      break;
1793
1794      case OP_WORDCHAR:      case OP_WORDCHAR:
1795      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1796        {        {
1797        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1798        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1799        }        }
1800      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1801      if (      if (
1802  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1646  for (;;) Line 1809  for (;;)
1809      break;      break;
1810
1811      case OP_ANYNL:      case OP_ANYNL:
1812      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1813        {        {
1814        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1815        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1816        }        }
1817      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1818      switch(c)      switch(c)
1819        {        {
# Line 1674  for (;;) Line 1837  for (;;)
1837      break;      break;
1838
1839      case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
1840      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1841        {        {
1842        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1843        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1844        }        }
1845      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1846      switch(c)      switch(c)
1847        {        {
# Line 1708  for (;;) Line 1871  for (;;)
1871      break;      break;
1872
1873      case OP_HSPACE:      case OP_HSPACE:
1874      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1875        {        {
1876        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1877        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1878        }        }
1879      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1880      switch(c)      switch(c)
1881        {        {
# Line 1742  for (;;) Line 1905  for (;;)
1905      break;      break;
1906
1907      case OP_NOT_VSPACE:      case OP_NOT_VSPACE:
1908      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1909        {        {
1910        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1911        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1912        }        }
1913      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1914      switch(c)      switch(c)
1915        {        {
# Line 1764  for (;;) Line 1927  for (;;)
1927      break;      break;
1928
1929      case OP_VSPACE:      case OP_VSPACE:
1930      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1931        {        {
1932        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1933        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1934        }        }
1935      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1936      switch(c)      switch(c)
1937        {        {
# Line 1791  for (;;) Line 1954  for (;;)
1954
1955      case OP_PROP:      case OP_PROP:
1956      case OP_NOTPROP:      case OP_NOTPROP:
1957      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
1958        {        {
1959        SCHECK_PARTIAL();        SCHECK_PARTIAL();
1960        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1961        }        }
1962      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1963        {        {
1964        const ucd_record *prop = GET_UCD(c);        const ucd_record *prop = GET_UCD(c);
# Line 1840  for (;;) Line 2003  for (;;)
2003      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2004
2005      case OP_EXTUNI:      case OP_EXTUNI:
2006      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
2007        {        {
2008        SCHECK_PARTIAL();        SCHECK_PARTIAL();
2009        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
2010        }        }
2011      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2012        {        {
2013        int category = UCD_CATEGORY(c);        int category = UCD_CATEGORY(c);
# Line 1924  for (;;) Line 2087  for (;;)
2087          break;          break;
2088
2089          default:               /* No repeat follows */          default:               /* No repeat follows */
2090          if (!match_ref(offset, eptr, length, md, ims))          if (!match_ref(offset, eptr, length, md, ims))
2091            {            {
2092            CHECK_PARTIAL();            CHECK_PARTIAL();
2093            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2094            }            }
2095          eptr += length;          eptr += length;
2096          continue;              /* With the main loop */          continue;              /* With the main loop */
2097          }          }
2098
2099        /* If the length of the reference is zero, just continue with the        /* If the length of the reference is zero, just continue with the
2100        main loop. */        main loop. */
2101
2102        if (length == 0) continue;        if (length == 0) continue;
2103
2104        /* First, ensure the minimum number of matches are present. We get back        /* First, ensure the minimum number of matches are present. We get back
# Line 2311  for (;;) Line 2474  for (;;)
2474        length = 1;        length = 1;
2475        ecode++;        ecode++;
2476        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2477        if (length > md->end_subject - eptr)        if (length > md->end_subject - eptr)
2478          {          {
2479          CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */          CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2480          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2481          }          }
2482        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);
2483        }        }
2484      else      else
# Line 2323  for (;;) Line 2486  for (;;)
2486
2487      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2488        {        {
2489        if (md->end_subject - eptr < 1)        if (md->end_subject - eptr < 1)
2490          {          {
2491          SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */          SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2492          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2493          }          }
2494        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);
2495        ecode += 2;        ecode += 2;
2496        }        }
# Line 2343  for (;;) Line 2506  for (;;)
2506        ecode++;        ecode++;
2507        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2508
2509        if (length > md->end_subject - eptr)        if (length > md->end_subject - eptr)
2510          {          {
2511          CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */          CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2512          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2513          }          }
2514
2515        /* If the pattern character's value is < 128, we have only one byte, and        /* If the pattern character's value is < 128, we have only one byte, and
2516        can use the fast lookup table. */        can use the fast lookup table. */
# Line 2382  for (;;) Line 2545  for (;;)
2545
2546      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2547        {        {
2548        if (md->end_subject - eptr < 1)        if (md->end_subject - eptr < 1)
2549          {          {
2550          SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */          SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2551          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2552          }          }
2553        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
2554        ecode += 2;        ecode += 2;
2555        }        }
# Line 2440  for (;;) Line 2603  for (;;)
2603      case OP_MINQUERY:      case OP_MINQUERY:
2604      c = *ecode++ - OP_STAR;      c = *ecode++ - OP_STAR;
2605      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
2606
2607      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
2608      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
2609      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
# Line 2553  for (;;) Line 2716  for (;;)
2716      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
2717
2718      fc = *ecode++;      fc = *ecode++;
2719
2720      /* The value of fc at this point is always less than 256, though we may or      /* The value of fc at this point is always less than 256, though we may or
2721      may not be in UTF-8 mode. The code is duplicated for the caseless and      may not be in UTF-8 mode. The code is duplicated for the caseless and
2722      caseful cases, for speed, since matching characters is likely to be quite      caseful cases, for speed, since matching characters is likely to be quite
# Line 2630  for (;;) Line 2793  for (;;)
2793            }            }
2794          if (fc != *eptr++) RRETURN(MATCH_NOMATCH);          if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
2795          }          }
2796
2797        if (min == max) continue;        if (min == max) continue;
2798
2799        if (minimize)        if (minimize)
2800          {          {
2801          for (fi = min;; fi++)          for (fi = min;; fi++)
# Line 2658  for (;;) Line 2821  for (;;)
2821            eptr++;            eptr++;
2822            }            }
2823          if (possessive) continue;          if (possessive) continue;
2824
2825          while (eptr >= pp)          while (eptr >= pp)
2826            {            {
2827            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);
# Line 2674  for (;;) Line 2837  for (;;)
2837      checking can be multibyte. */      checking can be multibyte. */
2838
2839      case OP_NOT:      case OP_NOT:
2840      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
2841        {        {
2842        SCHECK_PARTIAL();        SCHECK_PARTIAL();
2843        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
2844        }        }
2845      ecode++;      ecode++;
2846      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2847      if ((ims & PCRE_CASELESS) != 0)      if ((ims & PCRE_CASELESS) != 0)
# Line 3524  for (;;) Line 3687  for (;;)
3687          break;          break;
3688
3689          case OP_ALLANY:          case OP_ALLANY:
3690          if (eptr > md->end_subject - min)          if (eptr > md->end_subject - min)
3691            {            {
3692            SCHECK_PARTIAL();            SCHECK_PARTIAL();
3693            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
3694            }            }
3695          eptr += min;          eptr += min;
3696          break;          break;
3697
3698          case OP_ANYBYTE:          case OP_ANYBYTE:
3699          if (eptr > md->end_subject - min)          if (eptr > md->end_subject - min)
3700            {            {
3701            SCHECK_PARTIAL();            SCHECK_PARTIAL();
3702            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
3703            }            }
3704          eptr += min;          eptr += min;
3705          break;          break;
3706
# Line 4847  if (re == NULL || subject == NULL || Line 5010  if (re == NULL || subject == NULL ||
5010     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
5011  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
5012
5013    /* This information is for finding all the numbers associated with a given
5014    name, for condition testing. */
5015
5016    md->name_table = (uschar *)re + re->name_table_offset;
5017    md->name_count = re->name_count;
5018    md->name_entry_size = re->name_entry_size;
5019
5020  /* Fish out the optional data from the extra_data structure, first setting  /* Fish out the optional data from the extra_data structure, first setting
5021  the default values. */  the default values. */
5022
# Line 4914  md->jscript_compat = (re->options & PCRE Line 5084  md->jscript_compat = (re->options & PCRE
5084  md->notbol = (options & PCRE_NOTBOL) != 0;  md->notbol = (options & PCRE_NOTBOL) != 0;
5085  md->noteol = (options & PCRE_NOTEOL) != 0;  md->noteol = (options & PCRE_NOTEOL) != 0;
5086  md->notempty = (options & PCRE_NOTEMPTY) != 0;  md->notempty = (options & PCRE_NOTEMPTY) != 0;
5087    md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0;
5088  md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :  md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :
5089                ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;                ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;
5090  md->hitend = FALSE;  md->hitend = FALSE;
# Line 5078  if (!anchored) Line 5249  if (!anchored)
5249      }      }
5250    else    else
5251      if (!startline && study != NULL &&      if (!startline && study != NULL &&
5252        (study->options & PCRE_STUDY_MAPPED) != 0)        (study->flags & PCRE_STUDY_MAPPED) != 0)
5253          start_bits = study->start_bits;          start_bits = study->start_bits;
5254    }    }
5255
# Line 5204  for(;;) Line 5375  for(;;)
5375    /* Restore fudged end_subject */    /* Restore fudged end_subject */
5376
5377    end_subject = save_end_subject;    end_subject = save_end_subject;
5378
5379  #ifdef DEBUG  /* Sigh. Some compilers never learn. */    /* The following two optimizations are disabled for partial matching or if
printf(">>>> Match against: ");
pchars(start_match, end_subject - start_match, TRUE, md);
printf("\n");
#endif

/* If req_byte is set, we know that that character must appear in the
subject for the match to succeed. If the first character is set, req_byte
must be later in the subject; otherwise the test starts at the match point.
This optimization can save a huge amount of backtracking in patterns with
nested unlimited repeats that aren't going to match. Writing separate code
for cased/caseless versions makes it go faster, as does using an
autoincrement and backing off on a match.

HOWEVER: when the subject string is very, very long, searching to its end
can take a long time, and give bad performance on quite ordinary patterns.
This showed up when somebody was matching something like /^\d+C/ on a
32-megabyte string... so we don't do this when the string is sufficiently
long.

ALSO: this processing is disabled when partial matching is requested, or if
5380    disabling is explicitly requested. */    disabling is explicitly requested. */
5381
5382    if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&    if ((options & PCRE_NO_START_OPTIMIZE) == 0 && !md->partial)
5383        req_byte >= 0 &&      {
5384        end_subject - start_match < REQ_BYTE_MAX &&      /* If the pattern was studied, a minimum subject length may be set. This is
5385        !md->partial)      a lower bound; no actual string of that length may actually match the
5386      {      pattern. Although the value is, strictly, in characters, we treat it as
5387      register USPTR p = start_match + ((first_byte >= 0)? 1 : 0);      bytes to avoid spending too much time in this optimization. */
5388
5389      /* We don't need to repeat the search if we haven't yet reached the      if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
5390      place we found it at last time. */          end_subject - start_match < study->minlength)
5391          {
5392      if (p > req_byte_ptr)        rc = MATCH_NOMATCH;
5393          break;
5394          }
5395
5396        /* If req_byte is set, we know that that character must appear in the
5397        subject for the match to succeed. If the first character is set, req_byte
5398        must be later in the subject; otherwise the test starts at the match point.
5399        This optimization can save a huge amount of backtracking in patterns with
5400        nested unlimited repeats that aren't going to match. Writing separate code
5401        for cased/caseless versions makes it go faster, as does using an
5402        autoincrement and backing off on a match.
5403
5404        HOWEVER: when the subject string is very, very long, searching to its end
5405        can take a long time, and give bad performance on quite ordinary patterns.
5406        This showed up when somebody was matching something like /^\d+C/ on a
5407        32-megabyte string... so we don't do this when the string is sufficiently
5408        long. */
5409
5410        if (req_byte >= 0 && end_subject - start_match < REQ_BYTE_MAX)
5411        {        {
5412        if (req_byte_caseless)        register USPTR p = start_match + ((first_byte >= 0)? 1 : 0);
5413
5414          /* We don't need to repeat the search if we haven't yet reached the
5415          place we found it at last time. */
5416
5417          if (p > req_byte_ptr)
5418          {          {
5419          while (p < end_subject)          if (req_byte_caseless)
5420            {            {
5421            register int pp = *p++;            while (p < end_subject)
5422            if (pp == req_byte || pp == req_byte2) { p--; break; }              {
5423                register int pp = *p++;
5424                if (pp == req_byte || pp == req_byte2) { p--; break; }
5425                }
5426            }            }
5427          }          else
else
{
while (p < end_subject)
5428            {            {
5429            if (*p++ == req_byte) { p--; break; }            while (p < end_subject)
5430                {
5431                if (*p++ == req_byte) { p--; break; }
5432                }
5433            }            }
5434
5435            /* If we can't find the required character, break the matching loop,
5436            forcing a match failure. */
5437
5438            if (p >= end_subject)
5439              {
5440              rc = MATCH_NOMATCH;
5441              break;
5442              }
5443
5444            /* If we have found the required character, save the point where we
5445            found it, so that we don't search again next time round the loop if
5446            the start hasn't passed this character yet. */
5447
5448            req_byte_ptr = p;
5449          }          }

/* If we can't find the required character, break the matching loop,
forcing a match failure. */

if (p >= end_subject)
{
rc = MATCH_NOMATCH;
break;
}

/* If we have found the required character, save the point where we
found it, so that we don't search again next time round the loop if
the start hasn't passed this character yet. */

req_byte_ptr = p;
5450        }        }
5451      }      }
5452
5453    #ifdef DEBUG  /* Sigh. Some compilers never learn. */
5454      printf(">>>> Match against: ");
5455      pchars(start_match, end_subject - start_match, TRUE, md);
5456      printf("\n");
5457    #endif
5458
5459    /* OK, we can now run the match. If "hitend" is set afterwards, remember the    /* OK, we can now run the match. If "hitend" is set afterwards, remember the
5460    first starting point for which a partial match was found. */    first starting point for which a partial match was found. */
5461
5462    md->start_match_ptr = start_match;    md->start_match_ptr = start_match;
5463      md->start_used_ptr = start_match;
5464    md->match_call_count = 0;    md->match_call_count = 0;
5465    rc = match(start_match, md->start_code, start_match, 2, md, ims, NULL, 0, 0);    rc = match(start_match, md->start_code, start_match, 2, md, ims, NULL, 0, 0);
5466    if (md->hitend && start_partial == NULL) start_partial = start_match;    if (md->hitend && start_partial == NULL) start_partial = md->start_used_ptr;
5467
5468    switch(rc)    switch(rc)
5469      {      {
# Line 5309  for(;;) Line 5493  for(;;)
5493      rc = MATCH_NOMATCH;      rc = MATCH_NOMATCH;
5494      goto ENDLOOP;      goto ENDLOOP;
5495
5496      /* Any other return is some kind of error. */      /* Any other return is either a match, or some kind of error. */
5497
5498      default:      default:
5499      goto ENDLOOP;      goto ENDLOOP;
# Line 5391  if (rc == MATCH_MATCH) Line 5575  if (rc == MATCH_MATCH)
5575    too many to fit into the vector. */    too many to fit into the vector. */
5576
5577    rc = md->offset_overflow? 0 : md->end_offset_top/2;    rc = md->offset_overflow? 0 : md->end_offset_top/2;
5578
5579    /* If there is space, set up the whole thing as substring 0. The value of    /* If there is space, set up the whole thing as substring 0. The value of
5580    md->start_match_ptr might be modified if \K was encountered on the success    md->start_match_ptr might be modified if \K was encountered on the success
5581    matching path. */    matching path. */

Legend:
 Removed from v.428 changed lines Added in v.459