Skip to content

Commit

Permalink
Simplify output generator for integer
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 30, 2024
1 parent ccd32af commit 7d44197
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
5 changes: 4 additions & 1 deletion dev/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ size_t tests_passed, tests_failed;

#define do_test(buff_ptr, buff_size, exp_out, exp_out_len, fmt, ...) \
do { \
char my_buffer[1234]; \
int len = lwprintf_snprintf((buff_ptr), (buff_size), (fmt), ##__VA_ARGS__); \
if (len != (exp_out_len)) { \
printf("Test error on line: %d\r\n", __LINE__); \
Expand Down Expand Up @@ -120,6 +119,10 @@ main(void) {
do_test(buffer, sizeof(buffer), "1.234560e-09", 12, "%e", 0.00000000123456);
do_test(buffer, sizeof(buffer), "-000000000001.2346e-01", 22, "%022.4e", -0.123456);
do_test(buffer, sizeof(buffer), "-1.2346E+02", 11, "%.4E", -123.456);
do_test(buffer, sizeof(buffer), "0", 1, "%d", 0);
do_test(buffer, sizeof(buffer), "1", 1, "%d", 1);
do_test(buffer, sizeof(buffer), "10", 2, "%d", 10);
do_test(buffer, sizeof(buffer), "140", 3, "%d", 140);
do_test(buffer, sizeof(buffer), " 28", 3, "% 3u", (unsigned)28);
do_test(buffer, sizeof(buffer), "028", 3, "%03d", 28);
do_test(buffer, sizeof(buffer), "+28", 3, "%+03d", 28);
Expand Down
39 changes: 18 additions & 21 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,30 +359,27 @@ prv_out_str(lwprintf_int_t* lwi, const char* buff, size_t buff_size) {
*/
static int
prv_longest_unsigned_int_to_str(lwprintf_int_t* lwi, uint_maxtype_t num) {
uint_maxtype_t den, digit;
uint8_t digits_cnt;
char chr;
/* Start with digits length, support binary with int, that is 32-bits maximum width */
char num_buf[33], *num_buf_ptr = &num_buf[sizeof(num_buf)];

/* Check if number is zero */
lwi->m.flags.is_num_zero = (num) == 0;
if ((num) == 0) {
prv_out_str_before(lwi, 1);
lwi->out_fn(lwi, '0');
prv_out_str_after(lwi, 1);
} else { /* Start with digits length */
for (digits_cnt = 0, den = (num); den > 0; ++digits_cnt, den /= lwi->m.base) {}
for (den = 1; ((num) / den) >= lwi->m.base; den *= lwi->m.base) {}

prv_out_str_before(lwi, digits_cnt);
for (; den > 0;) {
digit = (num) / den;
(num) = (num) % den;
den = den / lwi->m.base;
chr = (char)digit + (char)(digit >= 10 ? ((lwi->m.flags.uc ? 'A' : 'a') - 10) : '0');
lwi->out_fn(lwi, chr);
}
prv_out_str_after(lwi, digits_cnt);
lwi->m.flags.is_num_zero = num == 0;

/* Fill the buffer backward */
*--num_buf_ptr = '\0';
do {
int digit = num % lwi->m.base;
num /= lwi->m.base;
*--num_buf_ptr = (char)digit + (char)(digit >= 10 ? ((lwi->m.flags.uc ? 'A' : 'a') - 10) : '0');
} while (num > 0);

/* Calculate and generate the output */
size_t len = sizeof(num_buf) - (size_t)((uintptr_t)num_buf_ptr - (uintptr_t)num_buf) - 1;
prv_out_str_before(lwi, len);
for (; *num_buf_ptr;) {
lwi->out_fn(lwi, *num_buf_ptr++);
}
prv_out_str_after(lwi, len);
return 1;
}

Expand Down

0 comments on commit 7d44197

Please sign in to comment.