From e5fdfd0ac11cba0802cc2e34d545c24d12ae1671 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Mon, 11 Sep 2023 13:22:33 +0200 Subject: [PATCH] Added a test for printing char * str = NULL and implementing printf behaviour for this case. --- Libft | 2 +- conversion.c | 7 +------ formatting.c | 11 +++++++++-- main.c | 6 ++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Libft b/Libft index f56fd06..b1a54dd 160000 --- a/Libft +++ b/Libft @@ -1 +1 @@ -Subproject commit f56fd060021c191397b4bbb600d5e9f4f1203fee +Subproject commit b1a54dd5836019e70a715e04420298092def5f3e diff --git a/conversion.c b/conversion.c index f10d4cd..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/08 17:52:41 by ljiriste ### ########.fr */ +/* Updated: 2023/09/11 12:41:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,11 +36,6 @@ static char *base_str_constr(char type, va_list *args) res = ft_strdup(va_arg(*args, char *)); else if (type == 'p') res = ft_itoa_base((intptr_t)va_arg(*args, void *), "0123456789abcdef"); - if (type == 'p' && ft_strncmp(res, "0", 2) == 0) - { - free (res); - res = ft_strdup("(nil)"); - } return (res); } diff --git a/formatting.c b/formatting.c index ae6f8ec..1b66f0b 100644 --- a/formatting.c +++ b/formatting.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */ -/* Updated: 2023/09/08 16:39:07 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" @@ -88,6 +88,13 @@ 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, &res, conv); create_alt(&res, conv); diff --git a/main.c b/main.c index feedf74..42ccfd7 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/06 17:26:17 by ljiriste #+# #+# */ -/* Updated: 2023/09/08 17:58:29 by ljiriste ### ########.fr */ +/* Updated: 2023/09/11 12:32:32 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -883,8 +883,10 @@ int main(void) i += ft_printf("\t%s\n", "Hello"); i += ft_printf("\t%.31s\n", "This is a not a very long text. Because this second part is not printed."); - char *s; + char *s = NULL; + i += ft_printf("Asd%s\n", s); + s = ft_strdup("The end."); i += ft_printf("\t%s%*s\n", "The end is 20 spaces away", ft_strlen(s) + 20, s); free(s); -- 2.30.2