johnezang/SDURLCache — comment on commit 9c264842e3abd547d301ef8cf4ebdb35b81c5200
File:  line 0
By johnezang:

[Ragel](http://www.complang.org/ragel/). Below is the ragel source I used. I then polished / pretty-printed and tweaked the output from `ragel`. Compiled with:

shell% ragel -F1 http-date.rl
shell% gcc -o http-date http-date.c
shell% ./http-date 'Sun, 06 Nov 1994 08:49:37 GMT' 'Sunday, 06-Nov-94 08:49:37 GMT' 'Sun Nov 6 08:49:37 1994' 'Sat Dec 24 14:34:26 2037' 'Sunday, 06-Nov-94 08:49:37 GMT' 'Sun, 06 Nov 1994 08:49:37 GMT'

```c
// http-date.rl

#include
#include
#include
#include

%%{
machine httpDate;

yearDigit = ( [0-9] @{ gdate.year = gdate.year * 10 + (fc - '0'); });
dayDigit = ( [0-9] @{ gdate.day = gdate.day * 10 + (fc - '0'); });
hourDigit = ( [0-9] @{ gdate.hour = gdate.hour * 10 + (fc - '0'); });
minuteDigit = ( [0-9] @{ gdate.minute = gdate.minute * 10 + (fc - '0'); });
secondDigit = ( [0-9] @{ gdate.second = gdate.second * 10.0 + (fc - '0'); });

wkday = ("Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun");
weekday = ("Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday");
month = (("Jan" @{ gdate.month = 1; }) | ("Feb" @{ gdate.month = 2; }) | ("Mar" @{ gdate.month = 3; }) |
("Apr" @{ gdate.month = 4; }) | ("May" @{ gdate.month = 5; }) | ("Jun" @{ gdate.month = 6; }) |
("Jul" @{ gdate.month = 7; }) | ("Aug" @{ gdate.month = 8; }) | ("Sep" @{ gdate.month = 9; }) |
("Oct" @{ gdate.month = 10; }) | ("Nov" @{ gdate.month = 11; }) | ("Dec" @{ gdate.month = 12; }));

year4Digits = (yearDigit . yearDigit . yearDigit . yearDigit);
year2Digits = ((yearDigit . yearDigit) @{ gdate.year += 1900; });
dayDigits = (dayDigit . dayDigit);
hourDigits = (hourDigit . hourDigit);
minuteDigits = (minuteDigit . minuteDigit);
secondDigits = (secondDigit . secondDigit);

date1 = (dayDigits . " " . month . " " . year4Digits);
date2 = (dayDigits . "-" . month . "-" . year2Digits);
date3 = (month . " " . (dayDigits | " " . dayDigit));
time = (hourDigits . ":" . minuteDigits . ":" . secondDigits);

rfc1123date = (wkday . "," . " " . date1 . " " . time . " " . "GMT");
rfc850date = (weekday . "," . " " . date2 . " " . time . " " . "GMT");
asctimedate = (wkday . " " . date3 . " " . time . " " . year4Digits);

HTTPdate = ((rfc1123date %{ parsed = 1; }) | (rfc850date %{ parsed = 1; }) | (asctimedate %{ parsed = 1; }));

main := HTTPdate;
}%%

%% write data nofinal;

typedef signed char SInt8;
typedef signed int SInt32;

struct __gdate {
SInt32 year;
SInt8 month;
SInt8 day;
SInt8 hour;
SInt8 minute;
double second;
};
typedef struct __gdate __gdate;

void scanner(char *buf) {
int cs;

int parsed = 0;
__gdate gdate;
memset(&gdate, 0, sizeof(__gdate));

%% write init;

{
int len = strlen(buf);
char *p = buf, *pe = p + len, *eof = pe;

%% write exec;
}

printf("parsed: %d, %d/%d/%d %d:%d:%.2f\n", parsed, gdate.month, gdate.day, gdate.year, gdate.hour, gdate.minute, gdate.second);
}

int main(int argc, char *argv[]) {
if(argc > 1) { int x; for(x = 1; x < argc; x++) { scanner(argv[x]); printf("-----\n"); }}
return 0;
}
```