/[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 23 by nigel, Sat Feb 24 21:38:41 2007 UTC revision 31 by nigel, Sat Feb 24 21:38:57 2007 UTC
# Line 7  Line 7 
7  #include <string.h>  #include <string.h>
8  #include <stdlib.h>  #include <stdlib.h>
9  #include <time.h>  #include <time.h>
10    #include <locale.h>
11    
12  /* Use the internal info for displaying the results of pcre_study(). */  /* Use the internal info for displaying the results of pcre_study(). */
13    
# Line 21  Line 22 
22  #endif  #endif
23  #endif  #endif
24    
25  #define LOOPREPEAT 10000  #define LOOPREPEAT 20000
26    
27    
28  static FILE *outfile;  static FILE *outfile;
# Line 273  compiled re. */ Line 274  compiled re. */
274    
275  static void *new_malloc(size_t size)  static void *new_malloc(size_t size)
276  {  {
277  if (log_store) fprintf(outfile, "Store size request: %d\n", (int)size);  if (log_store)
278      fprintf(outfile, "Memory allocation request: %d (code space %d)\n",
279        (int)size, (int)size - offsetof(real_pcre, code[0]));
280  return malloc(size);  return malloc(size);
281  }  }
282    
# Line 291  int study_options = 0; Line 294  int study_options = 0;
294  int op = 1;  int op = 1;
295  int timeit = 0;  int timeit = 0;
296  int showinfo = 0;  int showinfo = 0;
297    int showstore = 0;
298  int posix = 0;  int posix = 0;
299  int debug = 0;  int debug = 0;
300  int done = 0;  int done = 0;
# Line 305  outfile = stdout; Line 309  outfile = stdout;
309    
310  while (argc > 1 && argv[op][0] == '-')  while (argc > 1 && argv[op][0] == '-')
311    {    {
312    if (strcmp(argv[op], "-s") == 0) log_store = 1;    if (strcmp(argv[op], "-s") == 0 || strcmp(argv[op], "-m") == 0)
313        showstore = 1;
314    else if (strcmp(argv[op], "-t") == 0) timeit = 1;    else if (strcmp(argv[op], "-t") == 0) timeit = 1;
315    else if (strcmp(argv[op], "-i") == 0) showinfo = 1;    else if (strcmp(argv[op], "-i") == 0) showinfo = 1;
316    else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1;    else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1;
# Line 313  while (argc > 1 && argv[op][0] == '-') Line 318  while (argc > 1 && argv[op][0] == '-')
318    else    else
319      {      {
320      printf("*** Unknown option %s\n", argv[op]);      printf("*** Unknown option %s\n", argv[op]);
321        printf("Usage: pcretest [-d] [-i] [-p] [-s] [-t] [<input> [<output>]]\n");
322        printf("  -d   debug: show compiled code; implies -i\n"
323               "  -i   show information about compiled pattern\n"
324               "  -p   use POSIX interface\n"
325               "  -s   output store information\n"
326               "  -t   time compilation and execution\n");
327      return 1;      return 1;
328      }      }
329    op++;    op++;
# Line 357  while (!done) Line 368  while (!done)
368    pcre_extra *extra = NULL;    pcre_extra *extra = NULL;
369    regex_t preg;    regex_t preg;
370    const char *error;    const char *error;
371    unsigned char *p, *pp;    unsigned char *p, *pp, *ppp;
372      unsigned const char *tables = NULL;
373    int do_study = 0;    int do_study = 0;
374    int do_debug = 0;    int do_debug = debug;
375      int do_showinfo = showinfo;
376    int do_posix = 0;    int do_posix = 0;
377    int erroroffset, len, delimiter;    int erroroffset, len, delimiter;
378    
# Line 376  while (!done) Line 389  while (!done)
389    
390    delimiter = *p++;    delimiter = *p++;
391    
392    if (isalnum(delimiter))    if (isalnum(delimiter) || delimiter == '\\')
393      {      {
394      fprintf(outfile, "** Delimiter must not be alphameric\n");      fprintf(outfile, "** Delimiter must not be alphameric or \\\n");
395      goto SKIP_DATA;      goto SKIP_DATA;
396      }      }
397    
# Line 386  while (!done) Line 399  while (!done)
399    
400    for(;;)    for(;;)
401      {      {
402      while (*pp != 0 && *pp != delimiter) pp++;      while (*pp != 0)
403          {
404          if (*pp == '\\' && pp[1] != 0) pp++;
405            else if (*pp == delimiter) break;
406          pp++;
407          }
408      if (*pp != 0) break;      if (*pp != 0) break;
409    
410      len = sizeof(buffer) - (pp - buffer);      len = sizeof(buffer) - (pp - buffer);
# Line 406  while (!done) Line 424  while (!done)
424      if (infile != stdin) fprintf(outfile, "%s", (char *)pp);      if (infile != stdin) fprintf(outfile, "%s", (char *)pp);
425      }      }
426    
427      /* If the first character after the delimiter is backslash, make
428      the pattern end with backslash. This is purely to provide a way
429      of testing for the error message when a pattern ends with backslash. */
430    
431      if (pp[1] == '\\') *pp++ = '\\';
432    
433    /* Terminate the pattern at the delimiter */    /* Terminate the pattern at the delimiter */
434    
435    *pp++ = 0;    *pp++ = 0;
# Line 414  while (!done) Line 438  while (!done)
438    
439    options = 0;    options = 0;
440    study_options = 0;    study_options = 0;
441      log_store = showstore;  /* default from command line */
442    
443    while (*pp != 0)    while (*pp != 0)
444      {      {
445      switch (*pp++)      switch (*pp++)
# Line 422  while (!done) Line 448  while (!done)
448        case 'm': options |= PCRE_MULTILINE; break;        case 'm': options |= PCRE_MULTILINE; break;
449        case 's': options |= PCRE_DOTALL; break;        case 's': options |= PCRE_DOTALL; break;
450        case 'x': options |= PCRE_EXTENDED; break;        case 'x': options |= PCRE_EXTENDED; break;
451    
452        case 'A': options |= PCRE_ANCHORED; break;        case 'A': options |= PCRE_ANCHORED; break;
453        case 'D': do_debug = 1; break;        case 'D': do_debug = do_showinfo = 1; break;
454        case 'E': options |= PCRE_DOLLAR_ENDONLY; break;        case 'E': options |= PCRE_DOLLAR_ENDONLY; break;
455          case 'I': do_showinfo = 1; break;
456          case 'M': log_store = 1; break;
457        case 'P': do_posix = 1; break;        case 'P': do_posix = 1; break;
458        case 'S': do_study = 1; break;        case 'S': do_study = 1; break;
459        case 'U': options |= PCRE_UNGREEDY; break;        case 'U': options |= PCRE_UNGREEDY; break;
460        case 'X': options |= PCRE_EXTRA; break;        case 'X': options |= PCRE_EXTRA; break;
461    
462          case 'L':
463          ppp = pp;
464          while (*ppp != '\n' && *ppp != ' ') ppp++;
465          *ppp = 0;
466          if (setlocale(LC_CTYPE, (const char *)pp) == NULL)
467            {
468            fprintf(outfile, "** Failed to set locale \"%s\"\n", pp);
469            goto SKIP_DATA;
470            }
471          tables = pcre_maketables();
472          pp = ppp;
473          break;
474    
475        case '\n': case ' ': break;        case '\n': case ' ': break;
476        default:        default:
477        fprintf(outfile, "** Unknown option '%c'\n", pp[-1]);        fprintf(outfile, "** Unknown option '%c'\n", pp[-1]);
# Line 437  while (!done) Line 480  while (!done)
480      }      }
481    
482    /* Handle compiling via the POSIX interface, which doesn't support the    /* Handle compiling via the POSIX interface, which doesn't support the
483    timing, showing, or debugging options. */    timing, showing, or debugging options, nor the ability to pass over
484      local character tables. */
485    
486    if (posix || do_posix)    if (posix || do_posix)
487      {      {
# Line 469  while (!done) Line 513  while (!done)
513        clock_t start_time = clock();        clock_t start_time = clock();
514        for (i = 0; i < LOOPREPEAT; i++)        for (i = 0; i < LOOPREPEAT; i++)
515          {          {
516          re = pcre_compile((char *)p, options, &error, &erroroffset);          re = pcre_compile((char *)p, options, &error, &erroroffset, tables);
517          if (re != NULL) free(re);          if (re != NULL) free(re);
518          }          }
519        time_taken = clock() - start_time;        time_taken = clock() - start_time;
520        fprintf(outfile, "Compile time %.2f milliseconds\n",        fprintf(outfile, "Compile time %.3f milliseconds\n",
521          ((double)time_taken)/(4 * CLOCKS_PER_SEC));          ((double)time_taken * 1000.0) /
522            ((double)LOOPREPEAT * (double)CLOCKS_PER_SEC));
523        }        }
524    
525      re = pcre_compile((char *)p, options, &error, &erroroffset);      re = pcre_compile((char *)p, options, &error, &erroroffset, tables);
526    
527      /* Compilation failed; go back for another re, skipping to blank line      /* Compilation failed; go back for another re, skipping to blank line
528      if non-interactive. */      if non-interactive. */
# Line 501  while (!done) Line 546  while (!done)
546            }            }
547          fprintf(outfile, "\n");          fprintf(outfile, "\n");
548          }          }
549        continue;        goto CONTINUE;
550        }        }
551    
552      /* Compilation succeeded; print data if required */      /* Compilation succeeded; print data if required */
553    
554      if (showinfo || do_debug)      if (do_showinfo)
555        {        {
556        int first_char, count;        int first_char, count;
557    
558        if (debug || do_debug) print_internals(re, outfile);        if (do_debug) print_internals(re, outfile);
559    
560        count = pcre_info(re, &options, &first_char);        count = pcre_info(re, &options, &first_char);
561        if (count < 0) fprintf(outfile,        if (count < 0) fprintf(outfile,
# Line 560  while (!done) Line 605  while (!done)
605            extra = pcre_study(re, study_options, &error);            extra = pcre_study(re, study_options, &error);
606          time_taken = clock() - start_time;          time_taken = clock() - start_time;
607          if (extra != NULL) free(extra);          if (extra != NULL) free(extra);
608          fprintf(outfile, "  Study time %.2f milliseconds\n",          fprintf(outfile, "  Study time %.3f milliseconds\n",
609            ((double)time_taken)/(4 * CLOCKS_PER_SEC));            ((double)time_taken * 1000.0)/
610              ((double)LOOPREPEAT * (double)CLOCKS_PER_SEC));
611          }          }
612    
613        extra = pcre_study(re, study_options, &error);        extra = pcre_study(re, study_options, &error);
# Line 573  while (!done) Line 619  while (!done)
619        /* This looks at internal information. A bit kludgy to do it this        /* This looks at internal information. A bit kludgy to do it this
620        way, but it is useful for testing. */        way, but it is useful for testing. */
621    
622        else if (showinfo || do_debug)        else if (do_showinfo)
623          {          {
624          real_pcre_extra *xx = (real_pcre_extra *)extra;          real_pcre_extra *xx = (real_pcre_extra *)extra;
625          if ((xx->options & PCRE_STUDY_MAPPED) == 0)          if ((xx->options & PCRE_STUDY_MAPPED) == 0)
# Line 616  while (!done) Line 662  while (!done)
662      {      {
663      unsigned char *q;      unsigned char *q;
664      int count, c;      int count, c;
665        int copystrings = 0;
666        int getstrings = 0;
667        int getlist = 0;
668      int offsets[45];      int offsets[45];
669      int size_offsets = sizeof(offsets)/sizeof(int);      int size_offsets = sizeof(offsets)/sizeof(int);
670    
# Line 681  while (!done) Line 730  while (!done)
730          options |= PCRE_NOTBOL;          options |= PCRE_NOTBOL;
731          continue;          continue;
732    
733            case 'C':
734            while(isdigit(*p)) n = n * 10 + *p++ - '0';
735            copystrings |= 1 << n;
736            continue;
737    
738            case 'G':
739            while(isdigit(*p)) n = n * 10 + *p++ - '0';
740            getstrings |= 1 << n;
741            continue;
742    
743            case 'L':
744            getlist = 1;
745            continue;
746    
747          case 'O':          case 'O':
748          while(isdigit(*p)) n = n * 10 + *p++ - '0';          while(isdigit(*p)) n = n * 10 + *p++ - '0';
749          if (n <= (int)(sizeof(offsets)/sizeof(int))) size_offsets = n;          if (n <= (int)(sizeof(offsets)/sizeof(int))) size_offsets = n;
# Line 739  while (!done) Line 802  while (!done)
802          register int i;          register int i;
803          clock_t time_taken;          clock_t time_taken;
804          clock_t start_time = clock();          clock_t start_time = clock();
805          for (i = 0; i < 4000; i++)          for (i = 0; i < LOOPREPEAT; i++)
806            count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets,            count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets,
807              size_offsets);              size_offsets);
808          time_taken = clock() - start_time;          time_taken = clock() - start_time;
809          fprintf(outfile, "Execute time %.2f milliseconds\n",          fprintf(outfile, "Execute time %.3f milliseconds\n",
810            ((double)time_taken)/(4 * CLOCKS_PER_SEC));            ((double)time_taken * 1000.0)/
811              ((double)LOOPREPEAT * (double)CLOCKS_PER_SEC));
812          }          }
813    
814        count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets,        count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets,
# Line 759  while (!done) Line 823  while (!done)
823        if (count >= 0)        if (count >= 0)
824          {          {
825          int i;          int i;
826          count *= 2;          for (i = 0; i < count * 2; i += 2)
         for (i = 0; i < count; i += 2)  
827            {            {
828            if (offsets[i] < 0)            if (offsets[i] < 0)
829              fprintf(outfile, "%2d: <unset>\n", i/2);              fprintf(outfile, "%2d: <unset>\n", i/2);
# Line 771  while (!done) Line 834  while (!done)
834              fprintf(outfile, "\n");              fprintf(outfile, "\n");
835              }              }
836            }            }
837    
838            for (i = 0; i < 32; i++)
839              {
840              if ((copystrings & (1 << i)) != 0)
841                {
842                char buffer[16];
843                int rc = pcre_copy_substring((char *)dbuffer, offsets, count,
844                  i, buffer, sizeof(buffer));
845                if (rc < 0)
846                  fprintf(outfile, "copy substring %d failed %d\n", i, rc);
847                else
848                  fprintf(outfile, "%2dC %s (%d)\n", i, buffer, rc);
849                }
850              }
851    
852            for (i = 0; i < 32; i++)
853              {
854              if ((getstrings & (1 << i)) != 0)
855                {
856                const char *substring;
857                int rc = pcre_get_substring((char *)dbuffer, offsets, count,
858                  i, &substring);
859                if (rc < 0)
860                  fprintf(outfile, "get substring %d failed %d\n", i, rc);
861                else
862                  {
863                  fprintf(outfile, "%2dG %s (%d)\n", i, substring, rc);
864                  free((void *)substring);
865                  }
866                }
867              }
868    
869            if (getlist)
870              {
871              const char **stringlist;
872              int rc = pcre_get_substring_list((char *)dbuffer, offsets, count,
873                &stringlist);
874              if (rc < 0)
875                fprintf(outfile, "get substring list failed %d\n", rc);
876              else
877                {
878                for (i = 0; i < count; i++)
879                  fprintf(outfile, "%2dL %s\n", i, stringlist[i]);
880                if (stringlist[i] != NULL)
881                  fprintf(outfile, "string list not terminated by NULL\n");
882                free((void *)stringlist);
883                }
884              }
885    
886          }          }
887        else        else
888          {          {
# Line 784  while (!done) Line 896  while (!done)
896    if (posix || do_posix) regfree(&preg);    if (posix || do_posix) regfree(&preg);
897    if (re != NULL) free(re);    if (re != NULL) free(re);
898    if (extra != NULL) free(extra);    if (extra != NULL) free(extra);
899      if (tables != NULL)
900        {
901        free((void *)tables);
902        setlocale(LC_CTYPE, "C");
903        }
904    }    }
905    
906  fprintf(outfile, "\n");  fprintf(outfile, "\n");

Legend:
Removed from v.23  
changed lines
  Added in v.31

  ViewVC Help
Powered by ViewVC 1.1.5