Adds numeric prefixes to console printing.

This commit is contained in:
Felix Queißner 2016-05-05 17:16:47 +02:00
parent b3b0ac66fb
commit 7ae608fc3c
2 changed files with 22 additions and 0 deletions

View file

@ -59,6 +59,11 @@ private:
* Moves the hardware caret. * Moves the hardware caret.
*/ */
void updateCaret(); void updateCaret();
/**
* Prints the prefix for a given numeric base.
*/
void printNumericPrefix(uint32_t base);
public: public:
Console(Screen *screen); Console(Screen *screen);

View file

@ -168,6 +168,7 @@ template<>
Console & Console::operator << <uint32_t>(const NumericFormat<uint32_t> & fmt) Console & Console::operator << <uint32_t>(const NumericFormat<uint32_t> & fmt)
{ {
char buffer[13]; char buffer[13];
this->printNumericPrefix(fmt.base);
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base); size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++) {
this->put(buffer[i]); this->put(buffer[i]);
@ -179,6 +180,7 @@ template<>
Console & Console::operator << <int32_t>(const NumericFormat<int32_t> & fmt) Console & Console::operator << <int32_t>(const NumericFormat<int32_t> & fmt)
{ {
char buffer[13]; char buffer[13];
this->printNumericPrefix(fmt.base);
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base); size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++) {
this->put(buffer[i]); this->put(buffer[i]);
@ -190,6 +192,7 @@ template<>
Console & Console::operator << <uint64_t>(const NumericFormat<uint64_t> & fmt) Console & Console::operator << <uint64_t>(const NumericFormat<uint64_t> & fmt)
{ {
char buffer[13]; char buffer[13];
this->printNumericPrefix(fmt.base);
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base); size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++) {
this->put(buffer[i]); this->put(buffer[i]);
@ -201,9 +204,23 @@ template<>
Console & Console::operator << <int64_t>(const NumericFormat<int64_t> & fmt) Console & Console::operator << <int64_t>(const NumericFormat<int64_t> & fmt)
{ {
char buffer[13]; char buffer[13];
this->printNumericPrefix(fmt.base);
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base); size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) { for(size_t i = 0; i < len; i++) {
this->put(buffer[i]); this->put(buffer[i]);
} }
return *this; return *this;
} }
void Console::printNumericPrefix(uint32_t base)
{
switch(base) {
case 2: *this << "0b"; break;
case 8: *this << "0o"; break;
case 10: return;
case 16: *this << "0x"; break;
default:
*this << "[" << base << "]x";
break;
}
}