Manually reading a character literal
So I've started with the easiest to deal with annoyance, escape characters, this is the function I got so far:
Code:
int rdEscChr( int_c_voidp_t getchr, void *source, int c, char32_t *c32 ) {
uint_least64_t num = 0;
char text[5] = {0};
str_t str = {0};
if ( c !== '\\' ) {
c = -1;
goto rdEscChr_done;
}
str.txt = text;
str.cap = 5;
switch ( (c = getchr(source)) ) {
case '0':
c = getchr(source);
if ( !isdigit(c) )
break;
c = rdU64_base62( getchr, source, c, 8, 0, &num );
goto rdEscChr_done;
case 'b': num = '\b'; break;
case 'f': num = '\f': break;
case 'n': num = '\n'; break;
case 'r': num = '\r'; break;
case 't': num = '\t'; break;
case 'u':
text[0] = getchr(source);
text[1] = getchr(source);
text[2] = getchr(source);
text[3] = getchr(source);
str.len = 4;
(void)rdU64_base62( (int_c_voidp_t)sgetc, &str, sgetc(&str), 16, 0, &num );
break;
case 'v': num = '\v'; break;
case 'x':
text[0] = getchr(source);
text[1] = getchr(source);
str.len = 3;
(void)rdU64_base62( (int_c_voidp_t)sgetc, &str, sgetc(&str), 16, 0, &num );
break;
// Escape characters \ ' " and ^ do not need to be explicitly defined here
default: num = c;
}
c = getchr(source);
rdEscChr_done:
if ( c32 ) *c32 = (char32_t)num;
return c;
}
If you want the details of rdU64_base62, str_t and sgetc then refer back to a previous thread of mine: Manually reading an integer literal
I'll worry about making those C89 compatible later if I decide to put them in a library (which I am considering atm).
Anyways does anyone see any potential problems or missing stuff?
Edit: Within a minute or 2 of posting this I noticed a bunch of problems myself and fixed them, I've now replaced the above code with that.