Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Jun 27, 2023
1 parent c17dcab commit 210db7a
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ typedef long int float_long_t;
* \brief Float number splitted by parts
*/
typedef struct {
float_long_t integer_part; /*!< Integer type of double number */
double decimal_part_dbl; /*!< Decimal part of double number multiplied by 10^precision */
float_long_t decimal_part; /*!< Decimal part of double number in integer format */
double diff; /*!< Difference between decimal parts (double - int) */
float_long_t integer_part; /*!< Integer type of double number */
double decimal_part_dbl; /*!< Decimal part of double number multiplied by 10^precision */
float_long_t decimal_part; /*!< Decimal part of double number in integer format */
double diff; /*!< Difference between decimal parts (double - int) */

short digits_cnt_integer_part; /*!< Number of digits for integer part */
short digits_cnt_decimal_part; /*!< Number of digits for decimal part */
Expand Down Expand Up @@ -176,11 +176,11 @@ typedef struct lwprintf_int {
uint8_t is_num_zero : 1; /*!< Status if input number is zero */
} flags; /*!< List of flags */

int precision; /*!< Selected precision */
int width; /*!< Text width indicator */
uint8_t base; /*!< Base for number format output */
char type; /*!< Format type */
} m; /*!< Block that is reset on every start of format */
int precision; /*!< Selected precision */
int width; /*!< Text width indicator */
uint8_t base; /*!< Base for number format output */
char type; /*!< Format type */
} m; /*!< Block that is reset on every start of format */
} lwprintf_int_t;

/**
Expand Down Expand Up @@ -587,12 +587,7 @@ prv_double_to_str(lwprintf_int_t* p, double in_num) {
double orig_num = in_num;
int digits_cnt, exp_cnt = 0, chosen_precision;
char def_type = p->m.type;

#if LWPRINTF_CFG_SUPPORT_LONG_LONG
char str[22];
#else
char str[11];
#endif /* LWPRINTF_CFG_SUPPORT_LONG_LONG */
char str[LWPRINTF_CFG_SUPPORT_LONG_LONG ? 22 : 11];

/*
* Check for corner cases
Expand Down Expand Up @@ -651,7 +646,7 @@ prv_double_to_str(lwprintf_int_t* p, double in_num) {
#endif /* LWPRINTF_CFG_SUPPORT_TYPE_ENGINEERING */

/* Check precision data */
chosen_precision = p->m.precision; /* This is default value coming from app */
chosen_precision = p->m.precision; /* This is default value coming from app */
if (p->m.precision >= (int)LWPRINTF_ARRAYSIZE(powers_of_10)) {
p->m.precision = (int)LWPRINTF_ARRAYSIZE(powers_of_10) - 1; /* Limit to maximum precision */
/*
Expand Down Expand Up @@ -897,7 +892,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {

/* Check [.precision] */
p->m.precision = 0;
if (*fmt == '.') { /* Precision flag is detected */
if (*fmt == '.') { /* Precision flag is detected */
p->m.flags.precision = 1;
if (*++fmt == '*') { /* Variable check */
const int pr = (int)va_arg(arg, int);
Expand Down Expand Up @@ -1091,7 +1086,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {

d = (*ptr >> 0x04) & 0x0F; /* Print MSB */
p->out_fn(p, (char)(d) + (d >= 10 ? ((p->m.flags.uc ? 'A' : 'a') - 10) : '0'));
d = *ptr & 0x0F; /* Print LSB */
d = *ptr & 0x0F; /* Print LSB */
p->out_fn(p, (char)(d) + (d >= 10 ? ((p->m.flags.uc ? 'A' : 'a') - 10) : '0'));

if (is_space && i < (len - 1)) {
Expand All @@ -1106,7 +1101,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {
}
++fmt;
}
p->out_fn(p, '\0'); /* Output last zero number */
p->out_fn(p, '\0'); /* Output last zero number */
#if LWPRINTF_CFG_OS && !LWPRINTF_CFG_OS_MANUAL_PROTECT
if (IS_PRINT_MODE(p)) { /* Mutex only for print operation */
lwprintf_sys_mutex_release(&p->lwobj->mutex);
Expand Down

0 comments on commit 210db7a

Please sign in to comment.