123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056 |
- #ifndef RR_SPRINTF_H_INCLUDE
- #define RR_SPRINTF_H_INCLUDE
- #ifdef RR_SPRINTF_STATIC
- #define RRPUBLIC_DEC static
- #define RRPUBLIC_DEF static
- #else
- #ifdef __cplusplus
- #define RRPUBLIC_DEC extern "C"
- #define RRPUBLIC_DEF extern "C"
- #else
- #define RRPUBLIC_DEC extern
- #define RRPUBLIC_DEF
- #endif
- #endif
- #include <stdarg.h> // for va_list()
- #ifndef RR_SPRINTF_MIN
- #define RR_SPRINTF_MIN 512 // how many characters per callback
- #endif
- typedef char * RRSPRINTFCB( char * buf, void * user, int len );
- #ifndef RR_SPRINTF_DECORATE
- #define RR_SPRINTF_DECORATE(name) rr##name // define this before including if you want to change the names
- #endif
- #ifndef RR_SPRINTF_IMPLEMENTATION
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va );
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va );
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( sprintf ) ( char * buf, char const * fmt, ... );
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... );
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintfcb )( RRSPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va );
- RRPUBLIC_DEF void RR_SPRINTF_DECORATE( setseparators )( char comma, char period );
- #else
- #include <stdlib.h> // for va_arg()
- #define rU32 unsigned int
- #define rS32 signed int
- #ifdef _MSC_VER
- #define rU64 unsigned __int64
- #define rS64 signed __int64
- #else
- #define rU64 unsigned long long
- #define rS64 signed long long
- #endif
- #define rU16 unsigned short
- #ifndef rUINTa
- #if defined(__ppc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)
- #define rUINTa rU64
- #else
- #define rUINTa rU32
- #endif
- #endif
- #ifndef RR_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)
- #if defined(_MSC_VER) && (_MSC_VER<1900)
- #define RR_SPRINTF_MSVC_MODE
- #endif
- #endif
- #ifdef RR_SPRINTF_NOUNALIGNED // define this before inclusion to force rrsprint to always use aligned accesses
- #define RR_UNALIGNED(code)
- #else
- #define RR_UNALIGNED(code) code
- #endif
- #ifndef RR_SPRINTF_NOFLOAT
- static rS32 rrreal_to_str( char const * * start, rU32 * len, char *out, rS32 * decimal_pos, double value, rU32 frac_digits );
- static rS32 rrreal_to_parts( rS64 * bits, rS32 * expo, double value );
- #define RRSPECIAL 0x7000
- #endif
- static char RRperiod='.';
- static char RRcomma=',';
- static char rrdiglookup[201]="00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
- RRPUBLIC_DEF void RR_SPRINTF_DECORATE( setseparators )( char pcomma, char pperiod )
- {
- RRperiod=pperiod;
- RRcomma=pcomma;
- }
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintfcb )( RRSPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )
- {
- static char hex[]="0123456789abcdefxp";
- static char hexu[]="0123456789ABCDEFXP";
- char * bf;
- char const * f;
- int tlen = 0;
- bf = buf;
- f = fmt;
- for(;;)
- {
- rS32 fw,pr,tz; rU32 fl;
- #define LJ 1
- #define LP 2
- #define LS 4
- #define LX 8
- #define LZ 16
- #define BI 32
- #define CS 64
- #define NG 128
- #define KI 256
- #define HW 512
-
-
- #define chk_cb_bufL(bytes) { int len = (int)(bf-buf); if ((len+(bytes))>=RR_SPRINTF_MIN) { tlen+=len; if (0==(bf=buf=callback(buf,user,len))) goto done; } }
- #define chk_cb_buf(bytes) { if ( callback ) { chk_cb_bufL(bytes); } }
- #define flush_cb() { chk_cb_bufL(RR_SPRINTF_MIN-1); } //flush if there is even one byte in the buffer
- #define cb_buf_clamp(cl,v) cl = v; if ( callback ) { int lg = RR_SPRINTF_MIN-(int)(bf-buf); if (cl>lg) cl=lg; }
-
- for(;;)
- {
- while (((rUINTa)f)&3)
- {
- schk1: if (f[0]=='%') goto scandd;
- schk2: if (f[0]==0) goto endfmt;
- chk_cb_buf(1); *bf++=f[0]; ++f;
- }
- for(;;)
- {
- rU32 v,c;
- v=*(rU32*)f; c=(~v)&0x80808080;
- if ((v-0x26262626)&c) goto schk1;
- if ((v-0x01010101)&c) goto schk2;
- if (callback) if ((RR_SPRINTF_MIN-(int)(bf-buf))<4) goto schk1;
- *(rU32*)bf=v; bf+=4; f+=4;
- }
- } scandd:
- ++f;
-
- fw = 0; pr = -1; fl = 0; tz = 0;
-
-
- for(;;)
- {
- switch(f[0])
- {
-
- case '-': fl|=LJ; ++f; continue;
-
- case '+': fl|=LP; ++f; continue;
-
- case ' ': fl|=LS; ++f; continue;
-
- case '#': fl|=LX; ++f; continue;
-
- case '\'': fl|=CS; ++f; continue;
-
- case '$': fl|=KI; ++f; continue;
-
- case '0': fl|=LZ; ++f; goto flags_done;
- default: goto flags_done;
- }
- }
- flags_done:
-
-
- if ( f[0] == '*' ) {fw = va_arg(va,rU32); ++f;} else { while (( f[0] >= '0' ) && ( f[0] <= '9' )) { fw = fw * 10 + f[0] - '0'; f++; } }
-
- if ( f[0]=='.' ) { ++f; if ( f[0] == '*' ) {pr = va_arg(va,rU32); ++f;} else { pr = 0; while (( f[0] >= '0' ) && ( f[0] <= '9' )) { pr = pr * 10 + f[0] - '0'; f++; } } }
-
-
- switch(f[0])
- {
-
- case 'h': fl|=HW; ++f; break;
-
- case 'l': ++f; if ( f[0]=='l') { fl|=BI; ++f; } break;
-
- case 'j': fl|=BI; ++f; break;
-
- case 'z': case 't': fl|=((sizeof(char*)==8)?BI:0); ++f; break;
-
- case 'I': if ( ( f[1]=='6') && ( f[2]=='4') ) { fl|=BI; f+=3; } else if ( ( f[1]=='3') && ( f[2]=='2') ) { f+=3; } else { fl|=((sizeof(void*)==8)?BI:0); ++f; } break;
- default: break;
- }
-
- switch( f[0] )
- {
- #define NUMSZ 512 // big enough for e308 (with commas) or e-307
- char num[NUMSZ];
- char lead[8];
- char tail[8];
- char *s;
- char const *h;
- rU32 l,n,cs;
- rU64 n64;
- #ifndef RR_SPRINTF_NOFLOAT
- double fv;
- #endif
- rS32 dp; char const * sn;
- case 's':
-
- s = va_arg(va,char*); if (s==0) s = (char*)"null";
-
- sn = s;
- for(;;)
- {
- if ((((rUINTa)sn)&3)==0) break;
- lchk:
- if (sn[0]==0) goto ld;
- ++sn;
- }
- n = 0xffffffff;
- if (pr>=0) { n=(rU32)(sn-s); if (n>=(rU32)pr) goto ld; n=((rU32)(pr-n))>>2; }
- while(n)
- {
- rU32 v=*(rU32*)sn;
- if ((v-0x01010101)&(~v)&0x80808080UL) goto lchk;
- sn+=4;
- --n;
- }
- goto lchk;
- ld:
- l = (rU32) ( sn - s );
-
- if ( l > (rU32)pr ) l = pr;
- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
-
- goto scopy;
- case 'c':
-
- s = num + NUMSZ -1; *s = (char)va_arg(va,int);
- l = 1;
- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
- goto scopy;
- case 'n':
- { int * d = va_arg(va,int*);
- *d = tlen + (int)( bf - buf ); }
- break;
- #ifdef RR_SPRINTF_NOFLOAT
- case 'A':
- case 'a':
- case 'G':
- case 'g':
- case 'E':
- case 'e':
- case 'f':
- va_arg(va,double);
- s = (char*)"No float";
- l = 8;
- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
- goto scopy;
- #else
- case 'A':
- h=hexu;
- goto hexfloat;
- case 'a':
- h=hex;
- hexfloat:
- fv = va_arg(va,double);
- if (pr==-1) pr=6;
-
- if ( rrreal_to_parts( (rS64*)&n64, &dp, fv ) )
- fl |= NG;
-
- s = num+64;
-
- lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
- if (dp==-1023) dp=(n64)?-1022:0; else n64|=(((rU64)1)<<52);
- n64<<=(64-56);
- if (pr<15) n64+=((((rU64)8)<<56)>>(pr*4));
-
-
- #ifdef RR_SPRINTF_MSVC_MODE
- *s++='0';*s++='x';
- #else
- lead[1+lead[0]]='0'; lead[2+lead[0]]='x'; lead[0]+=2;
- #endif
- *s++=h[(n64>>60)&15]; n64<<=4;
- if ( pr ) *s++=RRperiod;
- sn = s;
-
- n = pr; if (n>13) n = 13; if (pr>(rS32)n) tz=pr-n; pr = 0;
- while(n--) { *s++=h[(n64>>60)&15]; n64<<=4; }
-
- tail[1]=h[17];
- if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';
- n = (dp>=1000)?6:((dp>=100)?5:((dp>=10)?4:3));
- tail[0]=(char)n;
- for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }
- dp = (int)(s-sn);
- l = (int)(s-(num+64));
- s = num+64;
- cs = 1 + (3<<24);
- goto scopy;
- case 'G':
- h=hexu;
- goto dosmallfloat;
- case 'g':
- h=hex;
- dosmallfloat:
- fv = va_arg(va,double);
- if (pr==-1) pr=6; else if (pr==0) pr = 1;
-
- if ( rrreal_to_str( &sn, &l, num, &dp, fv, (pr-1)|0x80000000 ) )
- fl |= NG;
-
- n = pr;
- if ( l > (rU32)pr ) l = pr; while ((l>1)&&(pr)&&(sn[l-1]=='0')) { --pr; --l; }
-
- if ((dp<=-4)||(dp>(rS32)n))
- {
- if ( pr > (rS32)l ) pr = l-1; else if ( pr ) --pr;
- goto doexpfromg;
- }
-
- if(dp>0) { pr=(dp<(rS32)l)?l-dp:0; } else { pr = -dp+((pr>(rS32)l)?l:pr); }
- goto dofloatfromg;
- case 'E':
- h=hexu;
- goto doexp;
- case 'e':
- h=hex;
- doexp:
- fv = va_arg(va,double);
- if (pr==-1) pr=6;
-
- if ( rrreal_to_str( &sn, &l, num, &dp, fv, pr|0x80000000 ) )
- fl |= NG;
- doexpfromg:
- tail[0]=0;
- lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
- if ( dp == RRSPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }
- s=num+64;
-
- *s++=sn[0];
- if (pr) *s++=RRperiod;
-
- if ((l-1)>(rU32)pr) l=pr+1;
- for(n=1;n<l;n++) *s++=sn[n];
-
- tz = pr-(l-1); pr=0;
-
- tail[1]=h[0xe];
- dp -= 1;
- if (dp<0) { tail[2]='-'; dp=-dp;} else tail[2]='+';
- #ifdef RR_SPRINTF_MSVC_MODE
- n = 5;
- #else
- n = (dp>=100)?5:4;
- #endif
- tail[0]=(char)n;
- for(;;) { tail[n]='0'+dp%10; if (n<=3) break; --n; dp/=10; }
- cs = 1 + (3<<24);
- goto flt_lead;
- case 'f':
- fv = va_arg(va,double);
- doafloat:
-
- if (fl&KI) {while(fl<0x4000000) { if ((fv<1024.0) && (fv>-1024.0)) break; fv/=1024.0; fl+=0x1000000; }}
- if (pr==-1) pr=6;
-
- if ( rrreal_to_str( &sn, &l, num, &dp, fv, pr ) )
- fl |= NG;
- dofloatfromg:
- tail[0]=0;
-
- lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
- if ( dp == RRSPECIAL ) { s=(char*)sn; cs=0; pr=0; goto scopy; }
- s=num+64;
-
- if (dp<=0)
- {
- rS32 i;
-
- *s++='0'; if (pr) *s++=RRperiod;
- n=-dp; if((rS32)n>pr) n=pr; i=n; while(i) { if ((((rUINTa)s)&3)==0) break; *s++='0'; --i; } while(i>=4) { *(rU32*)s=0x30303030; s+=4; i-=4; } while(i) { *s++='0'; --i; }
- if ((rS32)(l+n)>pr) l=pr-n; i=l; while(i) { *s++=*sn++; --i; }
- tz = pr-(n+l);
- cs = 1 + (3<<24);
- }
- else
- {
- cs = (fl&CS)?((600-(rU32)dp)%3):0;
- if ((rU32)dp>=l)
- {
-
- n=0; for(;;) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++=sn[n]; ++n; if (n>=l) break; } }
- if (n<(rU32)dp)
- {
- n = dp - n;
- if ((fl&CS)==0) { while(n) { if ((((rUINTa)s)&3)==0) break; *s++='0'; --n; } while(n>=4) { *(rU32*)s=0x30303030; s+=4; n-=4; } }
- while(n) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++='0'; --n; } }
- }
- cs = (int)(s-(num+64)) + (3<<24);
- if (pr) { *s++=RRperiod; tz=pr;}
- }
- else
- {
-
- n=0; for(;;) { if ((fl&CS) && (++cs==4)) { cs = 0; *s++=RRcomma; } else { *s++=sn[n]; ++n; if (n>=(rU32)dp) break; } }
- cs = (int)(s-(num+64)) + (3<<24);
- if (pr) *s++=RRperiod;
- if ((l-dp)>(rU32)pr) l=pr+dp;
- while(n<l) { *s++=sn[n]; ++n; }
- tz = pr-(l-dp);
- }
- }
- pr = 0;
-
-
- if (fl&KI) { tail[0]=1; tail[1]=' '; { if (fl>>24) { tail[2]="_kmgt"[fl>>24]; tail[0]=2; } } };
- flt_lead:
-
- l = (rU32) ( s-(num+64) );
- s=num+64;
- goto scopy;
- #endif
- case 'B':
- h = hexu;
- goto binary;
- case 'b':
- h = hex;
- binary:
- lead[0]=0;
- if (fl&LX) { lead[0]=2;lead[1]='0';lead[2]=h[0xb]; }
- l=(8<<4)|(1<<8);
- goto radixnum;
- case 'o':
- h = hexu;
- lead[0]=0;
- if (fl&LX) { lead[0]=1;lead[1]='0'; }
- l=(3<<4)|(3<<8);
- goto radixnum;
- case 'p':
- fl |= (sizeof(void*)==8)?BI:0;
- pr = sizeof(void*)*2;
- fl &= ~LZ;
-
-
- case 'X':
- h = hexu;
- goto dohexb;
- case 'x':
- h = hex; dohexb:
- l=(4<<4)|(4<<8);
- lead[0]=0;
- if (fl&LX) { lead[0]=2;lead[1]='0';lead[2]=h[16]; }
- radixnum:
-
- if ( fl&BI )
- n64 = va_arg(va,rU64);
- else
- n64 = va_arg(va,rU32);
- s = num + NUMSZ; dp = 0;
-
- tail[0]=0; if (n64==0) { lead[0]=0; if (pr==0) { l=0; cs = ( ((l>>4)&15)) << 24; goto scopy; } }
-
- for(;;) { *--s = h[n64&((1<<(l>>8))-1)]; n64>>=(l>>8); if ( ! ( (n64) || ((rS32) ( (num+NUMSZ) - s ) < pr ) ) ) break; if ( fl&CS) { ++l; if ((l&15)==((l>>4)&15)) { l&=~15; *--s=RRcomma; } } };
-
- cs = (rU32) ( (num+NUMSZ) - s ) + ( ( ((l>>4)&15)) << 24 );
-
- l = (rU32) ( (num+NUMSZ) - s );
-
- goto scopy;
- case 'u':
- case 'i':
- case 'd':
-
- if ( fl&BI )
- {
- rS64 i64 = va_arg(va,rS64); n64 = (rU64)i64; if ((f[0]!='u') && (i64<0)) { n64=(rU64)-i64; fl|=NG; }
- }
- else
- {
- rS32 i = va_arg(va,rS32); n64 = (rU32)i; if ((f[0]!='u') && (i<0)) { n64=(rU32)-i; fl|=NG; }
- }
- #ifndef RR_SPRINTF_NOFLOAT
- if (fl&KI) { if (n64<1024) pr=0; else if (pr==-1) pr=1; fv=(double)(rS64)n64; goto doafloat; }
- #endif
-
- s = num+NUMSZ; l=0;
-
- for(;;)
- {
-
- char * o=s-8;
- if (n64>=100000000) { n = (rU32)( n64 % 100000000); n64 /= 100000000; } else {n = (rU32)n64; n64 = 0; }
- if((fl&CS)==0) { while(n) { s-=2; *(rU16*)s=*(rU16*)&rrdiglookup[(n%100)*2]; n/=100; } }
- while (n) { if ( ( fl&CS) && (l++==3) ) { l=0; *--s=RRcomma; --o; } else { *--s=(char)(n%10)+'0'; n/=10; } }
- if (n64==0) { if ((s[0]=='0') && (s!=(num+NUMSZ))) ++s; break; }
- while (s!=o) if ( ( fl&CS) && (l++==3) ) { l=0; *--s=RRcomma; --o; } else { *--s='0'; }
- }
- tail[0]=0;
-
- lead[0]=0; if (fl&NG) { lead[0]=1; lead[1]='-'; } else if (fl&LS) { lead[0]=1; lead[1]=' '; } else if (fl&LP) { lead[0]=1; lead[1]='+'; };
-
- l = (rU32) ( (num+NUMSZ) - s ); if ( l == 0 ) { *--s='0'; l = 1; }
- cs = l + (3<<24);
- if (pr<0) pr = 0;
- scopy:
-
- if (pr<(rS32)l) pr = l;
- n = pr + lead[0] + tail[0] + tz;
- if (fw<(rS32)n) fw = n;
- fw -= n;
- pr -= l;
-
- if ( (fl&LJ)==0 )
- {
- if (fl&LZ)
- {
- pr = (fw>pr)?fw:pr;
- fw = 0;
- }
- else
- {
- fl &= ~CS;
- }
- }
-
- if (fw+pr)
- {
- rS32 i; rU32 c;
-
- if ( (fl&LJ)==0 ) while(fw>0) { cb_buf_clamp(i,fw); fw -= i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(rU32*)bf=0x20202020; bf+=4; i-=4; } while (i) {*bf++=' '; --i;} chk_cb_buf(1); }
-
-
- sn=lead+1; while(lead[0]) { cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
-
-
- c = cs >> 24; cs &= 0xffffff;
- cs = (fl&CS)?((rU32)(c-((pr+cs)%(c+1)))):0;
- while(pr>0) { cb_buf_clamp(i,pr); pr -= i; if((fl&CS)==0) { while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(rU32*)bf=0x30303030; bf+=4; i-=4; } } while (i) { if((fl&CS) && (cs++==c)) { cs = 0; *bf++=RRcomma; } else *bf++='0'; --i; } chk_cb_buf(1); }
- }
-
- sn=lead+1; while(lead[0]) { rS32 i; cb_buf_clamp(i,lead[0]); lead[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
-
- n = l; while (n) { rS32 i; cb_buf_clamp(i,n); n-=i; RR_UNALIGNED( while(i>=4) { *(rU32*)bf=*(rU32*)s; bf+=4; s+=4; i-=4; } ) while (i) {*bf++=*s++; --i;} chk_cb_buf(1); }
-
- while(tz) { rS32 i; cb_buf_clamp(i,tz); tz -= i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++='0'; --i; } while(i>=4) { *(rU32*)bf=0x30303030; bf+=4; i-=4; } while (i) {*bf++='0'; --i;} chk_cb_buf(1); }
-
- sn=tail+1; while(tail[0]) { rS32 i; cb_buf_clamp(i,tail[0]); tail[0] -= (char)i; while (i) {*bf++=*sn++; --i;} chk_cb_buf(1); }
-
- if (fl&LJ) if (fw>0) { while (fw) { rS32 i; cb_buf_clamp(i,fw); fw-=i; while(i) { if ((((rUINTa)bf)&3)==0) break; *bf++=' '; --i; } while(i>=4) { *(rU32*)bf=0x20202020; bf+=4; i-=4; } while (i--) *bf++=' '; chk_cb_buf(1); } }
- break;
- default:
- s = num + NUMSZ -1; *s = f[0];
- l = 1;
- fw=pr=fl=0;
- lead[0]=0; tail[0]=0; pr = 0; dp = 0; cs = 0;
- goto scopy;
- }
- ++f;
- }
- endfmt:
- if (!callback)
- *bf = 0;
- else
- flush_cb();
-
- done:
- return tlen + (int)(bf-buf);
- }
- #undef LJ
- #undef LP
- #undef LS
- #undef LX
- #undef LZ
- #undef BI
- #undef CS
- #undef NG
- #undef KI
- #undef NUMSZ
- #undef chk_cb_bufL
- #undef chk_cb_buf
- #undef flush_cb
- #undef cb_buf_clamp
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( sprintf )( char * buf, char const * fmt, ... )
- {
- va_list va;
- va_start( va, fmt );
- return RR_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );
- }
- typedef struct RRCCS
- {
- char * buf;
- int count;
- char tmp[ RR_SPRINTF_MIN ];
- } RRCCS;
- static char * rrclampcallback( char * buf, void * user, int len )
- {
- RRCCS * c = (RRCCS*)user;
- if ( len > c->count ) len = c->count;
- if (len)
- {
- if ( buf != c->buf )
- {
- char * s, * d, * se;
- d = c->buf; s = buf; se = buf+len;
- do{ *d++ = *s++; } while (s<se);
- }
- c->buf += len;
- c->count -= len;
- }
-
- if ( c->count <= 0 ) return 0;
- return ( c->count >= RR_SPRINTF_MIN ) ? c->buf : c->tmp;
- }
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )
- {
- RRCCS c;
- int l;
- if ( count == 0 )
- return 0;
- c.buf = buf;
- c.count = count;
- RR_SPRINTF_DECORATE( vsprintfcb )( rrclampcallback, &c, rrclampcallback(0,&c,0), fmt, va );
-
-
- l = (int)( c.buf - buf );
- if ( l >= count )
- l = count - 1;
- buf[l] = 0;
- return l;
- }
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( snprintf )( char * buf, int count, char const * fmt, ... )
- {
- va_list va;
- va_start( va, fmt );
- return RR_SPRINTF_DECORATE( vsnprintf )( buf, count, fmt, va );
- }
- RRPUBLIC_DEF int RR_SPRINTF_DECORATE( vsprintf )( char * buf, char const * fmt, va_list va )
- {
- return RR_SPRINTF_DECORATE( vsprintfcb )( 0, 0, buf, fmt, va );
- }
- #ifndef RR_SPRINTF_NOFLOAT
-
- #define RRCOPYFP(dest,src) { int cn; for(cn=0;cn<8;cn++) ((char*)&dest)[cn]=((char*)&src)[cn]; }
-
- static rS32 rrreal_to_parts( rS64 * bits, rS32 * expo, double value )
- {
- double d;
- rS64 b = 0;
-
- d = value;
- RRCOPYFP( b, d );
- *bits = b & ((((rU64)1)<<52)-1);
- *expo = ((b >> 52) & 2047)-1023;
-
- return (rS32)(b >> 63);
- }
- static double const rrbot[23]={1e+000,1e+001,1e+002,1e+003,1e+004,1e+005,1e+006,1e+007,1e+008,1e+009,1e+010,1e+011,1e+012,1e+013,1e+014,1e+015,1e+016,1e+017,1e+018,1e+019,1e+020,1e+021,1e+022};
- static double const rrnegbot[22]={1e-001,1e-002,1e-003,1e-004,1e-005,1e-006,1e-007,1e-008,1e-009,1e-010,1e-011,1e-012,1e-013,1e-014,1e-015,1e-016,1e-017,1e-018,1e-019,1e-020,1e-021,1e-022};
- static double const rrnegboterr[22]={-5.551115123125783e-018,-2.0816681711721684e-019,-2.0816681711721686e-020,-4.7921736023859299e-021,-8.1803053914031305e-022,4.5251888174113741e-023,4.5251888174113739e-024,-2.0922560830128471e-025,-6.2281591457779853e-026,-3.6432197315497743e-027,6.0503030718060191e-028,2.0113352370744385e-029,-3.0373745563400371e-030,1.1806906454401013e-032,-7.7705399876661076e-032,2.0902213275965398e-033,-7.1542424054621921e-034,-7.1542424054621926e-035,2.4754073164739869e-036,5.4846728545790429e-037,9.2462547772103625e-038,-4.8596774326570872e-039};
- static double const rrtop[13]={1e+023,1e+046,1e+069,1e+092,1e+115,1e+138,1e+161,1e+184,1e+207,1e+230,1e+253,1e+276,1e+299};
- static double const rrnegtop[13]={1e-023,1e-046,1e-069,1e-092,1e-115,1e-138,1e-161,1e-184,1e-207,1e-230,1e-253,1e-276,1e-299};
- static double const rrtoperr[13]={8388608,6.8601809640529717e+028,-7.253143638152921e+052,-4.3377296974619174e+075,-1.5559416129466825e+098,-3.2841562489204913e+121,-3.7745893248228135e+144,-1.7356668416969134e+167,-3.8893577551088374e+190,-9.9566444326005119e+213,6.3641293062232429e+236,-5.2069140800249813e+259,-5.2504760255204387e+282};
- static double const rrnegtoperr[13]={3.9565301985100693e-040,-2.299904345391321e-063,3.6506201437945798e-086,1.1875228833981544e-109,-5.0644902316928607e-132,-6.7156837247865426e-155,-2.812077463003139e-178,-5.7778912386589953e-201,7.4997100559334532e-224,-4.6439668915134491e-247,-6.3691100762962136e-270,-9.436808465446358e-293,8.0970921678014997e-317};
- #if defined(_MSC_VER) && (_MSC_VER<=1200)
- static rU64 const rrpot[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000,100000000000, 1000000000000,10000000000000,100000000000000,1000000000000000, 10000000000000000,100000000000000000,1000000000000000000,10000000000000000000U };
- #define rrtento19th ((rU64)1000000000000000000)
- #else
- static rU64 const rrpot[20]={1,10,100,1000, 10000,100000,1000000,10000000, 100000000,1000000000,10000000000ULL,100000000000ULL, 1000000000000ULL,10000000000000ULL,100000000000000ULL,1000000000000000ULL, 10000000000000000ULL,100000000000000000ULL,1000000000000000000ULL,10000000000000000000ULL };
- #define rrtento19th (1000000000000000000ULL)
- #endif
- #define rrddmulthi(oh,ol,xh,yh) \
- { \
- double ahi=0,alo,bhi=0,blo; \
- rS64 bt; \
- oh = xh * yh; \
- RRCOPYFP(bt,xh); bt&=((~(rU64)0)<<27); RRCOPYFP(ahi,bt); alo = xh-ahi; \
- RRCOPYFP(bt,yh); bt&=((~(rU64)0)<<27); RRCOPYFP(bhi,bt); blo = yh-bhi; \
- ol = ((ahi*bhi-oh)+ahi*blo+alo*bhi)+alo*blo; \
- }
- #define rrddtoS64(ob,xh,xl) \
- { \
- double ahi=0,alo,vh,t;\
- ob = (rS64)ph;\
- vh=(double)ob;\
- ahi = ( xh - vh );\
- t = ( ahi - xh );\
- alo = (xh-(ahi-t))-(vh+t);\
- ob += (rS64)(ahi+alo+xl);\
- }
- #define rrddrenorm(oh,ol) { double s; s=oh+ol; ol=ol-(s-oh); oh=s; }
- #define rrddmultlo(oh,ol,xh,xl,yh,yl) \
- ol = ol + ( xh*yl + xl*yh ); \
- #define rrddmultlos(oh,ol,xh,yl) \
- ol = ol + ( xh*yl ); \
- static void rrraise_to_power10( double *ohi, double *olo, double d, rS32 power )
- {
- double ph, pl;
- if ((power>=0) && (power<=22))
- {
- rrddmulthi(ph,pl,d,rrbot[power]);
- }
- else
- {
- rS32 e,et,eb;
- double p2h,p2l;
- e=power; if (power<0) e=-e;
- et = (e*0x2c9)>>14; if (et>13) et=13; eb = e-(et*23);
- ph = d; pl = 0.0;
- if (power<0)
- {
- if (eb) { --eb; rrddmulthi(ph,pl,d,rrnegbot[eb]); rrddmultlos(ph,pl,d,rrnegboterr[eb]); }
- if (et)
- {
- rrddrenorm(ph,pl);
- --et; rrddmulthi(p2h,p2l,ph,rrnegtop[et]); rrddmultlo(p2h,p2l,ph,pl,rrnegtop[et],rrnegtoperr[et]); ph=p2h;pl=p2l;
- }
- }
- else
- {
- if (eb)
- {
- e = eb; if (eb>22) eb=22; e -= eb;
- rrddmulthi(ph,pl,d,rrbot[eb]);
- if ( e ) { rrddrenorm(ph,pl); rrddmulthi(p2h,p2l,ph,rrbot[e]); rrddmultlos(p2h,p2l,rrbot[e],pl); ph=p2h;pl=p2l; }
- }
- if (et)
- {
- rrddrenorm(ph,pl);
- --et; rrddmulthi(p2h,p2l,ph,rrtop[et]); rrddmultlo(p2h,p2l,ph,pl,rrtop[et],rrtoperr[et]); ph=p2h;pl=p2l;
- }
- }
- }
- rrddrenorm(ph,pl);
- *ohi = ph; *olo = pl;
- }
- static rS32 rrreal_to_str( char const * * start, rU32 * len, char *out, rS32 * decimal_pos, double value, rU32 frac_digits )
- {
- double d;
- rS64 bits = 0;
- rS32 expo, e, ng, tens;
- d = value;
- RRCOPYFP(bits,d);
- expo = (bits >> 52) & 2047;
- ng = (rS32)(bits >> 63);
- if (ng) d=-d;
- if ( expo == 2047 )
- {
- *start = (bits&((((rU64)1)<<52)-1)) ? "NaN" : "Inf";
- *decimal_pos = RRSPECIAL;
- *len = 3;
- return ng;
- }
- if ( expo == 0 )
- {
- if ((bits<<1)==0)
- {
- *decimal_pos = 1;
- *start = out;
- out[0] = '0'; *len = 1;
- return ng;
- }
-
- {
- rS64 v = ((rU64)1)<<51;
- while ((bits&v)==0) { --expo; v >>= 1; }
- }
- }
-
- {
- double ph,pl;
-
- tens=expo-1023; tens = (tens<0)?((tens*617)/2048):(((tens*1233)/4096)+1);
-
- rrraise_to_power10( &ph, &pl, d, 18-tens );
-
- rrddtoS64( bits, ph,pl );
-
- if ( ((rU64)bits) >= rrtento19th ) ++tens;
- }
-
- frac_digits = ( frac_digits & 0x80000000 ) ? ( (frac_digits&0x7ffffff) + 1 ) : ( tens + frac_digits );
- if ( ( frac_digits < 24 ) )
- {
- rU32 dg = 1; if ((rU64)bits >= rrpot[9] ) dg=10; while( (rU64)bits >= rrpot[dg] ) { ++dg; if (dg==20) goto noround; }
- if ( frac_digits < dg )
- {
- rU64 r;
-
- e = dg - frac_digits;
- if ( (rU32)e >= 24 ) goto noround;
- r = rrpot[e];
- bits = bits + (r/2);
- if ( (rU64)bits >= rrpot[dg] ) ++tens;
- bits /= r;
- }
- noround:;
- }
-
- if ( bits )
- {
- rU32 n; for(;;) { if ( bits<=0xffffffff ) break; if (bits%1000) goto donez; bits/=1000; } n = (rU32)bits; while ((n%1000)==0) n/=1000; bits=n; donez:;
- }
-
- out += 64;
- e = 0;
- for(;;)
- {
- rU32 n;
- char * o = out-8;
-
- if (bits>=100000000) { n = (rU32)( bits % 100000000); bits /= 100000000; } else {n = (rU32)bits; bits = 0; }
- while(n) { out-=2; *(rU16*)out=*(rU16*)&rrdiglookup[(n%100)*2]; n/=100; e+=2; }
- if (bits==0) { if ((e) && (out[0]=='0')) { ++out; --e; } break; }
- while( out!=o ) { *--out ='0'; ++e; }
- }
-
- *decimal_pos = tens;
- *start = out;
- *len = e;
- return ng;
- }
- #undef rrddmulthi
- #undef rrddrenorm
- #undef rrddmultlo
- #undef rrddmultlos
- #undef RRSPECIAL
- #undef RRCOPYFP
-
- #endif
- #undef rU16
- #undef rU32
- #undef rS32
- #undef rU64
- #undef rS64
- #undef RRPUBLIC_DEC
- #undef RRPUBLIC_DEF
- #undef RR_SPRINTF_DECORATE
- #undef RR_UNALIGNED
- #endif
- #endif
|