1 /* Reads in up to MAX bytes and runs regcomp against them TIMES times, using
2 the regular expression given on the command line.
4 Uses the Henry Spencer V8 regular expressions which the kernel version of
7 See ../LICENCE for copyright
15 #include "regexp/regexp.c"
19 #define MAX_PATTERN_LEN 8192
21 static int hex2dec(char c
)
32 fprintf(stderr
, "hex2dec: bad value!\n");
37 /* takes a string with \xHH escapes and returns one with the characters
39 static char * pre_process(char * s
)
41 char * result
= malloc(strlen(s
) + 1);
42 int sindex
= 0, rindex
= 0;
43 while( sindex
< strlen(s
) )
45 if( sindex
+ 3 < strlen(s
) &&
46 s
[sindex
] == '\\' && s
[sindex
+1] == 'x' &&
47 isxdigit(s
[sindex
+ 2]) && isxdigit(s
[sindex
+ 3]) )
49 /* carefully remember to call tolower here... */
50 result
[rindex
] = tolower( hex2dec(s
[sindex
+ 2])*16 +
51 hex2dec(s
[sindex
+ 3] ) );
52 sindex
+= 3; /* 4 total */
55 result
[rindex
] = tolower(s
[sindex
]);
60 result
[rindex
] = '\0';
66 void doit(regexp
* pattern
, char ** argv
, int verbose
)
71 for(c
= 0; c
< MAX
; c
++){
74 if(EOF
== scanf("%c", &temp
))
83 for(c
= 0; c
< MAX
; c
++) input
[c
] = tolower(input
[c
]);
85 for(c
= 1; c
< TIMES
; c
++){
86 int result
= regexec(pattern
, input
);
93 if(TIMES
/20 > 0 && c
%(TIMES
/20) == 0){ fprintf(stderr
, "."); }
101 // Syntax: test_speed regex [verbose]
102 int main(int argc
, char ** argv
)
104 regexp
* pattern
= (regexp
*)malloc(sizeof(struct regexp
));
106 int patternlen
, i
, verbose
= 0;
109 fprintf(stderr
, "need an arg\n");
115 patternlen
= strlen(s
);
116 if(patternlen
> MAX_PATTERN_LEN
){
117 fprintf(stderr
, "Pattern too long! Max is %d\n", MAX_PATTERN_LEN
);
121 s
= pre_process(s
); /* do \xHH escapes */
123 pattern
= regcomp(s
, &patternlen
);
126 fprintf(stderr
, "error compiling regexp\n");
131 printf("running regexec \"%.16s...\" %d times\n", argv
[1], TIMES
);
133 doit(pattern
, argv
, verbose
);