Skip to content

Commit

Permalink
Merge branch 'develop-precision-strlen' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Dec 12, 2021
2 parents 62d5d6d + da808ec commit 9fa17d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
24 changes: 16 additions & 8 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ printf_run_fn(const char* expected, const char* fmt, ...) {
test_data_t* test;

/* Temporary strings array */
char b1[255] = { 0 }, b2[255] = { 0 };
char b1[255] = { 0 }, b2[sizeof(b1)] = { 0 };
int l1, l2;

/* Set variables to non-0 values */
memset(b1, 0xFFFFFFFF, sizeof(b1));
memset(b2, 0xFFFFFFFF, sizeof(b2));

console = GetStdHandle(STD_OUTPUT_HANDLE); /* Get console */

/* Generate strings with original and custom printf */
Expand Down Expand Up @@ -294,14 +298,8 @@ main(void) {
printf_run("0102b5", "%*k", 3, my_arr);
printf_run("01 02 b5", "% *k", 3, my_arr);

/* Print final output */
printf("------------------------\n");
printf("Number of tests run: %d\n", (int)(tests_passed + tests_failed));
printf("Number of tests passed: %d\n", (int)tests_passed);
printf("Number of tests failed: %d\n", (int)tests_failed);
printf("Coverage: %f %%\n", (float)((tests_passed * 100) / ((float)(tests_passed + tests_failed))));

test_result:
#if 1
/* Tests that failed */
printf("------------------------\n\n");
printf("Negative tests\n\n");
Expand All @@ -312,7 +310,9 @@ main(void) {
output_test_result(t);
}
}
#endif

#if 0
/* Tests that went through */
printf("------------------------\n\n");
printf("Positive tests\n\n");
Expand All @@ -323,5 +323,13 @@ main(void) {
output_test_result(t);
}
}
#endif

/* Print final output */
printf("------------------------\n");
printf("Number of tests run: %d\n", (int)(tests_passed + tests_failed));
printf("Number of tests passed: %d\n", (int)tests_passed);
printf("Number of tests failed: %d\n", (int)tests_failed);
printf("Coverage: %f %%\n", (float)((tests_passed * 100) / ((float)(tests_passed + tests_failed))));
return 0;
}
34 changes: 13 additions & 21 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,11 @@ prv_out_fn_print(lwprintf_int_t* p, const char c) {
*/
static int
prv_out_fn_write_buff(lwprintf_int_t* p, const char c) {
if (c != '\0' && p->n < (p->buff_size - 1)) {
p->buff[p->n++] = c;
p->buff[p->n] = '\0';
if (p->n < (p->buff_size - 1)) {
p->buff[p->n] = c;
if (c != '\0') {
p->buff[++p->n] = '\0';
}
return 1;
}
return 0;
Expand Down Expand Up @@ -388,15 +390,6 @@ prv_out_str_raw(lwprintf_int_t* p, const char* buff, size_t buff_size) {
*/
static int
prv_out_str(lwprintf_int_t* p, const char* buff, size_t buff_size) {
/*
* Find necessary buffer size used for string.
*
* When not known, get it from string length, but only if precision flag is not set.
* Setting precision flag with value == 0, means no output anyway
*/
if (buff_size == 0 && !p->m.flags.precision) {
buff_size = strlen(buff);
}
prv_out_str_before(p, buff_size); /* Implement pre-format */
prv_out_str_raw(p, buff, buff_size); /* Print actual string */
prv_out_str_after(p, buff_size); /* Implement post-format */
Expand Down Expand Up @@ -1065,15 +1058,14 @@ prv_format(lwprintf_int_t* p, va_list arg) {
#if LWPRINTF_CFG_SUPPORT_TYPE_STRING
case 's': {
const char* b = va_arg(arg, const char*);
size_t len = strlen(b);

/* Precision gives maximum output len */
if (p->m.flags.precision) {
if (len > p->m.precision) {
len = p->m.precision;
}
}
prv_out_str(p, b, len);
/*
* Calculate length of the string:
*
* - If precision is given, max len is up to precision value
* - if user selects write to buffer, go up to buffer size (-1 actually, but handled by write function)
* - Otherwise use max available system length
*/
prv_out_str(p, b, strnlen(b, p->m.flags.precision ? p->m.precision : (p->buff != NULL ? p->buff_size : SIZE_MAX)));
break;
}
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_STRING */
Expand Down

0 comments on commit 9fa17d3

Please sign in to comment.