Skip to content
Myr

stb_lib.h

This file

Path

tests/prerelease/stb_lib.h

3,305 lines · 104.3 KiB · C

Last changed by

Not known.

History of this file

Take it

1/* stb_lib.h - v1.00 - http://nothings.org/stb2   no warranty is offered or implied; use this code at your own risk34 ============================================================================5   You MUST                                                                  6                                                                             7      #define STB_LIB_IMPLEMENTATION8                                                                             9   in EXACTLY _one_ C or C++ file that includes this header, BEFORE the10   include, like this:                                                                11                                                                             12      #define STB_LIB_IMPLEMENTATION13      #include "stblib_files.h"14      15   All other files should just #include "stblib_files.h" without the #define.16 ============================================================================1718LICENSE1920 See end of file for license information.2122CREDITS2324 Written by Sean Barrett.2526 Fixes:27  Philipp Wiesemann    Robert Nix28  r-lyeh               blackpawn29  github:Mojofreem     Ryan Whitworth30  Vincent Isambart     Mike Sartain31  Eugene Opalev        Tim Sjostrand32  github:infatum       Dave Butler33*/3435#ifndef STB_INCLUDE_STB_LIB_H3637#include <stdarg.h>3839#if defined(_WIN32) && !defined(__MINGW32__)40   #ifndef _CRT_SECURE_NO_WARNINGS41   #define _CRT_SECURE_NO_WARNINGS42   #endif43   #ifndef _CRT_NONSTDC_NO_DEPRECATE44   #define _CRT_NONSTDC_NO_DEPRECATE45   #endif46   #ifndef _CRT_NON_CONFORMING_SWPRINTFS47   #define _CRT_NON_CONFORMING_SWPRINTFS48   #endif49   #if !defined(_MSC_VER) || _MSC_VER > 170050   #include <intrin.h> // _BitScanReverse51   #endif52#endif5354#include <stdlib.h>     // stdlib could have min/max55#include <stdio.h>      // need FILE56#include <string.h>     // stb_define_hash needs memcpy/memset57#include <time.h>       // stb_dirtree5859typedef unsigned char stb_uchar;60typedef unsigned char stb_uint8;61typedef unsigned int  stb_uint;62typedef unsigned short stb_uint16;63typedef          short stb_int16;64typedef   signed char  stb_int8;65#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)66  typedef unsigned long  stb_uint32;67  typedef          long  stb_int32;68#else69  typedef unsigned int   stb_uint32;70  typedef          int   stb_int32;71#endif72typedef char stb__testsize2_16[sizeof(stb_uint16)==2 ? 1 : -1];73typedef char stb__testsize2_32[sizeof(stb_uint32)==4 ? 1 : -1];7475#ifdef _MSC_VER76  typedef unsigned __int64 stb_uint64;77  typedef          __int64 stb_int64;78  #define STB_IMM_UINT64(literalui64) (literalui64##ui64)79#else80  // ??81  typedef unsigned long long stb_uint64;82  typedef          long long stb_int64;83  #define STB_IMM_UINT64(literalui64) (literalui64##ULL)84#endif85typedef char stb__testsize2_64[sizeof(stb_uint64)==8 ? 1 : -1];8687#ifdef __cplusplus88   #define STB_EXTERN   extern "C"89#else90   #define STB_EXTERN   extern91#endif9293// check for well-known debug defines94#if defined(DEBUG) || defined(_DEBUG) || defined(DBG)95   #ifndef NDEBUG96      #define STB_DEBUG97   #endif98#endif99100#ifdef STB_DEBUG101   #include <assert.h>102#endif103#endif // STB_INCLUDE_STB_LIB_H104105#ifdef STB_LIB_IMPLEMENTATION106   #include <assert.h>107   #include <stdarg.h>108   #include <stddef.h>109   #include <ctype.h>110   #include <math.h>111   #ifndef _WIN32112   #include <unistd.h>113   #else114   #include <io.h>      // _mktemp115   #include <direct.h>  // _rmdir116   #endif117   #include <sys/types.h> // stat()/_stat()118   #include <sys/stat.h>  // stat()/_stat()119#endif120121//////////////////////////////////////////////////////////////////////////////122//123//                         Miscellany124//125126#ifdef _WIN32127   #define stb_stricmp(a,b)    stricmp(a,b)128   #define stb_strnicmp(a,b,n) strnicmp(a,b,n)129#else130   #define stb_stricmp(a,b)    strcasecmp(a,b)131   #define stb_strnicmp(a,b,n) strncasecmp(a,b,n)132#endif133134#ifndef STB_INCLUDE_STB_LIB_H135STB_EXTERN void stb_fatal(char *fmt, ...);136STB_EXTERN void stb_swap(void *p, void *q, size_t sz);137STB_EXTERN double stb_linear_remap(double x, double x_min, double x_max,138                                  double out_min, double out_max);139140#define stb_arrcount(x)   (sizeof(x)/sizeof((x)[0]))141#define stb_lerp(t,a,b)               ( (a) + (t) * (float) ((b)-(a)) )142#define stb_unlerp(t,a,b)             ( ((t) - (a)) / (float) ((b) - (a)) )143144#endif145146147#ifdef STB_LIB_IMPLEMENTATION148void stb_fatal(char *s, ...)149{150   va_list a;151   va_start(a,s);152   fputs("Fatal error: ", stderr);153   vfprintf(stderr, s, a);154   va_end(a);155   fputs("\n", stderr);156   #ifdef STB_DEBUG157   #ifdef _MSC_VER158   #ifndef _WIN64159   __asm int 3;   // trap to debugger!160   #else161   __debugbreak();162   #endif163   #else164   __builtin_trap();165   #endif166   #endif167   exit(1);168}169170typedef struct { char d[4]; } stb__4;171typedef struct { char d[8]; } stb__8;172173// optimize the small cases, though you shouldn't be calling this for those!174void stb_swap(void *p, void *q, size_t sz)175{176   char buffer[256];177   if (p == q) return;178   if (sz == 4) {179      stb__4 temp    = * ( stb__4 *) p;180      * (stb__4 *) p = * ( stb__4 *) q;181      * (stb__4 *) q = temp;182      return;183   } else if (sz == 8) {184      stb__8 temp    = * ( stb__8 *) p;185      * (stb__8 *) p = * ( stb__8 *) q;186      * (stb__8 *) q = temp;187      return;188   }189190   while (sz > sizeof(buffer)) {191      stb_swap(p, q, sizeof(buffer));192      p = (char *) p + sizeof(buffer);193      q = (char *) q + sizeof(buffer);194      sz -= sizeof(buffer);195   }196197   memcpy(buffer, p     , sz);198   memcpy(p     , q     , sz);199   memcpy(q     , buffer, sz);200}201202#ifdef stb_linear_remap203#undef stb_linear_remap204#endif205206double stb_linear_remap(double x, double x_min, double x_max,207                                  double out_min, double out_max)208{209   return stb_lerp(stb_unlerp(x,x_min,x_max),out_min,out_max);210}211212#define stb_linear_remap(t,a,b,c,d)   stb_lerp(stb_unlerp(t,a,b),c,d)213#endif // STB_LIB_IMPLEMENTATION214215#ifndef STB_INCLUDE_STB_LIB_H216// avoid unnecessary function call, but define function so its address can be taken217#ifndef stb_linear_remap218#define stb_linear_remap(t,a,b,c,d)   stb_lerp(stb_unlerp(t,a,b),c,d)219#endif220#endif221222//////////////////////////////////////////////////////////////////////////////223//224//     cross-platform snprintf because they keep changing that,225//     and with old compilers without vararg macros we can't write226//     a macro wrapper to fix it up227228#ifndef STB_INCLUDE_STB_LIB_H229STB_EXTERN int  stb_snprintf(char *s, size_t n, const char *fmt, ...);230STB_EXTERN int  stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v);231STB_EXTERN char *stb_sprintf(const char *fmt, ...);232#endif233234#ifdef STB_LIB_IMPLEMENTATION235236int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v)237{238   int res;239   #ifdef _WIN32240   // Could use "_vsnprintf_s(s, n, _TRUNCATE, fmt, v)" ?241   res = _vsnprintf(s,n,fmt,v);242   #else243   res = vsnprintf(s,n,fmt,v);244   #endif245   if (n) s[n-1] = 0;246   // Unix returns length output would require, Windows returns negative when truncated.247   return (res >= (int) n || res < 0) ? -1 : res;248}249250int stb_snprintf(char *s, size_t n, const char *fmt, ...)251{252   int res;253   va_list v;254   va_start(v,fmt);255   res = stb_vsnprintf(s, n, fmt, v);256   va_end(v);257   return res;258}259260char *stb_sprintf(const char *fmt, ...)261{262   static char buffer[1024];263   va_list v;264   va_start(v,fmt);265   stb_vsnprintf(buffer,1024,fmt,v);266   va_end(v);267   return buffer;268}269#endif270271//////////////////////////////////////////////////////////////////////////////272//273//                         Windows UTF8 filename handling274//275// Windows stupidly treats 8-bit filenames as some dopey code page,276// rather than utf-8. If we want to use utf8 filenames, we have to277// convert them to WCHAR explicitly and call WCHAR versions of the278// file functions. So, ok, we do.279280281#ifndef STB_INCLUDE_STB_LIB_H282#ifdef _WIN32283   #define stb__fopen(x,y)    _wfopen((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))284   #define stb__windows(x,y)  x285#else286   #define stb__fopen(x,y)    fopen(x,y)287   #define stb__windows(x,y)  y288#endif289290291typedef unsigned short stb__wchar;292293STB_EXTERN stb__wchar * stb_from_utf8(stb__wchar *buffer, char *str, int n);294STB_EXTERN char       * stb_to_utf8  (char *buffer, stb__wchar *str, int n);295296STB_EXTERN stb__wchar *stb__from_utf8(char *str);297STB_EXTERN stb__wchar *stb__from_utf8_alt(char *str);298STB_EXTERN char *stb__to_utf8(stb__wchar *str);299#endif300301#ifdef STB_LIB_IMPLEMENTATION302stb__wchar * stb_from_utf8(stb__wchar *buffer, char *ostr, int n)303{304   unsigned char *str = (unsigned char *) ostr;305   stb_uint32 c;306   int i=0;307   --n;308   while (*str) {309      if (i >= n)310         return NULL;311      if (!(*str & 0x80))312         buffer[i++] = *str++;313      else if ((*str & 0xe0) == 0xc0) {314         if (*str < 0xc2) return NULL;315         c = (*str++ & 0x1f) << 6;316         if ((*str & 0xc0) != 0x80) return NULL;317         buffer[i++] = c + (*str++ & 0x3f);318      } else if ((*str & 0xf0) == 0xe0) {319         if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return NULL;320         if (*str == 0xed && str[1] > 0x9f) return NULL; // str[1] < 0x80 is checked below321         c = (*str++ & 0x0f) << 12;322         if ((*str & 0xc0) != 0x80) return NULL;323         c += (*str++ & 0x3f) << 6;324         if ((*str & 0xc0) != 0x80) return NULL;325         buffer[i++] = c + (*str++ & 0x3f);326      } else if ((*str & 0xf8) == 0xf0) {327         if (*str > 0xf4) return NULL;328         if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return NULL;329         if (*str == 0xf4 && str[1] > 0x8f) return NULL; // str[1] < 0x80 is checked below330         c = (*str++ & 0x07) << 18;331         if ((*str & 0xc0) != 0x80) return NULL;332         c += (*str++ & 0x3f) << 12;333         if ((*str & 0xc0) != 0x80) return NULL;334         c += (*str++ & 0x3f) << 6;335         if ((*str & 0xc0) != 0x80) return NULL;336         c += (*str++ & 0x3f);337         // utf-8 encodings of values used in surrogate pairs are invalid338         if ((c & 0xFFFFF800) == 0xD800) return NULL;339         if (c >= 0x10000) {340            c -= 0x10000;341            if (i + 2 > n) return NULL;342            buffer[i++] = 0xD800 | (0x3ff & (c >> 10));343            buffer[i++] = 0xDC00 | (0x3ff & (c      ));344         }345      } else346         return NULL;347   }348   buffer[i] = 0;349   return buffer;350}351352char * stb_to_utf8(char *buffer, stb__wchar *str, int n)353{354   int i=0;355   --n;356   while (*str) {357      if (*str < 0x80) {358         if (i+1 > n) return NULL;359         buffer[i++] = (char) *str++;360      } else if (*str < 0x800) {361         if (i+2 > n) return NULL;362         buffer[i++] = 0xc0 + (*str >> 6);363         buffer[i++] = 0x80 + (*str & 0x3f);364         str += 1;365      } else if (*str >= 0xd800 && *str < 0xdc00) {366         stb_uint32 c;367         if (i+4 > n) return NULL;368         c = ((str[0] - 0xd800) << 10) + ((str[1]) - 0xdc00) + 0x10000;369         buffer[i++] = 0xf0 + (c >> 18);370         buffer[i++] = 0x80 + ((c >> 12) & 0x3f);371         buffer[i++] = 0x80 + ((c >>  6) & 0x3f);372         buffer[i++] = 0x80 + ((c      ) & 0x3f);373         str += 2;374      } else if (*str >= 0xdc00 && *str < 0xe000) {375         return NULL;376      } else {377         if (i+3 > n) return NULL;378         buffer[i++] = 0xe0 + (*str >> 12);379         buffer[i++] = 0x80 + ((*str >> 6) & 0x3f);380         buffer[i++] = 0x80 + ((*str     ) & 0x3f);381         str += 1;382      }383   }384   buffer[i] = 0;385   return buffer;386}387388stb__wchar *stb__from_utf8(char *str)389{390   static stb__wchar buffer[4096];391   return stb_from_utf8(buffer, str, 4096);392}393394stb__wchar *stb__from_utf8_alt(char *str)395{396   static stb__wchar buffer[4096];397   return stb_from_utf8(buffer, str, 4096);398}399400char *stb__to_utf8(stb__wchar *str)401{402   static char buffer[4096];403   return stb_to_utf8(buffer, str, 4096);404}405#endif406407//////////////////////////////////////////////////////////////////////////////408//409//                            qsort Compare Routines410//                              NOT THREAD SAFE411412#ifndef STB_INCLUDE_STB_LIB_H413STB_EXTERN int (*stb_intcmp(int offset))(const void *a, const void *b);414STB_EXTERN int (*stb_qsort_strcmp(int offset))(const void *a, const void *b);415STB_EXTERN int (*stb_qsort_stricmp(int offset))(const void *a, const void *b);416STB_EXTERN int (*stb_floatcmp(int offset))(const void *a, const void *b);417STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);418STB_EXTERN int (*stb_ucharcmp(int offset))(const void *a, const void *b);419STB_EXTERN int (*stb_charcmp(int offset))(const void *a, const void *b);420#endif421422#ifdef STB_LIB_IMPLEMENTATION423static int stb__intcmpoffset, stb__ucharcmpoffset, stb__strcmpoffset;424static int stb__floatcmpoffset, stb__doublecmpoffset, stb__charcmpoffset;425426int stb__intcmp(const void *a, const void *b)427{428   const int p = *(const int *) ((const char *) a + stb__intcmpoffset);429   const int q = *(const int *) ((const char *) b + stb__intcmpoffset);430   return p < q ? -1 : p > q;431}432433int stb__ucharcmp(const void *a, const void *b)434{435   const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);436   const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);437   return p < q ? -1 : p > q;438}439440int stb__charcmp(const void *a, const void *b)441{442   const int p = *(const char *) ((const char *) a + stb__ucharcmpoffset);443   const int q = *(const char *) ((const char *) b + stb__ucharcmpoffset);444   return p < q ? -1 : p > q;445}446447int stb__floatcmp(const void *a, const void *b)448{449   const float p = *(const float *) ((const char *) a + stb__floatcmpoffset);450   const float q = *(const float *) ((const char *) b + stb__floatcmpoffset);451   return p < q ? -1 : p > q;452}453454int stb__doublecmp(const void *a, const void *b)455{456   const double p = *(const double *) ((const char *) a + stb__doublecmpoffset);457   const double q = *(const double *) ((const char *) b + stb__doublecmpoffset);458   return p < q ? -1 : p > q;459}460461int stb__qsort_strcmp(const void *a, const void *b)462{463   const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);464   const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);465   return strcmp(p,q);466}467468int stb__qsort_stricmp(const void *a, const void *b)469{470   const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);471   const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);472   return stb_stricmp(p,q);473}474475int (*stb_intcmp(int offset))(const void *, const void *)476{477   stb__intcmpoffset = offset;478   return &stb__intcmp;479}480481int (*stb_ucharcmp(int offset))(const void *, const void *)482{483   stb__ucharcmpoffset = offset;484   return &stb__ucharcmp;485}486487int (*stb_charcmp(int offset))(const void *, const void *)488{489   stb__charcmpoffset = offset;490   return &stb__ucharcmp;491}492493int (*stb_qsort_strcmp(int offset))(const void *, const void *)494{495   stb__strcmpoffset = offset;496   return &stb__qsort_strcmp;497}498499int (*stb_qsort_stricmp(int offset))(const void *, const void *)500{501   stb__strcmpoffset = offset;502   return &stb__qsort_stricmp;503}504505int (*stb_floatcmp(int offset))(const void *, const void *)506{507   stb__floatcmpoffset = offset;508   return &stb__floatcmp;509}510511int (*stb_doublecmp(int offset))(const void *, const void *)512{513   stb__doublecmpoffset = offset;514   return &stb__doublecmp;515}516#endif517518//////////////////////////////////////////////////////////////////////////////519//520//                           String Processing521//522523#ifndef STB_INCLUDE_STB_LIB_H524#define stb_prefixi(s,t)  (0==stb_strnicmp((s),(t),strlen(t)))525526enum stb_splitpath_flag527{528   STB_PATH = 1,529   STB_FILE = 2,530   STB_EXT  = 4,531   STB_PATH_FILE = STB_PATH + STB_FILE,532   STB_FILE_EXT  = STB_FILE + STB_EXT,533   STB_EXT_NO_PERIOD = 8,534};535536STB_EXTERN char * stb_skipwhite(char *s);537STB_EXTERN char * stb_trimwhite(char *s);538STB_EXTERN char * stb_skipnewline(char *s);539STB_EXTERN char * stb_strncpy(char *s, char *t, int n);540STB_EXTERN char * stb_substr(char *t, int n);541STB_EXTERN char * stb_duplower(char *s);542STB_EXTERN void   stb_tolower (char *s);543STB_EXTERN char * stb_strchr2 (char *s, char p1, char p2);544STB_EXTERN char * stb_strrchr2(char *s, char p1, char p2);545STB_EXTERN char * stb_strtok(char *output, char *src, char *delimit);546STB_EXTERN char * stb_strtok_keep(char *output, char *src, char *delimit);547STB_EXTERN char * stb_strtok_invert(char *output, char *src, char *allowed);548STB_EXTERN char * stb_dupreplace(char *s, char *find, char *replace);549STB_EXTERN void   stb_replaceinplace(char *s, char *find, char *replace);550STB_EXTERN char * stb_splitpath(char *output, char *src, int flag);551STB_EXTERN char * stb_splitpathdup(char *src, int flag);552STB_EXTERN char * stb_replacedir(char *output, char *src, char *dir);553STB_EXTERN char * stb_replaceext(char *output, char *src, char *ext);554STB_EXTERN void   stb_fixpath(char *path);555STB_EXTERN char * stb_shorten_path_readable(char *path, int max_len);556STB_EXTERN int    stb_suffix (char *s, char *t);557STB_EXTERN int    stb_suffixi(char *s, char *t);558STB_EXTERN int    stb_prefix (char *s, char *t);559STB_EXTERN char * stb_strichr(char *s, char t);560STB_EXTERN char * stb_stristr(char *s, char *t);561STB_EXTERN int    stb_prefix_count(char *s, char *t);562STB_EXTERN const char * stb_plural(int n);  // "s" or ""563STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n);564565STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count);566STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out);567STB_EXTERN char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out);568STB_EXTERN char **stb_tokens_allowempty(char *src, char *delimit, int *count);569STB_EXTERN char **stb_tokens_stripwhite(char *src, char *delimit, int *count);570STB_EXTERN char **stb_tokens_withdelim(char *src, char *delimit, int *count);571STB_EXTERN char **stb_tokens_quoted(char *src, char *delimit, int *count);572// with 'quoted', allow delimiters to appear inside quotation marks, and don't573// strip whitespace inside them (and we delete the quotation marks unless they574// appear back to back, in which case they're considered escaped)575#endif // STB_INCLUDE_STB_LIB_H576577#ifdef STB_LIB_IMPLEMENTATION578#include <ctype.h>579580size_t stb_strscpy(char *d, const char *s, size_t n)581{582   size_t len = strlen(s);583   if (len >= n) {584      if (n) d[0] = 0;585      return 0;586   }587   strcpy(d,s);588   return len + 1;589}590591const char *stb_plural(int n)592{593   return n == 1 ? "" : "s";594}595596int stb_prefix(char *s, char *t)597{598   while (*t)599      if (*s++ != *t++)600         return 0;601   return 1;602}603604int stb_prefix_count(char *s, char *t)605{606   int c=0;607   while (*t) {608      if (*s++ != *t++)609         break;610      ++c;611   }612   return c;613}614615int stb_suffix(char *s, char *t)616{617   size_t n = strlen(s);618   size_t m = strlen(t);619   if (m <= n)620      return 0 == strcmp(s+n-m, t);621   else622      return 0;623}624625int stb_suffixi(char *s, char *t)626{627   size_t n = strlen(s);628   size_t m = strlen(t);629   if (m <= n)630      return 0 == stb_stricmp(s+n-m, t);631   else632      return 0;633}634635// originally I was using this table so that I could create known sentinel636// values--e.g. change whitetable[0] to be true if I was scanning for whitespace,637// and false if I was scanning for nonwhite. I don't appear to be using that638// functionality anymore (I do for tokentable, though), so just replace it639// with isspace()640char *stb_skipwhite(char *s)641{642   while (isspace((unsigned char) *s)) ++s;643   return s;644}645646char *stb_skipnewline(char *s)647{648   if (s[0] == '\r' || s[0] == '\n') {649      if (s[0]+s[1] == '\r' + '\n') ++s;650      ++s;651   }652   return s;653}654655char *stb_trimwhite(char *s)656{657   int i,n;658   s = stb_skipwhite(s);659   n = (int) strlen(s);660   for (i=n-1; i >= 0; --i)661      if (!isspace(s[i]))662         break;663   s[i+1] = 0;664   return s;665}666667char *stb_strncpy(char *s, char *t, int n)668{669   strncpy(s,t,n);670   s[n-1] = 0;671   return s;672}673674char *stb_substr(char *t, int n)675{676   char *a;677   int z = (int) strlen(t);678   if (z < n) n = z;679   a = (char *) malloc(n+1);680   strncpy(a,t,n);681   a[n] = 0;682   return a;683}684685char *stb_duplower(char *s)686{687   char *p = strdup(s), *q = p;688   while (*q) {689      *q = tolower(*q);690      ++q;691   }692   return p;693}694695void stb_tolower(char *s)696{697   while (*s) {698      *s = tolower(*s);699      ++s;700   }701}702703char *stb_strchr2(char *s, char x, char y)704{705   for(; *s; ++s)706      if (*s == x || *s == y)707         return s;708   return NULL;709}710711char *stb_strrchr2(char *s, char x, char y)712{713   char *r = NULL;714   for(; *s; ++s)715      if (*s == x || *s == y)716         r = s;717   return r;718}719720char *stb_strichr(char *s, char t)721{722   if (tolower(t) == toupper(t))723      return strchr(s,t);724   return stb_strchr2(s, (char) tolower(t), (char) toupper(t));725}726727char *stb_stristr(char *s, char *t)728{729   size_t n = strlen(t);730   char *z;731   if (n==0) return s;732   while ((z = stb_strichr(s, *t)) != NULL) {733      if (0==stb_strnicmp(z, t, n))734         return z;735      s = z+1;736   }737   return NULL;738}739740static char *stb_strtok_raw(char *output, char *src, char *delimit, int keep, int invert)741{742   if (invert) {743      while (*src && strchr(delimit, *src) != NULL) {744         *output++ = *src++;745      }746   } else {747      while (*src && strchr(delimit, *src) == NULL) {748         *output++ = *src++;749      }750   }751   *output = 0;752   if (keep)753      return src;754   else755      return *src ? src+1 : src;756}757758char *stb_strtok(char *output, char *src, char *delimit)759{760   return stb_strtok_raw(output, src, delimit, 0, 0);761}762763char *stb_strtok_keep(char *output, char *src, char *delimit)764{765   return stb_strtok_raw(output, src, delimit, 1, 0);766}767768char *stb_strtok_invert(char *output, char *src, char *delimit)769{770   return stb_strtok_raw(output, src, delimit, 1,1);771}772773static char **stb_tokens_raw(char *src_, char *delimit, int *count,774                             int stripwhite, int allow_empty, char *start, char *end)775{776   int nested = 0;777   unsigned char *src = (unsigned char *) src_;778   static char stb_tokentable[256]; // rely on static initializion to 0779   static char stable[256],etable[256];780   char *out;781   char **result;782   int num=0;783   unsigned char *s;784785   s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 1;786   if (start) {787      s = (unsigned char *) start;         while (*s) stable[*s++] = 1;788      s = (unsigned char *) end;   if (s)  while (*s) stable[*s++] = 1;789      s = (unsigned char *) end;   if (s)  while (*s) etable[*s++] = 1;790   }791   stable[0] = 1;792793   // two passes through: the first time, counting how many794   s = (unsigned char *) src;795   while (*s) {796      // state: just found delimiter797      // skip further delimiters798      if (!allow_empty) {799         stb_tokentable[0] = 0;800         while (stb_tokentable[*s])801            ++s;802         if (!*s) break;803      }804      ++num;805      // skip further non-delimiters806      stb_tokentable[0] = 1;807      if (stripwhite == 2) { // quoted strings808         while (!stb_tokentable[*s]) {809            if (*s != '"')810               ++s;811            else {812               ++s;813               if (*s == '"')814                  ++s;   // "" -> ", not start a string815               else {816                  // begin a string817                  while (*s) {818                     if (s[0] == '"') {819                        if (s[1] == '"') s += 2; // "" -> "820                        else { ++s; break; } // terminating "821                     } else822                        ++s;823                  }824               }825            }826         }827      } else 828         while (nested || !stb_tokentable[*s]) {829            if (stable[*s]) {830               if (!*s) break;831               if (end ? etable[*s] : nested)832                  --nested;833               else834                  ++nested;835            }836            ++s;837         }838      if (allow_empty) {839         if (*s) ++s;840      }841   }842   // now num has the actual count... malloc our output structure843   // need space for all the strings: strings won't be any longer than844   // original input, since for every '\0' there's at least one delimiter845   result = (char **) malloc(sizeof(*result) * (num+1) + (s-src+1));846   if (result == NULL) return result;847   out = (char *) (result + (num+1));848   // second pass: copy out the data849   s = (unsigned char *) src;850   num = 0;851   nested = 0;852   while (*s) {853      char *last_nonwhite;854      // state: just found delimiter855      // skip further delimiters856      if (!allow_empty) {857         stb_tokentable[0] = 0;858         if (stripwhite)859            while (stb_tokentable[*s] || isspace(*s))860               ++s;861         else862            while (stb_tokentable[*s])863               ++s;864      } else if (stripwhite) {865         while (isspace(*s)) ++s;866      }867      if (!*s) break;868      // we're past any leading delimiters and whitespace869      result[num] = out;870      ++num;871      // copy non-delimiters872      stb_tokentable[0] = 1;873      last_nonwhite = out-1;874      if (stripwhite == 2) {875         while (!stb_tokentable[*s]) {876            if (*s != '"') {877               if (!isspace(*s)) last_nonwhite = out;878               *out++ = *s++;879            } else {880               ++s;881               if (*s == '"') {882                  if (!isspace(*s)) last_nonwhite = out;883                  *out++ = *s++; // "" -> ", not start string884               } else {885                  // begin a quoted string886                  while (*s) {887                     if (s[0] == '"') {888                        if (s[1] == '"') { *out++ = *s; s += 2; }889                        else { ++s; break; } // terminating "890                     } else891                        *out++ = *s++;892                  }893                  last_nonwhite = out-1; // all in quotes counts as non-white894               }895            }896         }897      } else {898         while (nested || !stb_tokentable[*s]) {899            if (!isspace(*s)) last_nonwhite = out;900            if (stable[*s]) {901               if (!*s) break;902               if (end ? etable[*s] : nested)903                  --nested;904               else905                  ++nested;906            }907            *out++ = *s++;908         }909      }910911      if (stripwhite) // rewind to last non-whitespace char912         out = last_nonwhite+1;913      *out++ = '\0';914915      if (*s) ++s; // skip delimiter916   }917   s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 0;918   if (start) {919      s = (unsigned char *) start;         while (*s) stable[*s++] = 1;920      s = (unsigned char *) end;   if (s)  while (*s) stable[*s++] = 1;921      s = (unsigned char *) end;   if (s)  while (*s) etable[*s++] = 1;922   }923   if (count != NULL) *count = num;924   result[num] = 0;925   return result;926}927928char **stb_tokens(char *src, char *delimit, int *count)929{930   return stb_tokens_raw(src,delimit,count,0,0,0,0);931}932933char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out)934{935   return stb_tokens_raw(src,delimit,count,0,0,nest_in,nest_out);936}937938char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out)939{940   return stb_tokens_raw(src,delimit,count,0,1,nest_in,nest_out);941}942943char **stb_tokens_allowempty(char *src, char *delimit, int *count)944{945   return stb_tokens_raw(src,delimit,count,0,1,0,0);946}947948char **stb_tokens_stripwhite(char *src, char *delimit, int *count)949{950   return stb_tokens_raw(src,delimit,count,1,1,0,0);951}952953char **stb_tokens_quoted(char *src, char *delimit, int *count)954{955   return stb_tokens_raw(src,delimit,count,2,1,0,0);956}957958char *stb_dupreplace(char *src, char *find, char *replace)959{960   size_t len_find = strlen(find);961   size_t len_replace = strlen(replace);962   int count = 0;963964   char *s,*p,*q;965966   s = strstr(src, find);967   if (s == NULL) return strdup(src);968   do {969      ++count;970      s = strstr(s + len_find, find);971   } while (s != NULL);972973   p = (char *)  malloc(strlen(src) + count * (len_replace - len_find) + 1);974   if (p == NULL) return p;975   q = p;976   s = src;977   for (;;) {978      char *t = strstr(s, find);979      if (t == NULL) {980         strcpy(q,s);981         assert(strlen(p) == strlen(src) + count*(len_replace-len_find));982         return p;983      }984      memcpy(q, s, t-s);985      q += t-s;986      memcpy(q, replace, len_replace);987      q += len_replace;988      s = t + len_find;989   }990}991992void stb_replaceinplace(char *src, char *find, char *replace)993{994   size_t len_find = strlen(find);995   size_t len_replace = strlen(replace);996   int delta;997998   char *s,*p,*q;9991000   delta = len_replace - len_find;1001   assert(delta <= 0);1002   if (delta > 0) return;10031004   p = strstr(src, find);1005   if (p == NULL) return;10061007   s = q = p;1008   while (*s) {1009      memcpy(q, replace, len_replace);1010      p += len_find;1011      q += len_replace;1012      s = strstr(p, find);1013      if (s == NULL) s = p + strlen(p);1014      memmove(q, p, s-p);1015      q += s-p;1016      p = s;1017   }1018   *q = 0;1019}10201021void stb_fixpath(char *path)1022{1023   for(; *path; ++path)1024      if (*path == '\\')1025         *path = '/';1026}10271028void stb__add_section(char *buffer, char *data, int curlen, int newlen)1029{1030   if (newlen < curlen) {1031      int z1 = newlen >> 1, z2 = newlen-z1;1032      memcpy(buffer, data, z1-1);1033      buffer[z1-1] = '.';1034      buffer[z1-0] = '.';1035      memcpy(buffer+z1+1, data+curlen-z2+1, z2-1);1036   } else1037      memcpy(buffer, data, curlen);1038}10391040char * stb_shorten_path_readable(char *path, int len)1041{1042   static char buffer[1024];1043   int n = strlen(path),n1,n2,r1,r2;1044   char *s;1045   if (n <= len) return path;1046   if (len > 1024) return path;1047   s = stb_strrchr2(path, '/', '\\');1048   if (s) {1049      n1 = s - path + 1;1050      n2 = n - n1;1051      ++s;1052   } else {1053      n1 = 0;1054      n2 = n;1055      s = path;1056   }1057   // now we need to reduce r1 and r2 so that they fit in len1058   if (n1 < len>>1) {1059      r1 = n1;1060      r2 = len - r1;1061   } else if (n2 < len >> 1) {1062      r2 = n2;1063      r1 = len - r2;1064   } else {1065      r1 = n1 * len / n;1066      r2 = n2 * len / n;1067      if (r1 < len>>2) r1 = len>>2, r2 = len-r1;1068      if (r2 < len>>2) r2 = len>>2, r1 = len-r2;1069   }1070   assert(r1 <= n1 && r2 <= n2);1071   if (n1)1072      stb__add_section(buffer, path, n1, r1);1073   stb__add_section(buffer+r1, s, n2, r2);1074   buffer[len] = 0;1075   return buffer;1076}10771078static char *stb__splitpath_raw(char *buffer, char *path, int flag)1079{1080   int len=0,x,y, n = (int) strlen(path), f1,f2;1081   char *s = stb_strrchr2(path, '/', '\\');1082   char *t = strrchr(path, '.');1083   if (s && t && t < s) t = NULL;1084   if (s) ++s;10851086   if (flag == STB_EXT_NO_PERIOD)1087      flag |= STB_EXT;10881089   if (!(flag & (STB_PATH | STB_FILE | STB_EXT))) return NULL;10901091   f1 = s == NULL ? 0 : s-path; // start of filename1092   f2 = t == NULL ? n : t-path; // just past end of filename10931094   if (flag & STB_PATH) {1095      x = 0; if (f1 == 0 && flag == STB_PATH) len=2;1096   } else if (flag & STB_FILE) {1097      x = f1;1098   } else {1099      x = f2;1100      if (flag & STB_EXT_NO_PERIOD)1101         if (buffer[x] == '.')1102            ++x;1103   }11041105   if (flag & STB_EXT)1106      y = n;1107   else if (flag & STB_FILE)1108      y = f2;1109   else1110      y = f1;11111112   if (buffer == NULL) {1113      buffer = (char *) malloc(y-x + len + 1);1114      if (!buffer) return NULL;1115   }11161117   if (len) { strcpy(buffer, "./"); return buffer; }1118   strncpy(buffer, path+x, y-x);1119   buffer[y-x] = 0;1120   return buffer;1121}11221123char *stb_splitpath(char *output, char *src, int flag)1124{1125   return stb__splitpath_raw(output, src, flag);1126}11271128char *stb_splitpathdup(char *src, int flag)1129{1130   return stb__splitpath_raw(NULL, src, flag);1131}11321133char *stb_replacedir(char *output, char *src, char *dir)1134{1135   char buffer[4096];1136   stb_splitpath(buffer, src, STB_FILE | STB_EXT);1137   if (dir)1138      sprintf(output, "%s/%s", dir, buffer);1139   else1140      strcpy(output, buffer);1141   return output;1142}11431144char *stb_replaceext(char *output, char *src, char *ext)1145{1146   char buffer[4096];1147   stb_splitpath(buffer, src, STB_PATH | STB_FILE);1148   if (ext)1149      sprintf(output, "%s.%s", buffer, ext[0] == '.' ? ext+1 : ext);1150   else1151      strcpy(output, buffer);1152   return output;1153}1154#endif115511561157//////////////////////////////////////////////////////////////////////////////1158//1159//                                stb_arr1160//1161//  An stb_arr is directly useable as a pointer (use the actual type in your1162//  definition), but when it resizes, it returns a new pointer and you can't1163//  use the old one, so you have to be careful to copy-in-out as necessary.1164//1165//  Use a NULL pointer as a 0-length array.1166//1167//     float *my_array = NULL, *temp;1168//1169//     // add elements on the end one at a time1170//     stb_arr_push(my_array, 0.0f);1171//     stb_arr_push(my_array, 1.0f);1172//     stb_arr_push(my_array, 2.0f);1173//1174//     assert(my_array[1] == 2.0f);1175//1176//     // add an uninitialized element at the end, then assign it1177//     *stb_arr_add(my_array) = 3.0f;1178//1179//     // add three uninitialized elements at the end1180//     temp = stb_arr_addn(my_array,3);1181//     temp[0] = 4.0f;1182//     temp[1] = 5.0f;1183//     temp[2] = 6.0f;1184//1185//     assert(my_array[5] == 5.0f);1186//1187//     // remove the last one1188//     stb_arr_pop(my_array);1189//1190//     assert(stb_arr_len(my_array) == 6);119111921193#ifndef STB_INCLUDE_STB_LIB_H11941195// simple functions written on top of other functions1196#define stb_arr_empty(a)       (  stb_arr_len(a) == 0 )1197#define stb_arr_add(a)         (  stb_arr_addn((a),1) )1198#define stb_arr_push(a,v)      ( *stb_arr_add(a)=(v)  )11991200typedef struct1201{1202   int len, limit;1203   unsigned int signature;1204   unsigned int padding; // make it a multiple of 16 so preserve alignment mod 161205} stb__arr;12061207#define stb_arr_signature      0x51bada7b  // ends with 0123 in decimal12081209// access the header block stored before the data1210#define stb_arrhead(a)         /*lint --e(826)*/ (((stb__arr *) (a)) - 1)1211#define stb_arrhead2(a)        /*lint --e(826)*/ (((stb__arr *) (a)) - 1)12121213#ifdef STB_DEBUG1214#define stb_arr_check(a)       assert(!a || stb_arrhead(a)->signature == stb_arr_signature)1215#define stb_arr_check2(a)      assert(!a || stb_arrhead2(a)->signature == stb_arr_signature)1216#else1217#define stb_arr_check(a)       ((void) 0)1218#define stb_arr_check2(a)      ((void) 0)1219#endif12201221// ARRAY LENGTH12221223// get the array length; special case if pointer is NULL1224#define stb_arr_len(a)         (a ? stb_arrhead(a)->len : 0)1225#define stb_arr_len2(a)        ((stb__arr *) (a) ? stb_arrhead2(a)->len : 0)1226#define stb_arr_lastn(a)       (stb_arr_len(a)-1)12271228// check whether a given index is valid -- tests 0 <= i < stb_arr_len(a) 1229#define stb_arr_valid(a,i)     (a ? (int) (i) < stb_arrhead(a)->len : 0)12301231// change the array length so is is exactly N entries long, creating1232// uninitialized entries as needed1233#define stb_arr_setlen(a,n)  \1234            (stb__arr_setlen((void **) &(a), sizeof(a[0]), (n)))12351236// change the array length so that N is a valid index (that is, so1237// it is at least N entries long), creating uninitialized entries as needed1238#define stb_arr_makevalid(a,n)  \1239            (stb_arr_len(a) < (n)+1 ? stb_arr_setlen(a,(n)+1),(a) : (a))12401241// remove the last element of the array, returning it1242#define stb_arr_pop(a)         ((stb_arr_check(a), (a))[--stb_arrhead(a)->len])12431244// access the last element in the array1245#define stb_arr_last(a)        ((stb_arr_check(a), (a))[stb_arr_len(a)-1])12461247// is iterator at end of list?1248#define stb_arr_end(a,i)       ((i) >= &(a)[stb_arr_len(a)])12491250// (internal) change the allocated length of the array1251#define stb_arr__grow(a,n)     (stb_arr_check(a), stb_arrhead(a)->len += (n))12521253// add N new uninitialized elements to the end of the array1254#define stb_arr__addn(a,n)     /*lint --e(826)*/ \1255                               ((stb_arr_len(a)+(n) > stb_arrcurmax(a))      \1256                                 ? (stb__arr_addlen((void **) &(a),sizeof(*a),(n)),0) \1257                                 : ((stb_arr__grow(a,n), 0)))12581259// add N new uninitialized elements to the end of the array, and return1260// a pointer to the first new one1261#define stb_arr_addn(a,n)      (stb_arr__addn((a),n),(a)+stb_arr_len(a)-(n))12621263// add N new uninitialized elements starting at index 'i'1264#define stb_arr_insertn(a,i,n) (stb__arr_insertn((void **) &(a), sizeof(*a), i, n))12651266// insert an element at i1267#define stb_arr_insert(a,i,v)  (stb__arr_insertn((void **) &(a), sizeof(*a), i, 1), ((a)[i] = v))12681269// delete N elements from the middle starting at index 'i'1270#define stb_arr_deleten(a,i,n) (stb__arr_deleten((void **) &(a), sizeof(*a), i, n))12711272// delete the i'th element1273#define stb_arr_delete(a,i)   stb_arr_deleten(a,i,1)12741275// delete the i'th element, swapping down from the end1276#define stb_arr_fastdelete(a,i)  \1277   (stb_swap(&a[i], &a[stb_arrhead(a)->len-1], sizeof(*a)), stb_arr_pop(a))127812791280// ARRAY STORAGE12811282// get the array maximum storage; special case if NULL1283#define stb_arrcurmax(a)       (a ? stb_arrhead(a)->limit : 0)1284#define stb_arrcurmax2(a)      (a ? stb_arrhead2(a)->limit : 0)12851286// set the maxlength of the array to n in anticipation of further growth1287#define stb_arr_setsize(a,n)   (stb_arr_check(a), stb__arr_setsize((void **) &(a),sizeof((a)[0]),n))12881289// make sure maxlength is large enough for at least N new allocations1290#define stb_arr_atleast(a,n)   (stb_arr_len(a)+(n) > stb_arrcurmax(a)      \1291                                 ? stb_arr_setsize((a), (n)) : 0)12921293// make a copy of a given array (copies contents via 'memcpy'!)1294#define stb_arr_copy(a)        stb__arr_copy(a, sizeof((a)[0]))12951296// compute the storage needed to store all the elements of the array1297#define stb_arr_storage(a)     (stb_arr_len(a) * sizeof((a)[0]))12981299#define stb_arr_for(v,arr)     for((v)=(arr); (v) < (arr)+stb_arr_len(arr); ++(v))13001301// IMPLEMENTATION13021303STB_EXTERN void stb_arr_free_(void **p);1304STB_EXTERN void *stb__arr_copy_(void *p, int elem_size);1305STB_EXTERN void stb__arr_setsize_(void **p, int size, int limit);1306STB_EXTERN void stb__arr_setlen_(void **p, int size, int newlen);1307STB_EXTERN void stb__arr_addlen_(void **p, int size, int addlen);1308STB_EXTERN void stb__arr_deleten_(void **p, int size, int loc, int n);1309STB_EXTERN void stb__arr_insertn_(void **p, int size, int loc, int n);13101311#define stb_arr_free(p)            stb_arr_free_((void **) &(p))13121313#ifndef STBLIB_MALLOC_WRAPPER // @Todo1314  #define stb__arr_setsize         stb__arr_setsize_1315  #define stb__arr_setlen          stb__arr_setlen_1316  #define stb__arr_addlen          stb__arr_addlen_1317  #define stb__arr_deleten         stb__arr_deleten_1318  #define stb__arr_insertn         stb__arr_insertn_1319  #define stb__arr_copy            stb__arr_copy_1320#else1321  #define stb__arr_addlen(p,s,n)    stb__arr_addlen_(p,s,n,__FILE__,__LINE__)1322  #define stb__arr_setlen(p,s,n)    stb__arr_setlen_(p,s,n,__FILE__,__LINE__)1323  #define stb__arr_setsize(p,s,n)   stb__arr_setsize_(p,s,n,__FILE__,__LINE__)1324  #define stb__arr_deleten(p,s,i,n) stb__arr_deleten_(p,s,i,n,__FILE__,__LINE__)1325  #define stb__arr_insertn(p,s,i,n) stb__arr_insertn_(p,s,i,n,__FILE__,__LINE__)1326  #define stb__arr_copy(p,s)        stb__arr_copy_(p,s,__FILE__,__LINE__)1327#endif1328#endif // STB_INCLUDE_STB_LIB_H13291330#ifdef STB_LIB_IMPLEMENTATION1331void stb_arr_malloc(void **target, void *context)1332{1333   stb__arr *q = (stb__arr *) malloc(sizeof(*q));1334   q->len = q->limit = 0;1335   q->signature = stb_arr_signature;1336   *target = (void *) (q+1);1337}13381339static void * stb__arr_malloc(int size)1340{1341   return malloc(size);1342}13431344void * stb__arr_copy_(void *p, int elem_size)1345{1346   stb__arr *q;1347   if (p == NULL) return p;1348   q = (stb__arr *) malloc(sizeof(*q) + elem_size * stb_arrhead2(p)->limit);1349   stb_arr_check2(p);1350   memcpy(q, stb_arrhead2(p), sizeof(*q) + elem_size * stb_arrhead2(p)->len);1351   return q+1;1352}13531354void stb_arr_free_(void **pp)1355{1356   void *p = *pp;1357   stb_arr_check2(p);1358   if (p) {1359      stb__arr *q = stb_arrhead2(p);1360      free(q);1361   }1362   *pp = NULL;1363}13641365static void stb__arrsize_(void **pp, int size, int limit, int len)1366{1367   void *p = *pp;1368   stb__arr *a;1369   stb_arr_check2(p);1370   if (p == NULL) {1371      if (len == 0 && size == 0) return;1372      a = (stb__arr *) stb__arr_malloc(sizeof(*a) + size*limit);1373      a->limit = limit;1374      a->len   = len;1375      a->signature = stb_arr_signature;1376   } else {1377      a = stb_arrhead2(p);1378      a->len = len;1379      if (a->limit < limit) {1380         void *p;1381         if (a->limit >= 4 && limit < a->limit * 2)1382            limit = a->limit * 2;1383         p = realloc(a, sizeof(*a) + limit*size);1384         if (p) {1385            a = (stb__arr *) p;1386            a->limit = limit;1387         } else {1388            // throw an error!1389         }1390      }1391   }1392   a->len = a->len < a->limit ? a->len : a->limit;1393   *pp = a+1;1394}13951396void stb__arr_setsize_(void **pp, int size, int limit)1397{1398   void *p = *pp;1399   stb_arr_check2(p);1400   stb__arrsize_(pp, size, limit, stb_arr_len2(p));1401}14021403void stb__arr_setlen_(void **pp, int size, int newlen)1404{1405   void *p = *pp;1406   stb_arr_check2(p);1407   if (stb_arrcurmax2(p) < newlen || p == NULL) {1408      stb__arrsize_(pp, size, newlen, newlen);1409   } else {1410      stb_arrhead2(p)->len = newlen;1411   }1412}14131414void stb__arr_addlen_(void **p, int size, int addlen)1415{1416   stb__arr_setlen_(p, size, stb_arr_len2(*p) + addlen);1417}14181419void stb__arr_insertn_(void **pp, int size, int i, int n)1420{1421   void *p = *pp;1422   if (n) {1423      int z;14241425      if (p == NULL) {1426         stb__arr_addlen_(pp, size, n);1427         return;1428      }14291430      z = stb_arr_len2(p);1431      stb__arr_addlen_(&p, size, n);1432      memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-i));1433   }1434   *pp = p;1435}14361437void stb__arr_deleten_(void **pp, int size, int i, int n)1438{1439   void *p = *pp;1440   if (n) {1441      memmove((char *) p + i*size, (char *) p + (i+n)*size, size * (stb_arr_len2(p)-(i+n)));1442      stb_arrhead2(p)->len -= n;1443   }1444   *pp = p;1445}1446#endif14471448//////////////////////////////////////////////////////////////////////////////1449//1450//                               Hashing1451//1452//      typical use for this is to make a power-of-two hash table.1453//1454//      let N = size of table (2^n)1455//      let H = stb_hash(str)1456//      let S = stb_rehash(H) | 11457//1458//      then hash probe sequence P(i) for i=0..N-11459//         P(i) = (H + S*i) & (N-1)1460//1461//      the idea is that H has 32 bits of hash information, but the1462//      table has only, say, 2^20 entries so only uses 20 of the bits.1463//      then by rehashing the original H we get 2^12 different probe1464//      sequences for a given initial probe location. (So it's optimal1465//      for 64K tables and its optimality decreases past that.)1466//1467//      ok, so I've added something that generates _two separate_1468//      32-bit hashes simultaneously which should scale better to1469//      very large tables.14701471#ifndef STB_INCLUDE_STB_LIB_H1472STB_EXTERN unsigned int stb_hash(char *str);1473STB_EXTERN unsigned int stb_hashptr(void *p);1474STB_EXTERN unsigned int stb_hashlen(char *str, int len);1475STB_EXTERN unsigned int stb_rehash_improved(unsigned int v);1476STB_EXTERN unsigned int stb_hash_fast(void *p, int len);1477STB_EXTERN unsigned int stb_hash2(char *str, unsigned int *hash2_ptr);1478STB_EXTERN unsigned int stb_hash_number(unsigned int hash);14791480#define stb_rehash(x)  ((x) + ((x) >> 6) + ((x) >> 19))1481#endif // STB_INCLUDE_STB_LIB_H14821483#ifdef STB_LIB_IMPLEMENTATION1484unsigned int stb_hash(char *str)1485{1486   unsigned int hash = 0;1487   while (*str)1488      hash = (hash << 7) + (hash >> 25) + *str++;1489   return hash + (hash >> 16);1490}14911492unsigned int stb_hashlen(char *str, int len)1493{1494   unsigned int hash = 0;1495   while (len-- > 0 && *str)1496      hash = (hash << 7) + (hash >> 25) + *str++;1497   return hash + (hash >> 16);1498}14991500unsigned int stb_hashptr(void *p)1501{1502    unsigned int x = (unsigned int)(size_t) p;15031504   // typically lacking in low bits and high bits1505   x = stb_rehash(x);1506   x += x << 16;15071508   // pearson's shuffle1509   x ^= x << 3;1510   x += x >> 5;1511   x ^= x << 2;1512   x += x >> 15;1513   x ^= x << 10;1514   return stb_rehash(x);1515}15161517unsigned int stb_rehash_improved(unsigned int v)1518{1519   return stb_hashptr((void *)(size_t) v);1520}15211522unsigned int stb_hash2(char *str, unsigned int *hash2_ptr)1523{1524   unsigned int hash1 = 0x3141592c;1525   unsigned int hash2 = 0x77f044ed;1526   while (*str) {1527      hash1 = (hash1 << 7) + (hash1 >> 25) + *str;1528      hash2 = (hash2 << 11) + (hash2 >> 21) + *str;1529      ++str;1530   }1531   *hash2_ptr = hash2 + (hash1 >> 16);1532   return       hash1 + (hash2 >> 16);1533}15341535// Paul Hsieh hash1536#define stb__get16_slow(p) ((p)[0] + ((p)[1] << 8))1537#if defined(_MSC_VER)1538   #define stb__get16(p) (*((unsigned short *) (p)))1539#else1540   #define stb__get16(p) stb__get16_slow(p)1541#endif15421543unsigned int stb_hash_fast(void *p, int len)1544{1545   unsigned char *q = (unsigned char *) p;1546   unsigned int hash = len;15471548   if (len <= 0 || q == NULL) return 0;15491550   /* Main loop */1551    if (((int)(size_t) q & 1) == 0) {1552      for (;len > 3; len -= 4) {1553         unsigned int val;1554         hash +=  stb__get16(q);1555         val   = (stb__get16(q+2) << 11);1556         hash  = (hash << 16) ^ hash ^ val;1557         q    += 4;1558         hash += hash >> 11;1559      }1560   } else {1561      for (;len > 3; len -= 4) {1562         unsigned int val;1563         hash +=  stb__get16_slow(q);1564         val   = (stb__get16_slow(q+2) << 11);1565         hash  = (hash << 16) ^ hash ^ val;1566         q    += 4;1567         hash += hash >> 11;1568      }1569   }15701571   /* Handle end cases */1572   switch (len) {1573      case 3: hash += stb__get16_slow(q);1574              hash ^= hash << 16;1575              hash ^= q[2] << 18;1576              hash += hash >> 11;1577              break;1578      case 2: hash += stb__get16_slow(q);1579              hash ^= hash << 11;1580              hash += hash >> 17;1581              break;1582      case 1: hash += q[0];1583              hash ^= hash << 10;1584              hash += hash >> 1;1585              break;1586      case 0: break;1587   }15881589   /* Force "avalanching" of final 127 bits */1590   hash ^= hash << 3;1591   hash += hash >> 5;1592   hash ^= hash << 4;1593   hash += hash >> 17;1594   hash ^= hash << 25;1595   hash += hash >> 6;15961597   return hash;1598}15991600unsigned int stb_hash_number(unsigned int hash)1601{1602   hash ^= hash << 3;1603   hash += hash >> 5;1604   hash ^= hash << 4;1605   hash += hash >> 17;1606   hash ^= hash << 25;1607   hash += hash >> 6;1608   return hash;1609}1610#endif16111612//////////////////////////////////////////////////////////////////////////////1613//1614//                     Instantiated data structures1615//1616// This is an attempt to implement a templated data structure.1617//1618// Hash table: call stb_define_hash(TYPE,N,KEY,K1,K2,HASH,VALUE)1619//     TYPE     -- will define a structure type containing the hash table1620//     N        -- the name, will prefix functions named:1621//                        N create1622//                        N destroy1623//                        N get1624//                        N set, N add, N update,1625//                        N remove1626//     KEY      -- the type of the key. 'x == y' must be valid1627//       K1,K2  -- keys never used by the app, used as flags in the hashtable1628//       HASH   -- a piece of code ending with 'return' that hashes key 'k'1629//     VALUE    -- the type of the value. 'x = y' must be valid1630//1631//  Note that stb_define_hash_base can be used to define more sophisticated1632//  hash tables, e.g. those that make copies of the key or use special1633//  comparisons (e.g. strcmp).16341635#define STB_(prefix,name)     stb__##prefix##name1636#define STB__(prefix,name)    prefix##name1637#define STB__use(x)           x1638#define STB__skip(x)16391640#define stb_declare_hash(PREFIX,TYPE,N,KEY,VALUE) \1641   typedef struct stb__st_##TYPE TYPE;\1642   PREFIX int STB__(N, init)(TYPE *h, int count);\1643   PREFIX int STB__(N, memory_usage)(TYPE *h);\1644   PREFIX TYPE * STB__(N, create)(void);\1645   PREFIX TYPE * STB__(N, copy)(TYPE *h);\1646   PREFIX void STB__(N, destroy)(TYPE *h);\1647   PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v);\1648   PREFIX VALUE STB__(N,get)(TYPE *a, KEY k);\1649   PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v);\1650   PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v);\1651   PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v);\1652   PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v);16531654#define STB_nocopy(x)        (x)1655#define STB_nodelete(x)      01656#define STB_nofields         1657#define STB_nonullvalue(x)1658#define STB_nullvalue(x)     x1659#define STB_safecompare(x)   x1660#define STB_nosafe(x)1661#define STB_noprefix16621663#ifdef __GNUC__1664#define STB__nogcc(x)1665#else1666#define STB__nogcc(x)  x1667#endif16681669#define stb_define_hash_base(PREFIX,TYPE,FIELDS,N,NC,LOAD_FACTOR,             \1670                             KEY,EMPTY,DEL,COPY,DISPOSE,SAFE,                 \1671                             VCOMPARE,CCOMPARE,HASH,                          \1672                             VALUE,HASVNULL,VNULL)                            \1673                                                                              \1674typedef struct                                                                \1675{                                                                             \1676   KEY   k;                                                                   \1677   VALUE v;                                                                   \1678} STB_(N,_hashpair);                                                          \1679                                                                              \1680STB__nogcc( typedef struct stb__st_##TYPE TYPE;  )                            \1681struct stb__st_##TYPE {                                                       \1682   FIELDS                                                                     \1683   STB_(N,_hashpair) *table;                                                  \1684   unsigned int mask;                                                         \1685   int count, limit;                                                          \1686   int deleted;                                                               \1687                                                                              \1688   int delete_threshhold;                                                     \1689   int grow_threshhold;                                                       \1690   int shrink_threshhold;                                                     \1691   unsigned char alloced, has_empty, has_del;                                 \1692   VALUE ev; VALUE dv;                                                        \1693};                                                                            \1694                                                                              \1695static unsigned int STB_(N, hash)(KEY k)                                      \1696{                                                                             \1697   HASH                                                                       \1698}                                                                             \1699                                                                              \1700PREFIX int STB__(N, init)(TYPE *h, int count)                                        \1701{                                                                             \1702   int i;                                                                     \1703   if (count < 4) count = 4;                                                  \1704   h->limit = count;                                                          \1705   h->count = 0;                                                              \1706   h->mask  = count-1;                                                        \1707   h->deleted = 0;                                                            \1708   h->grow_threshhold = (int) (count * LOAD_FACTOR);                          \1709   h->has_empty = h->has_del = 0;                                             \1710   h->alloced = 0;                                                            \1711   if (count <= 64)                                                           \1712      h->shrink_threshhold = 0;                                               \1713   else                                                                       \1714      h->shrink_threshhold = (int) (count * (LOAD_FACTOR/2.25));              \1715   h->delete_threshhold = (int) (count * (1-LOAD_FACTOR)/2);                  \1716   h->table = (STB_(N,_hashpair)*) malloc(sizeof(h->table[0]) * count);       \1717   if (h->table == NULL) return 0;                                            \1718   /* ideally this gets turned into a memset32 automatically */               \1719   for (i=0; i < count; ++i)                                                  \1720      h->table[i].k = EMPTY;                                                  \1721   return 1;                                                                  \1722}                                                                             \1723                                                                              \1724PREFIX int STB__(N, memory_usage)(TYPE *h)                                           \1725{                                                                             \1726   return sizeof(*h) + h->limit * sizeof(h->table[0]);                        \1727}                                                                             \1728                                                                              \1729PREFIX TYPE * STB__(N, create)(void)                                                 \1730{                                                                             \1731   TYPE *h = (TYPE *) malloc(sizeof(*h));                                     \1732   if (h) {                                                                   \1733      if (STB__(N, init)(h, 16))                                              \1734         h->alloced = 1;                                                      \1735      else { free(h); h=NULL; }                                               \1736   }                                                                          \1737   return h;                                                                  \1738}                                                                             \1739                                                                              \1740PREFIX void STB__(N, destroy)(TYPE *a)                                               \1741{                                                                             \1742   int i;                                                                     \1743   for (i=0; i < a->limit; ++i)                                               \1744      if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k, DEL))    \1745         DISPOSE(a->table[i].k);                                              \1746   free(a->table);                                                            \1747   if (a->alloced)                                                            \1748      free(a);                                                                \1749}                                                                             \1750                                                                              \1751static void STB_(N, rehash)(TYPE *a, int count);                              \1752                                                                              \1753PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v)                               \1754{                                                                             \1755   unsigned int h = STB_(N, hash)(k);                                         \1756   unsigned int n = h & a->mask, s;                                           \1757   if (CCOMPARE(k,EMPTY)){ if (a->has_empty) *v = a->ev; return a->has_empty;}\1758   if (CCOMPARE(k,DEL)) { if (a->has_del  ) *v = a->dv; return a->has_del;   }\1759   if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                               \1760   SAFE(if (!CCOMPARE(a->table[n].k,DEL)))                                    \1761   if (VCOMPARE(a->table[n].k,k)) { *v = a->table[n].v; return 1; }            \1762   s = stb_rehash(h) | 1;                                                     \1763   for(;;) {                                                                  \1764      n = (n + s) & a->mask;                                                  \1765      if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                            \1766      SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;)                        \1767      if (VCOMPARE(a->table[n].k,k))                                           \1768         { *v = a->table[n].v; return 1; }                                    \1769   }                                                                          \1770}                                                                             \1771                                                                              \1772HASVNULL(                                                                     \1773   PREFIX VALUE STB__(N,get)(TYPE *a, KEY k)                                         \1774   {                                                                          \1775      VALUE v;                                                                \1776      if (STB__(N,get_flag)(a,k,&v)) return v;                                \1777      else                           return VNULL;                            \1778   }                                                                          \1779)                                                                             \1780                                                                              \1781PREFIX int STB__(N,getkey)(TYPE *a, KEY k, KEY *kout)                                \1782{                                                                             \1783   unsigned int h = STB_(N, hash)(k);                                         \1784   unsigned int n = h & a->mask, s;                                           \1785   if (CCOMPARE(k,EMPTY)||CCOMPARE(k,DEL)) return 0;                          \1786   if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                               \1787   SAFE(if (!CCOMPARE(a->table[n].k,DEL)))                                    \1788   if (VCOMPARE(a->table[n].k,k)) { *kout = a->table[n].k; return 1; }         \1789   s = stb_rehash(h) | 1;                                                     \1790   for(;;) {                                                                  \1791      n = (n + s) & a->mask;                                                  \1792      if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                            \1793      SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;)                        \1794      if (VCOMPARE(a->table[n].k,k))                                          \1795         { *kout = a->table[n].k; return 1; }                                 \1796   }                                                                          \1797}                                                                             \1798                                                                              \1799static int STB_(N,addset)(TYPE *a, KEY k, VALUE v,                            \1800                             int allow_new, int allow_old, int copy)          \1801{                                                                             \1802   unsigned int h = STB_(N, hash)(k);                                         \1803   unsigned int n = h & a->mask;                                              \1804   int b = -1;                                                                \1805   if (CCOMPARE(k,EMPTY)) {                                                   \1806      if (a->has_empty ? allow_old : allow_new) {                             \1807          n=a->has_empty; a->ev = v; a->has_empty = 1; return !n;             \1808      } else return 0;                                                        \1809   }                                                                          \1810   if (CCOMPARE(k,DEL)) {                                                     \1811      if (a->has_del ? allow_old : allow_new) {                               \1812          n=a->has_del; a->dv = v; a->has_del = 1; return !n;                 \1813      } else return 0;                                                        \1814   }                                                                          \1815   if (!CCOMPARE(a->table[n].k, EMPTY)) {                                     \1816      unsigned int s;                                                         \1817      if (CCOMPARE(a->table[n].k, DEL))                                       \1818         b = n;                                                               \1819      else if (VCOMPARE(a->table[n].k,k)) {                                   \1820         if (allow_old)                                                       \1821            a->table[n].v = v;                                                \1822         return !allow_new;                                                   \1823      }                                                                       \1824      s = stb_rehash(h) | 1;                                                  \1825      for(;;) {                                                               \1826         n = (n + s) & a->mask;                                               \1827         if (CCOMPARE(a->table[n].k, EMPTY)) break;                           \1828         if (CCOMPARE(a->table[n].k, DEL)) {                                  \1829            if (b < 0) b = n;                                                 \1830         } else if (VCOMPARE(a->table[n].k,k)) {                              \1831            if (allow_old)                                                    \1832               a->table[n].v = v;                                             \1833            return !allow_new;                                                \1834         }                                                                    \1835      }                                                                       \1836   }                                                                          \1837   if (!allow_new) return 0;                                                  \1838   if (b < 0) b = n; else --a->deleted;                                       \1839   a->table[b].k = copy ? COPY(k) : k;                                        \1840   a->table[b].v = v;                                                         \1841   ++a->count;                                                                \1842   if (a->count > a->grow_threshhold)                                         \1843      STB_(N,rehash)(a, a->limit*2);                                          \1844   return 1;                                                                  \1845}                                                                             \1846                                                                              \1847PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,1,1);}\1848PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,0,1);}\1849PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v){return STB_(N,addset)(a,k,v,0,1,1);}\1850                                                                              \1851PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v)                                \1852{                                                                             \1853   unsigned int h = STB_(N, hash)(k);                                         \1854   unsigned int n = h & a->mask, s;                                           \1855   if (CCOMPARE(k,EMPTY)) { if (a->has_empty) { if(v)*v = a->ev; a->has_empty=0; return 1; } return 0; } \1856   if (CCOMPARE(k,DEL))   { if (a->has_del  ) { if(v)*v = a->dv; a->has_del  =0; return 1; } return 0; } \1857   if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                               \1858   if (SAFE(CCOMPARE(a->table[n].k,DEL) || ) !VCOMPARE(a->table[n].k,k)) {     \1859      s = stb_rehash(h) | 1;                                                  \1860      for(;;) {                                                               \1861         n = (n + s) & a->mask;                                               \1862         if (CCOMPARE(a->table[n].k,EMPTY)) return 0;                         \1863         SAFE(if (CCOMPARE(a->table[n].k, DEL)) continue;)                    \1864         if (VCOMPARE(a->table[n].k,k)) break;                                 \1865      }                                                                       \1866   }                                                                          \1867   DISPOSE(a->table[n].k);                                                    \1868   a->table[n].k = DEL;                                                       \1869   --a->count;                                                                \1870   ++a->deleted;                                                              \1871   if (v != NULL)                                                             \1872      *v = a->table[n].v;                                                     \1873   if (a->count < a->shrink_threshhold)                                       \1874      STB_(N, rehash)(a, a->limit >> 1);                                      \1875   else if (a->deleted > a->delete_threshhold)                                \1876      STB_(N, rehash)(a, a->limit);                                           \1877   return 1;                                                                  \1878}                                                                             \1879                                                                              \1880PREFIX TYPE * STB__(NC, copy)(TYPE *a)                                        \1881{                                                                             \1882   int i;                                                                     \1883   TYPE *h = (TYPE *) malloc(sizeof(*h));                                     \1884   if (!h) return NULL;                                                       \1885   if (!STB__(N, init)(h, a->limit)) { free(h); return NULL; }                \1886   h->count = a->count;                                                       \1887   h->deleted = a->deleted;                                                   \1888   h->alloced = 1;                                                            \1889   h->ev = a->ev; h->dv = a->dv;                                              \1890   h->has_empty = a->has_empty; h->has_del = a->has_del;                      \1891   memcpy(h->table, a->table, h->limit * sizeof(h->table[0]));                \1892   for (i=0; i < a->limit; ++i)                                               \1893      if (!CCOMPARE(h->table[i].k,EMPTY) && !CCOMPARE(h->table[i].k,DEL))     \1894         h->table[i].k = COPY(h->table[i].k);                                 \1895   return h;                                                                  \1896}                                                                             \1897                                                                              \1898static void STB_(N, rehash)(TYPE *a, int count)                               \1899{                                                                             \1900   int i;                                                                     \1901   TYPE b;                                                                    \1902   STB__(N, init)(&b, count);                                                 \1903   for (i=0; i < a->limit; ++i)                                               \1904      if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k,DEL))     \1905         STB_(N,addset)(&b, a->table[i].k, a->table[i].v,1,1,0);              \1906   free(a->table);                                                            \1907   a->table = b.table;                                                        \1908   a->mask = b.mask;                                                          \1909   a->count = b.count;                                                        \1910   a->limit = b.limit;                                                        \1911   a->deleted = b.deleted;                                                    \1912   a->delete_threshhold = b.delete_threshhold;                                \1913   a->grow_threshhold = b.grow_threshhold;                                    \1914   a->shrink_threshhold = b.shrink_threshhold;                                \1915}19161917#define STB_equal(a,b)  ((a) == (b))19181919#define stb_define_hash(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE)                      \1920   stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f,              \1921              KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe,               \1922              STB_equal,STB_equal,HASH,                                       \1923              VALUE,STB_nonullvalue,0)19241925#define stb_define_hash_vnull(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE,VNULL)          \1926   stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f,              \1927              KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe,               \1928              STB_equal,STB_equal,HASH,                                       \1929              VALUE,STB_nullvalue,VNULL)19301931//////////////////////////////////////////////////////////////////////////////1932//1933//                        stb_ptrmap1934//1935// An stb_ptrmap data structure is an O(1) hash table between pointers. One1936// application is to let you store "extra" data associated with pointers,1937// which is why it was originally called stb_extra.19381939#ifndef STB_INCLUDE_STB_LIB_H1940stb_declare_hash(STB_EXTERN, stb_ptrmap, stb_ptrmap_, void *, void *)1941stb_declare_hash(STB_EXTERN, stb_idict, stb_idict_, stb_int32, stb_int32)19421943STB_EXTERN void        stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *));1944STB_EXTERN stb_ptrmap *stb_ptrmap_new(void);19451946STB_EXTERN stb_idict * stb_idict_new_size(unsigned int size);1947STB_EXTERN void        stb_idict_remove_all(stb_idict *e);1948#endif // STB_INCLUDE_STB_LIB_H19491950#ifdef STB_LIB_IMPLEMENTATION19511952#define STB_EMPTY ((void *) 2)1953#define STB_EDEL  ((void *) 6)19541955stb_define_hash_base(STB_noprefix,stb_ptrmap, STB_nofields, stb_ptrmap_,stb_ptrmap_,0.85f,1956              void *,STB_EMPTY,STB_EDEL,STB_nocopy,STB_nodelete,STB_nosafe,1957              STB_equal,STB_equal,return stb_hashptr(k);,1958              void *,STB_nullvalue,NULL)19591960stb_ptrmap *stb_ptrmap_new(void)1961{1962   return stb_ptrmap_create();1963}19641965void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *))1966{1967   int i;1968   if (free_func)1969      for (i=0; i < e->limit; ++i)1970         if (e->table[i].k != STB_EMPTY && e->table[i].k != STB_EDEL) {1971            if (free_func == free)1972               free(e->table[i].v); // allow STB_MALLOC_WRAPPER to operate1973            else1974               free_func(e->table[i].v);1975         }1976   stb_ptrmap_destroy(e);1977}19781979// extra fields needed for stua_dict1980#define STB_IEMPTY  ((int) 1)1981#define STB_IDEL    ((int) 3)1982stb_define_hash_base(STB_noprefix, stb_idict, STB_nofields, stb_idict_,stb_idict_,0.85f,1983              stb_int32,STB_IEMPTY,STB_IDEL,STB_nocopy,STB_nodelete,STB_nosafe,1984              STB_equal,STB_equal,1985              return stb_rehash_improved(k);,stb_int32,STB_nonullvalue,0)19861987stb_idict * stb_idict_new_size(unsigned int size)1988{1989   stb_idict *e = (stb_idict *) malloc(sizeof(*e));1990   if (e) {1991      // round up to power of 21992      while ((size & (size-1)) != 0) // while more than 1 bit is set1993         size += (size & ~(size-1)); // add the lowest set bit1994      stb_idict_init(e, size);1995      e->alloced = 1;1996   }1997   return e;1998}19992000void stb_idict_remove_all(stb_idict *e)

The first 2,000 of 3,305 lines.