parseMonth implemented as a tiny loop and an integer switch. cpp
authorDustin Sallings <dustin@spy.net>
Thu Jun 07 09:52:29 2007 -0700 (2 years ago)
branchcpp
changeset 5255ca792142e8
parent 51ed2d915f1f69
child 53f5aaab9f19eb
child 571f07a8a43987
parseMonth implemented as a tiny loop and an integer switch.

That loop will *surely* be unrolled and any decent compiler should be able
to generate really optimal code from the switch.
logfiles.cpp
     1.1 --- a/logfiles.cpp	Thu Jun 07 02:05:22 2007 -0700
     1.2 +++ b/logfiles.cpp	Thu Jun 07 09:52:29 2007 -0700
     1.3 @@ -156,25 +156,25 @@
     1.4  /* Convert a three character month to the numeric value */
     1.5  TESTED_STATIC int parseMonth(const char *input) {
     1.6      int rv=-1;
     1.7 -	struct date_str tmp;
     1.8 -	static struct date_str dates[]={
     1.9 -		{"Jan/", 0}, {"Feb/", 1}, {"Mar/", 2}, {"Apr/", 3}, {"May/", 4},
    1.10 -		{"Jun/", 5}, {"Jul/", 6}, {"Aug/", 7}, {"Sep/", 8}, {"Oct/", 9},
    1.11 -		{"Nov/", 10}, {"Dec/", 11}
    1.12 -	};
    1.13 +	int inputInt=0;
    1.14  
    1.15 -	for(int i=0; i<12; i++) {
    1.16 -		if(memcmp(dates[i].datestr, input, 4) == 0) {
    1.17 -			rv=dates[i].val;
    1.18 -			// If it's not already, move it to the front of the list.  It'll
    1.19 -			// probably be in use for a while.
    1.20 -			if(i != 0) {
    1.21 -				tmp=dates[0];
    1.22 -				dates[0]=dates[i];
    1.23 -				dates[i]=tmp;
    1.24 -			}
    1.25 -			break;
    1.26 -		}
    1.27 +	for(int i=0; i<4 && input[i]; i++) {
    1.28 +		inputInt = (inputInt << 8) | input[i];
    1.29 +	}
    1.30 +
    1.31 +	switch(inputInt) {
    1.32 +		case 'Jan/': rv=0; break;
    1.33 +		case 'Feb/': rv=1; break;
    1.34 +		case 'Mar/': rv=2; break;
    1.35 +		case 'Apr/': rv=3; break;
    1.36 +		case 'May/': rv=4; break;
    1.37 +		case 'Jun/': rv=5; break;
    1.38 +		case 'Jul/': rv=6; break;
    1.39 +		case 'Aug/': rv=7; break;
    1.40 +		case 'Sep/': rv=8; break;
    1.41 +		case 'Oct/': rv=9; break;
    1.42 +		case 'Nov/': rv=10; break;
    1.43 +		case 'Dec/': rv=11; break;
    1.44  	}
    1.45  
    1.46  	return rv;