-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_p_common_fn_bonus.c
100 lines (89 loc) · 2.19 KB
/
ft_p_common_fn_bonus.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
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_p_common_fn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rhsu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/12 15:12:52 by rhsu #+# #+# */
/* Updated: 2023/10/12 16:35:22 by rhsu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* if nosign flag is 1, then will not print out the '-' sign */
int ft_pnum(char *s, int nosign)
{
int i;
i = 0;
if ((nosign) && (s[0] == '-'))
s++;
while (s[i])
{
ft_putchar_fd(s[i], 1);
i++;
}
return (i);
}
/* print ch number of times, using c */
int ft_pad_char(int c, char ch)
{
int r;
r = c;
while (c != 0)
{
ft_putchar_fd(ch, 1);
c--;
}
return (r);
}
/* return number of digits pointed in s, ignore '-' sign */
int ft_cnt_numdigits(char *s)
{
int i;
i = 0;
while (s[i])
i++;
if (s[0] == '-')
i--;
return (i);
}
/* prec defines minimum number of digits in s,
if s has less digits then prec, will return
the number of '0' to be padded to the left
in order to have 'prec' number of total printed digits
*/
int ft_cnt_zero_prefix(int prec, char *s)
{
int l;
l = ft_cnt_numdigits(s);
if (prec > l)
return (prec - l);
else
return (0);
}
/* prec defines minimum number of digits in s,
if s has less digits then prec, print
the number of '0' to be padded to the left
in order to have 'prec' number of total printed digits
*/
int ft_zero_prefix(int prec, char *s)
{
int l;
int cnt;
l = ft_strlen(s);
if (s[0] == '-')
l--;
if (prec > l)
{
cnt = prec - l;
l = 0;
while (l < cnt)
{
ft_putchar_fd('0', 1);
l++;
}
}
else
return (0);
return (l);
}