1
0
Fork 0

Merge branch 'out_function'

development
Marco Paland 7 years ago
commit 8609d76285

@ -26,7 +26,7 @@ Therefore I decided to write an own, final implementation which meets the follow
- NO dependencies, no libs, just one module file - NO dependencies, no libs, just one module file
- Support of all important flags, width and precision sub-specifiers (see below) - Support of all important flags, width and precision sub-specifiers (see below)
- Support of decimal/floating number representation (with an own fast itoa/ftoa) - Support of decimal/floating number representation (with an own fast itoa/ftoa)
- Reentrant and thread-safe, malloc free - Reentrant and thread-safe, malloc free, no static vars/buffers
- LINT and compiler L4 warning free, mature, coverity clean, automotive ready - LINT and compiler L4 warning free, mature, coverity clean, automotive ready
- Extensive test suite (> 310 test cases) passing - Extensive test suite (> 310 test cases) passing
- Simply the best *printf* around the net - Simply the best *printf* around the net
@ -119,24 +119,22 @@ The length sub-specifier modifies the length of the data type.
### Return value ### Return value
Upon successful return, all functions return the number of characters _written_, _excluding_ the null byte used to end the string.
Functions `snprintf()` and `vsnprintf()` don't write more than `count` bytes, _including_ the terminating null byte ('\0'). If the output was truncated
due to this limit, the return value is `count`, which indicates any truncation.
If an output error (invalid buffer etc.) is encountered, `-1` is returned.
Upon successful return, all functions return the number of characters written, _excluding_ the terminating null character used to end the string.
## Caveats Functions `snprintf()` and `vsnprintf()` don't write more than `count` bytes, _including_ the terminating null byte ('\0').
Currently `snprintf()` and `vsnprintf()` don't support `(v)snprintf(nullptr, 0, "Some text")` to get the length of the formatted string only. An error value of `-1` is returned. Anyway, if the output was truncated due to this limit, the return value is the number of characters that _could_ have been written.
Notice that a value equal or larger than `count` indicates a truncation. Only when the returned value is non-negative and less than `count`,
the string has been completely written.
If any error is encountered, `-1` is returned.
## Compiler switches/defines ## Compiler switches/defines
| Name | Default value | Description | | Name | Default value | Description |
|------|---------------|-------------| |------|---------------|-------------|
| PRINTF_BUFFER_SIZE | 128 | The buffer size used for the printf() function (not for sprintf/snprintf). Set to 0 if the printf() function is unused (just using sprintf/snprintf) | | PRINTF_NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
| PRINTF_NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value | | PRINTF_FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
| PRINTF_FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value | | PRINTF_FLOAT_SUPPORT | undefined | Define this to enable floating point (%f) support |
| PRINTF_FLOAT_SUPPORT | undefined | Define this to enable floating point (%f) support |
| PRINTF_LONG_LONG_SUPPORT | undefined | Define this to enable long long (%ll) support | | PRINTF_LONG_LONG_SUPPORT | undefined | Define this to enable long long (%ll) support |

@ -35,15 +35,12 @@
#include "printf.h" #include "printf.h"
// buffer size used omly for printf (created on stack)
#define PRINTF_BUFFER_SIZE 128U
// ntoa conversion buffer size, this must be big enough to hold // ntoa conversion buffer size, this must be big enough to hold
// one converted numeric number including padded zeros (created on stack) // one converted numeric number including padded zeros (dyn created on stack)
#define PRINTF_NTOA_BUFFER_SIZE 32U #define PRINTF_NTOA_BUFFER_SIZE 32U
// ftoa conversion buffer size, this must be big enough to hold // ftoa conversion buffer size, this must be big enough to hold
// one converted float number including padded zeros (created on stack) // one converted float number including padded zeros (dyn created on stack)
#define PRINTF_FTOA_BUFFER_SIZE 32U #define PRINTF_FTOA_BUFFER_SIZE 32U
// define this to support floating point (%f) // define this to support floating point (%f)
@ -66,6 +63,33 @@
#define FLAGS_PRECISION (1U << 8U) #define FLAGS_PRECISION (1U << 8U)
#define FLAGS_WIDTH (1U << 9U) #define FLAGS_WIDTH (1U << 9U)
// output function type
typedef void (*out_fct_type)(char character, char* buffer, size_t idx, size_t maxlen);
// internal buffer output
static inline void _out_buffer(char character, char* buffer, size_t idx, size_t maxlen)
{
if (idx < maxlen) {
buffer[idx] = character;
}
}
// internal null output
static inline void _out_null(char character, char* buffer, size_t idx, size_t maxlen)
{
(void)character; (void)buffer; (void)idx; (void)maxlen;
}
// internal _putchar wrapper
static inline void _out_char(char character, char* buffer, size_t idx, size_t maxlen)
{
(void)buffer; (void)idx; (void)maxlen;
_putchar(character);
}
// internal strlen // internal strlen
// \return The length of the string (excluding the terminating 0) // \return The length of the string (excluding the terminating 0)
@ -97,15 +121,8 @@ static inline unsigned int _atoi(const char** str)
// internal itoa format // internal itoa format
static size_t _ntoa_format(char* buffer, char* buf, size_t len, bool negative, unsigned int base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
{ {
if (maxlen == 0U) {
return 0U;
}
if (base > 16U) {
return 0U;
}
// pad leading zeros // pad leading zeros
while (!(flags & FLAGS_LEFT) && (len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { while (!(flags & FLAGS_LEFT) && (len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0'; buf[len++] = '0';
@ -150,22 +167,21 @@ static size_t _ntoa_format(char* buffer, char* buf, size_t len, bool negative, u
} }
// pad spaces up to given width // pad spaces up to given width
size_t idx = 0U;
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
for (size_t i = len; (i < width) && (i < maxlen); ++i) { for (size_t i = len; i < width; i++) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
// reverse string // reverse string
for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { for (size_t i = 0U; i < len; i++) {
buffer[idx++] = buf[len - i - 1U]; out(buf[len - i - 1U], buffer, idx++, maxlen);
} }
// append pad spaces up to given width // append pad spaces up to given width
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while ((idx < width) && (idx < maxlen)) { while (idx < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
@ -174,7 +190,7 @@ static size_t _ntoa_format(char* buffer, char* buf, size_t len, bool negative, u
// internal itoa for 'long' type // internal itoa for 'long' type
static size_t _ntoa_long(char* buffer, unsigned long value, bool negative, unsigned long base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
{ {
char buf[PRINTF_NTOA_BUFFER_SIZE]; char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
@ -182,19 +198,19 @@ static size_t _ntoa_long(char* buffer, unsigned long value, bool negative, unsig
// write if precision != 0 and value is != 0 // write if precision != 0 and value is != 0
if (!(flags & FLAGS_PRECISION) || value) { if (!(flags & FLAGS_PRECISION) || value) {
do { do {
char digit = (char)(value % base); const char digit = (char)(value % base);
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
value /= base; value /= base;
} while ((len < PRINTF_NTOA_BUFFER_SIZE) && value); } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
} }
return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
} }
// internal itoa for 'long long' type // internal itoa for 'long long' type
#if defined(PRINTF_LONG_LONG_SUPPORT) #if defined(PRINTF_LONG_LONG_SUPPORT)
static size_t _ntoa_long_long(char* buffer, unsigned long long value, bool negative, unsigned long long base, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
{ {
char buf[PRINTF_NTOA_BUFFER_SIZE]; char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
@ -202,19 +218,19 @@ static size_t _ntoa_long_long(char* buffer, unsigned long long value, bool negat
// write if precision != 0 and value is != 0 // write if precision != 0 and value is != 0
if (!(flags & FLAGS_PRECISION) || value) { if (!(flags & FLAGS_PRECISION) || value) {
do { do {
char digit = (char)(value % base); const char digit = (char)(value % base);
buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
value /= base; value /= base;
} while ((len < PRINTF_NTOA_BUFFER_SIZE) && value); } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
} }
return _ntoa_format(buffer, buf, len, negative, (unsigned int)base, maxlen, prec, width, flags); return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
} }
#endif // PRINTF_LONG_LONG_SUPPORT #endif // PRINTF_LONG_LONG_SUPPORT
#if defined(PRINTF_FLOAT_SUPPORT) #if defined(PRINTF_FLOAT_SUPPORT)
static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec, unsigned int width, unsigned int flags) static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{ {
char buf[PRINTF_FTOA_BUFFER_SIZE]; char buf[PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U; size_t len = 0U;
@ -325,22 +341,21 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec
} }
// pad spaces up to given width // pad spaces up to given width
size_t idx = 0U;
if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
for (size_t i = len; (i < width) && (i < maxlen); ++i) { for (size_t i = len; i < width; i++) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
// reverse string // reverse string
for (size_t i = 0U; (i < len) && (i < maxlen); ++i) { for (size_t i = 0U; i < len; i++) {
buffer[idx++] = buf[len - i - 1U]; out(buf[len - i - 1U], buffer, idx++, maxlen);
} }
// append pad spaces up to given width // append pad spaces up to given width
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while ((idx < width) && (idx < maxlen)) { while (idx < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
@ -350,22 +365,22 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec
// internal vsnprintf // internal vsnprintf
static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_list va) static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
{ {
unsigned int flags, width, precision, n; unsigned int flags, width, precision, n;
size_t idx = 0U; size_t idx = 0U;
// check if buffer is valid
if (!buffer) { if (!buffer) {
return -1; // use null output function
out = _out_null;
} }
while ((idx < buffer_len) && *format) while (*format)
{ {
// format specifier? %[flags][width][.precision][length] // format specifier? %[flags][width][.precision][length]
if (*format != '%') { if (*format != '%') {
// no // no
buffer[idx++] = *format; out(*format, buffer, idx++, maxlen);
format++; format++;
continue; continue;
} }
@ -473,30 +488,30 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_LONG_LONG_SUPPORT) #if defined(PRINTF_LONG_LONG_SUPPORT)
const long long value = va_arg(va, long long); const long long value = va_arg(va, long long);
idx += _ntoa_long_long(&buffer[idx], (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
#endif #endif
} }
else if (flags & FLAGS_LONG) { else if (flags & FLAGS_LONG) {
const long value = va_arg(va, long); const long value = va_arg(va, long);
idx += _ntoa_long(&buffer[idx], (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
} }
else { else {
const int value = va_arg(va, int); const int value = va_arg(va, int);
idx += _ntoa_long(&buffer[idx], (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
} }
} }
else { else {
// unsigned // unsigned
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_LONG_LONG_SUPPORT) #if defined(PRINTF_LONG_LONG_SUPPORT)
idx += _ntoa_long_long(&buffer[idx], va_arg(va, unsigned long long), false, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
#endif #endif
} }
else if (flags & FLAGS_LONG) { else if (flags & FLAGS_LONG) {
idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned long), false, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
} }
else { else {
idx += _ntoa_long(&buffer[idx], va_arg(va, unsigned int), false, base, buffer_len - idx, precision, width, flags); idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned int), false, base, precision, width, flags);
} }
} }
format++; format++;
@ -505,7 +520,7 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
#if defined(PRINTF_FLOAT_SUPPORT) #if defined(PRINTF_FLOAT_SUPPORT)
case 'f' : case 'f' :
case 'F' : case 'F' :
idx += _ftoa(va_arg(va, double), &buffer[idx], buffer_len - idx, precision, width, flags); idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
format++; format++;
break; break;
#endif // PRINTF_FLOAT_SUPPORT #endif // PRINTF_FLOAT_SUPPORT
@ -513,16 +528,16 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
size_t l = 1U; size_t l = 1U;
// pre padding // pre padding
if (!(flags & FLAGS_LEFT)) { if (!(flags & FLAGS_LEFT)) {
while ((idx < buffer_len) && (l++ < width)) { while (l++ < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
// char output // char output
buffer[idx++] = (char)va_arg(va, int); out((char)va_arg(va, int), buffer, idx++, maxlen);
// post padding // post padding
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while ((idx < buffer_len) && (l++ < width)) { while (l++ < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
format++; format++;
@ -537,18 +552,18 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
l = (l < precision ? l : precision); l = (l < precision ? l : precision);
} }
if (!(flags & FLAGS_LEFT)) { if (!(flags & FLAGS_LEFT)) {
while ((idx < buffer_len) && (l++ < width)) { while (l++ < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
// string output // string output
while ((idx < buffer_len) && (*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
buffer[idx++] = *(p++); out(*(p++), buffer, idx++, maxlen);
} }
// post padding // post padding
if (flags & FLAGS_LEFT) { if (flags & FLAGS_LEFT) {
while ((idx < buffer_len) && (l++ < width)) { while (l++ < width) {
buffer[idx++] = ' '; out(' ', buffer, idx++, maxlen);
} }
} }
format++; format++;
@ -558,34 +573,34 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
case 'p' : { case 'p' : {
width = sizeof(void*) * 2U; width = sizeof(void*) * 2U;
flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
if (sizeof(uintptr_t) == sizeof(long long)) {
#if defined(PRINTF_LONG_LONG_SUPPORT) #if defined(PRINTF_LONG_LONG_SUPPORT)
idx += _ntoa_long_long(&buffer[idx], (uintptr_t)va_arg(va, void*), false, 16U, buffer_len - idx, precision, width, flags); if (sizeof(uintptr_t) == sizeof(long long)) {
#endif idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags);
} }
else { else {
idx += _ntoa_long(&buffer[idx], (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, buffer_len - idx, precision, width, flags); #endif
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags);
#if defined(PRINTF_LONG_LONG_SUPPORT)
} }
#endif
format++; format++;
break; break;
} }
case '%' : case '%' :
buffer[idx++] = '%'; out('%', buffer, idx++, maxlen);
format++; format++;
break; break;
default : default :
buffer[idx++] = *format; out(*format, buffer, idx++, maxlen);
format++; format++;
break; break;
} }
} }
// termination // termination
if (buffer_len > 0U) { out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
buffer[idx == buffer_len ? buffer_len - 1U : idx] = (char)0;
}
// return written chars without terminating \0 // return written chars without terminating \0
return (int)idx; return (int)idx;
@ -594,16 +609,13 @@ static int _vsnprintf(char* buffer, size_t buffer_len, const char* format, va_li
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
int printf(const char* format, ...) int printf(const char* format, ...)
{ {
va_list va; va_list va;
va_start(va, format); va_start(va, format);
char buffer[PRINTF_BUFFER_SIZE]; const int ret = _vsnprintf(_out_char, nullptr, (size_t)-1, format, va);
int ret = _vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, va);
va_end(va); va_end(va);
for (size_t i = 0U; i < ret; ++i) {
_putchar(buffer[i]);
}
return ret; return ret;
} }
@ -612,7 +624,7 @@ int sprintf(char* buffer, const char* format, ...)
{ {
va_list va; va_list va;
va_start(va, format); va_start(va, format);
int ret = _vsnprintf(buffer, (size_t)-1, format, va); const int ret = _vsnprintf(_out_buffer, buffer, (size_t)-1, format, va);
va_end(va); va_end(va);
return ret; return ret;
} }
@ -622,7 +634,7 @@ int snprintf(char* buffer, size_t count, const char* format, ...)
{ {
va_list va; va_list va;
va_start(va, format); va_start(va, format);
int ret = _vsnprintf(buffer, count, format, va); const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
va_end(va); va_end(va);
return ret; return ret;
} }
@ -630,6 +642,6 @@ int snprintf(char* buffer, size_t count, const char* format, ...)
inline int vsnprintf(char* buffer, size_t count, const char* format, va_list va) inline int vsnprintf(char* buffer, size_t count, const char* format, va_list va)
{ {
return _vsnprintf(buffer, count, format, va); return _vsnprintf(_out_buffer, buffer, count, format, va);
} }

@ -1016,6 +1016,14 @@ TEST_CASE("pointer", "[]" ) {
REQUIRE(!strcmp(buffer, "0000000012345678")); REQUIRE(!strcmp(buffer, "0000000012345678"));
} }
test::sprintf(buffer, "%p-%p", (void*)0x12345678U, (void*)0x7EDCBA98U);
if (sizeof(void*) == 4U) {
REQUIRE(!strcmp(buffer, "12345678-7EDCBA98"));
}
else {
REQUIRE(!strcmp(buffer, "0000000012345678-000000007EDCBA98"));
}
if (sizeof(uintptr_t) == sizeof(uint64_t)) { if (sizeof(uintptr_t) == sizeof(uint64_t)) {
test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU); test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
REQUIRE(!strcmp(buffer, "00000000FFFFFFFF")); REQUIRE(!strcmp(buffer, "00000000FFFFFFFF"));
@ -1039,17 +1047,15 @@ TEST_CASE("buffer length", "[]" ) {
char buffer[100]; char buffer[100];
int ret; int ret;
// formatted length, this should return '4',
// but this feature is not implemented, returning 0
ret = test::snprintf(nullptr, 10, "%s", "Test"); ret = test::snprintf(nullptr, 10, "%s", "Test");
REQUIRE(ret == -1); REQUIRE(ret == 4);
ret = test::snprintf(nullptr, 0, "%s", "Test"); ret = test::snprintf(nullptr, 0, "%s", "Test");
REQUIRE(ret == -1); REQUIRE(ret == 4);
buffer[0] = (char)0xA5; buffer[0] = (char)0xA5;
ret = test::snprintf(buffer, 0, "%s", "Test"); ret = test::snprintf(buffer, 0, "%s", "Test");
REQUIRE(buffer[0] == (char)0xA5); REQUIRE(buffer[0] == (char)0xA5);
REQUIRE(ret == 0); REQUIRE(ret == 4);
buffer[0] = 0xCC; buffer[0] = 0xCC;
test::snprintf(buffer, 1, "%s", "Test"); test::snprintf(buffer, 1, "%s", "Test");
@ -1074,13 +1080,13 @@ TEST_CASE("ret value", "[]" ) {
ret = test::snprintf(buffer, 6, "0%s", "1234567"); ret = test::snprintf(buffer, 6, "0%s", "1234567");
REQUIRE(!strcmp(buffer, "01234")); REQUIRE(!strcmp(buffer, "01234"));
REQUIRE(ret == 6); // '567' are truncated REQUIRE(ret == 8); // '567' are truncated
ret = test::snprintf(buffer, 10, "hello, world"); ret = test::snprintf(buffer, 10, "hello, world");
REQUIRE(ret == 10); REQUIRE(ret == 12);
ret = test::snprintf(buffer, 3, "%d", 10000); ret = test::snprintf(buffer, 3, "%d", 10000);
REQUIRE(ret == 3); // '000' are truncated REQUIRE(ret == 5);
REQUIRE(strlen(buffer) == 2U); REQUIRE(strlen(buffer) == 2U);
REQUIRE(buffer[0] == '1'); REQUIRE(buffer[0] == '1');
REQUIRE(buffer[1] == '0'); REQUIRE(buffer[1] == '0');

Loading…
Cancel
Save