prf.c: remove buffer limitation on field width and string copy
The z_prf() function currently allocates a 200-byte buffer on the stack to copy strings into, and then perform left/right alignment and padding. Not only this is a pretty large chunk of stack usage, but this imposes limitations on field width and string length. Also the string is copied not only once but _thrice_ making this code less than optimal. Let's rework the code to get rid of both the field width limit and string length limit, as well as the two extra memory copy instances. While at it, let's fixes printf("%08s", "abcd") which used to produce "0000abcd". Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
parent
4ad2a8f990
commit
33312cfd98
2 changed files with 47 additions and 80 deletions
|
@ -45,8 +45,6 @@
|
|||
"66666666666666666666666666666666" \
|
||||
"666666666666666666666666666666666"
|
||||
|
||||
#define PRINTF_MAX_STRING_LENGTH 200
|
||||
|
||||
union raw_double_u {
|
||||
double d;
|
||||
struct {
|
||||
|
@ -580,7 +578,6 @@ void test_sprintf_integer(void)
|
|||
|
||||
void test_sprintf_string(void)
|
||||
{
|
||||
int len;
|
||||
char buffer[400];
|
||||
|
||||
sprintf(buffer, "%%");
|
||||
|
@ -596,19 +593,9 @@ void test_sprintf_string(void)
|
|||
"sprintf(%%s). "
|
||||
"Expected 'short string', got '%s'\n", buffer);
|
||||
|
||||
len = sprintf(buffer, "%s", REALLY_LONG_STRING);
|
||||
#if !defined CONFIG_NEWLIB_LIBC && !defined CONFIG_ARCH_POSIX
|
||||
zassert_true((len == PRINTF_MAX_STRING_LENGTH),
|
||||
"Internals changed. "
|
||||
"Max string length no longer %d got %d\n",
|
||||
PRINTF_MAX_STRING_LENGTH, len);
|
||||
#endif
|
||||
zassert_true((strncmp(buffer, REALLY_LONG_STRING,
|
||||
PRINTF_MAX_STRING_LENGTH) == 0),
|
||||
"First %d characters of REALLY_LONG_STRING "
|
||||
"not printed!\n",
|
||||
PRINTF_MAX_STRING_LENGTH);
|
||||
|
||||
sprintf(buffer, "%s", REALLY_LONG_STRING);
|
||||
zassert_true((strcmp(buffer, REALLY_LONG_STRING) == 0),
|
||||
"sprintf(%%s) of REALLY_LONG_STRING doesn't match!\n");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue