/[pcre]/code/trunk/pcretest.c
ViewVC logotype

Diff of /code/trunk/pcretest.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 35 by nigel, Sat Feb 24 21:39:05 2007 UTC revision 39 by nigel, Sat Feb 24 21:39:13 2007 UTC
# Line 12  Line 12 
12  /* Use the internal info for displaying the results of pcre_study(). */  /* Use the internal info for displaying the results of pcre_study(). */
13    
14  #include "internal.h"  #include "internal.h"
15    
16    /* It is possible to compile this test program without including support for
17    testing the POSIX interface, though this is not available via the standard
18    Makefile. */
19    
20    #if !defined NOPOSIX
21  #include "pcreposix.h"  #include "pcreposix.h"
22    #endif
23    
24  #ifndef CLOCKS_PER_SEC  #ifndef CLOCKS_PER_SEC
25  #ifdef CLK_TCK  #ifdef CLK_TCK
# Line 48  static const char *OP_names[] = { Line 55  static const char *OP_names[] = {
55  };  };
56    
57    
58  static void print_internals(pcre *re, FILE *outfile)  static void print_internals(pcre *re)
59  {  {
60  unsigned char *code = ((real_pcre *)re)->code;  unsigned char *code = ((real_pcre *)re)->code;
61    
# Line 366  while (!done) Line 373  while (!done)
373    {    {
374    pcre *re = NULL;    pcre *re = NULL;
375    pcre_extra *extra = NULL;    pcre_extra *extra = NULL;
376    
377    #if !defined NOPOSIX  /* There are still compilers that require no indent */
378    regex_t preg;    regex_t preg;
379    #endif
380    
381    const char *error;    const char *error;
382    unsigned char *p, *pp, *ppp;    unsigned char *p, *pp, *ppp;
383    unsigned const char *tables = NULL;    unsigned const char *tables = NULL;
# Line 460  while (!done) Line 471  while (!done)
471        case 'G': do_G = 1; break;        case 'G': do_G = 1; break;
472        case 'I': do_showinfo = 1; break;        case 'I': do_showinfo = 1; break;
473        case 'M': log_store = 1; break;        case 'M': log_store = 1; break;
474    
475    #if !defined NOPOSIX
476        case 'P': do_posix = 1; break;        case 'P': do_posix = 1; break;
477    #endif
478    
479        case 'S': do_study = 1; break;        case 'S': do_study = 1; break;
480        case 'U': options |= PCRE_UNGREEDY; break;        case 'U': options |= PCRE_UNGREEDY; break;
481        case 'X': options |= PCRE_EXTRA; break;        case 'X': options |= PCRE_EXTRA; break;
# Line 489  while (!done) Line 504  while (!done)
504    timing, showing, or debugging options, nor the ability to pass over    timing, showing, or debugging options, nor the ability to pass over
505    local character tables. */    local character tables. */
506    
507    #if !defined NOPOSIX
508    if (posix || do_posix)    if (posix || do_posix)
509      {      {
510      int rc;      int rc;
# Line 511  while (!done) Line 527  while (!done)
527    /* Handle compiling via the native interface */    /* Handle compiling via the native interface */
528    
529    else    else
530    #endif  /* !defined NOPOSIX */
531    
532      {      {
533      if (timeit)      if (timeit)
534        {        {
# Line 561  while (!done) Line 579  while (!done)
579        {        {
580        int first_char, count;        int first_char, count;
581    
582        if (do_debug) print_internals(re, outfile);        if (do_debug) print_internals(re);
583    
584        count = pcre_info(re, &options, &first_char);        count = pcre_info(re, &options, &first_char);
585        if (count < 0) fprintf(outfile,        if (count < 0) fprintf(outfile,
# Line 579  while (!done) Line 597  while (!done)
597              ((options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",              ((options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",
598              ((options & PCRE_EXTRA) != 0)? " extra" : "",              ((options & PCRE_EXTRA) != 0)? " extra" : "",
599              ((options & PCRE_UNGREEDY) != 0)? " ungreedy" : "");              ((options & PCRE_UNGREEDY) != 0)? " ungreedy" : "");
600    
601            if (((((real_pcre *)re)->options) & PCRE_ICHANGED) != 0)
602              fprintf(outfile, "Case state changes\n");
603    
604          if (first_char == -1)          if (first_char == -1)
605            {            {
606            fprintf(outfile, "First char at start or follows \\n\n");            fprintf(outfile, "First char at start or follows \\n\n");
# Line 594  while (!done) Line 616  while (!done)
616            else            else
617              fprintf(outfile, "First char = %d\n", first_char);              fprintf(outfile, "First char = %d\n", first_char);
618            }            }
619    
620            if (((((real_pcre *)re)->options) & PCRE_REQCHSET) != 0)
621              {
622              int req_char = ((real_pcre *)re)->req_char;
623              if (isprint(req_char))
624                fprintf(outfile, "Req char = \'%c\'\n", req_char);
625              else
626                fprintf(outfile, "Req char = %d\n", req_char);
627              }
628            else fprintf(outfile, "No req char\n");
629          }          }
630        }        }
631    
# Line 672  while (!done) Line 704  while (!done)
704      int copystrings = 0;      int copystrings = 0;
705      int getstrings = 0;      int getstrings = 0;
706      int getlist = 0;      int getlist = 0;
707        int gmatched = 0;
708      int start_offset = 0;      int start_offset = 0;
709      int offsets[45];      int offsets[45];
710      int size_offsets = sizeof(offsets)/sizeof(int);      int size_offsets = sizeof(offsets)/sizeof(int);
# Line 752  while (!done) Line 785  while (!done)
785          getlist = 1;          getlist = 1;
786          continue;          continue;
787    
788            case 'N':
789            options |= PCRE_NOTEMPTY;
790            continue;
791    
792          case 'O':          case 'O':
793          while(isdigit(*p)) n = n * 10 + *p++ - '0';          while(isdigit(*p)) n = n * 10 + *p++ - '0';
794          if (n <= (int)(sizeof(offsets)/sizeof(int))) size_offsets = n;          if (n <= (int)(sizeof(offsets)/sizeof(int))) size_offsets = n;
# Line 769  while (!done) Line 806  while (!done)
806      /* Handle matching via the POSIX interface, which does not      /* Handle matching via the POSIX interface, which does not
807      support timing. */      support timing. */
808    
809    #if !defined NOPOSIX
810      if (posix || do_posix)      if (posix || do_posix)
811        {        {
812        int rc;        int rc;
# Line 777  while (!done) Line 815  while (!done)
815        if ((options & PCRE_NOTBOL) != 0) eflags |= REG_NOTBOL;        if ((options & PCRE_NOTBOL) != 0) eflags |= REG_NOTBOL;
816        if ((options & PCRE_NOTEOL) != 0) eflags |= REG_NOTEOL;        if ((options & PCRE_NOTEOL) != 0) eflags |= REG_NOTEOL;
817    
818        rc = regexec(&preg, (unsigned char *)bptr,        rc = regexec(&preg, (const char *)bptr,
819          sizeof(pmatch)/sizeof(regmatch_t), pmatch, eflags);          sizeof(pmatch)/sizeof(regmatch_t), pmatch, eflags);
820    
821        if (rc != 0)        if (rc != 0)
# Line 809  while (!done) Line 847  while (!done)
847    
848      /* Handle matching via the native interface - repeats for /g and /G */      /* Handle matching via the native interface - repeats for /g and /G */
849    
850      else for (;;)      else
851    #endif  /* !defined NOPOSIX */
852    
853        for (;; gmatched++)    /* Loop for /g or /G */
854        {        {
855        if (timeit)        if (timeit)
856          {          {
# Line 818  while (!done) Line 859  while (!done)
859          clock_t start_time = clock();          clock_t start_time = clock();
860          for (i = 0; i < LOOPREPEAT; i++)          for (i = 0; i < LOOPREPEAT; i++)
861            count = pcre_exec(re, extra, (char *)bptr, len,            count = pcre_exec(re, extra, (char *)bptr, len,
862              (do_g? start_offset : 0), options, offsets, size_offsets);              start_offset, options, offsets, size_offsets);
863          time_taken = clock() - start_time;          time_taken = clock() - start_time;
864          fprintf(outfile, "Execute time %.3f milliseconds\n",          fprintf(outfile, "Execute time %.3f milliseconds\n",
865            ((double)time_taken * 1000.0)/            ((double)time_taken * 1000.0)/
# Line 826  while (!done) Line 867  while (!done)
867          }          }
868    
869        count = pcre_exec(re, extra, (char *)bptr, len,        count = pcre_exec(re, extra, (char *)bptr, len,
870          (do_g? start_offset : 0), options, offsets, size_offsets);          start_offset, options, offsets, size_offsets);
871    
872        if (count == 0)        if (count == 0)
873          {          {
# Line 834  while (!done) Line 875  while (!done)
875          count = size_offsets/3;          count = size_offsets/3;
876          }          }
877    
878          /* Matched */
879    
880        if (count >= 0)        if (count >= 0)
881          {          {
882          int i;          int i;
# Line 848  while (!done) Line 891  while (!done)
891              fprintf(outfile, "\n");              fprintf(outfile, "\n");
892              if (i == 0)              if (i == 0)
893                {                {
               start_offset = offsets[1];  
894                if (do_showrest)                if (do_showrest)
895                  {                  {
896                  fprintf(outfile, " 0+ ");                  fprintf(outfile, " 0+ ");
# Line 863  while (!done) Line 905  while (!done)
905            {            {
906            if ((copystrings & (1 << i)) != 0)            if ((copystrings & (1 << i)) != 0)
907              {              {
908              char buffer[16];              char copybuffer[16];
909              int rc = pcre_copy_substring((char *)bptr, offsets, count,              int rc = pcre_copy_substring((char *)bptr, offsets, count,
910                i, buffer, sizeof(buffer));                i, copybuffer, sizeof(copybuffer));
911              if (rc < 0)              if (rc < 0)
912                fprintf(outfile, "copy substring %d failed %d\n", i, rc);                fprintf(outfile, "copy substring %d failed %d\n", i, rc);
913              else              else
914                fprintf(outfile, "%2dC %s (%d)\n", i, buffer, rc);                fprintf(outfile, "%2dC %s (%d)\n", i, copybuffer, rc);
915              }              }
916            }            }
917    
# Line 906  while (!done) Line 948  while (!done)
948              free((void *)stringlist);              free((void *)stringlist);
949              }              }
950            }            }
   
951          }          }
952    
953          /* Failed to match */
954    
955        else        else
956          {          {
957          if (start_offset == 0)          if (gmatched == 0)
958            {            {
959            if (count == -1) fprintf(outfile, "No match\n");            if (count == -1) fprintf(outfile, "No match\n");
960              else fprintf(outfile, "Error %d\n", count);              else fprintf(outfile, "Error %d\n", count);
961            }            }
962          start_offset = -1;          break;  /* Out of the /g loop */
963          }          }
964    
965        if ((!do_g && !do_G) || start_offset <= 0) break;        /* If not /g or /G we are done */
966        if (do_G)  
967          if (!do_g && !do_G) break;
968    
969          /* If we have matched an empty string, set PCRE_NOTEMPTY for the next
970          match. This mimics what Perl's /g option does. */
971    
972          if (offsets[1] == offsets[0])
973            options |= PCRE_NOTEMPTY;
974          else
975            options &= ~PCRE_NOTEMPTY;
976    
977          /* For /g, update the start offset, leaving the rest alone */
978    
979          if (do_g) start_offset = offsets[1];
980    
981          /* For /G, update the pointer and length */
982    
983          else
984          {          {
985          bptr += start_offset;          bptr += offsets[1];
986          len -= start_offset;          len -= offsets[1];
987          }          }
988        }        }  /* End of loop for /g and /G */
989      }      }    /* End of loop for data lines */
990    
991    CONTINUE:    CONTINUE:
992    
993    #if !defined NOPOSIX
994    if (posix || do_posix) regfree(&preg);    if (posix || do_posix) regfree(&preg);
995    #endif
996    
997    if (re != NULL) free(re);    if (re != NULL) free(re);
998    if (extra != NULL) free(extra);    if (extra != NULL) free(extra);
999    if (tables != NULL)    if (tables != NULL)

Legend:
Removed from v.35  
changed lines
  Added in v.39

  ViewVC Help
Powered by ViewVC 1.1.5