-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecision.c
136 lines (125 loc) · 3.21 KB
/
precision.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* precision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/12 19:06:58 by smbaabu #+# #+# */
/* Updated: 2019/04/16 23:21:40 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int mantissa_length(char *s)
{
int i;
int len;
len = ft_strlen(s);
i = ft_stridx(s, '.');
return (len - i - 1);
}
static char *precision_e(char *s, int precision)
{
int e;
int mantissa;
char *tmp;
int len;
int neg;
tmp = s;
mantissa = mantissa_length(s) - 4;
e = ft_stridx(s, 'e');
precision = precision == -1 ? 6 : precision;
neg = s[0] == '-';
if (precision == 0)
s = join_and_free(ft_strsub(s, 0, neg + 1),
ft_strsub(s, e, ft_strlen(s)));
else if (mantissa > precision)
s = join_and_free(round_up(s, precision), ft_strsub(s, e, 4));
else if (precision > mantissa)
{
len = 2 + neg + precision + 4;
s = ft_strnewpad(len, '0');
ft_memcpy(s, tmp, e);
ft_memcpy(s + len - 4, tmp + e, 4);
}
free(tmp);
return (s);
}
static char *precision_f(char *s, int precision)
{
int len;
int mantissa;
int idx;
char *tmp;
tmp = s;
mantissa = mantissa_length(s);
if (precision == 0)
{
idx = ft_stridx(s, '.');
s = ft_strsub(s, 0, idx);
}
else if (precision == -1)
s = round_up(s, 6);
else if (precision < mantissa)
s = round_up(s, precision);
else if (precision > mantissa)
{
idx = ft_stridx(s, '.');
len = idx + precision + 1;
s = ft_memcpy(ft_strnewpad(len, '0'), s, len);
}
if (tmp != s)
free(tmp);
return (s);
}
static char *precision_diouxxp(char *s, int precision)
{
char *tmp;
int neg;
int len;
tmp = s;
if (precision > (len = num_digits(s)))
{
if ((neg = check_neg(tmp)) >= 0)
{
s = ft_strnewpad(precision + 1, '0');
s[0] = '-';
while (tmp[len] != '-')
s[precision--] = tmp[len--];
}
else
{
s = ft_strnewpad(precision, '0');
ft_memcpy(s + precision - len, tmp, len);
}
}
else if (precision == 0 && ft_strcmp(s, "0") == 0)
s = ft_strdup("");
if (s != tmp)
free(tmp);
return (s);
}
char *apply_precision(char *s, int precision, int specifier)
{
int len;
char *tmp;
if (specifier == F)
s = precision_f(s, precision);
else if (specifier == E)
s = precision_e(s, precision);
else if (specifier == G)
s = precision_g(s, precision);
else if ((specifier >= D && specifier <= XX) || specifier == P)
s = precision_diouxxp(s, precision);
else if (specifier != C)
{
len = ft_strlen(s);
if (len > precision && !(specifier == PP && precision == 0))
{
tmp = s;
s = ft_strsub(s, 0, precision);
free(tmp);
}
}
return (s);
}