print: allow padding size > 9

This commit is contained in:
King Kévin 2017-12-13 12:30:16 +01:00
parent 11924349db
commit d001ed14e6
1 changed files with 19 additions and 15 deletions

View File

@ -79,7 +79,7 @@ static size_t print_string(char** str, size_t* size, const char* s)
* @param[in] sign if sign should be printed
* @return number of characters printed
**/
static size_t print_unsigned(char** str, size_t* size, uint64_t u, uint8_t padding, bool sign) {
static size_t print_unsigned(char** str, size_t* size, uint64_t u, uint32_t padding, bool sign) {
char number[20] = {0}; // construct the number in reverse order (20 chars are required to store UINT64_MAX)
uint8_t digits = 0; // to count the number of digits
size_t length = 0; // number of characters printed
@ -93,7 +93,7 @@ static size_t print_unsigned(char** str, size_t* size, uint64_t u, uint8_t paddi
if (sign) { // print sign
length += print_char(str, size, '+'); // we only have positive numbers
}
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
for (uint32_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
length += print_char(str, size, '0'); // print 0
}
for (uint8_t digit = 0; digit < digits; digit++) { // go through all digits
@ -110,7 +110,7 @@ static size_t print_unsigned(char** str, size_t* size, uint64_t u, uint8_t paddi
* @param[in] sign if sign should be printed
* @return number of characters printed
**/
static size_t print_signed(char** str, size_t* size, int64_t d, uint8_t padding, bool sign) {
static size_t print_signed(char** str, size_t* size, int64_t d, uint32_t padding, bool sign) {
size_t length = 0; // number of characters printed
if (d<0) {
length += print_char(str, size, '-'); // print sign
@ -150,7 +150,7 @@ static size_t print_nibble(char** str, size_t* size, uint8_t nibble, bool upcase
* @param[in] upcase use upcase digits (A-F)
* @return number of characters printed
**/
static size_t print_hex(char** str, size_t* size, uint32_t hex, uint8_t padding, bool prefix, bool upcase) {
static size_t print_hex(char** str, size_t* size, uint32_t hex, uint32_t padding, bool prefix, bool upcase) {
size_t length = 0; // number of characters printed
if (prefix) { // print 0x prefix
length += print_char(str, size, '0');
@ -167,7 +167,7 @@ static size_t print_hex(char** str, size_t* size, uint32_t hex, uint8_t padding,
} else {
digits = 2;
}
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
for (uint32_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
length += print_char(str, size, '0'); // print 0
}
for (uint8_t digit = 0; digit < digits; digit++) { // go through all digits
@ -184,7 +184,7 @@ static size_t print_hex(char** str, size_t* size, uint32_t hex, uint8_t padding,
* @param[in] prefix if 0b prefix should be printed
* @return number of characters printed
**/
static size_t print_bits(char** str, size_t* size, uint32_t u, uint8_t padding, bool prefix) {
static size_t print_bits(char** str, size_t* size, uint32_t u, uint32_t padding, bool prefix) {
char bits[32] = {0}; // construct the bit string in reverse order
uint8_t digits = 0; // to count the number of digits
size_t length = 0; // number of characters printed
@ -199,7 +199,7 @@ static size_t print_bits(char** str, size_t* size, uint32_t u, uint8_t padding,
length += print_char(str, size, '0');
length += print_char(str, size, 'b');
}
for (uint8_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
for (uint32_t zeros = digits; zeros<padding; zeros++) { // print padding 0's
length += print_char(str, size, '0'); // print 0
}
for (uint8_t digit = 0; digit < digits; digit++) { // go through all bits
@ -218,7 +218,7 @@ static size_t print_bits(char** str, size_t* size, uint32_t u, uint8_t padding,
static size_t vsnprintf(char** str, size_t* size, const char *format, va_list va)
{
size_t length = 0; // number of characters printed
uint8_t padding = 0; // number of padding 0's
uint32_t padding = 0; // number of padding 0's
bool sign = false; // if sign needs to be printed
while (*format) { // go through format string
padding = 0; // reset padding
@ -241,15 +241,19 @@ static size_t vsnprintf(char** str, size_t* size, const char *format, va_list va
// check padding
if ('0'==*format) { // padding required
format++; // go to padding number
if (0==*format) { // end of string detected
goto end;
}
if (*format>='0' && *format<='9') {
padding = *format-'0';
format++; // go to format specifier
if (0==*format) { // end of string detected
while (*format>='0' && *format<='9') {
if (padding>UINT32_MAX/10) { // check for overflow
goto end;
}
padding *= 10; // go to next magnitude
if (padding>UINT32_MAX-(*format-'0')) { // check for overflow
goto end;
}
padding += *format-'0';
format++; // go to next character
}
if (0==*format) { // end of string detected
goto end;
}
}
// check format specifier