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.

1812 lines
54 KiB

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