Adds format functions with NumericFormat<T>, bin(), hex(), dec(), oct(). Adds 64bit support for number-to-string conversion.

This commit is contained in:
Felix Queißner 2016-05-04 17:33:03 +02:00
parent f4ac435a2b
commit 15cabe92ad
4 changed files with 148 additions and 0 deletions

View file

@ -17,6 +17,33 @@ struct BColor
Color color;
};
template<typename T>
struct NumericFormat
{
T value;
uint32_t base;
NumericFormat(T value, uint32_t base) : value(value), base(base) { }
};
namespace console_tools
{
template<typename T>
auto hex(T value) { return NumericFormat<T>(value, 16); }
template<typename T>
auto dec(T value) { return NumericFormat<T>(value, 10); }
template<typename T>
auto oct(T value) { return NumericFormat<T>(value, 8); }
template<typename T>
auto bin(T value) { return NumericFormat<T>(value, 2); }
template<typename T>
auto nbase(T value, uint32_t base) { return NumericFormat<T>(value, base); }
}
class Console
{
public:
@ -123,4 +150,7 @@ public:
template<typename T>
Console & operator << (pointer<T> ptr);
template<typename T>
Console & operator << (const NumericFormat<T> &fmt);
};

View file

@ -22,6 +22,11 @@ public:
size_t length,
uint32_t number,
uint32_t radix = 10);
static size_t toString(
char *buffer,
size_t length,
uint64_t number,
uint32_t radix = 10);
/**
* Converts a signed number to a string.
@ -36,4 +41,9 @@ public:
size_t length,
int32_t number,
uint32_t radix = 10);
static size_t toString(
char *buffer,
size_t length,
int64_t number,
uint32_t radix = 10);
};

View file

@ -162,4 +162,48 @@ Console & Console::operator << <virtual_t_ident>(virtual_t ptr)
{
*this << "virtual(0x" << ptr.data() << ")";
return *this;
}
template<>
Console & Console::operator << <uint32_t>(const NumericFormat<uint32_t> & fmt)
{
char buffer[13];
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) {
this->put(buffer[i]);
}
return *this;
}
template<>
Console & Console::operator << <int32_t>(const NumericFormat<int32_t> & fmt)
{
char buffer[13];
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) {
this->put(buffer[i]);
}
return *this;
}
template<>
Console & Console::operator << <uint64_t>(const NumericFormat<uint64_t> & fmt)
{
char buffer[13];
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) {
this->put(buffer[i]);
}
return *this;
}
template<>
Console & Console::operator << <int64_t>(const NumericFormat<int64_t> & fmt)
{
char buffer[13];
size_t len = Numeric::toString(buffer, sizeof(buffer), fmt.value, fmt.base);
for(size_t i = 0; i < len; i++) {
this->put(buffer[i]);
}
return *this;
}

View file

@ -66,4 +66,68 @@ size_t Numeric::toString(
else {
return Numeric::toString(buffer, length, static_cast<uint32_t>(number), radix);
}
}
size_t Numeric::toString(
char *buffer,
size_t length,
uint64_t number,
uint32_t radix)
{
if(length == 0) {
return 0;
}
if(number == 0) {
buffer[0] = '0';
return 1;
}
size_t len = 0;
while(number > 0)
{
buffer[len++] = getDigit(number % radix);
if(len >= length)
break;
number /= radix;
}
int half = len / 2;
for(int i = 0; i < half; i++)
{
char c = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = c;
}
return len;
}
size_t Numeric::toString(
char *buffer,
size_t length,
int64_t number,
uint32_t radix)
{
if(length == 0) {
return 0;
}
if(number == 0) {
buffer[0] = '0';
return 1;
}
if(number < 0) {
buffer[0] = '-';
length -= 1;
if(length == 0) {
return 0;
}
return Numeric::toString(&buffer[1], length, static_cast<uint64_t>(-number), radix) + 1;
}
else {
return Numeric::toString(buffer, length, static_cast<uint64_t>(number), radix);
}
}