diff --git a/include/my.h b/include/my.h index 001a866..f21248c 100644 --- a/include/my.h +++ b/include/my.h @@ -20,7 +20,7 @@ #define CHAR_TO_DIGIT(c) ((c) & 0xf) #define ARRAY_SIZE(array) ((sizeof array) / (sizeof array[0])) #define TODO(s) // renvoie rin - + #define ABS(v) (((v) < (0)) ? -(v) : (v)) typedef struct { char *str; diff --git a/src/baby/baby_put_nbr.c b/src/baby/baby_put_nbr.c index cbc4e43..7f64ee7 100644 --- a/src/baby/baby_put_nbr.c +++ b/src/baby/baby_put_nbr.c @@ -8,10 +8,6 @@ int baby_put_nbr(int nb, flags_t *flags, int i) { - if (flags->flags & FLAGS_PUT_SIGN) { - flags->prefix_buff.str = "+"; - flags->prefix_buff.count = 1; - } if (nb < 0) { flags->prefix_buff.str = "-"; flags->prefix_buff.count = 1; diff --git a/src/handler/put_float.c b/src/handler/put_float.c index 8719037..205066a 100644 --- a/src/handler/put_float.c +++ b/src/handler/put_float.c @@ -34,6 +34,19 @@ void inf_or_nan(double nbr, flags_t *flags) baby_put_nan(flags); } +static +void put_sign(flags_t *flags, double nbr, int copy) +{ + if (flags->flags & FLAGS_PUT_SIGN) { + flags->prefix_buff.str = "+"; + flags->prefix_buff.count = 1; + } + if (nbr < 0 && copy == 0) { + flags->prefix_buff.str = "-"; + flags->prefix_buff.count = 1; + } +} + static int put_point(flags_t *flags, int i) { @@ -53,10 +66,7 @@ void printf_put_float(flags_t *flags) if (isnan(nbr) || isinf(nbr)) return inf_or_nan(nbr, flags); - if (nbr < 0 && copy == 0) { - flags->prefix_buff.str = "-"; - flags->prefix_buff.count = 1; - } + put_sign(flags, nbr, copy); i = baby_put_nbr(copy, flags, i); i += put_point(flags, i); nbr -= copy; diff --git a/src/handler/put_hex.c b/src/handler/put_hex.c index d1a7ee5..bcd03eb 100644 --- a/src/handler/put_hex.c +++ b/src/handler/put_hex.c @@ -54,6 +54,8 @@ void printf_put_hex(flags_t *flags) { size_t nb = (size_t)va_arg(flags->args, void *); + if (flags->precision == 0 && nb == 0) + return; if (nb == 0) { flags->spec_buff.str = "0"; flags->spec_buff.count = 1; diff --git a/src/handler/put_oct.c b/src/handler/put_oct.c index 2634e04..adfa655 100644 --- a/src/handler/put_oct.c +++ b/src/handler/put_oct.c @@ -31,6 +31,8 @@ void printf_put_oct(flags_t *flags) { int nb = va_arg(flags->args, int); + if (flags->precision == 0 && nb == 0) + return; if (nb == 0) { flags->spec_buff.str = "0"; flags->spec_buff.count = 1; diff --git a/tests/main.c b/tests/main.c index c258e29..d34b47b 100644 --- a/tests/main.c +++ b/tests/main.c @@ -6,5 +6,6 @@ int main(void) { int a = 42; - my_printf("%s pad 3: %.3s %d", "youpii", "youpii", 42); + my_printf("%5.3s pad 3: %5.3s %d\n", "hello", "hello", 42); + printf("%5.3s pad 3: %5.3s %d\n", "hello", "helloo", 42); }