Enable CHPRINTF_USE_SPACE_FLAG in printf

It allow use ' ' in place '+' for better string formatting
Also add support output for complex numbers (but not enable it)
This commit is contained in:
DiSlord 2020-06-21 08:02:40 +03:00
parent 3213a8022c
commit 9bd8c99980

View file

@ -34,7 +34,7 @@
// Enable [flags], support: // Enable [flags], support:
// ' ' Prepends a space for positive signed-numeric types. positive = ' ', negative = '-'. This flag is ignored if the + flag exists. // ' ' Prepends a space for positive signed-numeric types. positive = ' ', negative = '-'. This flag is ignored if the + flag exists.
//#define CHPRINTF_USE_SPACE_FLAG #define CHPRINTF_USE_SPACE_FLAG
// Force putting trailing zeros on float value // Force putting trailing zeros on float value
#define CHPRINTF_FORCE_TRAILING_ZEROS #define CHPRINTF_FORCE_TRAILING_ZEROS
@ -195,7 +195,9 @@ static char *ftoaS(char *p, float num, uint32_t precision) {
; ;
prefix = num > 1e-3 ? ptr[-1] : 0; prefix = num > 1e-3 ? ptr[-1] : 0;
} }
// Auto set prescision else
precision++; // Add additional digit in place of prefix
// Auto set precision in place of value
uint32_t l = num; uint32_t l = num;
if (l < 10) if (l < 10)
precision+=2; precision+=2;
@ -241,6 +243,7 @@ static char *ftoaS(char *p, float num, uint32_t precision) {
#define PAD_ZERO 16 #define PAD_ZERO 16
#define PLUS_SPACE 32 #define PLUS_SPACE 32
#define DEFAULT_PRESCISION 64 #define DEFAULT_PRESCISION 64
#define COMPLEX 128
int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) { int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) {
char *p, *s, c, filler=' '; char *p, *s, c, filler=' ';
@ -286,6 +289,8 @@ int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) {
state|=POSITIVE; state|=POSITIVE;
else if (*fmt == '0') else if (*fmt == '0')
state|=PAD_ZERO; state|=PAD_ZERO;
// else if (*fmt == 'j')
// state|=COMPLEX;
#ifdef CHPRINTF_USE_SPACE_FLAG #ifdef CHPRINTF_USE_SPACE_FLAG
else if (*fmt == ' ') else if (*fmt == ' ')
state|=PLUS_SPACE; state|=PLUS_SPACE;
@ -365,6 +370,8 @@ int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) {
else if (state & PLUS_SPACE) else if (state & PLUS_SPACE)
*p++ = ' '; *p++ = ' ';
#endif #endif
// if (state & COMPLEX)
// *p++ = 'j';
p = long_to_string_with_divisor(p, value.l, 10, 0); p = long_to_string_with_divisor(p, value.l, 10, 0);
break; break;
case 'q': case 'q':
@ -386,6 +393,8 @@ int chvprintf(BaseSequentialStream *chp, const char *fmt, va_list ap) {
else if (state & PLUS_SPACE) else if (state & PLUS_SPACE)
*p++ = ' '; *p++ = ' ';
#endif #endif
// if (state & COMPLEX)
// *p++ = 'j';
if (value.f == INFINITY){ if (value.f == INFINITY){
*p++ = 0x19; *p++ = 0x19;
break; break;