Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 29, 2020
1 parent 703527a commit 9a56913
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 86 deletions.
141 changes: 75 additions & 66 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ size_t tests_passed, tests_failed;
* \brief Run printf with built-in and custom implementation.
* Compare results on returned length and actual content
*
* \param[in] expected: Expected result
* \param[in] fmt: Format to use
* \param[in] ...: Optional parameters
*/
static void
printf_run(const char* fmt, ...) {
printf_run(const char* expected, const char* fmt, ...) {
HANDLE console;
va_list va;
char b1[255] = { 0 }, b2[255] = { 0 };
Expand Down Expand Up @@ -68,88 +69,96 @@ main(void) {

additional_format_specifiers();

printf_run("% 3u", (unsigned)28);
printf_run("% 3u", (unsigned)123456);
printf_run("%03d", (unsigned)28);
printf_run("%+03d", (unsigned)28);
printf_run("%+3d", (unsigned)28);
printf_run("%03d", -28);
printf_run("%+03d", -28);
printf_run("%+3d", -28);
printf_run("%03u", (unsigned)123456);
printf_run("%-010uabc", (unsigned)123456);
printf_run("%010uabc", (unsigned)123456);
printf_run("%-10d", -123);
printf_run("%10d", -123);
printf_run("%-06d", -1234567);
printf_run("%06d", -1234567);
printf_run("%s", "This is my string");
printf_run("%10s", "This is my string");
printf_run("%0*d", 10, -123);
printf_run("%zu", (size_t)10);
printf_run("%ju", (uintmax_t)10);
printf_run(" 28", "% 3u", (unsigned)28);
printf_run("123456", "% 3u", (unsigned)123456);
printf_run("028", "%03d", (unsigned)28);
printf_run("+28", "%+03d", (unsigned)28);
printf_run("+28", "%+3d", (unsigned)28);
printf_run("-28", "%03d", -28);
printf_run(NULL, "%+03d", -28);
printf_run(NULL, "%+3d", -28);
printf_run(NULL, "%03u", (unsigned)123456);
printf_run(NULL, "%-010uabc", (unsigned)123456);
printf_run(NULL, "%010uabc", (unsigned)123456);
printf_run(NULL, "%-10d", -123);
printf_run(NULL, "%10d", -123);
printf_run(NULL, "%-06d", -1234567);
printf_run(NULL, "%06d", -1234567);
printf_run(NULL, "%s", "This is my string");
printf_run(NULL, "%10s", "This is my string");
printf_run(NULL, "%0*d", 10, -123);
printf_run(NULL, "%zu", (size_t)10);
printf_run(NULL, "%ju", (uintmax_t)10);

/* string */
printf_run("%*.*s", 8, 12, "This is my string");
printf_run("%*.*s", 8, 12, "Stri");
printf_run("%-6.10s", "This is my string");
printf_run("%6.10s", "This is my string");
printf_run("%-6.10s", "This is my string");
printf_run("%6.10s", "Th");
printf_run("%-6.10s", "Th");
printf_run("%*.*s", -6, 10, "Th");
printf_run("%*.*s", 6, 10, "Th");

printf_run("%.4s", "This is my string");
printf_run("%.6s", "1234");
printf_run("%.4s", "stri");
printf_run("%.4s%.2s", "123456", "abcdef");
printf_run("%.4.2s", "123456");
printf_run("%.*s", 3, "123456");
printf_run("%.3s", "");
printf_run("%yunknown");
printf_run(NULL, "%*.*s", 8, 12, "This is my string");
printf_run(NULL, "%*.*s", 8, 12, "Stri");
printf_run(NULL, "%-6.10s", "This is my string");
printf_run(NULL, "%6.10s", "This is my string");
printf_run(NULL, "%-6.10s", "This is my string");
printf_run(NULL, "%6.10s", "Th");
printf_run(NULL, "%-6.10s", "Th");
printf_run(NULL, "%*.*s", -6, 10, "Th");
printf_run(NULL, "%*.*s", 6, 10, "Th");

printf_run(NULL, "%.4s", "This is my string");
printf_run(NULL, "%.6s", "1234");
printf_run(NULL, "%.4s", "stri");
printf_run(NULL, "%.4s%.2s", "123456", "abcdef");
printf_run(NULL, "%.4.2s", "123456");
printf_run(NULL, "%.*s", 3, "123456");
printf_run(NULL, "%.3s", "");
printf_run(NULL, "%yunknown");

/* Alternate form */
printf_run("%#2X", 123);
printf_run("%#2x", 123);
printf_run("%#2o", 123);
printf_run("%#2X", 1);
printf_run("%#2x", 1);
printf_run("%#2o", 1);
printf_run("%#2X", 0);
printf_run("%#2x", 0);
printf_run("%#2o", 0);
printf_run(NULL, "%#2X", 123);
printf_run(NULL, "%#2x", 123);
printf_run(NULL, "%#2o", 123);
printf_run(NULL, "%#2X", 1);
printf_run(NULL, "%#2x", 1);
printf_run(NULL, "%#2o", 1);
printf_run(NULL, "%#2X", 0);
printf_run(NULL, "%#2x", 0);
printf_run(NULL, "%#2o", 0);
printf_run(NULL, "%#2B", 1);
printf_run(NULL, "%#2b", 1);
printf_run(NULL, "%#2B", 0);
printf_run(NULL, "%#2b", 0);
printf_run(NULL, "%#B", 0);
printf_run(NULL, "%#b", 0);
printf_run(NULL, "%#B", 6);
printf_run(NULL, "%#b", 6);

/* Pointers */
printf_run("%p", &tests_passed);
printf_run("0X%p", &tests_passed);
printf_run("0x%p", &tests_passed);
printf_run(NULL, "%p", &tests_passed);
printf_run(NULL, "0X%p", &tests_passed);
printf_run(NULL, "0x%p", &tests_passed);

/* Binary */
printf_run("%llb abc", 123);
printf_run("%llb abc", 123);
printf_run("%b", 4);
//printf_run("%10b", 8);
//printf_run("%010b", 8);
printf_run(NULL, "%llb abc", 123);
printf_run(NULL, "%llb abc", 123);
printf_run(NULL, "%b", 4);
//printf_run(NULL, "%10b", 8);
//printf_run(NULL, "%010b", 8);

/* Floats */
printf_run("%f", 12.13);
printf_run("%.3f", 12.1337);
printf_run("%.25f", 12.1337);
printf_run(NULL, "%f", 12.13);
printf_run(NULL, "%.3f", 12.1337);
printf_run(NULL, "%.25f", 12.1337);

/* Engineering */
printf_run("%e", 2.5f);
printf_run("%e", 43433.23f);
printf_run(NULL, "%e", 2.5f);
printf_run(NULL, "%e", 43433.23f);

/* A */
printf_run("%a", 2.5f);
printf_run("%a", 43433.23f);
printf_run(NULL, "%a", 2.5f);
printf_run(NULL, "%a", 43433.23f);

/* Array test */
uint8_t my_arr[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
printf_run("%5K", my_arr); /* Print fixed length */
printf_run("%*K", 3, my_arr); /* Print only first 3 elements of array */
printf_run("% *K", 3, my_arr); /* Print only first 3 elements of array with spaces between bytes */
printf_run(NULL, "%5K", my_arr); /* Print fixed length */
printf_run(NULL, "%*K", 3, my_arr); /* Print only first 3 elements of array */
printf_run(NULL, "% *K", 3, my_arr); /* Print only first 3 elements of array with spaces between bytes */

/* Print final output */
printf("----\r\n\r\n");
Expand Down
16 changes: 16 additions & 0 deletions lwprintf/src/include/lwprintf/lwprintf_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ extern "C" {
#define LWPRINTF_CFG_FLOAT_DEFAULT_PRECISION 6
#endif

/**
* \brief Enables `1` or disables `0` support for `%K` for hex byte array output
*
*/
#ifndef LWPRINTF_CFG_SUPPORT_TYPE_BYTE_ARRAY
#define LWPRINTF_CFG_SUPPORT_TYPE_BYTE_ARRAY 1
#endif

/**
* \brief Enables `1` or disables `0` support for `%s` for string output
*
*/
#ifndef LWPRINTF_CFG_SUPPORT_TYPE_STRING
#define LWPRINTF_CFG_SUPPORT_TYPE_STRING 1
#endif

/**
* \}
*/
Expand Down
32 changes: 12 additions & 20 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,11 @@ typedef struct lwprintf_int {
*/
#define LWPRINTF_GET_LW(p) ((p) != NULL ? (p) : (&lwprintf_default))

/**
* \brief Output function for lwprintf printf function
* \param[in] ch: Character to print
* \param[in] lw: LwPRINTF instance
* \return `ch` value on success, `0` otherwise
*/
int
prv_default_output_func(int ch, struct lwprintf* lw) {
LWPRINTF_UNUSED(ch);
LWPRINTF_UNUSED(lw);
return 0;
}

/**
* \brief LwPRINTF default structure used by application
*/
static lwprintf_t
lwprintf_default = {
.out_fn = prv_default_output_func
};
lwprintf_default;

/**
* \brief Rotate string of the input buffer in place
Expand Down Expand Up @@ -245,7 +230,7 @@ static int
prv_out_str_before(lwprintf_int_t* p, size_t buff_size) {
/* Check for width */
if (p->m.width > 0
/* If number is negative, add negative sign or if positive and has plus sign forced*/
/* If number is negative, add negative sign or if positive and has plus sign forced */
&& (p->m.flags.is_negative || p->m.flags.plus)) {
--p->m.width;
}
Expand All @@ -256,7 +241,7 @@ prv_out_str_before(lwprintf_int_t* p, size_t buff_size) {
if (p->m.width > 0) {
--p->m.width;
}
} else if (p->m.base == 16) {
} else if (p->m.base == 16 || p->m.base == 2) {
if (p->m.width > 2) {
p->m.width -= 2;
} else {
Expand All @@ -281,6 +266,9 @@ prv_out_str_before(lwprintf_int_t* p, size_t buff_size) {
} else if (p->m.base == 16) {
p->out_fn(p, '0');
p->out_fn(p, p->m.flags.uc ? 'X' : 'x');
} else if (p->m.base == 2) {
p->out_fn(p, '0');
p->out_fn(p, p->m.flags.uc ? 'B' : 'b');
}
}

Expand Down Expand Up @@ -662,8 +650,8 @@ prv_format(lwprintf_int_t* p, va_list arg) {
p->m.base = 10;
} else if (*fmt == 'x' || *fmt == 'X') {
p->m.base = 16;
p->m.flags.uc = *fmt == 'X';/* Select if uppercase text shall be printed */
}
p->m.flags.uc = *fmt == 'X' || *fmt == 'B'; /* Select if uppercase text shall be printed */

/* Check for different length parameters */
if (0) {
Expand Down Expand Up @@ -694,6 +682,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {
#endif /* LWPRINTF_CFG_SUPPORT_LONG_LONG */
}
break;
#if LWPRINTF_CFG_SUPPORT_TYPE_STRING
case 's': {
const char* b = va_arg(arg, const char*);
size_t len = strlen(b);
Expand All @@ -707,6 +696,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {
prv_out_str(p, b, len);
break;
}
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_STRING */
#if LWPRINTF_CFG_SUPPORT_TYPE_POINTER
case 'p': {
p->m.base = 16; /* Go to hex format */
Expand Down Expand Up @@ -735,7 +725,6 @@ prv_format(lwprintf_int_t* p, va_list arg) {
prv_out_str_raw(p, "NaN", 3); /* Print string */
//prv_double_to_str(p, (double)va_arg(arg, double));
break;
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_FLOAT */
case 'e':
case 'E':
case 'g':
Expand All @@ -750,9 +739,11 @@ prv_format(lwprintf_int_t* p, va_list arg) {

break;
}
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_FLOAT */
case '%':
p->out_fn(p, '%');
break;
#if LWPRINTF_CFG_SUPPORT_TYPE_BYTE_ARRAY
/*
* This is to print unsigned-char formatted pointer in hex string
*
Expand Down Expand Up @@ -782,6 +773,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {
}
break;
}
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_BYTE_ARRAY */
default:
p->out_fn(p, *fmt);
}
Expand Down

0 comments on commit 9a56913

Please sign in to comment.