Files
42-ft_printf/src/ft_putnbr_base.c
T
da7eandClaude Opus 5.5 c8a9ee10cf Merge mandatory and bonus into a single ft_printf, fix edge cases, add README
- Use the flag-aware implementation for both make and make bonus
  (bonus/ moved to src/, _bonus suffix dropped, bonus rule = all)
- Do not treat zero-length output (%s with "", %.0d with 0) as an error
- Return -1 without printing when a % has no conversion ("abc%", "%5")
- Keep field width with '#', '+' and ' '; ignore '0' with '-'
- Fix '#' prefix on zero, '+'/' ' with zero padding, INT_MIN negation UB
- Add README.md required by the subject

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 22:22:28 +02:00

71 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:16:44 by dgaillet #+# #+# */
/* Updated: 2025/11/21 15:04:59 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_putnbr_base_extra(unsigned int nbr, char *base, int base_size)
{
int i;
char temp;
if (nbr == 0)
return (0);
i = 1 + ft_putnbr_base_extra(nbr / base_size, base, base_size);
temp = base[nbr % base_size];
if (write(1, &temp, 1) < 0)
return (-10000);
return (i);
}
int ft_putnbr_base(unsigned int nbr, char *base, int base_size, int limit)
{
char temp;
if (nbr == 0 && limit != 0)
{
temp = base[0];
if (write(1, &temp, 1) < 0)
return (-10000);
return (1);
}
return (ft_putnbr_base_extra(nbr, base, base_size));
}
static int ft_putnbr_base_ll_extra(unsigned long long nbr, char *base,
int base_size)
{
int i;
char temp;
if (nbr == 0)
return (0);
i = 1 + ft_putnbr_base_ll_extra(nbr / base_size, base, base_size);
temp = base[nbr % base_size];
if (write(1, &temp, 1) < 0)
return (-10000);
return (i);
}
int ft_putnbr_base_ll(unsigned long long nbr, char *base, int base_size)
{
char temp;
if (nbr == 0)
{
temp = base[0];
if (write(1, &temp, 1) < 0)
return (-10000);
return (1);
}
return (ft_putnbr_base_ll_extra(nbr, base, base_size));
}