-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_putnbr.c
88 lines (79 loc) · 2.45 KB
/
ft_printf_putnbr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnakano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/12 11:02:54 by dnakano #+# #+# */
/* Updated: 2020/10/18 12:57:51 by dnakano ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int ft_printf_putnbr_width_digit(t_llong nbr, t_uint radix,
t_printf_flags *flags)
{
int count;
int digit;
t_ullong nbr_abs;
digit = flags->precision;
count = 0;
if (nbr < 0 || flags->flag & (FLAG_PUTPOSSPACE | FLAG_PUTPOSSIGN))
count++;
if (nbr == 0)
{
if (digit == 0)
return (flags->flag & (FLAG_PUTPOSSPACE | FLAG_PUTPOSSIGN) ? 1 : 0);
return (digit > 1 ? digit + count : 1 + count);
}
nbr_abs = nbr < 0 ? -nbr : nbr;
while (nbr_abs > 0 || digit > 0)
{
nbr_abs /= radix;
digit--;
count++;
}
return (count);
}
int ft_printf_putnbr_unsigned_width_digit(t_ullong nbr, t_uint radix,
t_printf_flags *flags)
{
int count;
int digit;
digit = flags->precision;
count = 0;
if (nbr == 0)
{
if (digit == 0)
return (0);
return (digit > 1 ? digit + count : 1 + count);
}
while (nbr > 0 || digit > 0)
{
nbr /= radix;
digit--;
count++;
}
return (count);
}
void ft_printf_putnbr_unsigned_base_digit(t_ullong nbr, t_uint radix,
int digit, int flag_upcase)
{
const char *base;
const char baselow[] = "0123456789abcdef";
const char baseup[] = "0123456789ABCDEF";
if (radix > 16)
return ;
base = (flag_upcase ? baseup : baselow);
if (nbr >= radix || digit - 1 > 0)
ft_printf_putnbr_unsigned_base_digit(nbr / radix, radix, digit - 1,
flag_upcase);
ft_putchar_fd(base[nbr % radix], 1);
}
void ft_printf_putnbr_base_digit(t_llong nbr, t_uint radix, int digit,
int flag_upcase)
{
t_ullong nbr_abs;
nbr_abs = nbr < 0 ? -nbr : nbr;
ft_printf_putnbr_unsigned_base_digit(nbr_abs, radix, digit, flag_upcase);
}