LibC: printf string persision works now

This commit is contained in:
Bananymous 2023-06-03 15:07:02 +03:00
parent a4cb5d8360
commit 4f84faf392
1 changed files with 18 additions and 12 deletions

View File

@ -3,7 +3,6 @@
#include <bits/printf.h> #include <bits/printf.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -199,7 +198,7 @@ static void floating_point_to_exponent_string(char* buffer, T value, bool upper,
int exponent = 0; int exponent = 0;
if (value != (T)0.0) if (value != (T)0.0)
{ {
exponent = (int)floorl(BAN::Math::log10<T>(value)); exponent = (int)BAN::Math::log10<T>(value);
value /= BAN::Math::pow<T>(10.0, exponent); value /= BAN::Math::pow<T>(10.0, exponent);
} }
@ -287,6 +286,7 @@ extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun
else if (*format == '*') else if (*format == '*')
{ {
percision = va_arg(arguments, int); percision = va_arg(arguments, int);
format++;
} }
if (percision < 0) if (percision < 0)
percision = -1; percision = -1;
@ -295,9 +295,11 @@ extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun
// TODO: Lenght modifier // TODO: Lenght modifier
static char conversion[1024]; char conversion[1024];
const char* string = nullptr; const char* string = nullptr;
int length = -1;
switch (*format) switch (*format)
{ {
case 'd': case 'd':
@ -371,6 +373,12 @@ extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun
case 's': case 's':
{ {
string = va_arg(arguments, const char*); string = va_arg(arguments, const char*);
if (options.percision != -1)
{
length = 0;
while (string[length] && length < options.percision)
length++;
}
format++; format++;
break; break;
} }
@ -420,23 +428,21 @@ extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun
if (string) if (string)
{ {
int len = strlen(string); if (length == -1)
length = strlen(string);
if (options.width == -1) if (options.width == -1)
options.width = 0; options.width = 0;
if (!options.left_justified) if (!options.left_justified)
for (int i = len; i < options.width; i++) for (int i = length; i < options.width; i++)
BAN_PRINTF_PUTC(' '); BAN_PRINTF_PUTC(' ');
while (*string) for (int i = 0; i < length && string[i]; i++)
{ BAN_PRINTF_PUTC(string[i]);
BAN_PRINTF_PUTC(*string);
string++;
}
if (options.left_justified) if (options.left_justified)
for (int i = len; i < options.width; i++) for (int i = length; i < options.width; i++)
BAN_PRINTF_PUTC(' '); BAN_PRINTF_PUTC(' ');
} }
} }