From 34bbb034af3828fd51581f62f4856b80b553b7e5 Mon Sep 17 00:00:00 2001 From: Axy Date: Fri, 31 Oct 2025 23:53:32 +0100 Subject: [PATCH] extra fixes --- .gitignore | 2 +- Makefile | 2 +- ft_printf.c | 50 ++++++++++++++++++++++++++------------------------ libftprintf.h | 2 +- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 53a78ff..a22a1c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o *.a -a.out +*.out diff --git a/Makefile b/Makefile index 87e2b67..ca674ac 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ ${NAME} : ${OBJS} ar -rcs $@ $^ clean : - rm -f ${OBJS} ${BONUS_OBJS} + rm -f ${OBJS} fclean : clean rm -f ${NAME} diff --git a/ft_printf.c b/ft_printf.c index d743650..fe4badc 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/10/29 14:24:47 by agilliar #+# #+# */ -/* Updated: 2025/10/31 23:10:28 by agilliar ### ########.fr */ +/* Updated: 2025/10/31 23:52:32 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,12 +76,12 @@ typedef struct s_format bool enable_precision; } t_format; -typedef struct s_uintptr_format +typedef struct s_uint64_format { bool print_zeros; - uintptr_t base; + uint64_t base; const char *base_s; -} t_uintptr_format; +} t_uint64_format; int ft_putchar_sim(char c, bool sim) { @@ -101,17 +101,17 @@ int ft_putnchar(char c, int n, bool sim) return (n); } -uintptr_t ft_int_abs(int64_t n) +uint64_t ft_int_abs(int64_t n) { if (n < 0) return (-n); return (n); } -t_uintptr_format ft_uintptr_format_new(const t_format *format) +t_uint64_format ft_uint64_format_new(const t_format *format) { - t_uintptr_format res; - t_specifier spec; + t_uint64_format res; + t_specifier spec; res.print_zeros = !format->enable_precision; spec = format->specifier; @@ -125,12 +125,12 @@ t_uintptr_format ft_uintptr_format_new(const t_format *format) } /* - Prints a uintptr_t without prefixes, padding or precision padding + Prints a uint64_t without prefixes, padding or precision padding */ -int ft_put_uintptr(t_uintptr_format format, uintptr_t n, bool sim) +int ft_put_uint64(t_uint64_format format, uint64_t n, bool sim) { int written; - uintptr_t base; + uint64_t base; const char *base_s; if (!n && !format.print_zeros) @@ -141,17 +141,19 @@ int ft_put_uintptr(t_uintptr_format format, uintptr_t n, bool sim) if (n < base) written = 0; else - written = ft_put_uintptr(format, n / base, sim); + written = ft_put_uint64(format, n / base, sim); if (written == -1 || ft_putchar_sim(base_s[n % base], sim) == -1) return (-1); return (written + 1); } -int ft_putprefix(const t_format *format, bool sim) +int ft_putprefix(const t_format *format, bool sim, uint64_t n) { t_specifier spec; char c; + if (n == 0) + return ; spec = format->specifier; if (!format->num_prefix && spec != SPECIFIER_PTR) return (0); @@ -205,23 +207,23 @@ int ft_putformat_str(const t_format *format, t_step_arg arg, bool sim) int ft_putformat_ptr(const t_format *format, t_step_arg arg, bool sim) { int written; - uintptr_t val; + uint64_t val; - val = (uintptr_t) arg.v_ptr; + val = (uint64_t) arg.v_ptr; if (!arg.v_ptr) { arg.v_ptr = "(nil)"; return (ft_putformat_str(format, arg, sim)); } - if (ft_putprefix(format, sim) == -1) + if (ft_putprefix(format, sim, val) == -1) return (-1); - written = ft_put_uintptr(ft_uintptr_format_new(format), val, sim); + written = ft_put_uint64(ft_uint64_format_new(format), val, sim); if (written == -1) return (-1); return (written + 2); } -uintptr_t ft_int_arg_abs(const t_format *format, t_step_arg arg) +uint64_t ft_int_arg_abs(const t_format *format, t_step_arg arg) { if (format->specifier == SPECIFIER_INT) return (ft_int_abs(arg.v_int)); @@ -252,12 +254,10 @@ int ft_putformat_scalar(const t_format *format, t_step_arg arg, bool sim) sign = ft_putsign(format, arg, sim); if (sign == -1) return (-1); - prefix = 0; - if (ft_int_arg_abs(format, arg) != 0) - prefix = ft_putprefix(format, sim); + prefix = ft_putprefix(format, sim, ft_int_arg_abs(format, arg)); if (prefix == -1) return (-1); - core = ft_put_uintptr(ft_uintptr_format_new(format), + core = ft_put_uint64(ft_uint64_format_new(format), ft_int_arg_abs(format, arg), true); precision = ft_precision_pad(format, core, sim); if (precision == -1) @@ -265,7 +265,7 @@ int ft_putformat_scalar(const t_format *format, t_step_arg arg, bool sim) zero_pad = ft_zero_pad(format, sign + prefix + precision + core, sim); if (zero_pad == -1) return (-1); - if (ft_put_uintptr(ft_uintptr_format_new(format), + if (ft_put_uint64(ft_uint64_format_new(format), ft_int_arg_abs(format, arg), sim) == -1) return (-1); return (sign + prefix + zero_pad + precision + core); @@ -470,10 +470,11 @@ int ft_printf(const char *s, ...) return (count); } +/* #include #include -#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); res = 1;}} +#define TEST_PRINTF(...) {int r1 = printf(__VA_ARGS__); fflush(stdout); int r2 = ft_printf(__VA_ARGS__); if (r1 != r2) {fprintf(stderr, "printf failed, r1: %i, r2: %i\n", r1, r2); res = 1;}} int main(void) { @@ -538,3 +539,4 @@ int main(void) return res; } +*/ diff --git a/libftprintf.h b/libftprintf.h index d93cb02..b43b38d 100644 --- a/libftprintf.h +++ b/libftprintf.h @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/10/29 14:21:01 by agilliar #+# #+# */ -/* Updated: 2025/10/30 12:19:53 by agilliar ### ########.fr */ +/* Updated: 2025/10/31 23:33:11 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ -- 2.51.0