
Originally Posted by
Hodor
Is it safe assigning -1 to 'num' (or c32 for that matter) considering num is unsigned? Other than that I guess the function is perfect. Except, why is 'source' a void*?
again Hodor the getchr() and source variable are abstracted to indicate the function doesn't care how the characters are provided only that they are provided, as for that -1 I'll have a look what for what you mean 1st, in mean time I'll post my updated function:
Code:
int ishex( int c ) {
return ((c >= '0' && c <='9')
|| (c >= 'A' && c <= 'F')
|| (c >= 'a' && c <= 'f'));
}
int rdEscChr( int_c_voidp_t getchr, void *source, int c, char32_t *c32 ) {
uint_least64_t num = 0;
char *mbc = (char*)c32, *var;
int i, m;
if ( c != '\\' ) {
c = -1;
goto rdEscChr_done;
}
switch ( (m = getchr(source)) ) {
case 'a': num = '\a'; break;
case 'b': num = '\b'; break;
case 'e': num = '\e'; break;
case 'f': num = '\f'; break;
case 'n': num = '\n'; break;
case 'r': num = '\r'; break;
case 't': num = '\t'; break;
case 'u': c = rdU64_base62( getchr, source, getchr(source), 4, 16, 0, &num ); goto rdEscChr_done;
case 'U': c = rdU64_base62( getchr, source, getchr(source), 8, 16, 0, &num ); goto rdEscChr_done;
case 'v': num = '\v'; break;
case 'x': c = rdU64_base62( getchr, source, getchr(source), 4, 16, 0, &num ); goto rdEscChr_done;
// Escape characters \ ' " and ^ do not need to be explicitly defined here
default:
if ( isdigit(m) ) {
c = rdU64_base62( getchr, source, m, 3, 8, 0, &num );
goto rdEscChr_done;
}
else num = m;
}
c = getchr(source);
rdEscChr_done:
if ( c32 ) {
var = getenv("MBCHAR_DIR");
if ( var && var[0] == '>') {
for ( i = 0; i < 4; ++i ) {
if ( mbc[i] == 0 ) {
break;
}
}
if ( i == 4 ) {
*c32 = num;
return -1;
}
*c32 |= (num << (i * 8));
}
else *c32 = (((*c32) << 8) | (char32_t)num);
}
return c;
}
In the middle of manually testing stuff, most of escaped characters work fine, I think I will be adding a new parameter to rdU64_base62 though to limit the number of characters read (or set infinite if the parameter is 0)
Edit: made some small changes, updated the above to reflect