-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathft_intmax_toa.c
More file actions
63 lines (57 loc) · 1.63 KB
/
ft_intmax_toa.c
File metadata and controls
63 lines (57 loc) · 1.63 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_intmax_toa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: scamargo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/30 17:04:08 by scamargo #+# #+# */
/* Updated: 2018/02/01 15:41:18 by scamargo ### ########.fr */
/* */
/* ************************************************************************** */
#include <inttypes.h>
#include "ft_printf.h"
static int num_len(intmax_t num)
{
int len;
len = 0;
while ((num = num / 10))
len++;
return (len);
}
static void set_intmax_toa(char **p_str, intmax_t num)
{
if (num < 10)
{
**p_str = num + '0';
(*p_str)++;
return ;
}
set_intmax_toa(p_str, num / 10);
set_intmax_toa(p_str, num % 10);
}
char *ft_intmax_toa(intmax_t num)
{
int len;
char *str;
char *str_copy;
len = num_len(num);
len += (num < 0) ? 1 : 0;
if(!(str = ft_memalloc(len + 1)))
return (0);
str_copy = str;
if (num == INT64_MIN)
{
*str_copy++ = '-';
*str_copy++ = '9';
num = 223372036854775808;
}
else if (num < 0)
{
*str_copy++ = '-';
num *= -1;
}
set_intmax_toa(&str_copy, num);
*str_copy = '\0';
return (str);
}