Skip to content

Commit ddd3b01

Browse files
authored
Merge pull request #12632 from evedon/ed-minimal-snprintf
Fix snprintf in minimal-printf library
2 parents b62fdae + a3c3656 commit ddd3b01

File tree

2 files changed

+24
-58
lines changed

2 files changed

+24
-58
lines changed

TESTS/mbed_platform/minimal-printf/compliance/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,11 @@ static control_t test_snprintf_d(const size_t call_count)
635635
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);
636636
#endif
637637

638+
int a = 2;
639+
int b = 3;
640+
result_minimal = mbed_snprintf(0, 0, "%d + %d = %d\n", a, b, a + b);
641+
TEST_ASSERT_EQUAL_INT(10, result_minimal);
642+
638643
return CaseNext;
639644
}
640645

platform/source/minimal-printf/mbed_printf_implementation.c

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@
2222
#include <stdint.h>
2323
#include <stddef.h>
2424

25-
/***************************/
26-
/* MBED */
27-
/***************************/
28-
#if TARGET_LIKE_MBED
29-
30-
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
31-
static char mbed_stdio_out_prev = 0;
32-
#endif
33-
34-
35-
/***************************/
36-
/* Linux */
37-
/***************************/
38-
#else
25+
#if !TARGET_LIKE_MBED
3926
/* Linux implementation is for debug only */
4027
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT 1
4128
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_SET_FLOATING_POINT_MAX_DECIMALS 6
@@ -106,7 +93,6 @@ static void mbed_minimal_formatted_string_signed(char *buffer, size_t length, in
10693
static void mbed_minimal_formatted_string_unsigned(char *buffer, size_t length, int *result, MBED_UNSIGNED_STORAGE value, FILE *stream);
10794
static void mbed_minimal_formatted_string_hexadecimal(char *buffer, size_t length, int *result, MBED_UNSIGNED_STORAGE value, FILE *stream, bool upper);
10895
static void mbed_minimal_formatted_string_void_pointer(char *buffer, size_t length, int *result, const void *value, FILE *stream);
109-
static void mbed_minimal_formatted_string_character(char *buffer, size_t length, int *result, char character, FILE *stream);
11096
static void mbed_minimal_formatted_string_string(char *buffer, size_t length, int *result, const char *string, size_t precision, FILE *stream);
11197

11298

@@ -122,21 +108,23 @@ static void mbed_minimal_putchar(char *buffer, size_t length, int *result, char
122108
{
123109
/* only continue if 'result' doesn't overflow */
124110
if ((*result >= 0) && (*result <= INT_MAX - 1)) {
125-
if (buffer) {
126-
/* write data only if there's enough space */
127-
if ((size_t)*result < length) {
128-
buffer[*result] = data;
129-
}
130-
131-
/* increment 'result' even if data was not written. This ensures that
132-
'mbed_minimal_formatted_string' returns the correct value. */
133-
*result += 1;
134-
} else {
111+
if (stream) {
135112
if (fputc(data, stream) == EOF) {
136113
*result = EOF;
137114
} else {
138115
*result += 1;
139116
}
117+
} else {
118+
if (buffer) {
119+
/* write data only if there's enough space */
120+
if ((size_t)*result < length) {
121+
buffer[*result] = data;
122+
}
123+
}
124+
125+
/* increment 'result' even if data was not written. This ensures that
126+
'mbed_minimal_formatted_string' returns the correct value. */
127+
*result += 1;
140128
}
141129
}
142130
}
@@ -280,7 +268,7 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
280268
mbed_minimal_formatted_string_signed(buffer, length, result, integer, stream);
281269

282270
/* write decimal point */
283-
mbed_minimal_formatted_string_character(buffer, length, result, '.', stream);
271+
mbed_minimal_putchar(buffer, length, result, '.', stream);
284272

285273
/* get decimal part */
286274
double precision = 1.0;
@@ -325,33 +313,6 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
325313
}
326314
#endif
327315

328-
/**
329-
* @brief Print character.
330-
*
331-
* @param buffer The buffer to store output (NULL for stdout).
332-
* @param[in] length The length of the buffer.
333-
* @param result The current output location.
334-
* @param[in] value The character to be printed.
335-
*/
336-
static void mbed_minimal_formatted_string_character(char *buffer, size_t length, int *result, char character, FILE *stream)
337-
{
338-
/* write character */
339-
if (buffer) {
340-
mbed_minimal_putchar(buffer, length, result, character, stream);
341-
} else {
342-
/* convert \n to \r\n if enabled in platform configuration */
343-
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
344-
if (character == '\n' && mbed_stdio_out_prev != '\r') {
345-
mbed_minimal_putchar(buffer, length, result, '\r', stream);
346-
}
347-
348-
/* cache character */
349-
mbed_stdio_out_prev = character;
350-
#endif
351-
mbed_minimal_putchar(buffer, length, result, character, stream);
352-
}
353-
}
354-
355316
/**
356317
* @brief Print string with precision.
357318
*
@@ -511,7 +472,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
511472
#else
512473
/* If 64 bit is not enabled, print %ll[di] rather than truncated value */
513474
if (length_modifier == LENGTH_LL) {
514-
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
475+
mbed_minimal_putchar(buffer, length, &result, '%', stream);
515476
if (next == '%') {
516477
// Continue printing loop after `%`
517478
index = next_index;
@@ -570,7 +531,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
570531
#else
571532
/* If 64 bit is not enabled, print %ll[uxX] rather than truncated value */
572533
if (length_modifier == LENGTH_LL) {
573-
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
534+
mbed_minimal_putchar(buffer, length, &result, '%', stream);
574535
if (next == '%') {
575536
// Continue printing loop after `%`
576537
index = next_index;
@@ -636,7 +597,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
636597
char value = va_arg(arguments, MBED_SIGNED_NATIVE_TYPE);
637598
index = next_index;
638599

639-
mbed_minimal_formatted_string_character(buffer, length, &result, value, stream);
600+
mbed_minimal_putchar(buffer, length, &result, value, stream);
640601
}
641602
/* string */
642603
else if (next == 's') {
@@ -653,7 +614,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
653614
mbed_minimal_formatted_string_void_pointer(buffer, length, &result, value, stream);
654615
} else {
655616
// Unrecognised, or `%%`. Print the `%` that led us in.
656-
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
617+
mbed_minimal_putchar(buffer, length, &result, '%', stream);
657618
if (next == '%') {
658619
// Continue printing loop after `%%`
659620
index = next_index;
@@ -665,7 +626,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
665626
/* not a format specifier */
666627
{
667628
/* write normal character */
668-
mbed_minimal_formatted_string_character(buffer, length, &result, format[index], stream);
629+
mbed_minimal_putchar(buffer, length, &result, format[index], stream);
669630
}
670631
}
671632

0 commit comments

Comments
 (0)