From 5695f2e65c0ee7b9e2d25f8794e4acec4ac3e1f9 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 12 Sep 2023 14:33:36 +0200 Subject: [PATCH] Applying changes made in ft_printf and its dependecies. These changes should have been made in master in the first place and then merged to testing. But that is not possible now so I'm doing it this way. --- Libft | 2 +- conversion.c | 55 ++++++++++++++++++++++++++++++++++++++-------------- formatting.c | 55 ++++++++++++++++++++++++++++++++++++++-------------- ft_printf.c | 2 +- padding.c | 9 ++++++--- 5 files changed, 88 insertions(+), 35 deletions(-) diff --git a/Libft b/Libft index f3d297f..b1a54dd 160000 --- a/Libft +++ b/Libft @@ -1 +1 @@ -Subproject commit f3d297fe160d8798b15c8a830b57281288abaaf6 +Subproject commit b1a54dd5836019e70a715e04420298092def5f3e diff --git a/conversion.c b/conversion.c index c7f722a..9423cb3 100644 --- a/conversion.c +++ b/conversion.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */ -/* Updated: 2023/09/05 12:10:06 by ljiriste ### ########.fr */ +/* Updated: 2023/09/11 12:41:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,17 +44,43 @@ static int valid_toprint(t_to_print tp) return (tp.left_pad && tp.zero_pad && tp.main_part && tp.right_pad); } -static size_t to_print_len(t_to_print tp) +static size_t to_print_len(t_to_print *tp) { size_t len; len = 0; - len += ft_strlen(tp.left_pad); - len += (tp.sign != '\0'); - len += ft_strlen(tp.alt); - len += ft_strlen(tp.zero_pad); - len += ft_strlen(tp.main_part); - len += ft_strlen(tp.right_pad); + len += ft_strlen(tp->left_pad); + len += (tp->sign != '\0'); + len += ft_strlen(tp->alt); + len += ft_strlen(tp->zero_pad); + len += ft_strlen(tp->main_part); + len += ft_strlen(tp->right_pad); + return (len); +} + +int tp_print_and_free_members(t_to_print *tp, t_conv conv) +{ + int len; + + len = to_print_len(tp); + ft_putstr_fd(tp->left_pad, 1); + if (tp->sign != '\0') + ft_putchar_fd(tp->sign, 1); + ft_putstr_fd(tp->alt, 1); + ft_putstr_fd(tp->zero_pad, 1); + if (*(tp->main_part) == '\0' && conv.type == 'c') + { + ft_putchar_fd('\0', 1); + ++len; + } + else + ft_putstr_fd(tp->main_part, 1); + ft_putstr_fd(tp->right_pad, 1); + free(tp->left_pad); + free(tp->alt); + free(tp->zero_pad); + free(tp->main_part); + free(tp->right_pad); return (len); } @@ -66,16 +92,15 @@ int handle_conversion(const char **format, va_list *args) ++(*format); conv = parse_format(format, args); + if (conv.type == '%') + { + ft_putchar_fd('%', 1); + return (1); + } temp = base_str_constr(conv.type, args); to_print = formatting(temp, conv); free(temp); if (!valid_toprint(to_print)) return (-1); - ft_putstr_fd(to_print.left_pad, 1); - ft_putchar_fd(to_print.sign, 1); - ft_putstr_fd(to_print.zero_pad, 1); - ft_putstr_fd(to_print.alt, 1); - ft_putstr_fd(to_print.main_part, 1); - ft_putstr_fd(to_print.right_pad, 1); - return (to_print_len(to_print)); + return (tp_print_and_free_members(&to_print, conv)); } diff --git a/formatting.c b/formatting.c index 089ea6f..1b66f0b 100644 --- a/formatting.c +++ b/formatting.c @@ -6,33 +6,51 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */ -/* Updated: 2023/09/05 12:11:32 by ljiriste ### ########.fr */ +/* Updated: 2023/09/11 12:42:41 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include // NULL +#include // NULL, free +#include #include "libft.h" #include "ft_printf.h" -static char *create_alt(t_conv conv) +static int starts_with_zero(t_to_print *tp, t_conv conv) { - if (conv.type == 'p') - return (ft_strdup("0x")); + return ((intmax_t)ft_strlen(tp->main_part) < conv.prec); +} + +static void create_alt(t_to_print *tp, t_conv conv) +{ + if (conv.type == 'p' && *(tp->main_part) != '(') + tp->alt = ft_strdup("0x"); + else if (conv.type == 's' || conv.type == 'c' + || ft_strncmp(tp->main_part, "0", 2) == 0 + || ((*(tp->main_part) == '\0' || !conv.flags.alt_mode) + && conv.type != 'o')) + tp->alt = ft_strdup(""); else if (conv.flags.alt_mode) { - if (conv.type == 'o') - return (ft_strdup("0")); - else if (conv.type == 'x') - return (ft_strdup("0x")); + if (conv.type == 'x') + tp->alt = ft_strdup("0x"); else if (conv.type == 'X') - return (ft_strdup("0X")); + tp->alt = ft_strdup("0X"); + else if (conv.type == 'o' && !starts_with_zero(tp, conv)) + tp->alt = ft_strdup("0"); + else + tp->alt = ft_strdup(""); } - return (ft_strdup("")); + else + tp->alt = ft_strdup(""); + return ; } -static void create_main(char *str, t_conv conv, t_to_print *tp) +static void create_main(char *str, t_to_print *tp, t_conv conv) { - if (conv.type == 'd' || conv.type == 'i') + if (conv.type != 's' && conv.type != 'c' + && ft_strncmp(str, "0", 2) == 0 && conv.prec == 0) + tp->main_part = ft_strdup(""); + else if (conv.type == 'd' || conv.type == 'i') { if (*str == '-') { @@ -70,9 +88,16 @@ t_to_print formatting(char *str, t_conv conv) { t_to_print res; + if ((conv.type == 'p' && ft_strncmp(str, "0", 2) == 0)) + { + free (str); + str = ft_strdup("(nil)"); + } + else if (conv.type == 's' && str == NULL) + str = ft_strdup("(null)"); init_printed(&res); - create_main(str, conv, &res); - res.alt = create_alt(conv); + create_main(str, &res, conv); + create_alt(&res, conv); create_padding(&res, conv); return (res); } diff --git a/ft_printf.c b/ft_printf.c index db1fbce..8fdd78b 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */ -/* Updated: 2023/09/05 12:10:57 by ljiriste ### ########.fr */ +/* Updated: 2023/09/06 18:01:29 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/padding.c b/padding.c index 600d5cf..2551dfb 100644 --- a/padding.c +++ b/padding.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:25:46 by ljiriste #+# #+# */ -/* Updated: 2023/09/05 12:13:14 by ljiriste ### ########.fr */ +/* Updated: 2023/09/08 17:19:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,10 @@ void create_padding(t_to_print *tp, t_conv conv) size_t len; len = ft_strlen(tp->main_part); - tp->zero_pad = repeat_char('0', conv.prec - len); + if (conv.type == 's' || conv.type == 'c') + tp->zero_pad = ft_strdup(""); + else + tp->zero_pad = repeat_char('0', conv.prec - len); len += ft_strlen(tp->zero_pad) + ft_strlen(tp->alt) + (tp->sign != '\0'); if (conv.flags.left_adjust) { @@ -67,7 +70,7 @@ void create_padding(t_to_print *tp, t_conv conv) else { tp->right_pad = ft_strdup(""); - if (!conv.flags.zero_pad || conv.type == 's') + if (!conv.flags.zero_pad || conv.type == 's' || conv.type == 'c') tp->left_pad = repeat_char(' ', conv.minwidth - len); else if (conv.flags.zero_pad) { -- 2.30.2