From dd2e898a13855acd7034a2ee73584995d7452f4f Mon Sep 17 00:00:00 2001 From: markus schnalke Date: Wed, 7 Sep 2016 16:31:58 +0200 Subject: [PATCH] Replace lookup tables for obscure hashes with more readable code Time-performance doesn't matter here. If you think differently, demonstrate it convincingly! --- sbr/dtimep.lex | 81 +++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 60 deletions(-) diff --git a/sbr/dtimep.lex b/sbr/dtimep.lex index db2b193..0581736 100644 --- a/sbr/dtimep.lex +++ b/sbr/dtimep.lex @@ -56,67 +56,29 @@ */ int europeandate = 0; -/* -** Table to convert month names to numeric month. We use the -** fact that the low order 5 bits of the sum of the 2nd & 3rd -** characters of the name is a hash with no collisions for the 12 -** valid month names. (The mask to 5 bits maps any combination of -** upper and lower case into the same hash value). -*/ -static int month_map[] = { - 0, - 6, /* 1 - Jul */ - 3, /* 2 - Apr */ - 5, /* 3 - Jun */ - 0, - 10, /* 5 - Nov */ - 0, - 1, /* 7 - Feb */ - 11, /* 8 - Dec */ - 0, - 0, - 0, - 0, - 0, - 0, - 0, /*15 - Jan */ - 0, - 0, - 0, - 2, /*19 - Mar */ - 0, - 8, /*21 - Sep */ - 0, - 9, /*23 - Oct */ - 0, - 0, - 4, /*26 - May */ - 0, - 7 /*28 - Aug */ +static char *monthnames[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + NULL }; -/* -** Lookup table for day-of-week using the same hash trick as for above -** name-of-month table, but using the first and second character, not -** second and third. -** -** Compute index into table using: (day_name[0] & 7) + (day_name[1] & 4) -*/ -static int day_map[] = { - 0, - 0, - 0, - 6, /* 3 - Sat */ - 4, /* 4 - Thu */ - 0, - 5, /* 6 - Fri */ - 0, /* 7 - Sun */ - 2, /* 8 - Tue */ - 1 /* 9 - Mon */, - 0, - 3 /*11 - Wed */ +static char *daynames[] = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL }; +static int +name2num(char *name, char *names[]) +{ + int i; + + for (i=0; names[i]; i++) { + if (strncasecmp(name, names[i], strlen(names[i]))==0) { + return i; + } + } + return 0; +} + /* ** The SET* macros will parse for the appropriate field, and leave the ** cp pointer at the first character after the desired field. Be @@ -129,10 +91,9 @@ static int day_map[] = { */ #define INIT() { cp = yytext;} -#define SETWDAY() { tw.tw_wday= day_map[(cp[0] & 7) + (cp[1] & 4)]; \ +#define SETWDAY() { tw.tw_wday = name2num(cp, daynames); \ tw.tw_flags &= ~TW_SDAY; tw.tw_flags |= TW_SEXP; SKIPA(); } -#define SETMON() { cp++; tw.tw_mon = month_map[(cp[0] + cp[1]) & 0x1f]; \ - SKIPA(); } +#define SETMON() { tw.tw_mon = name2num(cp, monthnames); SKIPA(); } #define SETMON_NUM() { tw.tw_mon = atoi(cp)-1; SKIPD(); } #define SETYEAR() { tw.tw_year = atoi(cp); SKIPD(); } #define SETDAY() { tw.tw_mday = atoi(cp); tw.tw_flags |= TW_YES; SKIPD(); } -- 1.7.10.4