12 #define BUFFERLEN 2048
15 #define MAX_DIGS (BUFFERLEN \
27 #define CHAR_COMMA ','
28 #define CHAR_TOOBIG '%'
30 #define CHAR_MINUS '-'
32 #define CHAR_DOLLAR '$'
33 #define CHAR_SPACE ' '
35 #define CHAR_EXP_SINGLE 'E'
37 #define CHAR_EXP_DOUBLE 'D'
40 #define SNG_AUTODIGS 7
41 #define DBL_AUTODIGS 15
42 #define DBL_MAXDIGS 16
44 #define CHARS_NAN ('#' << 24 | 'N' << 16 | 'A' << 8 | 'N')
45 #define CHARS_INF ('#' << 24 | 'I' << 16 | 'N' << 8 | 'F')
46 #define CHARS_IND ('#' << 24 | 'I' << 16 | 'N' << 8 | 'D')
47 #define CHARS_TRUNC ('$' << 24 | '0' << 16 | '0' << 8 | '0')
49 #define ADD_CHAR( c ) \
51 DBG_ASSERT( p >= buffer ); \
54 else if( p == buffer ) \
67 union{
double d;
unsigned long long ll; } dtoll;
106 #define VAL_ISNEG 0x1
107 #define VAL_ISINF 0x2
108 #define VAL_ISIND 0x4
109 #define VAL_ISNAN 0x8
111 #define VAL_ISFLOAT 0x10
112 #define VAL_ISSNG 0x20
158 unsigned long long ret = 1, a = 10;
161 if( n & 1 ) ret *= a;
173 unsigned long long a64;
176 while( a64 >= (
int)1.E+8 )
182 if( a32 >= (
int)1.E+4 ) ret += 4;
else a32 *= (int)1.E+4;
183 if( a32 >= (
int)1.E+6 ) ret += 2;
else a32 *= (int)1.E+2;
184 if( a32 >= (
int)1.E+7 ) ret += 1;
201 unsigned long long b, ret;
205 if( n > 19 )
return 0;
210 if( (a % b) >= (b + 1) / 2 )
220 int c, nc, nnc, len, doexit;
231 nc = ( ctx->
chars > 1? ctx->
ptr[1] : -1 );
232 nnc = ( ctx->
chars > 2? ctx->
ptr[2] : -1 );
254 ((nc ==
'$') && (nnc ==
'$')) ||
255 ((nc ==
'*') && (nnc ==
'*')) ||
256 ((nc ==
'.') && (nnc ==
'#')) )
289 buffer[len++] = (char)c;
309 int c, nc, strchars, doexit, i;
314 if( ctx->
chars == 0 )
328 while( ctx->
chars > 0 )
331 nc = ( ctx->
chars > 1? ctx->
ptr[1] : -1 );
338 buffer[0] = s->
data[0];
357 if( (strchars != -1) || (nc ==
' ') || (nc ==
'\\') )
368 for( i = 0; i < strchars; i++ )
374 memcpy( buffer, s->
data, strchars );
375 buffer[strchars] =
'\0';
379 for( i = 0; i < strchars; i++ )
380 if( buffer[i] ==
'\0' )
435 int c, nc, strchars, doexit, i, length;
440 if( ctx->
chars == 0 ) {
454 while( ctx->
chars > 0 ) {
456 nc = ctx->
chars > 1 ? ctx->
ptr[1] : -1;
481 if( (strchars != -1) || (nc ==
' ') || (nc ==
'\\') ) {
485 if( length < strchars ) {
489 for( i = 0; i < strchars; i++ )
497 for( i = 0; i < strchars; i++ )
498 if( buffer[i] ==
'\0' )
513 if( strchars > -1 ) {
543 unsigned long long val,
int val_exp,
int flags,
549 int val_digs, val_zdigs;
550 unsigned long long val0;
551 int val_digs0, val_exp0;
552 int val_isneg, val_isfloat, val_issng;
557 int doexit, padchar, intdigs, decdigs, expdigs;
558 int adddollar, addcommas, signatend, signatstart, plussign, toobig;
559 int intdigs2, expsignchar, totdigs, decpoint;
567 if( ctx->
chars == 0 )
596 while( ctx->
chars > 0 )
599 if( signatend || isamp )
606 nc = ( ctx->
chars > 1? *(ctx->
ptr+1): -1 );
617 else if( decdigs != -1 )
629 if( decdigs != -1 || expdigs != 0 )
641 if( (intdigs == 0 && decdigs == -1) )
647 else if( intdigs == 1 && lc ==
'*' )
664 else if( intdigs == 0 && decdigs == -1 )
685 if( decdigs != -1 || expdigs != 0 )
707 else if( intdigs == 0 && decdigs == -1 )
750 if( intdigs == 0 && decdigs == -1 && !signatstart )
779 val_isneg = ( (flags &
VAL_ISNEG) != 0 );
781 val_issng = ( (flags &
VAL_ISSNG) != 0 );
845 while( val_digs > 1 && (val % 10) == 0 )
854 if( val_digs + val_exp > 0 )
856 intdigs = val_digs + val_exp;
870 if( intdigs > 16 || (val_issng && intdigs > 7) ||
874 decdigs = val_digs - 1;
876 expdigs = 2 +
hNumDigits( abs(val_digs + val_exp - 1) );
889 if( intdigs + 1 + decdigs >
MAX_DIGS )
891 decdigs -= ((intdigs + 1 + decdigs) -
MAX_DIGS);
894 intdigs -= (-1 - decdigs);
926 else if( val_isneg && !signatstart )
936 for( ; expdigs > 0; --expdigs )
943 val_digs0 = val_digs;
947 if( val_exp < -decdigs )
949 val_exp += (-decdigs - val_exp0);
950 val_digs -= (-decdigs - val_exp0);
967 intdigs2 = val_digs + val_exp;
968 if( intdigs2 < 0 ) intdigs2 = 0;
971 intdigs2 += (intdigs2 - 1) / 3;
985 val_digs = val_digs0;
993 if( intdigs2 > intdigs )
999 if( val_exp > -decdigs)
1001 val_zdigs = val_exp - -decdigs;
1013 if( intdigs == -1 || (intdigs == 0 && decdigs == 0) )
1023 totdigs = intdigs + decdigs;
1029 if( !isamp && !val_isneg && !(signatstart || signatend) )
1031 if( intdigs >= 1 && totdigs > 1 )
1040 val_zdigs = decdigs;
1042 else if( val_digs < totdigs )
1045 val_zdigs = totdigs - val_digs ;
1046 val_exp -= val_zdigs;
1048 else if( val_digs > totdigs )
1051 val_exp += (val_digs - totdigs);
1081 for( ; expdigs > 3; --expdigs )
1094 }
while( val_exp > 9 );
1115 if( chars != 0 && val_digs < 5 )
1136 for( ; decdigs > 0; --decdigs )
1143 else if( val_digs > 0 )
1171 if( addcommas && (i & 3) == 3 && val_digs > 0 )
1175 else if( val_zdigs > 0 )
1180 else if( val_digs > 0 )
1197 if( i == 0 && intdigs > 0 )
1239 for( ; intdigs > 0; --intdigs )
1245 for( ; toobig > 0; --toobig )
1286 long double val_ld = value;
1287 unsigned long long val_ull;
1292 val_exp = 1 + (int)floor( log10( val_ld ) - 0.5 );
1296 val_ld /= pow( (
long double)10.0, val_exp );
1297 if( val_ld >= (
long double)1.E+16 )
1299 val_ld /= (
long double)10.0;
1304 val_ull = (
unsigned long long)(val_ld + 0.5);
1305 DBG_ASSERT( val_ull >= (
unsigned long long)1.E+15 || val_ull == 0 );
1306 DBG_ASSERT( val_ull <= (
unsigned long long)1.E+16 );
1308 *pval_exp = val_exp;
1314 unsigned long long val_ull;
1319 pow2 = (val_ull >> 52) - 1023;
1320 val_ull &= (1ull << 52)-1;
1324 val_ull |= (1ull << 52);
1338 if( val_ull < (1ull << 63) )
1347 val_ull = (val_ull - 3) / 5 + 1;
1357 if( val_ull <= 0x3333333333333333ull )
1365 val_ull = val_ull / 2 + (val_ull & (val_ull / 2) & 1);
1391 unsigned long long val_ull = 1;
1405 value = fabs( value );
1412 else if(
hIsInd( value ) )
1414 else if(
hIsNan( value ) )
1420 return hPrintNumber( fnum, val_ull, val_exp, flags, mask );
1427 unsigned long long val_ull = 1;
1441 value_f = fabs( value_f );
1448 else if(
hIsInd( value_f ) )
1450 else if(
hIsNan( value_f ) )
1456 return hPrintNumber( fnum, val_ull, val_exp, flags, mask );
1467 unsigned long long val_ull;