Skip to content

Commit

Permalink
Added support for tabsize for toString.
Browse files Browse the repository at this point in the history
  • Loading branch information
l4m4re committed May 9, 2018
1 parent f259481 commit de23d2f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
62 changes: 38 additions & 24 deletions src/toString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,58 +34,58 @@
#endif


static const char* NumberToString(unsigned long n, uint8_t base=DEC);
static const char* NumberToString(long n, uint8_t base=DEC);
static const char* FloatToString(double number, uint8_t digits);
static const char* NumberToString(unsigned long n, int tabsize, uint8_t base);
static const char* NumberToString(long n, int tabsize, uint8_t base);
static const char* FloatToString(double number, int tabsize, uint8_t digits);


// Public Methods //////////////////////////////////////////////////////////////


const char* toString(char b, int base)
const char* toString(char b, int tabsize, int base)
{
return NumberToString((long) b, base);
return NumberToString((long) b, tabsize, base);
}

const char* toString(unsigned char b, int base)
const char* toString(unsigned char b, int tabsize, int base)
{
return NumberToString((unsigned long) b, base);
return NumberToString((unsigned long) b, tabsize, base);
}

const char* toString(int n, int base)
const char* toString(int n, int tabsize, int base)
{
return NumberToString((long) n, base);
return NumberToString((long) n, tabsize, base);
}

const char* toString(unsigned int n, int base)
const char* toString(unsigned int n, int tabsize, int base)
{
return NumberToString((unsigned long) n, base);
return NumberToString((unsigned long) n, tabsize, base);
}

const char* toString(long n, int base)
const char* toString(long n, int tabsize, int base)
{
return NumberToString(n, base);
return NumberToString(n, tabsize, base);
}

const char* toString(unsigned long n, int base)
const char* toString(unsigned long n, int tabsize, int base)
{
return NumberToString(n, base);
return NumberToString(n, tabsize, base);
}

const char* toString(double n, int digits)
const char* toString(double n, int tabsize, int digits)
{
return FloatToString(n, digits);
return FloatToString(n, tabsize, digits);
}

const char* toString(Fix16 n, int digits)
const char* toString(Fix16 n, int tabsize, int digits)
{
return FloatToString(static_cast<double>(n), digits);
return FloatToString(static_cast<double>(n), tabsize, digits);
}


// Private Methods /////////////////////////////////////////////////////////////

static const char* NumberToString(unsigned long n, uint8_t base)
static const char* NumberToString(unsigned long n, int tabsize, uint8_t base)
{
static char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
Expand All @@ -102,10 +102,12 @@ static const char* NumberToString(unsigned long n, uint8_t base)
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);

while( strlen(str) < tabsize ) *--str = ' ';

return str;
}

static const char* NumberToString(long n, uint8_t base)
static const char* NumberToString(long n, int tabsize, uint8_t base)
{
static char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
Expand All @@ -131,10 +133,12 @@ static const char* NumberToString(long n, uint8_t base)

if( isneg ) *--str = '-';

while( strlen(str) < tabsize ) *--str = ' ';

return str;
}

static const char* FloatToString(double number, uint8_t digits)
static const char* FloatToString(double number, int tabsize, uint8_t digits)
{
static char buf[20];
char *str = &buf[0];
Expand Down Expand Up @@ -163,7 +167,7 @@ static const char* FloatToString(double number, uint8_t digits)
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
strcpy(str, NumberToString(int_part));
strcpy(str, NumberToString(int_part,0,DEC));
while( *str != 0 ) str++;

// Print the decimal point, but only if there are digits beyond
Expand All @@ -177,11 +181,21 @@ static const char* FloatToString(double number, uint8_t digits)
remainder *= 10.0;
unsigned int toPrint = (unsigned int)(remainder);

strcpy(str, NumberToString((unsigned long)remainder));
strcpy(str, NumberToString((unsigned long)remainder,0,DEC));
while( !*str ) str++;

remainder -= toPrint;
}

if( int len=strlen(buf) < tabsize )
{
int shift = tabsize - len;
char *str = &buf[0] + tabsize + 1;
*str-- = '\0';

for( int i=0; i<len; i++) *str-- = *(str-shift);
for( int i=0; i<tabsize-len; i++) *str-- = ' ';
}

return buf;
}
16 changes: 8 additions & 8 deletions src/toString.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
#define OCT 8
#define BIN 2

const char* toString(char, int = DEC);
const char* toString(unsigned char, int = DEC);
const char* toString(int, int = DEC);
const char* toString(unsigned int, int = DEC);
const char* toString(long, int = DEC);
const char* toString(unsigned long, int = DEC);
const char* toString(double, int digits = 2); // Max 8 digits.
const char* toString(Fix16, int digits = 2); // Max 8 digits.
const char* toString(char, int tabsize=4, int = DEC);
const char* toString(unsigned char, int tabsize=4, int = DEC);
const char* toString(int, int tabsize=4, int = DEC);
const char* toString(unsigned int, int tabsize=4, int = DEC);
const char* toString(long, int tabsize=4, int = DEC);
const char* toString(unsigned long, int tabsize=4, int = DEC);
const char* toString(double, int tabsize=4, int digits = 2); // Max 8 digits.
const char* toString(Fix16, int tabsize=4, int digits = 2); // Max 8 digits.

#endif

0 comments on commit de23d2f

Please sign in to comment.