stb_sprintf.h 57 KB

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