stb_sprintf: PR 613

This commit is contained in:
Sean Barrett 2020-02-02 07:47:53 -08:00
parent 81b4fd7342
commit e802821e4d

View File

@ -18,7 +18,7 @@
// Leonard Ritter // Leonard Ritter
// Stefano Zanotti // Stefano Zanotti
// Adam Allison // Adam Allison
// Kevin Schmidt // Arvid Gerstmann
// //
// LICENSE: // LICENSE:
// //
@ -140,37 +140,25 @@ PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
"...512 char string..." ( 35.0x/32.5x faster!) "...512 char string..." ( 35.0x/32.5x faster!)
*/ */
#if defined(__clang__) #if defined(__has_feature)
#if defined(__has_feature) && defined(__has_attribute) #if __has_feature(address_sanitizer)
#if __has_feature(address_sanitizer) #define STBI__ASAN __attribute__((no_sanitize("address")))
#if __has_attribute(__no_sanitize__)
#define STBSP__ASAN __attribute__((__no_sanitize__("address")))
#elif __has_attribute(__no_sanitize_address__)
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
#elif __has_attribute(__no_address_safety_analysis__)
#define STBSP__ASAN __attribute__((__no_address_safety_analysis__))
#endif #endif
#endif
#endif
#elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
#if __SANITIZE_ADDRESS__
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
#endif
#endif #endif
#ifndef STBSP__ASAN #ifndef STBI__ASAN
#define STBSP__ASAN #define STBI__ASAN
#endif #endif
#ifdef STB_SPRINTF_STATIC #ifdef STB_SPRINTF_STATIC
#define STBSP__PUBLICDEC static #define STBSP__PUBLICDEC static
#define STBSP__PUBLICDEF static STBSP__ASAN #define STBSP__PUBLICDEF static STBI__ASAN
#else #else
#ifdef __cplusplus #ifdef __cplusplus
#define STBSP__PUBLICDEC extern "C" #define STBSP__PUBLICDEC extern "C"
#define STBSP__PUBLICDEF extern "C" STBSP__ASAN #define STBSP__PUBLICDEF extern "C" STBI__ASAN
#else #else
#define STBSP__PUBLICDEC extern #define STBSP__PUBLICDEC extern
#define STBSP__PUBLICDEF STBSP__ASAN #define STBSP__PUBLICDEF STBI__ASAN
#endif #endif
#endif #endif
@ -1355,12 +1343,14 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, .
typedef struct stbsp__context { typedef struct stbsp__context {
char *buf; char *buf;
int count; int count;
int length;
char tmp[STB_SPRINTF_MIN]; char tmp[STB_SPRINTF_MIN];
} stbsp__context; } stbsp__context;
static char *stbsp__clamp_callback(char *buf, void *user, int len) static char *stbsp__clamp_callback(char *buf, void *user, int len)
{ {
stbsp__context *c = (stbsp__context *)user; stbsp__context *c = (stbsp__context *)user;
c->length += len;
if (len > c->count) if (len > c->count)
len = c->count; len = c->count;
@ -1380,7 +1370,7 @@ static char *stbsp__clamp_callback(char *buf, void *user, int len)
} }
if (c->count <= 0) if (c->count <= 0)
return 0; return c->tmp;
return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can
} }
@ -1388,29 +1378,27 @@ static char * stbsp__count_clamp_callback( char * buf, void * user, int len )
{ {
stbsp__context * c = (stbsp__context*)user; stbsp__context * c = (stbsp__context*)user;
c->count += len; c->length += len;
return c->tmp; // go direct into buffer if you can return c->tmp; // go direct into buffer if you can
} }
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va ) STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )
{ {
stbsp__context c; stbsp__context c;
int l;
if ( (count == 0) && !buf ) if ( (count == 0) && !buf )
{ {
c.count = 0; c.length = 0;
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va ); STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );
l = c.count;
} }
else else
{ {
if ( count == 0 ) int l;
return 0;
c.buf = buf; c.buf = buf;
c.count = count; c.count = count;
c.length = 0;
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va ); STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );
@ -1421,7 +1409,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
buf[l] = 0; buf[l] = 0;
} }
return l; return c.length;
} }
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)