Format s now doesn't print (null) when precision is too low.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 27 Sep 2023 15:23:46 +0000 (17:23 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 27 Sep 2023 15:23:46 +0000 (17:23 +0200)
Zero padding flag is now ignored when precision is given for numeric conversion.

formatting.c
padding.c

index 13569375761a2807924a39f0ab857c8a0c9cfefb..0b3be256db08f34ffbbc2355c87a3cf7d5aa064e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index 8f5278f6f3bdf693c24f9df9e59cad12c647d1fc..9f3c1fc2680898943c77d975cbfbbe2fe86fb806 100644 (file)
--- a/padding.c
+++ b/padding.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 ;
 }