1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/* Reads in up to MAX bytes and runs regcomp against them TIMES times, using
the regular expression given on the command line.
Uses the Henry Spencer V8 regular expressions which the kernel version of
l7-filter uses.
See ../LICENCE for copyright
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include "regexp/regexp.c"
#define MAX 1500
#define TIMES 100000
#define MAX_PATTERN_LEN 8192
static int hex2dec(char c)
{
switch (c)
{
case '0' ... '9':
return c - '0';
case 'a' ... 'f':
return c - 'a' + 10;
case 'A' ... 'F':
return c - 'A' + 10;
default:
fprintf(stderr, "hex2dec: bad value!\n");
exit(1);
}
}
/* takes a string with \xHH escapes and returns one with the characters
they stand for */
static char * pre_process(char * s)
{
char * result = malloc(strlen(s) + 1);
int sindex = 0, rindex = 0;
while( sindex < strlen(s) )
{
if( sindex + 3 < strlen(s) &&
s[sindex] == '\\' && s[sindex+1] == 'x' &&
isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
{
/* carefully remember to call tolower here... */
result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
hex2dec(s[sindex + 3] ) );
sindex += 3; /* 4 total */
}
else
result[rindex] = tolower(s[sindex]);
sindex++;
rindex++;
}
result[rindex] = '\0';
return result;
}
void doit(regexp * pattern, char ** argv, int verbose)
{
char input[MAX];
int c;
for(c = 0; c < MAX; c++){
char temp = 0;
while(temp == 0){
if(EOF == scanf("%c", &temp))
goto out;
input[c] = temp;
}
}
out:
input[c-1] = '\0';
for(c = 0; c < MAX; c++) input[c] = tolower(input[c]);
for(c = 1; c < TIMES; c++){
int result = regexec(pattern, input);
if(c == 1)
if(result)
printf("match\t");
else
printf("no_match\t");
if(TIMES/20 > 0 && c%(TIMES/20) == 0){ fprintf(stderr, "."); }
}
if(verbose)
puts("");
else
printf(" ");
}
// Syntax: test_speed regex [verbose]
int main(int argc, char ** argv)
{
regexp * pattern = (regexp *)malloc(sizeof(struct regexp));
char * s = argv[1];
int patternlen, i, verbose = 0;
if(argc < 2){
fprintf(stderr, "need an arg\n");
return 1;
}
if(argc > 2)
verbose = 1;
patternlen = strlen(s);
if(patternlen > MAX_PATTERN_LEN){
fprintf(stderr, "Pattern too long! Max is %d\n", MAX_PATTERN_LEN);
return 1;
}
s = pre_process(s); /* do \xHH escapes */
pattern = regcomp(s, &patternlen);
if(!pattern){
fprintf(stderr, "error compiling regexp\n");
exit(1);
}
if(verbose)
printf("running regexec \"%.16s...\" %d times\n", argv[1], TIMES);
doit(pattern, argv, verbose);
return 0;
}
|