/* stb.h - v2.32 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h1no warranty is offered or implied; use this code at your own risk23This is a single header file with a bunch of useful utilities4for getting stuff done in C/C++.56Documentation: http://nothings.org/stb/stb_h.html7Unit tests: http://nothings.org/stb/stb.c8910============================================================================11You MUST1213#define STB_DEFINE1415in EXACTLY _one_ C or C++ file that includes this header, BEFORE the16include, like this:1718#define STB_DEFINE19#include "stb.h"2021All other files should just #include "stb.h" without the #define.22============================================================================232425Version History26272.32 stb_intcmprev, stb_uidict, fix random numbers on Linux282.31 stb_ucharcmp292.30 MinGW fix302.29 attempt to fix use of swprintf()312.28 various new functionality322.27 test _WIN32 not WIN32 in STB_THREADS332.26 various warning & bugfixes342.25 various warning & bugfixes352.24 various warning & bugfixes362.23 fix 2.22372.22 64-bit fixes from '!='; fix stb_sdict_copy() to have preferred name382.21 utf-8 decoder rejects "overlong" encodings; attempted 64-bit improvements392.20 fix to hash "copy" function--reported by someone with handle "!="402.19 ???412.18 stb_readdir_subdirs_mask422.17 stb_cfg_dir432.16 fix stb_bgio_, add stb_bgio_stat(); begin a streaming wrapper442.15 upgraded hash table template to allow:45- aggregate keys (explicit comparison func for EMPTY and DEL keys)46- "static" implementations (so they can be culled if unused)472.14 stb_mprintf482.13 reduce identifiable strings in STB_NO_STB_STRINGS492.12 fix STB_ONLY -- lots of uint32s, TRUE/FALSE things had crept in502.11 fix bug in stb_dirtree_get() which caused "c://path" sorts of stuff512.10 STB_F(), STB_I() inline constants (also KI,KU,KF,KD)522.09 stb_box_face_vertex_axis_side532.08 bugfix stb_trimwhite()542.07 colored printing in windows (why are we in 1985?)552.06 comparison functions are now functions-that-return-functions and56accept a struct-offset as a parameter (not thread-safe)572.05 compile and pass tests under Linux (but no threads); thread cleanup582.04 stb_cubic_bezier_1d, smoothstep, avoid dependency on registry592.03 ?602.02 remove integrated documentation612.01 integrate various fixes; stb_force_uniprocessor622.00 revised stb_dupe to use multiple hashes631.99 stb_charcmp641.98 stb_arr_deleten, stb_arr_insertn651.97 fix stb_newell_normal()661.96 stb_hash_number()671.95 hack stb__rec_max; clean up recursion code to use new functions681.94 stb_dirtree; rename stb_extra to stb_ptrmap691.93 stb_sem_new() API cleanup (no blockflag-starts blocked; use 'extra')701.92 stb_threadqueue--multi reader/writer queue, fixed size or resizeable711.91 stb_bgio_* for reading disk asynchronously721.90 stb_mutex uses CRITICAL_REGION; new stb_sync primitive for thread73joining; workqueue supports stb_sync instead of stb_semaphore741.89 support ';' in constant-string wildcards; stb_mutex wrapper (can75implement with EnterCriticalRegion eventually)761.88 portable threading API (only for win32 so far); worker thread queue771.87 fix wildcard handling in stb_readdir_recursive781.86 support ';' in wildcards791.85 make stb_regex work with non-constant strings;80beginnings of stb_introspect()811.84 (forgot to make notes)821.83 whoops, stb_keep_if_different wasn't deleting the temp file831.82 bring back stb_compress from stb_file.h for cmirror841.81 various bugfixes, STB_FASTMALLOC_INIT inits FASTMALLOC in release851.80 stb_readdir returns utf8; write own utf8-utf16 because lib was wrong861.79 stb_write871.78 calloc() support for malloc wrapper, STB_FASTMALLOC881.77 STB_FASTMALLOC891.76 STB_STUA - Lua-like language; (stb_image, stb_csample, stb_bilinear)901.75 alloc/free array of blocks; stb_hheap bug; a few stb_ps_ funcs;91hash*getkey, hash*copy; stb_bitset; stb_strnicmp; bugfix stb_bst921.74 stb_replaceinplace; use stdlib C function to convert utf8 to UTF-16931.73 fix performance bug & leak in stb_ischar (C++ port lost a 'static')941.72 remove stb_block, stb_block_manager, stb_decompress (to stb_file.h)951.71 stb_trimwhite, stb_tokens_nested, etc.961.70 back out 1.69 because it might problemize mixed builds; stb_filec()971.69 (stb_file returns 'char *' in C++)981.68 add a special 'tree root' data type for stb_bst; stb_arr_end991.67 full C++ port. (stb_block_manager)1001.66 stb_newell_normal1011.65 stb_lex_item_wild -- allow wildcard items which MUST match entirely1021.64 stb_data1031.63 stb_log_name1041.62 stb_define_sort; C++ cleanup1051.61 stb_hash_fast -- Paul Hsieh's hash function (beats Bob Jenkins'?)1061.60 stb_delete_directory_recursive1071.59 stb_readdir_recursive1081.58 stb_bst variant with parent pointer for O(1) iteration, not O(log N)1091.57 replace LCG random with Mersenne Twister (found a public domain one)1101.56 stb_perfect_hash, stb_ischar, stb_regex1111.55 new stb_bst API allows multiple BSTs per node (e.g. secondary keys)1121.54 bugfix: stb_define_hash, stb_wildmatch, regexp1131.53 stb_define_hash; recoded stb_extra, stb_sdict use it1141.52 stb_rand_define, stb_bst, stb_reverse1151.51 fix 'stb_arr_setlen(NULL, 0)'1161.50 stb_wordwrap1171.49 minor improvements to enable the scripting language1181.48 better approach for stb_arr using stb_malloc; more invasive, clearer1191.47 stb_lex (lexes stb.h at 1.5ML/s on 3Ghz P4; 60/70% of optimal/flex)1201.46 stb_wrapper_*, STB_MALLOC_WRAPPER1211.45 lightly tested DFA acceleration of regexp searching1221.44 wildcard matching & searching; regexp matching & searching1231.43 stb_temp1241.42 allow stb_arr to use stb_malloc/realloc; note this is global1251.41 make it compile in C++; (disable stb_arr in C++)1261.40 stb_dupe tweak; stb_swap; stb_substr1271.39 stb_dupe; improve stb_file_max to be less stupid1281.38 stb_sha1_file: generate sha1 for file, even > 4GB1291.37 stb_file_max; partial support for utf8 filenames in Windows1301.36 remove STB__NO_PREFIX - poor interaction with IDE, not worth it131streamline stb_arr to make it separately publishable1321.35 bugfixes for stb_sdict, stb_malloc(0), stristr1331.34 (streaming interfaces for stb_compress)1341.33 stb_alloc; bug in stb_getopt; remove stb_overflow1351.32 (stb_compress returns, smaller&faster; encode window & 64-bit len)1361.31 stb_prefix_count1371.30 (STB__NO_PREFIX - remove stb_ prefixes for personal projects)1381.29 stb_fput_varlen64, etc.1391.28 stb_sha11401.27 ?1411.26 stb_extra1421.25 ?1431.24 stb_copyfile1441.23 stb_readdir1451.22 ?1461.21 ?1471.20 ?1481.19 ?1491.18 ?1501.17 ?1511.16 ?1521.15 stb_fixpath, stb_splitpath, stb_strchr21531.14 stb_arr1541.13 ?stb, stb_log, stb_fatal1551.12 ?stb_hash21561.11 miniML1571.10 stb_crc32, stb_adler321581.09 stb_sdict1591.08 stb_bitreverse, stb_ispow2, stb_big32160stb_fopen, stb_fput_varlen, stb_fput_ranged161stb_fcmp, stb_feq1621.07 (stb_encompress)1631.06 stb_compress1641.05 stb_tokens, (stb_hheap)1651.04 stb_rand1661.03 ?(s-strings)1671.02 ?stb_filelen, stb_tokens1681.01 stb_tolower1691.00 stb_hash, stb_intcmp170stb_file, stb_stringfile, stb_fgets171stb_prefix, stb_strlower, stb_strtok172stb_image173(stb_array), (stb_arena)174175Parenthesized items have since been removed.176177LICENSE178179See end of file for license information.180181CREDITS182183Written by Sean Barrett.184185Fixes:186Philipp Wiesemann187Robert Nix188r-lyeh189blackpawn190github:Mojofreem191Ryan Whitworth192Vincent Isambart193Mike Sartain194Eugene Opalev195Tim Sjostrand196github:infatum197Dave Butler (Croepha)198*/199200#ifndef STB__INCLUDE_STB_H201#define STB__INCLUDE_STB_H202203#define STB_VERSION 1204205/* In addition to trimming out all the stuff FAudio does not use, we are also206* binding various stdlib functions stb.h uses to FAudio's stdlib.207* -flibit208*/209#ifdef STB_MEMCPY_OVERRIDE210#ifdef memcpy /* Thanks Apple! */211#undef memcpy212#endif213#define memcpy FAudio_memcpy214#endif215216//////////////////////////////////////////////////////////////////////////////217//218// Miscellany219//220221STB_EXTERN void stb_swap(void *p, void *q, size_t sz);222223#ifdef STB_DEFINE224typedef struct { char d[4]; } stb__4;225typedef struct { char d[8]; } stb__8;226227// optimize the small cases, though you shouldn't be calling this for those!228void stb_swap(void *p, void *q, size_t sz)229{230char buffer[256];231if (p == q) return;232if (sz == 4) {233stb__4 temp = * ( stb__4 *) p;234* (stb__4 *) p = * ( stb__4 *) q;235* (stb__4 *) q = temp;236return;237} else if (sz == 8) {238stb__8 temp = * ( stb__8 *) p;239* (stb__8 *) p = * ( stb__8 *) q;240* (stb__8 *) q = temp;241return;242}243244while (sz > sizeof(buffer)) {245stb_swap(p, q, sizeof(buffer));246p = (char *) p + sizeof(buffer);247q = (char *) q + sizeof(buffer);248sz -= sizeof(buffer);249}250251memcpy(buffer, p , sz);252memcpy(p , q , sz);253memcpy(q , buffer, sz);254}255#endif256257//////////////////////////////////////////////////////////////////////////////258//259// Random Numbers via Meresenne Twister or LCG260//261262STB_EXTERN unsigned int stb_srandLCG(unsigned int seed);263STB_EXTERN unsigned int stb_randLCG(void);264265STB_EXTERN void stb_srand(unsigned int seed);266STB_EXTERN unsigned int stb_rand(void);267STB_EXTERN double stb_frand(void);268269#define stb_rand_define(x,y) \270\271unsigned int x(void) \272{ \273static unsigned int stb__rand = y; \274stb__rand = stb__rand * 2147001325 + 715136305; /* BCPL */ \275return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16)); \276}277278#ifdef STB_DEFINE279static unsigned int stb__rand_seed=0;280281unsigned int stb_srandLCG(unsigned int seed)282{283unsigned int previous = stb__rand_seed;284stb__rand_seed = seed;285return previous;286}287288unsigned int stb_randLCG(void)289{290stb__rand_seed = stb__rand_seed * 2147001325 + 715136305; // BCPL generator291// shuffle non-random bits to the middle, and xor to decorrelate with seed292return 0x31415926 ^ ((stb__rand_seed >> 16) + (stb__rand_seed << 16));293}294295// public domain Mersenne Twister by Michael Brundage296#define STB__MT_LEN 624297298int stb__mt_index = STB__MT_LEN*sizeof(int)+1;299unsigned int stb__mt_buffer[STB__MT_LEN];300301void stb_srand(unsigned int seed)302{303int i;304unsigned int old = stb_srandLCG(seed);305for (i = 0; i < STB__MT_LEN; i++)306stb__mt_buffer[i] = stb_randLCG();307stb_srandLCG(old);308stb__mt_index = STB__MT_LEN*sizeof(unsigned int);309}310311#define STB__MT_IA 397312#define STB__MT_IB (STB__MT_LEN - STB__MT_IA)313#define STB__UPPER_MASK 0x80000000314#define STB__LOWER_MASK 0x7FFFFFFF315#define STB__MATRIX_A 0x9908B0DF316#define STB__TWIST(b,i,j) ((b)[i] & STB__UPPER_MASK) | ((b)[j] & STB__LOWER_MASK)317#define STB__MAGIC(s) (((s)&1)*STB__MATRIX_A)318319unsigned int stb_rand()320{321unsigned int * b = stb__mt_buffer;322int idx = stb__mt_index;323unsigned int s,r;324int i;325326if (idx >= STB__MT_LEN*sizeof(unsigned int)) {327if (idx > STB__MT_LEN*sizeof(unsigned int))328stb_srand(0);329idx = 0;330i = 0;331for (; i < STB__MT_IB; i++) {332s = STB__TWIST(b, i, i+1);333b[i] = b[i + STB__MT_IA] ^ (s >> 1) ^ STB__MAGIC(s);334}335for (; i < STB__MT_LEN-1; i++) {336s = STB__TWIST(b, i, i+1);337b[i] = b[i - STB__MT_IB] ^ (s >> 1) ^ STB__MAGIC(s);338}339340s = STB__TWIST(b, STB__MT_LEN-1, 0);341b[STB__MT_LEN-1] = b[STB__MT_IA-1] ^ (s >> 1) ^ STB__MAGIC(s);342}343stb__mt_index = idx + sizeof(unsigned int);344345r = *(unsigned int *)((unsigned char *)b + idx);346347r ^= (r >> 11);348r ^= (r << 7) & 0x9D2C5680;349r ^= (r << 15) & 0xEFC60000;350r ^= (r >> 18);351352return r;353}354355double stb_frand(void)356{357return stb_rand() / ((double) (1 << 16) * (1 << 16));358}359360#endif361362#undef STB_EXTERN363#endif // STB_INCLUDE_STB_H364365/*366------------------------------------------------------------------------------367This software is available under 2 licenses -- choose whichever you prefer.368------------------------------------------------------------------------------369ALTERNATIVE A - MIT License370Copyright (c) 2017 Sean Barrett371Permission is hereby granted, free of charge, to any person obtaining a copy of372this software and associated documentation files (the "Software"), to deal in373the Software without restriction, including without limitation the rights to374use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies375of the Software, and to permit persons to whom the Software is furnished to do376so, subject to the following conditions:377The above copyright notice and this permission notice shall be included in all378copies or substantial portions of the Software.379THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR380IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,381FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE382AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER383LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,384OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE385SOFTWARE.386------------------------------------------------------------------------------387ALTERNATIVE B - Public Domain (www.unlicense.org)388This is free and unencumbered software released into the public domain.389Anyone is free to copy, modify, publish, use, compile, sell, or distribute this390software, either in source code form or as a compiled binary, for any purpose,391commercial or non-commercial, and by any means.392In jurisdictions that recognize copyright laws, the author or authors of this393software dedicate any and all copyright interest in the software to the public394domain. We make this dedication for the benefit of the public at large and to395the detriment of our heirs and successors. We intend this dedication to be an396overt act of relinquishment in perpetuity of all present and future rights to397this software under copyright law.398THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR399IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,400FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE401AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN402ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION403WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.404------------------------------------------------------------------------------405*/406407408