Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1833 lines
54 KiB

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