Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 2 semanas
hace 2 semanas
hace 2 semanas
hace 2 semanas
hace 2 semanas
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. // NOTE(djledda): This library has been modified to support my string struct, inspired by the Digital Grove codebase by Ryan Fleury.
  2. // stb_sprintf - v1.10 - public domain snprintf() implementation
  3. // originally by Jeff Roberts / RAD Game Tools, 2015/10/20
  4. // http://github.com/nothings/stb
  5. //
  6. // allowed types: sc uidBboXx p AaGgEef n
  7. // lengths : hh h ll j z t I64 I32 I
  8. //
  9. // Contributors:
  10. // Fabian "ryg" Giesen (reformatting)
  11. // github:aganm (attribute format)
  12. //
  13. // Contributors (bugfixes):
  14. // github:d26435
  15. // github:trex78
  16. // github:account-login
  17. // Jari Komppa (SI suffixes)
  18. // Rohit Nirmal
  19. // Marcin Wojdyr
  20. // Leonard Ritter
  21. // Stefano Zanotti
  22. // Adam Allison
  23. // Arvid Gerstmann
  24. // Markus Kolb
  25. //
  26. // LICENSE:
  27. //
  28. // See end of file for license information.
  29. #ifndef STB_SPRINTF_H_INCLUDE
  30. #define STB_SPRINTF_H_INCLUDE
  31. /*
  32. Single file sprintf replacement.
  33. Originally written by Jeff Roberts at RAD Game Tools - 2015/10/20.
  34. Hereby placed in public domain.
  35. This is a full sprintf replacement that supports everything that
  36. the C runtime sprintfs support, including float/double, 64-bit integers,
  37. hex floats, field parameters (%*.*d stuff), length reads backs, etc.
  38. Why would you need this if sprintf already exists? Well, first off,
  39. it's *much* faster (see below). It's also much smaller than the CRT
  40. versions code-space-wise. We've also added some simple improvements
  41. that are super handy (commas in thousands, callbacks at buffer full,
  42. for example). Finally, the format strings for MSVC and GCC differ
  43. for 64-bit integers (among other small things), so this lets you use
  44. the same format strings in cross platform code.
  45. It uses the standard single file trick of being both the header file
  46. and the source itself. If you just include it normally, you just get
  47. the header file function definitions. To get the code, you include
  48. it from a C or C++ file and define STB_SPRINTF_IMPLEMENTATION first.
  49. It only uses va_args macros from the C runtime to do it's work. It
  50. does cast doubles to S64s and shifts and divides U64s, which does
  51. drag in CRT code on most platforms.
  52. It compiles to roughly 8K with float support, and 4K without.
  53. As a comparison, when using MSVC static libs, calling sprintf drags
  54. in 16K.
  55. API:
  56. ====
  57. int stbsp_sprintf( char * buf, char const * fmt, ... )
  58. int stbsp_snprintf( char * buf, int count, char const * fmt, ... )
  59. Convert an arg list into a buffer. stbsp_snprintf always returns
  60. a zero-terminated string (unlike regular snprintf).
  61. int stbsp_vsprintf( char * buf, char const * fmt, va_list va )
  62. int stbsp_vsnprintf( char * buf, int count, char const * fmt, va_list va )
  63. Convert a va_list arg list into a buffer. stbsp_vsnprintf always returns
  64. a zero-terminated string (unlike regular snprintf).
  65. int stbsp_vsprintfcb( STBSP_SPRINTFCB * callback, void * user, char * buf, char const * fmt, va_list va )
  66. typedef char * STBSP_SPRINTFCB( char const * buf, void * user, int len );
  67. Convert into a buffer, calling back every STB_SPRINTF_MIN chars.
  68. Your callback can then copy the chars out, print them or whatever.
  69. This function is actually the workhorse for everything else.
  70. The buffer you pass in must hold at least STB_SPRINTF_MIN characters.
  71. // you return the next buffer to use or 0 to stop converting
  72. void stbsp_set_separators( char comma, char period )
  73. Set the comma and period characters to use.
  74. FLOATS/DOUBLES:
  75. ===============
  76. This code uses a internal float->ascii conversion method that uses
  77. doubles with error correction (double-doubles, for ~105 bits of
  78. precision). This conversion is round-trip perfect - that is, an atof
  79. of the values output here will give you the bit-exact double back.
  80. One difference is that our insignificant digits will be different than
  81. with MSVC or GCC (but they don't match each other either). We also
  82. don't attempt to find the minimum length matching float (pre-MSVC15
  83. doesn't either).
  84. If you don't need float or doubles at all, define STB_SPRINTF_NOFLOAT
  85. and you'll save 4K of code space.
  86. 64-BIT INTS:
  87. ============
  88. This library also supports 64-bit integers and you can use MSVC style or
  89. GCC style indicators (%I64d or %lld). It supports the C99 specifiers
  90. for size_t and ptr_diff_t (%jd %zd) as well.
  91. EXTRAS:
  92. =======
  93. Like some GCCs, for integers and floats, you can use a ' (single quote)
  94. specifier and commas will be inserted on the thousands: "%'d" on 12345
  95. would print 12,345.
  96. For integers and floats, you can use a "$" specifier and the number
  97. will be converted to float and then divided to get kilo, mega, giga or
  98. tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is
  99. "2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn
  100. 2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three
  101. $:s: "%$$$d" -> "2.42 M". To remove the space between the number and the
  102. suffix, add "_" specifier: "%_$d" -> "2.53M".
  103. In addition to octal and hexadecimal conversions, you can print
  104. integers in binary: "%b" for 256 would print 100.
  105. PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
  106. ===================================================================
  107. "%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)
  108. "%24d" across all 32-bit ints (4.5x/4.2x faster)
  109. "%x" across all 32-bit ints (4.5x/3.8x faster)
  110. "%08x" across all 32-bit ints (4.3x/3.8x faster)
  111. "%f" across e-10 to e+10 floats (7.3x/6.0x faster)
  112. "%e" across e-10 to e+10 floats (8.1x/6.0x faster)
  113. "%g" across e-10 to e+10 floats (10.0x/7.1x faster)
  114. "%f" for values near e-300 (7.9x/6.5x faster)
  115. "%f" for values near e+300 (10.0x/9.1x faster)
  116. "%e" for values near e-300 (10.1x/7.0x faster)
  117. "%e" for values near e+300 (9.2x/6.0x faster)
  118. "%.320f" for values near e-300 (12.6x/11.2x faster)
  119. "%a" for random values (8.6x/4.3x faster)
  120. "%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)
  121. "%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)
  122. "%s%s%s" for 64 char strings (7.1x/7.3x faster)
  123. "...512 char string..." ( 35.0x/32.5x faster!)
  124. */
  125. #if defined(__clang__)
  126. #if defined(__has_feature) && defined(__has_attribute)
  127. #if __has_feature(address_sanitizer)
  128. #if __has_attribute(__no_sanitize__)
  129. #define STBSP__ASAN __attribute__((__no_sanitize__("address")))
  130. #elif __has_attribute(__no_sanitize_address__)
  131. #define STBSP__ASAN __attribute__((__no_sanitize_address__))
  132. #elif __has_attribute(__no_address_safety_analysis__)
  133. #define STBSP__ASAN __attribute__((__no_address_safety_analysis__))
  134. #endif
  135. #endif
  136. #endif
  137. #elif defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  138. #if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__
  139. #define STBSP__ASAN __attribute__((__no_sanitize_address__))
  140. #endif
  141. #endif
  142. #ifndef STBSP__ASAN
  143. #define STBSP__ASAN
  144. #endif
  145. #ifdef STB_SPRINTF_STATIC
  146. #define STBSP__PUBLICDEC static
  147. #define STBSP__PUBLICDEF static STBSP__ASAN
  148. #else
  149. #ifdef __cplusplus
  150. #define STBSP__PUBLICDEC extern "C"
  151. #define STBSP__PUBLICDEF extern "C" STBSP__ASAN
  152. #else
  153. #define STBSP__PUBLICDEC extern
  154. #define STBSP__PUBLICDEF STBSP__ASAN
  155. #endif
  156. #endif
  157. #if defined(__has_attribute)
  158. #if __has_attribute(format)
  159. #define STBSP__ATTRIBUTE_FORMAT(fmt,va) __attribute__((format(printf,fmt,va)))
  160. #endif
  161. #endif
  162. #ifndef STBSP__ATTRIBUTE_FORMAT
  163. #define STBSP__ATTRIBUTE_FORMAT(fmt,va)
  164. #endif
  165. #ifdef _MSC_VER
  166. #define STBSP__NOTUSED(v) (void)(v)
  167. #else
  168. #define STBSP__NOTUSED(v) (void)sizeof(v)
  169. #endif
  170. #include <stdarg.h> // for va_arg(), va_list()
  171. #include <stddef.h> // size_t, ptrdiff_t
  172. #ifndef STB_SPRINTF_MIN
  173. #define STB_SPRINTF_MIN 512 // how many characters per callback
  174. #endif
  175. typedef char *STBSP_SPRINTFCB(const char *buf, void *user, int len);
  176. #ifndef STB_SPRINTF_DECORATE
  177. #define STB_SPRINTF_DECORATE(name) stbsp_##name // define this before including if you want to change the names
  178. #endif
  179. STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va);
  180. STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va);
  181. STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(2,3);
  182. STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(3,4);
  183. STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va);
  184. STBSP__PUBLICDEC void STB_SPRINTF_DECORATE(set_separators)(char comma, char period);
  185. #endif // STB_SPRINTF_H_INCLUDE
  186. #ifdef STB_SPRINTF_IMPLEMENTATION
  187. #define stbsp__uint32 unsigned int
  188. #define stbsp__int32 signed int
  189. #ifdef _MSC_VER
  190. #define stbsp__uint64 unsigned __int64
  191. #define stbsp__int64 signed __int64
  192. #else
  193. #define stbsp__uint64 unsigned long long
  194. #define stbsp__int64 signed long long
  195. #endif
  196. #define stbsp__uint16 unsigned short
  197. #ifndef stbsp__uintptr
  198. #if defined(__ppc64__) || defined(__powerpc64__) || defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64) || defined(__s390x__)
  199. #define stbsp__uintptr stbsp__uint64
  200. #else
  201. #define stbsp__uintptr stbsp__uint32
  202. #endif
  203. #endif
  204. #ifndef STB_SPRINTF_MSVC_MODE // used for MSVC2013 and earlier (MSVC2015 matches GCC)
  205. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  206. #define STB_SPRINTF_MSVC_MODE
  207. #endif
  208. #endif
  209. #ifdef STB_SPRINTF_NOUNALIGNED // define this before inclusion to force stbsp_sprintf to always use aligned accesses
  210. #define STBSP__UNALIGNED(code)
  211. #else
  212. #define STBSP__UNALIGNED(code) code
  213. #endif
  214. #ifndef STB_SPRINTF_NOFLOAT
  215. // internal float utility functions
  216. static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits);
  217. static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value);
  218. #define STBSP__SPECIAL 0x7000
  219. #endif
  220. static char stbsp__period = '.';
  221. static char stbsp__comma = ',';
  222. static struct
  223. {
  224. short temp; // force next field to be 2-byte aligned
  225. char pair[201];
  226. } stbsp__digitpair =
  227. {
  228. 0,
  229. "00010203040506070809101112131415161718192021222324"
  230. "25262728293031323334353637383940414243444546474849"
  231. "50515253545556575859606162636465666768697071727374"
  232. "75767778798081828384858687888990919293949596979899"
  233. };
  234. STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod)
  235. {
  236. stbsp__period = pperiod;
  237. stbsp__comma = pcomma;
  238. }
  239. #define STBSP__LEFTJUST 1
  240. #define STBSP__LEADINGPLUS 2
  241. #define STBSP__LEADINGSPACE 4
  242. #define STBSP__LEADING_0X 8
  243. #define STBSP__LEADINGZERO 16
  244. #define STBSP__INTMAX 32
  245. #define STBSP__TRIPLET_COMMA 64
  246. #define STBSP__NEGATIVE 128
  247. #define STBSP__METRIC_SUFFIX 256
  248. #define STBSP__HALFWIDTH 512
  249. #define STBSP__METRIC_NOSPACE 1024
  250. #define STBSP__METRIC_1024 2048
  251. #define STBSP__METRIC_JEDEC 4096
  252. static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)
  253. {
  254. sign[0] = 0;
  255. if (fl & STBSP__NEGATIVE) {
  256. sign[0] = 1;
  257. sign[1] = '-';
  258. } else if (fl & STBSP__LEADINGSPACE) {
  259. sign[0] = 1;
  260. sign[1] = ' ';
  261. } else if (fl & STBSP__LEADINGPLUS) {
  262. sign[0] = 1;
  263. sign[1] = '+';
  264. }
  265. }
  266. static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uint32 limit)
  267. {
  268. char const * sn = s;
  269. // get up to 4-byte alignment
  270. for (;;) {
  271. if (((stbsp__uintptr)sn & 3) == 0)
  272. break;
  273. if (!limit || *sn == 0)
  274. return (stbsp__uint32)(sn - s);
  275. ++sn;
  276. --limit;
  277. }
  278. // scan over 4 bytes at a time to find terminating 0
  279. // this will intentionally scan up to 3 bytes past the end of buffers,
  280. // but becase it works 4B aligned, it will never cross page boundaries
  281. // (hence the STBSP__ASAN markup; the over-read here is intentional
  282. // and harmless)
  283. while (limit >= 4) {
  284. stbsp__uint32 v = *(stbsp__uint32 *)sn;
  285. // bit hack to find if there's a 0 byte in there
  286. if ((v - 0x01010101) & (~v) & 0x80808080UL)
  287. break;
  288. sn += 4;
  289. limit -= 4;
  290. }
  291. // handle the last few characters to find actual size
  292. while (limit && *sn) {
  293. ++sn;
  294. --limit;
  295. }
  296. return (stbsp__uint32)(sn - s);
  297. }
  298. STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va)
  299. {
  300. static char hex[] = "0123456789abcdefxp";
  301. static char hexu[] = "0123456789ABCDEFXP";
  302. char *bf;
  303. char const *f;
  304. int tlen = 0;
  305. bf = buf;
  306. f = fmt;
  307. for (;;) {
  308. stbsp__int32 fw, pr, tz;
  309. stbsp__uint32 fl;
  310. // macros for the callback buffer stuff
  311. #define stbsp__chk_cb_bufL(bytes) \
  312. { \
  313. int len = (int)(bf - buf); \
  314. if ((len + (bytes)) >= STB_SPRINTF_MIN) { \
  315. tlen += len; \
  316. if (0 == (bf = buf = callback(buf, user, len))) \
  317. goto done; \
  318. } \
  319. }
  320. #define stbsp__chk_cb_buf(bytes) \
  321. { \
  322. if (callback) { \
  323. stbsp__chk_cb_bufL(bytes); \
  324. } \
  325. }
  326. #define stbsp__flush_cb() \
  327. { \
  328. stbsp__chk_cb_bufL(STB_SPRINTF_MIN - 1); \
  329. } // flush if there is even one byte in the buffer
  330. #define stbsp__cb_buf_clamp(cl, v) \
  331. cl = v; \
  332. if (callback) { \
  333. int lg = STB_SPRINTF_MIN - (int)(bf - buf); \
  334. if (cl > lg) \
  335. cl = lg; \
  336. }
  337. // fast copy everything up to the next % (or end of string)
  338. for (;;) {
  339. while (((stbsp__uintptr)f) & 3) {
  340. schk1:
  341. if (f[0] == '%')
  342. goto scandd;
  343. schk2:
  344. if (f[0] == 0)
  345. goto endfmt;
  346. stbsp__chk_cb_buf(1);
  347. *bf++ = f[0];
  348. ++f;
  349. }
  350. for (;;) {
  351. // Check if the next 4 bytes contain %(0x25) or end of string.
  352. // Using the 'hasless' trick:
  353. // https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord
  354. stbsp__uint32 v, c;
  355. v = *(stbsp__uint32 *)f;
  356. c = (~v) & 0x80808080;
  357. if (((v ^ 0x25252525) - 0x01010101) & c)
  358. goto schk1;
  359. if ((v - 0x01010101) & c)
  360. goto schk2;
  361. if (callback)
  362. if ((STB_SPRINTF_MIN - (int)(bf - buf)) < 4)
  363. goto schk1;
  364. #ifdef STB_SPRINTF_NOUNALIGNED
  365. if(((stbsp__uintptr)bf) & 3) {
  366. bf[0] = f[0];
  367. bf[1] = f[1];
  368. bf[2] = f[2];
  369. bf[3] = f[3];
  370. } else
  371. #endif
  372. {
  373. *(stbsp__uint32 *)bf = v;
  374. }
  375. bf += 4;
  376. f += 4;
  377. }
  378. }
  379. scandd:
  380. ++f;
  381. // ok, we have a percent, read the modifiers first
  382. fw = 0;
  383. pr = -1;
  384. fl = 0;
  385. tz = 0;
  386. // flags
  387. for (;;) {
  388. switch (f[0]) {
  389. // if we have left justify
  390. case '-':
  391. fl |= STBSP__LEFTJUST;
  392. ++f;
  393. continue;
  394. // if we have leading plus
  395. case '+':
  396. fl |= STBSP__LEADINGPLUS;
  397. ++f;
  398. continue;
  399. // if we have leading space
  400. case ' ':
  401. fl |= STBSP__LEADINGSPACE;
  402. ++f;
  403. continue;
  404. // if we have leading 0x
  405. case '#':
  406. fl |= STBSP__LEADING_0X;
  407. ++f;
  408. continue;
  409. // if we have thousand commas
  410. case '\'':
  411. fl |= STBSP__TRIPLET_COMMA;
  412. ++f;
  413. continue;
  414. // if we have kilo marker (none->kilo->kibi->jedec)
  415. case '$':
  416. if (fl & STBSP__METRIC_SUFFIX) {
  417. if (fl & STBSP__METRIC_1024) {
  418. fl |= STBSP__METRIC_JEDEC;
  419. } else {
  420. fl |= STBSP__METRIC_1024;
  421. }
  422. } else {
  423. fl |= STBSP__METRIC_SUFFIX;
  424. }
  425. ++f;
  426. continue;
  427. // if we don't want space between metric suffix and number
  428. case '_':
  429. fl |= STBSP__METRIC_NOSPACE;
  430. ++f;
  431. continue;
  432. // if we have leading zero
  433. case '0':
  434. fl |= STBSP__LEADINGZERO;
  435. ++f;
  436. goto flags_done;
  437. default: goto flags_done;
  438. }
  439. }
  440. flags_done:
  441. // get the field width
  442. if (f[0] == '*') {
  443. fw = va_arg(va, stbsp__uint32);
  444. ++f;
  445. } else {
  446. while ((f[0] >= '0') && (f[0] <= '9')) {
  447. fw = fw * 10 + f[0] - '0';
  448. f++;
  449. }
  450. }
  451. // get the precision
  452. if (f[0] == '.') {
  453. ++f;
  454. if (f[0] == '*') {
  455. pr = va_arg(va, stbsp__uint32);
  456. ++f;
  457. } else {
  458. pr = 0;
  459. while ((f[0] >= '0') && (f[0] <= '9')) {
  460. pr = pr * 10 + f[0] - '0';
  461. f++;
  462. }
  463. }
  464. }
  465. // handle integer size overrides
  466. switch (f[0]) {
  467. // are we halfwidth?
  468. case 'h':
  469. fl |= STBSP__HALFWIDTH;
  470. ++f;
  471. if (f[0] == 'h')
  472. ++f; // QUARTERWIDTH
  473. break;
  474. // are we 64-bit (unix style)
  475. case 'l':
  476. fl |= ((sizeof(long) == 8) ? STBSP__INTMAX : 0);
  477. ++f;
  478. if (f[0] == 'l') {
  479. fl |= STBSP__INTMAX;
  480. ++f;
  481. }
  482. break;
  483. // are we 64-bit on intmax? (c99)
  484. case 'j':
  485. fl |= (sizeof(size_t) == 8) ? STBSP__INTMAX : 0;
  486. ++f;
  487. break;
  488. // are we 64-bit on size_t or ptrdiff_t? (c99)
  489. case 'z':
  490. fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;
  491. ++f;
  492. break;
  493. case 't':
  494. fl |= (sizeof(ptrdiff_t) == 8) ? STBSP__INTMAX : 0;
  495. ++f;
  496. break;
  497. // are we 64-bit (msft style)
  498. case 'I':
  499. if ((f[1] == '6') && (f[2] == '4')) {
  500. fl |= STBSP__INTMAX;
  501. f += 3;
  502. } else if ((f[1] == '3') && (f[2] == '2')) {
  503. f += 3;
  504. } else {
  505. fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0);
  506. ++f;
  507. }
  508. break;
  509. default: break;
  510. }
  511. // handle each replacement
  512. switch (f[0]) {
  513. #define STBSP__NUMSZ 512 // big enough for e308 (with commas) or e-307
  514. char num[STBSP__NUMSZ];
  515. char lead[8];
  516. char tail[8];
  517. char *s;
  518. char const *h;
  519. stbsp__uint32 l, n, cs;
  520. stbsp__uint64 n64;
  521. #ifndef STB_SPRINTF_NOFLOAT
  522. double fv;
  523. #endif
  524. stbsp__int32 dp;
  525. char const *sn;
  526. case 's':
  527. // get the string
  528. s = va_arg(va, char *);
  529. if (s == 0)
  530. s = (char *)"null";
  531. // get the length, limited to desired precision
  532. // always limit to ~0u chars since our counts are 32b
  533. l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
  534. lead[0] = 0;
  535. tail[0] = 0;
  536. pr = 0;
  537. dp = 0;
  538. cs = 0;
  539. // copy the string in
  540. goto scopy;
  541. case 'S':
  542. {
  543. // string struct
  544. string str = va_arg(va, string);
  545. s = (char *)str.str;
  546. sn = (const char *)(str.str + str.length);
  547. l = (unsigned int)str.length;
  548. lead[0] = 0;
  549. tail[0] = 0;
  550. pr = 0;
  551. dp = 0;
  552. cs = 0;
  553. goto scopy;
  554. } break;
  555. case 'c': // char
  556. // get the character
  557. s = num + STBSP__NUMSZ - 1;
  558. *s = (char)va_arg(va, int);
  559. l = 1;
  560. lead[0] = 0;
  561. tail[0] = 0;
  562. pr = 0;
  563. dp = 0;
  564. cs = 0;
  565. goto scopy;
  566. case 'n': // weird write-bytes specifier
  567. {
  568. int *d = va_arg(va, int *);
  569. *d = tlen + (int)(bf - buf);
  570. } break;
  571. #ifdef STB_SPRINTF_NOFLOAT
  572. case 'A': // float
  573. case 'a': // hex float
  574. case 'G': // float
  575. case 'g': // float
  576. case 'E': // float
  577. case 'e': // float
  578. case 'f': // float
  579. va_arg(va, double); // eat it
  580. s = (char *)"No float";
  581. l = 8;
  582. lead[0] = 0;
  583. tail[0] = 0;
  584. pr = 0;
  585. cs = 0;
  586. STBSP__NOTUSED(dp);
  587. goto scopy;
  588. #else
  589. case 'A': // hex float
  590. case 'a': // hex float
  591. h = (f[0] == 'A') ? hexu : hex;
  592. fv = va_arg(va, double);
  593. if (pr == -1)
  594. pr = 6; // default is 6
  595. // read the double into a string
  596. if (stbsp__real_to_parts((stbsp__int64 *)&n64, &dp, fv))
  597. fl |= STBSP__NEGATIVE;
  598. s = num + 64;
  599. stbsp__lead_sign(fl, lead);
  600. if (dp == -1023)
  601. dp = (n64) ? -1022 : 0;
  602. else
  603. n64 |= (((stbsp__uint64)1) << 52);
  604. n64 <<= (64 - 56);
  605. if (pr < 15)
  606. n64 += ((((stbsp__uint64)8) << 56) >> (pr * 4));
  607. // add leading chars
  608. #ifdef STB_SPRINTF_MSVC_MODE
  609. *s++ = '0';
  610. *s++ = 'x';
  611. #else
  612. lead[1 + lead[0]] = '0';
  613. lead[2 + lead[0]] = 'x';
  614. lead[0] += 2;
  615. #endif
  616. *s++ = h[(n64 >> 60) & 15];
  617. n64 <<= 4;
  618. if (pr)
  619. *s++ = stbsp__period;
  620. sn = s;
  621. // print the bits
  622. n = pr;
  623. if (n > 13)
  624. n = 13;
  625. if (pr > (stbsp__int32)n)
  626. tz = pr - n;
  627. pr = 0;
  628. while (n--) {
  629. *s++ = h[(n64 >> 60) & 15];
  630. n64 <<= 4;
  631. }
  632. // print the expo
  633. tail[1] = h[17];
  634. if (dp < 0) {
  635. tail[2] = '-';
  636. dp = -dp;
  637. } else
  638. tail[2] = '+';
  639. n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));
  640. tail[0] = (char)n;
  641. for (;;) {
  642. tail[n] = '0' + dp % 10;
  643. if (n <= 3)
  644. break;
  645. --n;
  646. dp /= 10;
  647. }
  648. dp = (int)(s - sn);
  649. l = (int)(s - (num + 64));
  650. s = num + 64;
  651. cs = 1 + (3 << 24);
  652. goto scopy;
  653. case 'G': // float
  654. case 'g': // float
  655. h = (f[0] == 'G') ? hexu : hex;
  656. fv = va_arg(va, double);
  657. if (pr == -1)
  658. pr = 6;
  659. else if (pr == 0)
  660. pr = 1; // default is 6
  661. // read the double into a string
  662. if (stbsp__real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000))
  663. fl |= STBSP__NEGATIVE;
  664. // clamp the precision and delete extra zeros after clamp
  665. n = pr;
  666. if (l > (stbsp__uint32)pr)
  667. l = pr;
  668. while ((l > 1) && (pr) && (sn[l - 1] == '0')) {
  669. --pr;
  670. --l;
  671. }
  672. // should we use %e
  673. if ((dp <= -4) || (dp > (stbsp__int32)n)) {
  674. if (pr > (stbsp__int32)l)
  675. pr = l - 1;
  676. else if (pr)
  677. --pr; // when using %e, there is one digit before the decimal
  678. goto doexpfromg;
  679. }
  680. // this is the insane action to get the pr to match %g semantics for %f
  681. if (dp > 0) {
  682. pr = (dp < (stbsp__int32)l) ? l - dp : 0;
  683. } else {
  684. pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr);
  685. }
  686. goto dofloatfromg;
  687. case 'E': // float
  688. case 'e': // float
  689. h = (f[0] == 'E') ? hexu : hex;
  690. fv = va_arg(va, double);
  691. if (pr == -1)
  692. pr = 6; // default is 6
  693. // read the double into a string
  694. if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000))
  695. fl |= STBSP__NEGATIVE;
  696. doexpfromg:
  697. tail[0] = 0;
  698. stbsp__lead_sign(fl, lead);
  699. if (dp == STBSP__SPECIAL) {
  700. s = (char *)sn;
  701. cs = 0;
  702. pr = 0;
  703. goto scopy;
  704. }
  705. s = num + 64;
  706. // handle leading chars
  707. *s++ = sn[0];
  708. if (pr)
  709. *s++ = stbsp__period;
  710. // handle after decimal
  711. if ((l - 1) > (stbsp__uint32)pr)
  712. l = pr + 1;
  713. for (n = 1; n < l; n++)
  714. *s++ = sn[n];
  715. // trailing zeros
  716. tz = pr - (l - 1);
  717. pr = 0;
  718. // dump expo
  719. tail[1] = h[0xe];
  720. dp -= 1;
  721. if (dp < 0) {
  722. tail[2] = '-';
  723. dp = -dp;
  724. } else
  725. tail[2] = '+';
  726. #ifdef STB_SPRINTF_MSVC_MODE
  727. n = 5;
  728. #else
  729. n = (dp >= 100) ? 5 : 4;
  730. #endif
  731. tail[0] = (char)n;
  732. for (;;) {
  733. tail[n] = '0' + dp % 10;
  734. if (n <= 3)
  735. break;
  736. --n;
  737. dp /= 10;
  738. }
  739. cs = 1 + (3 << 24); // how many tens
  740. goto flt_lead;
  741. case 'f': // float
  742. fv = va_arg(va, double);
  743. doafloat:
  744. // do kilos
  745. if (fl & STBSP__METRIC_SUFFIX) {
  746. double divisor;
  747. divisor = 1000.0f;
  748. if (fl & STBSP__METRIC_1024)
  749. divisor = 1024.0;
  750. while (fl < 0x4000000) {
  751. if ((fv < divisor) && (fv > -divisor))
  752. break;
  753. fv /= divisor;
  754. fl += 0x1000000;
  755. }
  756. }
  757. if (pr == -1)
  758. pr = 6; // default is 6
  759. // read the double into a string
  760. if (stbsp__real_to_str(&sn, &l, num, &dp, fv, pr))
  761. fl |= STBSP__NEGATIVE;
  762. dofloatfromg:
  763. tail[0] = 0;
  764. stbsp__lead_sign(fl, lead);
  765. if (dp == STBSP__SPECIAL) {
  766. s = (char *)sn;
  767. cs = 0;
  768. pr = 0;
  769. goto scopy;
  770. }
  771. s = num + 64;
  772. // handle the three decimal varieties
  773. if (dp <= 0) {
  774. stbsp__int32 i;
  775. // handle 0.000*000xxxx
  776. *s++ = '0';
  777. if (pr)
  778. *s++ = stbsp__period;
  779. n = -dp;
  780. if ((stbsp__int32)n > pr)
  781. n = pr;
  782. i = n;
  783. while (i) {
  784. if ((((stbsp__uintptr)s) & 3) == 0)
  785. break;
  786. *s++ = '0';
  787. --i;
  788. }
  789. while (i >= 4) {
  790. *(stbsp__uint32 *)s = 0x30303030;
  791. s += 4;
  792. i -= 4;
  793. }
  794. while (i) {
  795. *s++ = '0';
  796. --i;
  797. }
  798. if ((stbsp__int32)(l + n) > pr)
  799. l = pr - n;
  800. i = l;
  801. while (i) {
  802. *s++ = *sn++;
  803. --i;
  804. }
  805. tz = pr - (n + l);
  806. cs = 1 + (3 << 24); // how many tens did we write (for commas below)
  807. } else {
  808. cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0;
  809. if ((stbsp__uint32)dp >= l) {
  810. // handle xxxx000*000.0
  811. n = 0;
  812. for (;;) {
  813. if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
  814. cs = 0;
  815. *s++ = stbsp__comma;
  816. } else {
  817. *s++ = sn[n];
  818. ++n;
  819. if (n >= l)
  820. break;
  821. }
  822. }
  823. if (n < (stbsp__uint32)dp) {
  824. n = dp - n;
  825. if ((fl & STBSP__TRIPLET_COMMA) == 0) {
  826. while (n) {
  827. if ((((stbsp__uintptr)s) & 3) == 0)
  828. break;
  829. *s++ = '0';
  830. --n;
  831. }
  832. while (n >= 4) {
  833. *(stbsp__uint32 *)s = 0x30303030;
  834. s += 4;
  835. n -= 4;
  836. }
  837. }
  838. while (n) {
  839. if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
  840. cs = 0;
  841. *s++ = stbsp__comma;
  842. } else {
  843. *s++ = '0';
  844. --n;
  845. }
  846. }
  847. }
  848. cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens
  849. if (pr) {
  850. *s++ = stbsp__period;
  851. tz = pr;
  852. }
  853. } else {
  854. // handle xxxxx.xxxx000*000
  855. n = 0;
  856. for (;;) {
  857. if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
  858. cs = 0;
  859. *s++ = stbsp__comma;
  860. } else {
  861. *s++ = sn[n];
  862. ++n;
  863. if (n >= (stbsp__uint32)dp)
  864. break;
  865. }
  866. }
  867. cs = (int)(s - (num + 64)) + (3 << 24); // cs is how many tens
  868. if (pr)
  869. *s++ = stbsp__period;
  870. if ((l - dp) > (stbsp__uint32)pr)
  871. l = pr + dp;
  872. while (n < l) {
  873. *s++ = sn[n];
  874. ++n;
  875. }
  876. tz = pr - (l - dp);
  877. }
  878. }
  879. pr = 0;
  880. // handle k,m,g,t
  881. if (fl & STBSP__METRIC_SUFFIX) {
  882. char idx;
  883. idx = 1;
  884. if (fl & STBSP__METRIC_NOSPACE)
  885. idx = 0;
  886. tail[0] = idx;
  887. tail[1] = ' ';
  888. {
  889. if (fl >> 24) { // SI kilo is 'k', JEDEC and SI kibits are 'K'.
  890. if (fl & STBSP__METRIC_1024)
  891. tail[idx + 1] = "_KMGT"[fl >> 24];
  892. else
  893. tail[idx + 1] = "_kMGT"[fl >> 24];
  894. idx++;
  895. // If printing kibits and not in jedec, add the 'i'.
  896. if (fl & STBSP__METRIC_1024 && !(fl & STBSP__METRIC_JEDEC)) {
  897. tail[idx + 1] = 'i';
  898. idx++;
  899. }
  900. tail[0] = idx;
  901. }
  902. }
  903. };
  904. flt_lead:
  905. // get the length that we copied
  906. l = (stbsp__uint32)(s - (num + 64));
  907. s = num + 64;
  908. goto scopy;
  909. #endif
  910. case 'B': // upper binary
  911. case 'b': // lower binary
  912. h = (f[0] == 'B') ? hexu : hex;
  913. lead[0] = 0;
  914. if (fl & STBSP__LEADING_0X) {
  915. lead[0] = 2;
  916. lead[1] = '0';
  917. lead[2] = h[0xb];
  918. }
  919. l = (8 << 4) | (1 << 8);
  920. goto radixnum;
  921. case 'o': // octal
  922. h = hexu;
  923. lead[0] = 0;
  924. if (fl & STBSP__LEADING_0X) {
  925. lead[0] = 1;
  926. lead[1] = '0';
  927. }
  928. l = (3 << 4) | (3 << 8);
  929. goto radixnum;
  930. case 'p': // pointer
  931. fl |= (sizeof(void *) == 8) ? STBSP__INTMAX : 0;
  932. pr = sizeof(void *) * 2;
  933. fl &= ~STBSP__LEADINGZERO; // 'p' only prints the pointer with zeros
  934. // fall through - to X
  935. case 'X': // upper hex
  936. case 'x': // lower hex
  937. h = (f[0] == 'X') ? hexu : hex;
  938. l = (4 << 4) | (4 << 8);
  939. lead[0] = 0;
  940. if (fl & STBSP__LEADING_0X) {
  941. lead[0] = 2;
  942. lead[1] = '0';
  943. lead[2] = h[16];
  944. }
  945. radixnum:
  946. // get the number
  947. if (fl & STBSP__INTMAX)
  948. n64 = va_arg(va, stbsp__uint64);
  949. else
  950. n64 = va_arg(va, stbsp__uint32);
  951. s = num + STBSP__NUMSZ;
  952. dp = 0;
  953. // clear tail, and clear leading if value is zero
  954. tail[0] = 0;
  955. if (n64 == 0) {
  956. lead[0] = 0;
  957. if (pr == 0) {
  958. l = 0;
  959. cs = 0;
  960. goto scopy;
  961. }
  962. }
  963. // convert to string
  964. for (;;) {
  965. *--s = h[n64 & ((1 << (l >> 8)) - 1)];
  966. n64 >>= (l >> 8);
  967. if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))
  968. break;
  969. if (fl & STBSP__TRIPLET_COMMA) {
  970. ++l;
  971. if ((l & 15) == ((l >> 4) & 15)) {
  972. l &= ~15;
  973. *--s = stbsp__comma;
  974. }
  975. }
  976. };
  977. // get the tens and the comma pos
  978. cs = (stbsp__uint32)((num + STBSP__NUMSZ) - s) + ((((l >> 4) & 15)) << 24);
  979. // get the length that we copied
  980. l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);
  981. // copy it
  982. goto scopy;
  983. case 'u': // unsigned
  984. case 'i':
  985. case 'd': // integer
  986. // get the integer and abs it
  987. if (fl & STBSP__INTMAX) {
  988. stbsp__int64 i64 = va_arg(va, stbsp__int64);
  989. n64 = (stbsp__uint64)i64;
  990. if ((f[0] != 'u') && (i64 < 0)) {
  991. n64 = (stbsp__uint64)-i64;
  992. fl |= STBSP__NEGATIVE;
  993. }
  994. } else {
  995. stbsp__int32 i = va_arg(va, stbsp__int32);
  996. n64 = (stbsp__uint32)i;
  997. if ((f[0] != 'u') && (i < 0)) {
  998. n64 = (stbsp__uint32)-i;
  999. fl |= STBSP__NEGATIVE;
  1000. }
  1001. }
  1002. #ifndef STB_SPRINTF_NOFLOAT
  1003. if (fl & STBSP__METRIC_SUFFIX) {
  1004. if (n64 < 1024)
  1005. pr = 0;
  1006. else if (pr == -1)
  1007. pr = 1;
  1008. fv = (double)(stbsp__int64)n64;
  1009. goto doafloat;
  1010. }
  1011. #endif
  1012. // convert to string
  1013. s = num + STBSP__NUMSZ;
  1014. l = 0;
  1015. for (;;) {
  1016. // do in 32-bit chunks (avoid lots of 64-bit divides even with constant denominators)
  1017. char *o = s - 8;
  1018. if (n64 >= 100000000) {
  1019. n = (stbsp__uint32)(n64 % 100000000);
  1020. n64 /= 100000000;
  1021. } else {
  1022. n = (stbsp__uint32)n64;
  1023. n64 = 0;
  1024. }
  1025. if ((fl & STBSP__TRIPLET_COMMA) == 0) {
  1026. do {
  1027. s -= 2;
  1028. *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];
  1029. n /= 100;
  1030. } while (n);
  1031. }
  1032. while (n) {
  1033. if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {
  1034. l = 0;
  1035. *--s = stbsp__comma;
  1036. --o;
  1037. } else {
  1038. *--s = (char)(n % 10) + '0';
  1039. n /= 10;
  1040. }
  1041. }
  1042. if (n64 == 0) {
  1043. if ((s[0] == '0') && (s != (num + STBSP__NUMSZ)))
  1044. ++s;
  1045. break;
  1046. }
  1047. while (s != o)
  1048. if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {
  1049. l = 0;
  1050. *--s = stbsp__comma;
  1051. --o;
  1052. } else {
  1053. *--s = '0';
  1054. }
  1055. }
  1056. tail[0] = 0;
  1057. stbsp__lead_sign(fl, lead);
  1058. // get the length that we copied
  1059. l = (stbsp__uint32)((num + STBSP__NUMSZ) - s);
  1060. if (l == 0) {
  1061. *--s = '0';
  1062. l = 1;
  1063. }
  1064. cs = l + (3 << 24);
  1065. if (pr < 0)
  1066. pr = 0;
  1067. scopy:
  1068. // get fw=leading/trailing space, pr=leading zeros
  1069. if (pr < (stbsp__int32)l)
  1070. pr = l;
  1071. n = pr + lead[0] + tail[0] + tz;
  1072. if (fw < (stbsp__int32)n)
  1073. fw = n;
  1074. fw -= n;
  1075. pr -= l;
  1076. // handle right justify and leading zeros
  1077. if ((fl & STBSP__LEFTJUST) == 0) {
  1078. if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr
  1079. {
  1080. pr = (fw > pr) ? fw : pr;
  1081. fw = 0;
  1082. } else {
  1083. fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas
  1084. }
  1085. }
  1086. // copy the spaces and/or zeros
  1087. if (fw + pr) {
  1088. stbsp__int32 i;
  1089. stbsp__uint32 c;
  1090. // copy leading spaces (or when doing %8.4d stuff)
  1091. if ((fl & STBSP__LEFTJUST) == 0)
  1092. while (fw > 0) {
  1093. stbsp__cb_buf_clamp(i, fw);
  1094. fw -= i;
  1095. while (i) {
  1096. if ((((stbsp__uintptr)bf) & 3) == 0)
  1097. break;
  1098. *bf++ = ' ';
  1099. --i;
  1100. }
  1101. while (i >= 4) {
  1102. *(stbsp__uint32 *)bf = 0x20202020;
  1103. bf += 4;
  1104. i -= 4;
  1105. }
  1106. while (i) {
  1107. *bf++ = ' ';
  1108. --i;
  1109. }
  1110. stbsp__chk_cb_buf(1);
  1111. }
  1112. // copy leader
  1113. sn = lead + 1;
  1114. while (lead[0]) {
  1115. stbsp__cb_buf_clamp(i, lead[0]);
  1116. lead[0] -= (char)i;
  1117. while (i) {
  1118. *bf++ = *sn++;
  1119. --i;
  1120. }
  1121. stbsp__chk_cb_buf(1);
  1122. }
  1123. // copy leading zeros
  1124. c = cs >> 24;
  1125. cs &= 0xffffff;
  1126. cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;
  1127. while (pr > 0) {
  1128. stbsp__cb_buf_clamp(i, pr);
  1129. pr -= i;
  1130. if ((fl & STBSP__TRIPLET_COMMA) == 0) {
  1131. while (i) {
  1132. if ((((stbsp__uintptr)bf) & 3) == 0)
  1133. break;
  1134. *bf++ = '0';
  1135. --i;
  1136. }
  1137. while (i >= 4) {
  1138. *(stbsp__uint32 *)bf = 0x30303030;
  1139. bf += 4;
  1140. i -= 4;
  1141. }
  1142. }
  1143. while (i) {
  1144. if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {
  1145. cs = 0;
  1146. *bf++ = stbsp__comma;
  1147. } else
  1148. *bf++ = '0';
  1149. --i;
  1150. }
  1151. stbsp__chk_cb_buf(1);
  1152. }
  1153. }
  1154. // copy leader if there is still one
  1155. sn = lead + 1;
  1156. while (lead[0]) {
  1157. stbsp__int32 i;
  1158. stbsp__cb_buf_clamp(i, lead[0]);
  1159. lead[0] -= (char)i;
  1160. while (i) {
  1161. *bf++ = *sn++;
  1162. --i;
  1163. }
  1164. stbsp__chk_cb_buf(1);
  1165. }
  1166. // copy the string
  1167. n = l;
  1168. while (n) {
  1169. stbsp__int32 i;
  1170. stbsp__cb_buf_clamp(i, n);
  1171. n -= i;
  1172. STBSP__UNALIGNED(while (i >= 4) {
  1173. *(stbsp__uint32 volatile *)bf = *(stbsp__uint32 volatile *)s;
  1174. bf += 4;
  1175. s += 4;
  1176. i -= 4;
  1177. })
  1178. while (i) {
  1179. *bf++ = *s++;
  1180. --i;
  1181. }
  1182. stbsp__chk_cb_buf(1);
  1183. }
  1184. // copy trailing zeros
  1185. while (tz) {
  1186. stbsp__int32 i;
  1187. stbsp__cb_buf_clamp(i, tz);
  1188. tz -= i;
  1189. while (i) {
  1190. if ((((stbsp__uintptr)bf) & 3) == 0)
  1191. break;
  1192. *bf++ = '0';
  1193. --i;
  1194. }
  1195. while (i >= 4) {
  1196. *(stbsp__uint32 *)bf = 0x30303030;
  1197. bf += 4;
  1198. i -= 4;
  1199. }
  1200. while (i) {
  1201. *bf++ = '0';
  1202. --i;
  1203. }
  1204. stbsp__chk_cb_buf(1);
  1205. }
  1206. // copy tail if there is one
  1207. sn = tail + 1;
  1208. while (tail[0]) {
  1209. stbsp__int32 i;
  1210. stbsp__cb_buf_clamp(i, tail[0]);
  1211. tail[0] -= (char)i;
  1212. while (i) {
  1213. *bf++ = *sn++;
  1214. --i;
  1215. }
  1216. stbsp__chk_cb_buf(1);
  1217. }
  1218. // handle the left justify
  1219. if (fl & STBSP__LEFTJUST)
  1220. if (fw > 0) {
  1221. while (fw) {
  1222. stbsp__int32 i;
  1223. stbsp__cb_buf_clamp(i, fw);
  1224. fw -= i;
  1225. while (i) {
  1226. if ((((stbsp__uintptr)bf) & 3) == 0)
  1227. break;
  1228. *bf++ = ' ';
  1229. --i;
  1230. }
  1231. while (i >= 4) {
  1232. *(stbsp__uint32 *)bf = 0x20202020;
  1233. bf += 4;
  1234. i -= 4;
  1235. }
  1236. while (i--)
  1237. *bf++ = ' ';
  1238. stbsp__chk_cb_buf(1);
  1239. }
  1240. }
  1241. break;
  1242. default: // unknown, just copy code
  1243. s = num + STBSP__NUMSZ - 1;
  1244. *s = f[0];
  1245. l = 1;
  1246. fw = fl = 0;
  1247. lead[0] = 0;
  1248. tail[0] = 0;
  1249. pr = 0;
  1250. dp = 0;
  1251. cs = 0;
  1252. goto scopy;
  1253. }
  1254. ++f;
  1255. }
  1256. endfmt:
  1257. if (!callback)
  1258. *bf = 0;
  1259. else
  1260. stbsp__flush_cb();
  1261. done:
  1262. return tlen + (int)(bf - buf);
  1263. }
  1264. // cleanup
  1265. #undef STBSP__LEFTJUST
  1266. #undef STBSP__LEADINGPLUS
  1267. #undef STBSP__LEADINGSPACE
  1268. #undef STBSP__LEADING_0X
  1269. #undef STBSP__LEADINGZERO
  1270. #undef STBSP__INTMAX
  1271. #undef STBSP__TRIPLET_COMMA
  1272. #undef STBSP__NEGATIVE
  1273. #undef STBSP__METRIC_SUFFIX
  1274. #undef STBSP__NUMSZ
  1275. #undef stbsp__chk_cb_bufL
  1276. #undef stbsp__chk_cb_buf
  1277. #undef stbsp__flush_cb
  1278. #undef stbsp__cb_buf_clamp
  1279. // ============================================================================
  1280. // wrapper functions
  1281. STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...)
  1282. {
  1283. int result;
  1284. va_list va;
  1285. va_start(va, fmt);
  1286. result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);
  1287. va_end(va);
  1288. return result;
  1289. }
  1290. typedef struct stbsp__context {
  1291. char *buf;
  1292. int count;
  1293. int length;
  1294. char tmp[STB_SPRINTF_MIN];
  1295. } stbsp__context;
  1296. static char *stbsp__clamp_callback(const char *buf, void *user, int len)
  1297. {
  1298. stbsp__context *c = (stbsp__context *)user;
  1299. c->length += len;
  1300. if (len > c->count)
  1301. len = c->count;
  1302. if (len) {
  1303. if (buf != c->buf) {
  1304. const char *s, *se;
  1305. char *d;
  1306. d = c->buf;
  1307. s = buf;
  1308. se = buf + len;
  1309. do {
  1310. *d++ = *s++;
  1311. } while (s < se);
  1312. }
  1313. c->buf += len;
  1314. c->count -= len;
  1315. }
  1316. if (c->count <= 0)
  1317. return c->tmp;
  1318. return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can
  1319. }
  1320. static char * stbsp__count_clamp_callback( const char * buf, void * user, int len )
  1321. {
  1322. stbsp__context * c = (stbsp__context*)user;
  1323. (void) sizeof(buf);
  1324. c->length += len;
  1325. return c->tmp; // go direct into buffer if you can
  1326. }
  1327. STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )
  1328. {
  1329. stbsp__context c;
  1330. if ( (count == 0) && !buf )
  1331. {
  1332. c.length = 0;
  1333. STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );
  1334. }
  1335. else
  1336. {
  1337. int l;
  1338. c.buf = buf;
  1339. c.count = count;
  1340. c.length = 0;
  1341. STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );
  1342. // zero-terminate
  1343. l = (int)( c.buf - buf );
  1344. if ( l >= count ) // should never be greater, only equal (or less) than count
  1345. l = count - 1;
  1346. buf[l] = 0;
  1347. }
  1348. return c.length;
  1349. }
  1350. STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)
  1351. {
  1352. int result;
  1353. va_list va;
  1354. va_start(va, fmt);
  1355. result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va);
  1356. va_end(va);
  1357. return result;
  1358. }
  1359. STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va)
  1360. {
  1361. return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);
  1362. }
  1363. // =======================================================================
  1364. // low level float utility functions
  1365. #ifndef STB_SPRINTF_NOFLOAT
  1366. // copies d to bits w/ strict aliasing (this compiles to nothing on /Ox)
  1367. #define STBSP__COPYFP(dest, src) \
  1368. { \
  1369. int cn; \
  1370. for (cn = 0; cn < 8; cn++) \
  1371. ((char *)&dest)[cn] = ((char *)&src)[cn]; \
  1372. }
  1373. // get float info
  1374. static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value)
  1375. {
  1376. double d;
  1377. stbsp__int64 b = 0;
  1378. // load value and round at the frac_digits
  1379. d = value;
  1380. STBSP__COPYFP(b, d);
  1381. *bits = b & ((((stbsp__uint64)1) << 52) - 1);
  1382. *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023);
  1383. return (stbsp__int32)((stbsp__uint64) b >> 63);
  1384. }
  1385. static double const stbsp__bot[23] = {
  1386. 1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011,
  1387. 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022
  1388. };
  1389. static double const stbsp__negbot[22] = {
  1390. 1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011,
  1391. 1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022
  1392. };
  1393. static double const stbsp__negboterr[22] = {
  1394. -5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023,
  1395. 4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029,
  1396. -3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035,
  1397. 2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039
  1398. };
  1399. static double const stbsp__top[13] = {
  1400. 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
  1401. };
  1402. static double const stbsp__negtop[13] = {
  1403. 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
  1404. };
  1405. static double const stbsp__toperr[13] = {
  1406. 8388608,
  1407. 6.8601809640529717e+028,
  1408. -7.253143638152921e+052,
  1409. -4.3377296974619174e+075,
  1410. -1.5559416129466825e+098,
  1411. -3.2841562489204913e+121,
  1412. -3.7745893248228135e+144,
  1413. -1.7356668416969134e+167,
  1414. -3.8893577551088374e+190,
  1415. -9.9566444326005119e+213,
  1416. 6.3641293062232429e+236,
  1417. -5.2069140800249813e+259,
  1418. -5.2504760255204387e+282
  1419. };
  1420. static double const stbsp__negtoperr[13] = {
  1421. 3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109,
  1422. -5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201,
  1423. 7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293,
  1424. 8.0970921678014997e-317
  1425. };
  1426. #if defined(_MSC_VER) && (_MSC_VER <= 1200)
  1427. static stbsp__uint64 const stbsp__powten[20] = {
  1428. 1,
  1429. 10,
  1430. 100,
  1431. 1000,
  1432. 10000,
  1433. 100000,
  1434. 1000000,
  1435. 10000000,
  1436. 100000000,
  1437. 1000000000,
  1438. 10000000000,
  1439. 100000000000,
  1440. 1000000000000,
  1441. 10000000000000,
  1442. 100000000000000,
  1443. 1000000000000000,
  1444. 10000000000000000,
  1445. 100000000000000000,
  1446. 1000000000000000000,
  1447. 10000000000000000000U
  1448. };
  1449. #define stbsp__tento19th ((stbsp__uint64)1000000000000000000)
  1450. #else
  1451. static stbsp__uint64 const stbsp__powten[20] = {
  1452. 1,
  1453. 10,
  1454. 100,
  1455. 1000,
  1456. 10000,
  1457. 100000,
  1458. 1000000,
  1459. 10000000,
  1460. 100000000,
  1461. 1000000000,
  1462. 10000000000ULL,
  1463. 100000000000ULL,
  1464. 1000000000000ULL,
  1465. 10000000000000ULL,
  1466. 100000000000000ULL,
  1467. 1000000000000000ULL,
  1468. 10000000000000000ULL,
  1469. 100000000000000000ULL,
  1470. 1000000000000000000ULL,
  1471. 10000000000000000000ULL
  1472. };
  1473. #define stbsp__tento19th (1000000000000000000ULL)
  1474. #endif
  1475. #define stbsp__ddmulthi(oh, ol, xh, yh) \
  1476. { \
  1477. double ahi = 0, alo, bhi = 0, blo; \
  1478. stbsp__int64 bt; \
  1479. oh = xh * yh; \
  1480. STBSP__COPYFP(bt, xh); \
  1481. bt &= ((~(stbsp__uint64)0) << 27); \
  1482. STBSP__COPYFP(ahi, bt); \
  1483. alo = xh - ahi; \
  1484. STBSP__COPYFP(bt, yh); \
  1485. bt &= ((~(stbsp__uint64)0) << 27); \
  1486. STBSP__COPYFP(bhi, bt); \
  1487. blo = yh - bhi; \
  1488. ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \
  1489. }
  1490. #define stbsp__ddtoS64(ob, xh, xl) \
  1491. { \
  1492. double ahi = 0, alo, vh, t; \
  1493. ob = (stbsp__int64)xh; \
  1494. vh = (double)ob; \
  1495. ahi = (xh - vh); \
  1496. t = (ahi - xh); \
  1497. alo = (xh - (ahi - t)) - (vh + t); \
  1498. ob += (stbsp__int64)(ahi + alo + xl); \
  1499. }
  1500. #define stbsp__ddrenorm(oh, ol) \
  1501. { \
  1502. double s; \
  1503. s = oh + ol; \
  1504. ol = ol - (s - oh); \
  1505. oh = s; \
  1506. }
  1507. #define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh);
  1508. #define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl);
  1509. static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__int32 power) // power can be -323 to +350
  1510. {
  1511. double ph, pl;
  1512. if ((power >= 0) && (power <= 22)) {
  1513. stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);
  1514. } else {
  1515. stbsp__int32 e, et, eb;
  1516. double p2h, p2l;
  1517. e = power;
  1518. if (power < 0)
  1519. e = -e;
  1520. et = (e * 0x2c9) >> 14; /* %23 */
  1521. if (et > 13)
  1522. et = 13;
  1523. eb = e - (et * 23);
  1524. ph = d;
  1525. pl = 0.0;
  1526. if (power < 0) {
  1527. if (eb) {
  1528. --eb;
  1529. stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]);
  1530. stbsp__ddmultlos(ph, pl, d, stbsp__negboterr[eb]);
  1531. }
  1532. if (et) {
  1533. stbsp__ddrenorm(ph, pl);
  1534. --et;
  1535. stbsp__ddmulthi(p2h, p2l, ph, stbsp__negtop[et]);
  1536. stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__negtop[et], stbsp__negtoperr[et]);
  1537. ph = p2h;
  1538. pl = p2l;
  1539. }
  1540. } else {
  1541. if (eb) {
  1542. e = eb;
  1543. if (eb > 22)
  1544. eb = 22;
  1545. e -= eb;
  1546. stbsp__ddmulthi(ph, pl, d, stbsp__bot[eb]);
  1547. if (e) {
  1548. stbsp__ddrenorm(ph, pl);
  1549. stbsp__ddmulthi(p2h, p2l, ph, stbsp__bot[e]);
  1550. stbsp__ddmultlos(p2h, p2l, stbsp__bot[e], pl);
  1551. ph = p2h;
  1552. pl = p2l;
  1553. }
  1554. }
  1555. if (et) {
  1556. stbsp__ddrenorm(ph, pl);
  1557. --et;
  1558. stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]);
  1559. stbsp__ddmultlo(p2h, p2l, ph, pl, stbsp__top[et], stbsp__toperr[et]);
  1560. ph = p2h;
  1561. pl = p2l;
  1562. }
  1563. }
  1564. }
  1565. stbsp__ddrenorm(ph, pl);
  1566. *ohi = ph;
  1567. *olo = pl;
  1568. }
  1569. // given a float value, returns the significant bits in bits, and the position of the
  1570. // decimal point in decimal_pos. +/-INF and NAN are specified by special values
  1571. // returned in the decimal_pos parameter.
  1572. // frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000
  1573. static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits)
  1574. {
  1575. double d;
  1576. stbsp__int64 bits = 0;
  1577. stbsp__int32 expo, e, ng, tens;
  1578. d = value;
  1579. STBSP__COPYFP(bits, d);
  1580. expo = (stbsp__int32)((bits >> 52) & 2047);
  1581. ng = (stbsp__int32)((stbsp__uint64) bits >> 63);
  1582. if (ng)
  1583. d = -d;
  1584. if (expo == 2047) // is nan or inf?
  1585. {
  1586. *start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf";
  1587. *decimal_pos = STBSP__SPECIAL;
  1588. *len = 3;
  1589. return ng;
  1590. }
  1591. if (expo == 0) // is zero or denormal
  1592. {
  1593. if (((stbsp__uint64) bits << 1) == 0) // do zero
  1594. {
  1595. *decimal_pos = 1;
  1596. *start = out;
  1597. out[0] = '0';
  1598. *len = 1;
  1599. return ng;
  1600. }
  1601. // find the right expo for denormals
  1602. {
  1603. stbsp__int64 v = ((stbsp__uint64)1) << 51;
  1604. while ((bits & v) == 0) {
  1605. --expo;
  1606. v >>= 1;
  1607. }
  1608. }
  1609. }
  1610. // find the decimal exponent as well as the decimal bits of the value
  1611. {
  1612. double ph, pl;
  1613. // log10 estimate - very specifically tweaked to hit or undershoot by no more than 1 of log10 of all expos 1..2046
  1614. tens = expo - 1023;
  1615. tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1);
  1616. // move the significant bits into position and stick them into an int
  1617. stbsp__raise_to_power10(&ph, &pl, d, 18 - tens);
  1618. // get full as much precision from double-double as possible
  1619. stbsp__ddtoS64(bits, ph, pl);
  1620. // check if we undershot
  1621. if (((stbsp__uint64)bits) >= stbsp__tento19th)
  1622. ++tens;
  1623. }
  1624. // now do the rounding in integer land
  1625. frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits);
  1626. if ((frac_digits < 24)) {
  1627. stbsp__uint32 dg = 1;
  1628. if ((stbsp__uint64)bits >= stbsp__powten[9])
  1629. dg = 10;
  1630. while ((stbsp__uint64)bits >= stbsp__powten[dg]) {
  1631. ++dg;
  1632. if (dg == 20)
  1633. goto noround;
  1634. }
  1635. if (frac_digits < dg) {
  1636. stbsp__uint64 r;
  1637. // add 0.5 at the right position and round
  1638. e = dg - frac_digits;
  1639. if ((stbsp__uint32)e >= 24)
  1640. goto noround;
  1641. r = stbsp__powten[e];
  1642. bits = bits + (r / 2);
  1643. if ((stbsp__uint64)bits >= stbsp__powten[dg])
  1644. ++tens;
  1645. bits /= r;
  1646. }
  1647. noround:;
  1648. }
  1649. // kill long trailing runs of zeros
  1650. if (bits) {
  1651. stbsp__uint32 n;
  1652. for (;;) {
  1653. if (bits <= 0xffffffff)
  1654. break;
  1655. if (bits % 1000)
  1656. goto donez;
  1657. bits /= 1000;
  1658. }
  1659. n = (stbsp__uint32)bits;
  1660. while ((n % 1000) == 0)
  1661. n /= 1000;
  1662. bits = n;
  1663. donez:;
  1664. }
  1665. // convert to string
  1666. out += 64;
  1667. e = 0;
  1668. for (;;) {
  1669. stbsp__uint32 n;
  1670. char *o = out - 8;
  1671. // do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)
  1672. if (bits >= 100000000) {
  1673. n = (stbsp__uint32)(bits % 100000000);
  1674. bits /= 100000000;
  1675. } else {
  1676. n = (stbsp__uint32)bits;
  1677. bits = 0;
  1678. }
  1679. while (n) {
  1680. out -= 2;
  1681. *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];
  1682. n /= 100;
  1683. e += 2;
  1684. }
  1685. if (bits == 0) {
  1686. if ((e) && (out[0] == '0')) {
  1687. ++out;
  1688. --e;
  1689. }
  1690. break;
  1691. }
  1692. while (out != o) {
  1693. *--out = '0';
  1694. ++e;
  1695. }
  1696. }
  1697. *decimal_pos = tens;
  1698. *start = out;
  1699. *len = e;
  1700. return ng;
  1701. }
  1702. #undef stbsp__ddmulthi
  1703. #undef stbsp__ddrenorm
  1704. #undef stbsp__ddmultlo
  1705. #undef stbsp__ddmultlos
  1706. #undef STBSP__SPECIAL
  1707. #undef STBSP__COPYFP
  1708. #endif // STB_SPRINTF_NOFLOAT
  1709. // clean up
  1710. #undef stbsp__uint16
  1711. #undef stbsp__uint32
  1712. #undef stbsp__int32
  1713. #undef stbsp__uint64
  1714. #undef stbsp__int64
  1715. #undef STBSP__UNALIGNED
  1716. #endif // STB_SPRINTF_IMPLEMENTATION
  1717. /*
  1718. ------------------------------------------------------------------------------
  1719. This software is available under 2 licenses -- choose whichever you prefer.
  1720. ------------------------------------------------------------------------------
  1721. ALTERNATIVE A - MIT License
  1722. Copyright (c) 2017 Sean Barrett
  1723. Permission is hereby granted, free of charge, to any person obtaining a copy of
  1724. this software and associated documentation files (the "Software"), to deal in
  1725. the Software without restriction, including without limitation the rights to
  1726. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  1727. of the Software, and to permit persons to whom the Software is furnished to do
  1728. so, subject to the following conditions:
  1729. The above copyright notice and this permission notice shall be included in all
  1730. copies or substantial portions of the Software.
  1731. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1732. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1733. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1734. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1735. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1736. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1737. SOFTWARE.
  1738. ------------------------------------------------------------------------------
  1739. ALTERNATIVE B - Public Domain (www.unlicense.org)
  1740. This is free and unencumbered software released into the public domain.
  1741. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  1742. software, either in source code form or as a compiled binary, for any purpose,
  1743. commercial or non-commercial, and by any means.
  1744. In jurisdictions that recognize copyright laws, the author or authors of this
  1745. software dedicate any and all copyright interest in the software to the public
  1746. domain. We make this dedication for the benefit of the public at large and to
  1747. the detriment of our heirs and successors. We intend this dedication to be an
  1748. overt act of relinquishment in perpetuity of all present and future rights to
  1749. this software under copyright law.
  1750. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1751. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1752. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1753. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1754. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  1755. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  1756. ------------------------------------------------------------------------------
  1757. */