Fixed the print functions. Yay!
This commit is contained in:
parent
c2a0d01995
commit
2334d80225
2 changed files with 43 additions and 75 deletions
|
@ -46,7 +46,7 @@ reverse(char* str,
|
||||||
char* p = str;
|
char* p = str;
|
||||||
char* t = str + max - 1;
|
char* t = str + max - 1;
|
||||||
char c;
|
char c;
|
||||||
while (t > p) {
|
while (p < t) {
|
||||||
c = *p;
|
c = *p;
|
||||||
*p++ = *t;
|
*p++ = *t;
|
||||||
*t-- = c;
|
*t-- = c;
|
||||||
|
@ -78,39 +78,53 @@ uppercase(char* str)
|
||||||
* that `p - buffer` is the number of characters converted.
|
* that `p - buffer` is the number of characters converted.
|
||||||
*/
|
*/
|
||||||
static usize
|
static usize
|
||||||
_doConvertFromInteger(u32 value,
|
_doConvertFromInteger(u64 value,
|
||||||
char* buffer,
|
char* buffer,
|
||||||
usize length,
|
usize length,
|
||||||
u8 base,
|
u8 base,
|
||||||
bool capitalized)
|
bool capitalized)
|
||||||
{
|
{
|
||||||
char* p = buffer;
|
static const char* digitsLower = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
int place;
|
static const char* digitsUpper = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
do {
|
|
||||||
if (usize(p - buffer) >= length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
place = value % base;
|
|
||||||
value /= base;
|
|
||||||
*p++ = (place < 10) ? place + '0' : (place - 10) + (capitalized ? 'A' : 'a');
|
|
||||||
} while (value != 0);
|
|
||||||
|
|
||||||
return p - buffer;
|
const char* digits = capitalized ? digitsUpper : digitsLower;
|
||||||
|
|
||||||
|
if (length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (value == 0) {
|
||||||
|
buffer[0] = digits[0];
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 place;
|
||||||
|
usize i;
|
||||||
|
for (i = 0; i < length && value != 0; i++) {
|
||||||
|
place = value % base;
|
||||||
|
buffer[i] = digits[place];
|
||||||
|
value /= base;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
|
||||||
fromInteger(i32 value,
|
usize
|
||||||
|
fromInteger(i64 value,
|
||||||
char* buffer,
|
char* buffer,
|
||||||
usize length,
|
usize length,
|
||||||
u8 base,
|
u8 base,
|
||||||
bool capitalized)
|
bool capitalized)
|
||||||
{
|
{
|
||||||
|
if (base < 2 || base > 36) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const bool negative = base == 10 && value < 0;
|
const bool negative = base == 10 && value < 0;
|
||||||
if (negative) {
|
if (negative) {
|
||||||
value *= -1;
|
value *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
usize convertedLength = _doConvertFromInteger(u32(value), buffer, length, base, capitalized);
|
usize convertedLength = _doConvertFromInteger(value, buffer, length, base, capitalized);
|
||||||
|
|
||||||
if (convertedLength < length && negative) {
|
if (convertedLength < length && negative) {
|
||||||
buffer[convertedLength++] = '-';
|
buffer[convertedLength++] = '-';
|
||||||
|
@ -121,72 +135,27 @@ fromInteger(i32 value,
|
||||||
|
|
||||||
reverse(buffer, convertedLength);
|
reverse(buffer, convertedLength);
|
||||||
|
|
||||||
return buffer;
|
return convertedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char*
|
usize
|
||||||
fromUnsignedInteger(u32 value,
|
fromUnsignedInteger(u64 value,
|
||||||
char* buffer,
|
char* buffer,
|
||||||
usize length,
|
usize length,
|
||||||
u8 base,
|
u8 base,
|
||||||
bool capitalized)
|
bool capitalized)
|
||||||
{
|
{
|
||||||
usize convertedLength = _doConvertFromInteger(u32(value), buffer, length, base, capitalized);
|
if (base < 2 || base > 36) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
usize convertedLength = _doConvertFromInteger(value, buffer, length, base, capitalized);
|
||||||
if (convertedLength < length) {
|
if (convertedLength < length) {
|
||||||
buffer[convertedLength] = '\0';
|
buffer[convertedLength] = '\0';
|
||||||
}
|
}
|
||||||
reverse(buffer, convertedLength);
|
reverse(buffer, convertedLength);
|
||||||
return buffer;
|
return convertedLength;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
char*
|
|
||||||
fromLongInteger(i64 value,
|
|
||||||
char* buffer,
|
|
||||||
usize length,
|
|
||||||
u8 base,
|
|
||||||
bool capitalized)
|
|
||||||
{
|
|
||||||
const bool negative = base == 10 && value < 0;
|
|
||||||
if (negative) {
|
|
||||||
value *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do large ints in two passes. First do the high-order bits, then the lower order bits.
|
|
||||||
usize convertedLength;
|
|
||||||
convertedLength = _doConvertFromInteger(u32(value >> 32), buffer, length, base, capitalized);
|
|
||||||
convertedLength += _doConvertFromInteger(u32(value), &buffer[convertedLength], length - convertedLength, base, capitalized);
|
|
||||||
|
|
||||||
if (convertedLength < length && negative) {
|
|
||||||
buffer[convertedLength++] = '-';
|
|
||||||
}
|
|
||||||
if (convertedLength < length) {
|
|
||||||
buffer[convertedLength] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
reverse(buffer, convertedLength);
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
char*
|
|
||||||
fromUnsignedLongInteger(u64 value,
|
|
||||||
char* buffer,
|
|
||||||
usize length,
|
|
||||||
u8 base,
|
|
||||||
bool capitalized)
|
|
||||||
{
|
|
||||||
// Do large ints in two passes. First do the high-order bits, then the lower order bits.
|
|
||||||
usize convertedLength;
|
|
||||||
convertedLength = _doConvertFromInteger(u32(value >> 32), buffer, length, base, capitalized);
|
|
||||||
convertedLength += _doConvertFromInteger(u32(value), &buffer[convertedLength], length - convertedLength, base, capitalized);
|
|
||||||
if (convertedLength < length) {
|
|
||||||
buffer[convertedLength] = '\0';
|
|
||||||
}
|
|
||||||
reverse(buffer, convertedLength);
|
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,19 +34,18 @@ void uppercase(char *str);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a signed integer to a string.
|
* Convert an integer to a string.
|
||||||
*
|
*
|
||||||
* @param [in] value The value to convert
|
* @param [in] value The value to convert
|
||||||
* @param [in,out] buffer Buffer to write to
|
* @param [in,out] buffer Buffer to write to
|
||||||
* @param [in] length Length of the buffer
|
* @param [in] length Length of the buffer
|
||||||
* @param [in] base Base to convert to. Default is base 10.
|
* @param [in] base Base to convert to. Default is base 10.
|
||||||
* @param [in] capitalized Should the alphabetic digits be capitalized? Default is false.
|
* @param [in] capitalized Should the alphabetic digits be capitalized? Default is false.
|
||||||
|
* @return Number of bytes converted.
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
char* fromInteger(i32 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
usize fromInteger(i64 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
||||||
char* fromUnsignedInteger(u32 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
usize fromUnsignedInteger(u64 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
||||||
char* fromLongInteger(i64 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
|
||||||
char* fromUnsignedLongInteger(u64 value, char* buffer, usize length, u8 base = 10, bool capitalized = false);
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
/** Convert a bool to a string. */
|
/** Convert a bool to a string. */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue