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.
-Subproject commit f3d297fe160d8798b15c8a830b57281288abaaf6
+Subproject commit b1a54dd5836019e70a715e04420298092def5f3e
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
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);
}
++(*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));
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stddef.h> // NULL
+#include <stdlib.h> // NULL, free
+#include <limits.h>
#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 == '-')
{
{
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);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
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)
{
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)
{