From c41244a4b88b91227cb38f11dfbdcd798a934e6d Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 27 Sep 2023 17:23:46 +0200 Subject: [PATCH] Format s now doesn't print (null) when precision is too low. Zero padding flag is now ignored when precision is given for numeric conversion. --- formatting.c | 7 +++++-- padding.c | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/formatting.c b/formatting.c index 1356937..0b3be25 100644 --- a/formatting.c +++ b/formatting.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */ -/* Updated: 2023/09/15 15:33:50 by ljiriste ### ########.fr */ +/* Updated: 2023/09/27 17:01:46 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,8 +93,11 @@ t_to_print formatting(char **str, t_conv conv) free (*str); *str = ft_strdup("(nil)"); } - else if (conv.type == 's' && *str == NULL) + else if (conv.type == 's' && *str == NULL + && (conv.prec >= 6 || conv.prec < 0)) *str = ft_strdup("(null)"); + else if (conv.type == 's' && *str == NULL) + *str = ft_strdup(""); init_printed(&res); create_main(*str, &res, conv); create_alt(&res, conv); diff --git a/padding.c b/padding.c index 8f5278f..9f3c1fc 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/27 15:16:21 by ljiriste ### ########.fr */ +/* Updated: 2023/09/27 16:48:26 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,31 +52,31 @@ static void lengthen_by_zeros(char **str, int n) return ; } -void create_padding(t_to_print *tp, t_conv conv) +void create_padding(t_to_print *tp, t_conv cv) { size_t len; - len = ft_strlen(tp->main_part) + (!tp->main_part[0] && conv.type == 'c'); - if (conv.type == 's' || conv.type == 'c') + len = ft_strlen(tp->main_part) + (!tp->main_part[0] && cv.type == 'c'); + if (cv.type == 's' || cv.type == 'c') tp->zero_pad = ft_strdup(""); else - tp->zero_pad = repeat_char('0', conv.prec - len); + tp->zero_pad = repeat_char('0', cv.prec - len); len += ft_strlen(tp->zero_pad) + ft_strlen(tp->alt) + (tp->sign != '\0'); - if (conv.flags.left_adjust) + if (cv.flags.left_adjust) { - tp->right_pad = repeat_char(' ', conv.minwidth - len); + tp->right_pad = repeat_char(' ', cv.minwidth - len); tp->left_pad = ft_strdup(""); } else { tp->right_pad = ft_strdup(""); - 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) + if (cv.flags.zero_pad && cv.prec < 0 && ft_strchr("diouxX", cv.type)) { tp->left_pad = ft_strdup(""); - lengthen_by_zeros(&(tp->zero_pad), conv.minwidth - len); + lengthen_by_zeros(&(tp->zero_pad), cv.minwidth - len); } + else + tp->left_pad = repeat_char(' ', cv.minwidth - len); } return ; } -- 2.30.2