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;
|
||||
|
||||
static void __strrev(char* arr, int size)
|
||||
static void printf_strrev(char* arr, int size)
|
||||
{
|
||||
int left = 0;
|
||||
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;
|
||||
|
||||
@ -42,14 +42,14 @@ template <typename IntegerType> static char* __unsignedtoa(IntegerType number, c
|
||||
number /= base;
|
||||
}
|
||||
|
||||
__strrev(arr, i);
|
||||
printf_strrev(arr, i);
|
||||
|
||||
arr[i] = '\0';
|
||||
|
||||
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;
|
||||
|
||||
@ -80,7 +80,7 @@ template <typename IntegerType> static char* __signedtoa(IntegerType number, cha
|
||||
i++;
|
||||
}
|
||||
|
||||
__strrev(arr, i);
|
||||
printf_strrev(arr, i);
|
||||
|
||||
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_unsigned_long = false;
|
||||
|
||||
@ -148,13 +167,11 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
||||
switch (current_char)
|
||||
{
|
||||
case 'c': {
|
||||
buffer[buffer_insert_index++] = (char)va_arg(ap, int);
|
||||
if (buffer_insert_index == 1024) flush_buffer();
|
||||
append_char(va_arg(ap, int));
|
||||
break;
|
||||
}
|
||||
case '%': {
|
||||
buffer[buffer_insert_index++] = '%';
|
||||
if (buffer_insert_index == 1024) flush_buffer();
|
||||
append_char('%');
|
||||
break;
|
||||
}
|
||||
case 'z': {
|
||||
@ -168,118 +185,54 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
char result[32];
|
||||
if (is_unsigned_long)
|
||||
{
|
||||
char result[25];
|
||||
__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();
|
||||
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 10);
|
||||
is_unsigned_long = is_long = false;
|
||||
}
|
||||
else if (is_long)
|
||||
{
|
||||
char result[25];
|
||||
__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();
|
||||
printf_signed_to_string(va_arg(ap, int64_t), result, 10);
|
||||
is_unsigned_long = is_long = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
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();
|
||||
}
|
||||
else { printf_signed_to_string(va_arg(ap, int32_t), result, 10); }
|
||||
append_string(result);
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
char result[32];
|
||||
if (is_unsigned_long || is_long)
|
||||
{
|
||||
char result[25];
|
||||
__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();
|
||||
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 10);
|
||||
is_unsigned_long = is_long = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
else { printf_unsigned_to_string(va_arg(ap, uint32_t), result, 10); }
|
||||
append_string(result);
|
||||
}
|
||||
case 'x': {
|
||||
char result[32];
|
||||
if (is_unsigned_long || is_long)
|
||||
{
|
||||
char result[25];
|
||||
__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();
|
||||
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 16);
|
||||
is_unsigned_long = is_long = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
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();
|
||||
}
|
||||
else { printf_unsigned_to_string(va_arg(ap, uint32_t), result, 16); }
|
||||
append_string(result);
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
char result[25];
|
||||
__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();
|
||||
char result[32];
|
||||
printf_unsigned_to_string(va_arg(ap, uint64_t), result, 16);
|
||||
append_string(result);
|
||||
break;
|
||||
}
|
||||
case 'm': {
|
||||
const char* str = 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();
|
||||
append_string(strerror(errno));
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
const char* str = 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();
|
||||
append_string(va_arg(ap, const char*));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
@ -288,11 +241,7 @@ static int internal_printf(const char* format, PutString put_string_callback, ss
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[buffer_insert_index++] = current_char;
|
||||
if (buffer_insert_index == 1024) flush_buffer();
|
||||
}
|
||||
else { append_char(current_char); }
|
||||
format_index++;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user