diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2012-06-04 22:11:31 -0800 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2012-06-04 22:11:31 -0800 |
| commit | 8491f4dde17cf9776d2ffdb3711c191f8826a2cf (patch) | |
| tree | 88df94f213c4dc31cf401cafd797a0e7ae1852e3 /src/posixregex.c | |
| parent | fe6e4dfa61dd74fcd73d97327558caf63097aeca (diff) | |
| download | dmstools-8491f4dde17cf9776d2ffdb3711c191f8826a2cf.tar.gz dmstools-8491f4dde17cf9776d2ffdb3711c191f8826a2cf.tar.bz2 dmstools-8491f4dde17cf9776d2ffdb3711c191f8826a2cf.tar.xz dmstools-8491f4dde17cf9776d2ffdb3711c191f8826a2cf.zip | |
Optimizing regex submatch loop.
Diffstat (limited to 'src/posixregex.c')
| -rw-r--r-- | src/posixregex.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/posixregex.c b/src/posixregex.c index 46f6efc..2800293 100644 --- a/src/posixregex.c +++ b/src/posixregex.c | |||
| @@ -203,6 +203,7 @@ int main(int argc, char * argv[]) | |||
| 203 | int verbosity; | 203 | int verbosity; |
| 204 | int substring; | 204 | int substring; |
| 205 | int reg_cflags; | 205 | int reg_cflags; |
| 206 | unsigned nmatch; | ||
| 206 | } opt; | 207 | } opt; |
| 207 | 208 | ||
| 208 | // getopt options | 209 | // getopt options |
| @@ -326,6 +327,10 @@ int main(int argc, char * argv[]) | |||
| 326 | return(1); | 327 | return(1); |
| 327 | }; | 328 | }; |
| 328 | 329 | ||
| 330 | // determines number of subexpressions | ||
| 331 | opt.nmatch = (MAX_MATCHES > regex.re_nsub) ? regex.re_nsub : MAX_MATCHES; | ||
| 332 | opt.nmatch++; | ||
| 333 | |||
| 329 | // Loops through command line arguments and tests the compiled expressions | 334 | // Loops through command line arguments and tests the compiled expressions |
| 330 | // against each argument. | 335 | // against each argument. |
| 331 | for (x = optind; x < argc; x++) | 336 | for (x = optind; x < argc; x++) |
| @@ -336,7 +341,7 @@ int main(int argc, char * argv[]) | |||
| 336 | printf("%3i: %s ==> ", x-optind+1, arg); | 341 | printf("%3i: %s ==> ", x-optind+1, arg); |
| 337 | 342 | ||
| 338 | // tests the buffer against the regular expression | 343 | // tests the buffer against the regular expression |
| 339 | err = regexec(®ex, arg, (size_t)MAX_MATCHES, matches, 0); | 344 | err = regexec(®ex, arg, opt.nmatch, matches, 0); |
| 340 | 345 | ||
| 341 | // skips displaying substring if error was encountered | 346 | // skips displaying substring if error was encountered |
| 342 | if ( ((err)) && ((opt.substring)) ) | 347 | if ( ((err)) && ((opt.substring)) ) |
| @@ -370,7 +375,7 @@ int main(int argc, char * argv[]) | |||
| 370 | if (opt.verbosity) | 375 | if (opt.verbosity) |
| 371 | { | 376 | { |
| 372 | // displays submatches | 377 | // displays submatches |
| 373 | for(y = 0; ((y < MAX_MATCHES) && (y <= regex.re_nsub)); y++) | 378 | for(y = 0; (y < opt.nmatch); y++) |
| 374 | { | 379 | { |
| 375 | // copies sub matches in buffer string | 380 | // copies sub matches in buffer string |
| 376 | memset(str, 0, (size_t)BUFFER_SIZE); | 381 | memset(str, 0, (size_t)BUFFER_SIZE); |
