]> Untitled Git - axy/ft/ft_printf.git/commitdiff
Partial testing
author= <=>
Fri, 31 Oct 2025 17:44:47 +0000 (18:44 +0100)
committer= <=>
Fri, 31 Oct 2025 17:44:47 +0000 (18:44 +0100)
ft_printf.c

index 0d370b22eca843666695e9b2781cb35fdb7b5f50..d22b9f91a76ccfd4d7243d4b6e60e1b76f5d311d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/10/29 14:24:47 by agilliar          #+#    #+#             */
-/*   Updated: 2025/10/31 13:10:29 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/10/31 17:45:11 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -243,11 +243,11 @@ int       ft_putsign(const t_format *format, t_step_arg arg, bool sim)
 
 int    ft_putformat_scalar(const t_format *format, t_step_arg arg, bool sim)
 {
-       int                     sign;
-       int                     prefix;
-       int                     core;
-       int                     precision;
-       int                     zero_pad;
+       int     sign;
+       int     prefix;
+       int     core;
+       int     precision;
+       int     zero_pad;
 
        sign = ft_putsign(format, arg, sim);
        if (sign == -1)
@@ -302,7 +302,7 @@ int ft_putformat(const t_format *format, t_step_arg arg)
                        && ft_putnchar(' ', format->width - len, false) == -1)
                || (format->justify == JUSTIFY_RIGHT && f(format, arg, false) == -1))
                return (-1);
-       return (len);
+       return (len + (len < format->width) * (format->width - len));
 }
 
 t_format       ft_format_default(void)
@@ -392,6 +392,12 @@ t_specifier        ft_parse_specifier(char c)
        return (SPECIFIER_NONE);
 }
 
+void   ft_format_normalize(t_format *format)
+{
+       if (format->enable_precision || format->justify == JUSTIFY_LEFT)
+               format->zero_pad = false;
+}
+
 t_format       ft_parse_format(const char **s)
 {
        t_format        res;
@@ -402,6 +408,7 @@ t_format    ft_parse_format(const char **s)
        ft_parse_width(s, &res);
        ft_parse_precision(s, &res);
        res.specifier = ft_parse_specifier(*((*s)++));
+       ft_format_normalize(&res);
        return (res);
 }
 
@@ -463,8 +470,32 @@ int        ft_printf(const char *s, ...)
 
 #include <stdio.h>
 
+#define TEST_PRINTF(...) {int r1 = printf(__VA_ARGS__); int r2 = ft_printf(__VA_ARGS__); if (r1 != r2) fprintf(stderr, "printf failed, r1: %i, r2: %i\n", r1, r2); }
+
 int    main(void)
 {
-       ft_printf("%12.5u\n", 120);
-       printf("%12.5u\n", 120);
+       TEST_PRINTF("this %corks\n", 'w');
+       TEST_PRINTF("this %3corks\n", 'w');
+       TEST_PRINTF("this %s\n", "is awesome");
+       TEST_PRINTF("this %.5s\n", "is awesome");
+       TEST_PRINTF("this %2s\n", "is awesome");
+       TEST_PRINTF("this %10.6s\n", "is awesome");
+       TEST_PRINTF("this %.6s\n", "is awesome");
+       TEST_PRINTF("this %1.6s\n", "is awesome");
+       TEST_PRINTF("this %.6s\n", NULL);
+       TEST_PRINTF("this %p\n", NULL);
+       TEST_PRINTF("this %-20p a\n", "test");
+       TEST_PRINTF("this %20p a\n", "test");
+       TEST_PRINTF("this %i a\n", 12);
+       TEST_PRINTF("this %d a\n", 12);
+       TEST_PRINTF("this %-10d a\n", 12);
+       TEST_PRINTF("this %-10.5d a\n", 12);
+       TEST_PRINTF("this % 10.5d a\n", 12);
+       TEST_PRINTF("this % 10.5d a\n", -123);
+       TEST_PRINTF("this % .5d a\n", 12);
+       TEST_PRINTF("this %+10.5d a\n", 12);
+       TEST_PRINTF("this %+10.5d a\n", -123);
+       TEST_PRINTF("this %010.5d a\n", 12);
+       TEST_PRINTF("this %0-10.5d a\n", 12);
+       TEST_PRINTF("%s%p\n", "this ptr is: ", "heyo");
 }