/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_itoa_base.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/02/01 15:26:43 by bchanot           #+#    #+#             */
/*   Updated: 2018/10/13 20:32:40 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static void	ft_itoa_loop(t_int64 nb, char **s, t_uint8 base, size_t depth)
{
	const char	*hex = "0123456789abcdef";
	t_uint64	index;

	index = nb % base;
	if (nb / base > 0)
		ft_itoa_loop(nb / base, s, base, (depth - 1));
	(*s)[depth] = hex[index];
}

char		*ft_itoa_base_depth(t_int64 nb, t_uint8 base, size_t depth)
{
	char	*s;
	size_t	i;

	if (!(s = ft_strnew(depth + 1)))
		return (NULL);
	i = 0;
	while (i <= depth)
		s[i++] = '0';
	ft_itoa_loop(nb, &s, base, depth);
	return (s);
}

static void	ft_utoa_loop(t_uint64 nb, char **s, t_uint8 base, size_t depth)
{
	const char	*hex = "0123456789abcdef";
	t_uint64	index;

	index = nb % base;
	if (nb / base > 0)
		ft_utoa_loop(nb / base, s, base, (depth - 1));
	(*s)[depth] = hex[index];
}

char		*ft_utoa_base_depth(t_uint64 nb, t_uint8 base, size_t depth)
{
	char	*s;
	size_t	i;

	if (!(s = ft_strnew(depth + 1)))
		return (NULL);
	i = 0;
	while (i <= depth)
		s[i++] = '0';
	ft_utoa_loop(nb, &s, base, depth);
	return (s);
}
