Skip to content
Myr

stb_sprintf: fix size-only snprintf query

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent ccee11a · Browse files · 1 file+31 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−12 removed

  • other:+31 added−12 removed

1 file+31 added−12 removed

modified:stb_sprintf.h+31 added-12 removed
1// stb_sprintf - v1.04 - public domain snprintf() implementation1// stb_sprintf - v1.05 - public domain snprintf() implementation2// originally by Jeff Roberts / RAD Game Tools, 2015/10/203// http://github.com/nothings/stb4//5// allowed types: sc uidBboXx p AaGgEef n
7 unmodified lines
13// github:trex7814// Jari Komppa (SI suffixes)15// Rohit Nirmal16// Marcin Wojdyr17// Leonard Ritter18//19// LICENSE:20//21// See end of file for license information.
1321 unmodified lines
1343 return 0;1344 return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can1345}13461346STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va)1347static char * stbsp__count_clamp_callback( char * buf, void * user, int len )1348{1349 stbsp__context * c = (stbsp__context*)user;13501351 c->count += len;1352 return c->tmp; // go direct into buffer if you can1353}13541355STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )1356{1357 stbsp__context c;1358 int l;13591351 if (count == 0)1352 return 0;1360 if ( (count == 0) && !buf )1361 {1362 c.count = 0;13631354 c.buf = buf;1355 c.count = count;1364 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );1365 l = c.count;1366 }1367 else1368 {1369 if ( count == 0 )1370 return 0;13711357 STB_SPRINTF_DECORATE(vsprintfcb)(stbsp__clamp_callback, &c, stbsp__clamp_callback(0, &c, 0), fmt, va);1372 c.buf = buf;1373 c.count = count;13741359 // zero-terminate1360 l = (int)(c.buf - buf);1361 if (l >= count) // should never be greater, only equal (or less) than count1362 l = count - 1;1363 buf[l] = 0;1375 STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );13761377 // zero-terminate1378 l = (int)( c.buf - buf );1379 if ( l >= count ) // should never be greater, only equal (or less) than count1380 l = count - 1;1381 buf[l] = 0;1382 }13831384 return l;1385}13861387STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)
446 unmodified lines