Newer month parser. cpp
authorDustin Sallings <dustin@spy.net>
Wed Jun 06 16:36:12 2007 -0700 (2007-06-06)
branchcpp
changeset 397d5d101dac02
parent 38 871bab4f88e0
child 40 30c502e19239
Newer month parser.

This is mostly generated from gensearch, but I ended up doing a couple of
manual tweaks to avoid having to rewrite the parser for a small change.

1) Don't check the length of the input.
2) My strings are terminated with /, not \0.

It felt strange to write ``don't check the lenght'' of something, but a
couple of notes on that:

1) The match is doing memory comparisons and will fail if it finds a
character that doesn't lead to a match (which is what \0 is).
2) strlen() is O(n) and these strings are arbitrarily long. It'd only slow
me down to check the min lenth, and the max length isn't interesting.
logfiles.cpp
     1.1 --- a/logfiles.cpp	Wed Jun 06 16:08:49 2007 -0700
     1.2 +++ b/logfiles.cpp	Wed Jun 06 16:36:12 2007 -0700
     1.3 @@ -8,6 +8,7 @@
     1.4  
     1.5  #include <stdio.h>
     1.6  #include <time.h>
     1.7 +#include <ctype.h>
     1.8  #include <sys/time.h>
     1.9  #include <sys/types.h>
    1.10  
    1.11 @@ -151,42 +152,72 @@
    1.12  }
    1.13  
    1.14  /* Convert a three character month to the numeric value */
    1.15 -static int parseMonth(char *s)
    1.16 -{
    1.17 -	int rv=-1;
    1.18 +// generated by gensearch.py
    1.19 +static int parseMonth(const char *input) {
    1.20 +    int rv=-1;
    1.21 +    const char *p=input;
    1.22  
    1.23 -	assert(s != NULL);
    1.24 -	assert(s[0] != 0x00);
    1.25 -	assert(s[1] != 0x00);
    1.26 -	assert(s[2] != 0x00);
    1.27 -
    1.28 -	if(strncmp(s, "Jan", 3)==0) {
    1.29 -		rv=0;
    1.30 -	} else if(strncmp(s, "Feb", 3)==0) {
    1.31 -		rv=1;
    1.32 -	} else if(strncmp(s, "Mar", 3)==0) {
    1.33 -		rv=2;
    1.34 -	} else if(strncmp(s, "Apr", 3)==0) {
    1.35 -		rv=3;
    1.36 -	} else if(strncmp(s, "May", 3)==0) {
    1.37 -		rv=4;
    1.38 -	} else if(strncmp(s, "Jun", 3)==0) {
    1.39 -		rv=5;
    1.40 -	} else if(strncmp(s, "Jul", 3)==0) {
    1.41 -		rv=6;
    1.42 -	} else if(strncmp(s, "Aug", 3)==0) {
    1.43 -		rv=7;
    1.44 -	} else if(strncmp(s, "Sep", 3)==0) {
    1.45 -		rv=8;
    1.46 -	} else if(strncmp(s, "Oct", 3)==0) {
    1.47 -		rv=9;
    1.48 -	} else if(strncmp(s, "Nov", 3)==0) {
    1.49 -		rv=10;
    1.50 -	} else if(strncmp(s, "Dec", 3)==0) {
    1.51 -		rv=11;
    1.52 +    /* Collapsing anything with fewer than 4 paths */
    1.53 +	/* 12 matches for "" */
    1.54 +	switch(p[0]) {
    1.55 +		case 'A':
    1.56 +			/* 2 match(es) for "A" */
    1.57 +			if(memcmp(p, "Apr/", 4) == 0) {
    1.58 +				rv=3;
    1.59 +			} else if(memcmp(p, "Aug/", 4) == 0) {
    1.60 +				rv=7;
    1.61 +			}
    1.62 +		break;
    1.63 +		case 'D':
    1.64 +			/* 1 match(es) for "D" */
    1.65 +			if(memcmp(p, "Dec/", 4) == 0) {
    1.66 +				rv=11;
    1.67 +			}
    1.68 +		break;
    1.69 +		case 'F':
    1.70 +			/* 1 match(es) for "F" */
    1.71 +			if(memcmp(p, "Feb/", 4) == 0) {
    1.72 +				rv=1;
    1.73 +			}
    1.74 +		break;
    1.75 +		case 'J':
    1.76 +			/* 3 match(es) for "J" */
    1.77 +			if(memcmp(p, "Jan/", 4) == 0) {
    1.78 +				rv=0;
    1.79 +			} else if(memcmp(p, "Jun/", 4) == 0) {
    1.80 +				rv=5;
    1.81 +			} else if(memcmp(p, "Jul/", 4) == 0) {
    1.82 +				rv=6;
    1.83 +			}
    1.84 +		break;
    1.85 +		case 'M':
    1.86 +			/* 2 match(es) for "M" */
    1.87 +			if(memcmp(p, "Mar/", 4) == 0) {
    1.88 +				rv=2;
    1.89 +			} else if(memcmp(p, "May/", 4) == 0) {
    1.90 +				rv=4;
    1.91 +			}
    1.92 +		break;
    1.93 +		case 'O':
    1.94 +			/* 1 match(es) for "O" */
    1.95 +			if(memcmp(p, "Oct/", 4) == 0) {
    1.96 +				rv=9;
    1.97 +			}
    1.98 +		break;
    1.99 +		case 'N':
   1.100 +			/* 1 match(es) for "N" */
   1.101 +			if(memcmp(p, "Nov/", 4) == 0) {
   1.102 +				rv=10;
   1.103 +			}
   1.104 +		break;
   1.105 +		case 'S':
   1.106 +			/* 1 match(es) for "S" */
   1.107 +			if(memcmp(p, "Sep/", 4) == 0) {
   1.108 +				rv=8;
   1.109 +			}
   1.110 +		break;
   1.111  	}
   1.112 -
   1.113 -	return(rv);
   1.114 +	return rv;
   1.115  }
   1.116  
   1.117  class BadTimestamp : public std::exception {