u3: refactors u3s_sift_ud() control flow

This commit is contained in:
Joe Bryan 2021-04-21 22:08:39 -07:00
parent c084dc8c66
commit 5730958d90

View File

@ -857,92 +857,90 @@ u3s_cue_atom(u3_atom a)
return u3s_cue_bytes((c3_d)len_w, byt_y);
}
#define NONZERO(a) ( ((a) >= '1') && ((a) <= '9') )
#define DIGIT(a) ( ((a) >= '0') && ((a) <= '9') )
#define NOT_DEC(a) ( ((a) < '0') || ((a) > '9') )
/* u3s_sift_ud_bytes: parse @ud
*/
u3_weak
u3s_sift_ud_bytes(c3_w len_w, c3_y* byt_y)
{
// +ape:ag: just 0 or
c3_s val_s;
if ( !len_w ) return u3_none;
// +ape:ag: just 0
//
if ( (1 == len_w) && ('0' == *byt_y) ) {
return (u3_noun)0;
if ( '0' == *byt_y ) {
return ( 1 == len_w ) ? (u3_noun)0 : u3_none;
}
// if ( (1 == len_w) && ('0' == *byt_y) ) return (u3_noun)val_s;
// +ted:ab: leading nonzero, 0-2 digits
//
if ( len_w && NONZERO(*byt_y) ) {
c3_s val_s = *byt_y++ - '0';
if ( NOT_DEC(*byt_y) ) return u3_none;
// 0 digits
val_s = *byt_y++ - '0';
if ( 0 == --len_w ) return (u3_noun)val_s;
// 1 digit
//
if ( '.' == *byt_y ) goto tid_ab;
if ( NOT_DEC(*byt_y) ) return u3_none;
val_s *= 10;
val_s += *byt_y++ - '0';
if ( 0 == --len_w ) return (u3_noun)val_s;
// 2 digits
//
if ( '.' == *byt_y ) goto tid_ab;
if ( NOT_DEC(*byt_y) ) return u3_none;
val_s *= 10;
val_s += *byt_y++ - '0';
if ( 0 == --len_w ) return (u3_noun)val_s;
tid_ab:
// +tid:ab: dot-prefixed 3-digit blocks
//
if ( len_w % 4 ) return u3_none;
{
// XX estimate size, allocate once
//
if ( 0 == --len_w ) return (u3_noun)val_s;
mpz_t a_mp;
mpz_init_set_ui(a_mp, val_s);
// 1 digit
//
if ( !DIGIT(*byt_y) ) goto fail;
val_s *= 10;
val_s += *byt_y++ - '0';
if ( 0 == --len_w ) return (u3_noun)val_s;
// 2 digits
//
if ( !DIGIT(*byt_y) ) goto fail;
val_s *= 10;
val_s += *byt_y++ - '0';
if ( 0 == --len_w ) return (u3_noun)val_s;
// +tid:ab: dot-prefixed 3-digit blocks
//
if ( 0 != (len_w % 4) ) goto fail;
{
// XX estimate size, allocate once
//
mpz_t a_mp;
mpz_init_set_ui(a_mp, val_s);
while ( len_w ) {
if ( ('.' != byt_y[0])
|| !DIGIT(byt_y[1])
|| !DIGIT(byt_y[2])
|| !DIGIT(byt_y[1]) )
{
mpz_clear(a_mp);
goto fail;
}
byt_y++;
val_s = *byt_y++ - '0';
val_s *= 10;
val_s += *byt_y++ - '0';
val_s *= 10;
val_s += *byt_y++ - '0';
mpz_mul_ui(a_mp, a_mp, 1000);
mpz_add_ui(a_mp, a_mp, val_s);
len_w -= 4;
while ( len_w ) {
if ( ('.' != byt_y[0])
|| NOT_DEC(byt_y[1])
|| NOT_DEC(byt_y[2])
|| NOT_DEC(byt_y[1]) )
{
mpz_clear(a_mp);
return u3_none;
}
return u3i_mp(a_mp);
}
}
byt_y++;
fail:
return u3_none;
val_s = *byt_y++ - '0';
val_s *= 10;
val_s += *byt_y++ - '0';
val_s *= 10;
val_s += *byt_y++ - '0';
mpz_mul_ui(a_mp, a_mp, 1000);
mpz_add_ui(a_mp, a_mp, val_s);
len_w -= 4;
}
return u3i_mp(a_mp);
}
}
#undef DIGIT
#undef NONZERO
#undef NOT_DEC
/* u3s_sift_ud: parse @ud.
*/