libc: Make the userspace printf much better
This commit is contained in:
parent
315d2f9f24
commit
33f6765a5c
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
typedef long int ssize_t;
|
typedef long int ssize_t;
|
||||||
|
|
||||||
static void __strrev(char* arr, int size)
|
static void printf_strrev(char* arr, int size)
|
||||||
{
|
{
|
||||||
int left = 0;
|
int left = 0;
|
||||||
int right = size - 1;
|
int right = size - 1;
|
||||||
@ -23,7 +23,7 @@ static void __strrev(char* arr, int size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename IntegerType> static char* __unsignedtoa(IntegerType number, char* arr, int base)
|
template <typename IntegerType> static char* printf_unsigned_to_string(IntegerType number, char* arr, int base)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@ -42,14 +42,14 @@ template <typename IntegerType> static char* __unsignedtoa(IntegerType number, c
|
|||||||
number /= base;
|
number /= base;
|
||||||
}
|
}
|
||||||
|
|
||||||
__strrev(arr, i);
|
printf_strrev(arr, i);
|
||||||
|
|
||||||
arr[i] = '\0';
|
arr[i] = '\0';
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename IntegerType> static char* __signedtoa(IntegerType number, char* arr, int base)
|
template <typename IntegerType> static char* printf_signed_to_string(IntegerType number, char* arr, int base)
|
||||||
{
|
{
|
||||||
int i = 0, negative = 0;
|
int i = 0, negative = 0;
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ template <typename IntegerType> static char* __signedtoa(IntegerType number, cha
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
__strrev(arr, i);
|
printf_strrev(arr, i);
|
||||||
|
|
||||||
arr[i] = '\0';
|
arr[i] = '\0';
|
||||||
|
|
||||||
@ -125,6 +125,25 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto append_string = [&](const char* str) {
|
||||||
|
while (strlen(str) > 1024)
|
||||||
|
{
|
||||||
|
flush_buffer();
|
||||||
|
memcpy(buffer, str, 1024);
|
||||||
|
str += 1024;
|
||||||
|
buffer_insert_index = 1024;
|
||||||
|
}
|
||||||
|
if (buffer_insert_index + strlen(str) > 1024) flush_buffer();
|
||||||
|
memcpy(buffer + buffer_insert_index, str, strlen(str));
|
||||||
|
buffer_insert_index += strlen(str);
|
||||||
|
if (buffer_insert_index == 1024) flush_buffer();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto append_char = [&](char c) {
|
||||||
|
buffer[buffer_insert_index++] = c;
|
||||||
|
if (buffer_insert_index == 1024) flush_buffer();
|
||||||
|
};
|
||||||
|
|
||||||
bool is_long = false;
|
bool is_long = false;
|
||||||
bool is_unsigned_long = false;
|
bool is_unsigned_long = false;
|
||||||
|
|
||||||
@ -148,13 +167,11 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
|||||||
switch (current_char)
|
switch (current_char)
|
||||||
{
|
{
|
||||||
case 'c': {
|
case 'c': {
|
||||||
buffer[buffer_insert_index++] = (char)va_arg(ap, int);
|
append_char(va_arg(ap, int));
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '%': {
|
case '%': {
|
||||||
buffer[buffer_insert_index++] = '%';
|
append_char('%');
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'z': {
|
case 'z': {
|
||||||
@ -168,118 +185,54 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': {
|
case 'd': {
|
||||||
|
char result[32];
|
||||||
if (is_unsigned_long)
|
if (is_unsigned_long)
|
||||||
{
|
{
|
||||||
char result[25];
|
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 10);
|
||||||
__unsignedtoa<unsigned long>(va_arg(ap, uint64_t), result, 10);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
is_unsigned_long = is_long = false;
|
is_unsigned_long = is_long = false;
|
||||||
}
|
}
|
||||||
else if (is_long)
|
else if (is_long)
|
||||||
{
|
{
|
||||||
char result[25];
|
printf_signed_to_string(va_arg(ap, int64_t), result, 10);
|
||||||
__signedtoa<long>(va_arg(ap, int64_t), result, 10);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
is_unsigned_long = is_long = false;
|
is_unsigned_long = is_long = false;
|
||||||
}
|
}
|
||||||
else
|
else { printf_signed_to_string(va_arg(ap, int32_t), result, 10); }
|
||||||
{
|
append_string(result);
|
||||||
char result[25];
|
|
||||||
__signedtoa<int>(va_arg(ap, int32_t), result, 10);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'u': {
|
case 'u': {
|
||||||
|
char result[32];
|
||||||
if (is_unsigned_long || is_long)
|
if (is_unsigned_long || is_long)
|
||||||
{
|
{
|
||||||
char result[25];
|
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 10);
|
||||||
__unsignedtoa<unsigned long>(va_arg(ap, uint64_t), result, 10);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
is_unsigned_long = is_long = false;
|
is_unsigned_long = is_long = false;
|
||||||
}
|
}
|
||||||
else
|
else { printf_unsigned_to_string(va_arg(ap, uint32_t), result, 10); }
|
||||||
{
|
append_string(result);
|
||||||
char result[25];
|
|
||||||
__unsignedtoa<unsigned int>(va_arg(ap, uint32_t), result, 10);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case 'x': {
|
case 'x': {
|
||||||
|
char result[32];
|
||||||
if (is_unsigned_long || is_long)
|
if (is_unsigned_long || is_long)
|
||||||
{
|
{
|
||||||
char result[25];
|
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 16);
|
||||||
__unsignedtoa<unsigned long>(va_arg(ap, uint64_t), result, 16);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
is_unsigned_long = is_long = false;
|
is_unsigned_long = is_long = false;
|
||||||
}
|
}
|
||||||
else
|
else { printf_unsigned_to_string(va_arg(ap, uint32_t), result, 16); }
|
||||||
{
|
append_string(result);
|
||||||
char result[25];
|
|
||||||
__unsignedtoa<unsigned int>(va_arg(ap, uint32_t), result, 16);
|
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': {
|
case 'p': {
|
||||||
char result[25];
|
char result[32];
|
||||||
__unsignedtoa<unsigned long>(va_arg(ap, uint64_t), result, 16);
|
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 16);
|
||||||
if (buffer_insert_index + strlen(result) > 1024) flush_buffer();
|
append_string(result);
|
||||||
memcpy(buffer + buffer_insert_index, result, strlen(result));
|
|
||||||
buffer_insert_index += strlen(result);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'm': {
|
case 'm': {
|
||||||
const char* str = strerror(errno);
|
append_string(strerror(errno));
|
||||||
while (strlen(str) > 1024)
|
|
||||||
{
|
|
||||||
flush_buffer();
|
|
||||||
memcpy(buffer, str, 1024);
|
|
||||||
str += 1024;
|
|
||||||
buffer_insert_index = 1024;
|
|
||||||
}
|
|
||||||
if (buffer_insert_index + strlen(str) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, str, strlen(str));
|
|
||||||
buffer_insert_index += strlen(str);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 's': {
|
case 's': {
|
||||||
const char* str = va_arg(ap, const char*);
|
append_string(va_arg(ap, const char*));
|
||||||
while (strlen(str) > 1024)
|
|
||||||
{
|
|
||||||
flush_buffer();
|
|
||||||
memcpy(buffer, str, 1024);
|
|
||||||
str += 1024;
|
|
||||||
buffer_insert_index = 1024;
|
|
||||||
}
|
|
||||||
if (buffer_insert_index + strlen(str) > 1024) flush_buffer();
|
|
||||||
memcpy(buffer + buffer_insert_index, str, strlen(str));
|
|
||||||
buffer_insert_index += strlen(str);
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@ -288,11 +241,7 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else { append_char(current_char); }
|
||||||
{
|
|
||||||
buffer[buffer_insert_index++] = current_char;
|
|
||||||
if (buffer_insert_index == 1024) flush_buffer();
|
|
||||||
}
|
|
||||||
format_index++;
|
format_index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user