lib/os/cbprintf_nano.c: several improvements

This makes cbprintf_nano.c much closer to the standard printf and
therefore more useful. The following are now implemented:

- right justification for everything (only for numbers previously)
- precision value for numbers, chars and strings
- width/precision passed as arguments with *
- "unlimited" padding length
- lower/uppercase hex output
- the #, + and ' ' flags are supported

And the code was heavily reworked to reduce its size as much as
possible to mitigate the size growth. Still, the binary resulting
from cbprintf_nano.c is now between 10% and 20% bigger depending on
the architecture. This is still far smaller than cbprintf_complete.c
which remains about twice as big on average even without FP support.

Many unit tests that were skipped with CONFIG_CBPRINTF_NANO are now
enabled, and a few more were added for good measure.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2021-02-18 17:35:13 -05:00 committed by Carles Cufí
commit fb73ac392c
3 changed files with 292 additions and 317 deletions

View file

@ -1,41 +1,49 @@
/* /*
* Copyright (c) 2010, 2013-2014 Wind River Systems, Inc. * Copyright (c) 2010, 2013-2014 Wind River Systems, Inc.
* Copyright (c) 2020 Nordic Semiconductor ASA * Copyright (c) 2020 Nordic Semiconductor ASA
* Copyright (c) 2021 BayLibre, SAS
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <sys/cbprintf.h> #include <sys/cbprintf.h>
#include <toolchain.h>
#include <linker/sections.h>
#include <syscall_handler.h>
#include <logging/log.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/util.h>
enum pad_type {
PAD_NONE,
PAD_ZERO_BEFORE,
PAD_SPACE_BEFORE,
PAD_SPACE_AFTER,
};
#ifdef CONFIG_CBPRINTF_FULL_INTEGRAL #ifdef CONFIG_CBPRINTF_FULL_INTEGRAL
typedef intmax_t int_value_type; typedef intmax_t int_value_type;
typedef uintmax_t uint_value_type; typedef uintmax_t uint_value_type;
#define DIGITS_BUFLEN 21
BUILD_ASSERT(sizeof(uint_value_type) <= 8U,
"DIGITS_BUFLEN may be incorrect");
#else #else
typedef int32_t int_value_type; typedef int32_t int_value_type;
typedef uint32_t uint_value_type; typedef uint32_t uint_value_type;
#define DIGITS_BUFLEN 10
#endif #endif
/* Maximum number of digits in a printed decimal value (hex is always #define ALPHA(fmt) (((fmt) & 0x60) - '0' - 10 + 1)
* less, obviously). Funny formula produces 10 max digits for 32 bit,
* 21 for 64. It may be incorrect for other value lengths.
*/
#define DIGITS_BUFLEN (11U * (sizeof(uint_value_type) / 4U) - 1U)
BUILD_ASSERT(sizeof(uint_value_type) <= 8U, /* Convert value to string, storing characters downwards */
"DIGITS_BUFLEN formula may be incorrect"); static inline int convert_value(uint_value_type num, unsigned int base,
unsigned int alpha, char *buftop)
{
int i = 0;
do {
unsigned int c = num % base;
if (c >= 10) {
c += alpha;
}
buftop[--i] = c + '0';
num /= base;
} while (num);
return -i;
}
#define OUTC(_c) do { \ #define OUTC(_c) do { \
out((int)(_c), ctx); \ out((int)(_c), ctx); \
@ -44,76 +52,8 @@ BUILD_ASSERT(sizeof(uint_value_type) <= 8U,
} \ } \
} while (false) } while (false)
static void print_digits(cbprintf_cb out, void *ctx, uint_value_type num, #define PAD_ZERO BIT(0)
unsigned int base, bool pad_before, char pad_char, #define PAD_TAIL BIT(1)
int min_width, size_t *countp)
{
size_t count = 0;
char buf[DIGITS_BUFLEN];
int i = 0;
/* Print it backwards, low digits first */
do {
char c = num % base;
if (c >= 10) {
c += 'a' - '0' - 10;
}
buf[i++] = c + '0';
num /= base;
} while (num);
int pad = MAX(min_width - i, 0);
for (/**/; pad > 0 && pad_before; pad--) {
OUTC(pad_char);
}
do {
OUTC(buf[--i]);
} while (i > 0);
for (/**/; pad > 0; pad--) {
OUTC(pad_char);
}
*countp += count;
}
static void print_hex(cbprintf_cb out, void *ctx, uint_value_type num,
enum pad_type padding, int min_width, size_t *countp)
{
print_digits(out, ctx, num, 16U, padding != PAD_SPACE_AFTER,
padding == PAD_ZERO_BEFORE ? '0' : ' ', min_width,
countp);
}
static void print_dec(cbprintf_cb out, void *ctx, uint_value_type num,
enum pad_type padding, int min_width, size_t *countp)
{
print_digits(out, ctx, num, 10U, padding != PAD_SPACE_AFTER,
padding == PAD_ZERO_BEFORE ? '0' : ' ', min_width,
countp);
}
static bool ok64(cbprintf_cb out, void *ctx, long long val, size_t *countp)
{
size_t count = 0;
if (sizeof(int_value_type) < 8U && val != (int_value_type) val) {
const char *cp = "ERR";
do {
OUTC(*cp++);
} while (*cp);
*countp += count;
return false;
}
return true;
}
static bool negative(uint_value_type val)
{
const uint_value_type hibit = ~(((uint_value_type) ~1) >> 1U);
return (val & hibit) != 0U;
}
/** /**
* @brief Printk internals * @brief Printk internals
@ -122,175 +62,238 @@ static bool negative(uint_value_type val)
* @param fmt Format string * @param fmt Format string
* @param ap Variable parameters * @param ap Variable parameters
* *
* @return N/A * @return printed byte count if CONFIG_CBPRINTF_LIBC_SUBSTS is set
*/ */
int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap) int cbvprintf(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
{ {
size_t count = 0; size_t count = 0;
int might_format = 0; /* 1 if encountered a '%' */ char buf[DIGITS_BUFLEN];
enum pad_type padding = PAD_NONE; char *prefix, *data;
int padlen, min_width = -1; int min_width, precision, data_len;
char length_mod = 0; char padding_mode, length_mod, special;
/* fmt has already been adjusted if needed */ /* we pre-increment in the loop afterwards */
while (*fmt) { fmt--;
if (!might_format) {
if (*fmt != '%') { start:
OUTC(*fmt); while (*++fmt != '%') {
} else { if (*fmt == 0) {
might_format = 1; return count;
min_width = -1; }
padding = PAD_NONE; OUTC(*fmt);
length_mod = 0; }
min_width = -1;
precision = -1;
prefix = "";
padding_mode = 0;
length_mod = 0;
special = 0;
for (fmt++ ; ; fmt++) {
switch (*fmt) {
case 0:
return count;
case '%':
OUTC('%');
goto start;
case '-':
padding_mode = PAD_TAIL;
continue;
case '.':
precision = 0;
padding_mode &= ~PAD_ZERO;
continue;
case '0':
if (min_width < 0 && precision < 0 && !padding_mode) {
padding_mode = PAD_ZERO;
continue;
} }
} else { __fallthrough;
switch (*fmt) {
case '-': case '1':
padding = PAD_SPACE_AFTER; case '2':
goto still_might_format; case '3':
case '0': case '4':
if (min_width < 0 && padding == PAD_NONE) { case '5':
padding = PAD_ZERO_BEFORE; case '6':
goto still_might_format; case '7':
} case '8':
__fallthrough; case '9':
case '1': if (precision >= 0) {
case '2': precision = 10 * precision + *fmt - '0';
case '3': } else {
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (min_width < 0) { if (min_width < 0) {
min_width = 0; min_width = 0;
} }
min_width = 10 * min_width + *fmt - '0'; min_width = 10 * min_width + *fmt - '0';
if (padding == PAD_NONE) {
padding = PAD_SPACE_BEFORE;
}
goto still_might_format;
case 'h':
case 'l':
case 'z':
if (*fmt == 'h' && length_mod == 'h') {
length_mod = 'H';
} else if (*fmt == 'l' && length_mod == 'l') {
length_mod = 'L';
} else if (length_mod == 0) {
length_mod = *fmt;
} else {
OUTC('%');
OUTC(*fmt);
break;
}
goto still_might_format;
case 'd':
case 'i':
case 'u': {
uint_value_type d;
if (length_mod == 'z') {
d = va_arg(ap, ssize_t);
} else if (length_mod == 'l') {
d = va_arg(ap, long);
} else if (length_mod == 'L') {
long long lld = va_arg(ap,
long long);
if (!ok64(out, ctx, lld, &count)) {
break;
}
d = (uint_value_type) lld;
} else if (*fmt == 'u') {
d = va_arg(ap, unsigned int);
} else {
d = va_arg(ap, int);
}
if (*fmt != 'u' && negative(d)) {
OUTC('-');
d = -d;
min_width--;
}
print_dec(out, ctx, d, padding, min_width,
&count);
break;
} }
case 'p': continue;
case 'x':
case 'X': {
uint_value_type x;
if (*fmt == 'p') { case '*':
const char *cp; if (precision >= 0) {
precision = va_arg(ap, int);
x = (uintptr_t)va_arg(ap, void *); } else {
if (x == 0) { min_width = va_arg(ap, int);
cp = "(nil)"; if (min_width < 0) {
} else { min_width = -min_width;
cp = "0x"; padding_mode = PAD_TAIL;
}
do {
OUTC(*cp++);
} while (*cp);
if (x == 0) {
padlen = min_width - 5;
goto pad_string;
}
min_width -= 2;
} else if (length_mod == 'l') {
x = va_arg(ap, unsigned long);
} else if (length_mod == 'L') {
x = va_arg(ap, unsigned long long);
} else {
x = va_arg(ap, unsigned int);
} }
print_hex(out, ctx, x, padding, min_width,
&count);
break;
} }
case 's': { continue;
char *s = va_arg(ap, char *);
char *start = s;
while (*s) { case '+':
OUTC(*s++); case ' ':
} case '#':
padlen = min_width - (s - start); special = *fmt;
continue;
pad_string: case 'h':
if (padding == PAD_SPACE_AFTER) { case 'l':
while (padlen-- > 0) { case 'z':
OUTC(' '); if (*fmt == 'h' && length_mod == 'h') {
} length_mod = 'H';
} } else if (*fmt == 'l' && length_mod == 'l') {
break; length_mod = 'L';
} } else if (length_mod == 0) {
case 'c': { length_mod = *fmt;
int c = va_arg(ap, int); } else {
OUTC(c);
break;
}
case '%': {
OUTC('%');
break;
}
default:
OUTC('%'); OUTC('%');
OUTC(*fmt); OUTC(*fmt);
break; goto start;
} }
might_format = 0; continue;
}
still_might_format:
++fmt;
}
return count; case 'd':
case 'i':
case 'u': {
uint_value_type d;
if (length_mod == 'z') {
d = va_arg(ap, ssize_t);
} else if (length_mod == 'l') {
d = va_arg(ap, long);
} else if (length_mod == 'L') {
long long lld = va_arg(ap, long long);
if (sizeof(int_value_type) < 8U &&
lld != (int_value_type) lld) {
data = "ERR";
data_len = 3;
precision = 0;
break;
}
d = (uint_value_type) lld;
} else if (*fmt == 'u') {
d = va_arg(ap, unsigned int);
} else {
d = va_arg(ap, int);
}
if (*fmt != 'u' && (int_value_type)d < 0) {
d = -d;
prefix = "-";
min_width--;
} else if (special == ' ') {
prefix = " ";
min_width--;
} else if (special == '+') {
prefix = "+";
min_width--;
}
data_len = convert_value(d, 10, 0, buf + sizeof(buf));
data = buf + sizeof(buf) - data_len;
break;
}
case 'p':
case 'x':
case 'X': {
uint_value_type x;
if (*fmt == 'p') {
x = (uintptr_t)va_arg(ap, void *);
if (x == 0) {
data = "(nil)";
data_len = 5;
precision = 0;
break;
}
special = '#';
} else if (length_mod == 'l') {
x = va_arg(ap, unsigned long);
} else if (length_mod == 'L') {
x = va_arg(ap, unsigned long long);
} else {
x = va_arg(ap, unsigned int);
}
if (special == '#') {
prefix = (*fmt & 0x20) ? "0x" : "0X";
min_width -= 2;
}
data_len = convert_value(x, 16, ALPHA(*fmt),
buf + sizeof(buf));
data = buf + sizeof(buf) - data_len;
break;
}
case 's': {
data = va_arg(ap, char *);
data_len = strlen(data);
if (precision >= 0 && data_len > precision) {
data_len = precision;
}
precision = 0;
break;
}
case 'c': {
int c = va_arg(ap, int);
buf[0] = c;
data = buf;
data_len = 1;
precision = 0;
break;
}
default:
OUTC('%');
OUTC(*fmt);
goto start;
}
if (precision < 0 && (padding_mode & PAD_ZERO)) {
precision = min_width;
}
min_width -= data_len;
precision -= data_len;
if (precision > 0) {
min_width -= precision;
}
if (!(padding_mode & PAD_TAIL)) {
while (--min_width >= 0) {
OUTC(' ');
}
}
while (*prefix) {
OUTC(*prefix++);
}
while (--precision >= 0) {
OUTC('0');
}
while (--data_len >= 0) {
OUTC(*data++);
}
while (--min_width >= 0) {
OUTC(' ');
}
goto start;
}
} }

View file

@ -117,8 +117,7 @@ static void help_item_print(const struct shell *shell, const char *item_name,
} }
if (!IS_ENABLED(CONFIG_NEWLIB_LIBC) && if (!IS_ENABLED(CONFIG_NEWLIB_LIBC) &&
!IS_ENABLED(CONFIG_ARCH_POSIX) && !IS_ENABLED(CONFIG_ARCH_POSIX)) {
!IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
/* print option name */ /* print option name */
z_shell_fprintf(shell, SHELL_NORMAL, "%s%-*s%s:", tabulator, z_shell_fprintf(shell, SHELL_NORMAL, "%s%-*s%s:", tabulator,
item_name_width, item_name, tabulator); item_name_width, item_name, tabulator);

View file

@ -301,18 +301,6 @@ static void test_c(void)
rc = TEST_PRF("%c", 'a'); rc = TEST_PRF("%c", 'a');
PRF_CHECK("a", rc); PRF_CHECK("a", rc);
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("short test for nano\n");
return;
}
rc = TEST_PRF("%lc", (wint_t)'a');
if (ENABLED_USE_LIBC) {
PRF_CHECK("a", rc);
} else {
PRF_CHECK("%lc", rc);
}
rc = prf("/%256c/", 'a'); rc = prf("/%256c/", 'a');
const char *bp = buf; const char *bp = buf;
@ -328,6 +316,18 @@ static void test_c(void)
zassert_equal(*bp, 'a', NULL); zassert_equal(*bp, 'a', NULL);
++bp; ++bp;
zassert_equal(*bp, '/', NULL); zassert_equal(*bp, '/', NULL);
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("short test for nano\n");
return;
}
rc = TEST_PRF("%lc", (wint_t)'a');
if (ENABLED_USE_LIBC) {
PRF_CHECK("a", rc);
} else {
PRF_CHECK("%lc", rc);
}
} }
static void test_s(void) static void test_s(void)
@ -340,20 +340,16 @@ static void test_s(void)
PRF_CHECK("/123/", rc); PRF_CHECK("/123/", rc);
rc = TEST_PRF("/%6s/%-6s/%2s/", s, s, s); rc = TEST_PRF("/%6s/%-6s/%2s/", s, s, s);
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) { PRF_CHECK("/ 123/123 /123/", rc);
PRF_CHECK("/123/123 /123/", rc);
} else { rc = TEST_PRF("/%.6s/%.2s/%.s/", s, s, s);
PRF_CHECK("/ 123/123 /123/", rc); PRF_CHECK("/123/12//", rc);
}
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) { if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("short test for nano\n"); TC_PRINT("short test for nano\n");
return; return;
} }
rc = TEST_PRF("/%.6s/%.2s/%.s/", s, s, s);
PRF_CHECK("/123/12//", rc);
rc = TEST_PRF("%ls", ws); rc = TEST_PRF("%ls", ws);
if (ENABLED_USE_LIBC) { if (ENABLED_USE_LIBC) {
PRF_CHECK("abc", rc); PRF_CHECK("abc", rc);
@ -381,11 +377,15 @@ static void test_d_length(void)
int min = -1234567890; int min = -1234567890;
int max = 1876543210; int max = 1876543210;
long long svll = 123LL << 48; long long svll = 123LL << 48;
long long svll2 = -2LL;
int rc; int rc;
rc = TEST_PRF("%d/%d", min, max); rc = TEST_PRF("%d/%d", min, max);
PRF_CHECK("-1234567890/1876543210", rc); PRF_CHECK("-1234567890/1876543210", rc);
rc = TEST_PRF("%u/%u", min, max);
PRF_CHECK("3060399406/1876543210", rc);
if (!IS_ENABLED(CONFIG_CBPRINTF_NANO)) { if (!IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
rc = TEST_PRF("%hd/%hd", min, max); rc = TEST_PRF("%hd/%hd", min, max);
PRF_CHECK("-722/-14614", rc); PRF_CHECK("-722/-14614", rc);
@ -403,13 +403,13 @@ static void test_d_length(void)
PRF_CHECK("%ld/%ld", rc); PRF_CHECK("%ld/%ld", rc);
} }
rc = TEST_PRF("/%lld/%lld/", svll, -svll); rc = TEST_PRF("/%lld/%lld/%lld/", svll, -svll, svll2);
if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)) { if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)) {
PRF_CHECK("/34621422135410688/-34621422135410688/", rc); PRF_CHECK("/34621422135410688/-34621422135410688/-2/", rc);
} else if (IS_ENABLED(CONFIG_CBPRINTF_COMPLETE)) { } else if (IS_ENABLED(CONFIG_CBPRINTF_COMPLETE)) {
PRF_CHECK("/%lld/%lld/", rc); PRF_CHECK("/%lld/%lld/%lld/", rc);
} else if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) { } else if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
PRF_CHECK("/ERR/ERR/", rc); PRF_CHECK("/ERR/ERR/-2/", rc);
} else { } else {
zassert_true(false, "Missed case!"); zassert_true(false, "Missed case!");
} }
@ -459,11 +459,6 @@ static void test_d_flags(void)
int sv = 123; int sv = 123;
int rc; int rc;
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("skipped test for nano\n");
return;
}
/* Stuff related to sign */ /* Stuff related to sign */
rc = TEST_PRF("/%d/%-d/%+d/% d/", rc = TEST_PRF("/%d/%-d/%+d/% d/",
sv, sv, sv, sv); sv, sv, sv, sv);
@ -512,10 +507,15 @@ static void test_x_length(void)
int rc; int rc;
rc = TEST_PRF("%x/%X", min, max); rc = TEST_PRF("%x/%X", min, max);
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) { PRF_CHECK("4c3c2c1c/4D3D2D1D", rc);
PRF_CHECK("4c3c2c1c/4d3d2d1d", rc);
} else { rc = TEST_PRF("%lx/%lX", (unsigned long)min, (unsigned long)max);
if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)
|| (sizeof(long) <= 4)
|| IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
PRF_CHECK("4c3c2c1c/4D3D2D1D", rc); PRF_CHECK("4c3c2c1c/4D3D2D1D", rc);
} else {
PRF_CHECK("%lx/%lX", rc);
} }
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) { if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
@ -529,14 +529,6 @@ static void test_x_length(void)
rc = TEST_PRF("%hhx/%hhX", min, max); rc = TEST_PRF("%hhx/%hhX", min, max);
PRF_CHECK("1c/1D", rc); PRF_CHECK("1c/1D", rc);
rc = TEST_PRF("%lx/%lX", (unsigned long)min, (unsigned long)max);
if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)
|| (sizeof(long) <= 4)) {
PRF_CHECK("4c3c2c1c/4D3D2D1D", rc);
} else {
PRF_CHECK("%lx/%lX", rc);
}
if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)) { if (IS_ENABLED(CONFIG_CBPRINTF_FULL_INTEGRAL)) {
rc = TEST_PRF("%llx/%llX", (unsigned long long)min, rc = TEST_PRF("%llx/%llX", (unsigned long long)min,
(unsigned long long)max); (unsigned long long)max);
@ -578,11 +570,6 @@ static void test_x_flags(void)
unsigned int sv = 0x123; unsigned int sv = 0x123;
int rc; int rc;
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("skipped test for nano\n");
return;
}
/* Stuff related to sign flags, which have no effect on /* Stuff related to sign flags, which have no effect on
* unsigned conversions, and alternate form * unsigned conversions, and alternate form
*/ */
@ -891,11 +878,6 @@ static void test_fp_flags(void)
static void test_star_width(void) static void test_star_width(void)
{ {
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("skipped test for nano\n");
return;
}
int rc; int rc;
rc = TEST_PRF("/%3c/%-3c/", 'a', 'a'); rc = TEST_PRF("/%3c/%-3c/", 'a', 'a');
@ -907,17 +889,17 @@ static void test_star_width(void)
static void test_star_precision(void) static void test_star_precision(void)
{ {
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("skipped test for nano\n");
return;
}
int rc; int rc;
rc = TEST_PRF("/%.*x/%10.*x/", rc = TEST_PRF("/%.*x/%10.*x/",
5, 0x12, 5, 0x12); 5, 0x12, 5, 0x12);
PRF_CHECK("/00012/ 00012/", rc); PRF_CHECK("/00012/ 00012/", rc);
if (IS_ENABLED(CONFIG_CBPRINTF_NANO)) {
TC_PRINT("short test for nano\n");
return;
}
if (IS_ENABLED(CONFIG_CBPRINTF_FP_SUPPORT)) { if (IS_ENABLED(CONFIG_CBPRINTF_FP_SUPPORT)) {
double dv = 1.2345678; double dv = 1.2345678;
@ -1001,20 +983,15 @@ static void test_p(void)
rc = TEST_PRF("%p", NULL); rc = TEST_PRF("%p", NULL);
PRF_CHECK("(nil)", rc); PRF_CHECK("(nil)", rc);
/* Nano doesn't support left-justification of pointer reset_out();
* values. rc = rawprf("/%12p/", ptr);
*/ zassert_equal(rc, 14, NULL);
if (!IS_ENABLED(CONFIG_CBPRINTF_NANO)) { zassert_equal(strncmp("/ 0xcafe21/", buf, rc), 0, NULL);
reset_out();
rc = rawprf("/%12p/", ptr);
zassert_equal(rc, 14, NULL);
zassert_equal(strncmp("/ 0xcafe21/", buf, rc), 0, NULL);
reset_out(); reset_out();
rc = rawprf("/%12p/", NULL); rc = rawprf("/%12p/", NULL);
zassert_equal(rc, 14, NULL); zassert_equal(rc, 14, NULL);
zassert_equal(strncmp("/ (nil)/", buf, rc), 0, NULL); zassert_equal(strncmp("/ (nil)/", buf, rc), 0, NULL);
}
reset_out(); reset_out();
rc = rawprf("/%-12p/", ptr); rc = rawprf("/%-12p/", ptr);
@ -1026,14 +1003,10 @@ static void test_p(void)
zassert_equal(rc, 14, NULL); zassert_equal(rc, 14, NULL);
zassert_equal(strncmp("/(nil) /", buf, rc), 0, NULL); zassert_equal(strncmp("/(nil) /", buf, rc), 0, NULL);
/* Nano doesn't support zero-padding of pointer values. reset_out();
*/ rc = rawprf("/%.8p/", ptr);
if (!IS_ENABLED(CONFIG_CBPRINTF_NANO)) { zassert_equal(rc, 12, NULL);
reset_out(); zassert_equal(strncmp("/0x00cafe21/", buf, rc), 0, NULL);
rc = rawprf("/%.8p/", ptr);
zassert_equal(rc, 12, NULL);
zassert_equal(strncmp("/0x00cafe21/", buf, rc), 0, NULL);
}
} }
static int out_counter(int c, static int out_counter(int c,