blob: eed55fb94e4cd1493653374f1b71ad7c22505735 (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */
/* Updated: 2019/10/30 04:06:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "header.h"
#define MIN_INT (1 << 31)
#define MAX_INT (~(1 << 31))
int strrchr_index(const char *s, char c)
{
int i;
i = ft_strlen((char*)s) - 1;
while (s[i] != c)
{
if (i == 0)
return (-1);
i--;
}
return (i);
}
static int nbrlen_radix(long long int nbr, int radix)
{
int counter;
long long unsigned int u_nbr;
if (nbr == 0)
return (1);
counter = 0;
u_nbr = nbr;
if (nbr < 0)
{
counter++;
u_nbr = -nbr;
}
while (u_nbr > 0)
{
u_nbr /= radix;
counter++;
}
return (counter);
}
char *ft_itoa_base(long long int n, char *base)
{
char *str;
int len;
int radix;
long long unsigned int u_nbr;
radix = ft_strlen(base);
len = nbrlen_radix(n, radix);
if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
str[len] = '\0';
u_nbr = n < 0 ? -n : n;
if (n < 0)
str[0] = '-';
while (--len >= (n < 0 ? 1 : 0))
{
str[len] = base[u_nbr % radix];
u_nbr /= radix;
}
return (str);
}
static int nbrlen_unsigned_radix(long long unsigned int nbr, int radix)
{
int counter;
if (nbr == 0)
return (1);
counter = 0;
while (nbr > 0)
{
nbr /= radix;
counter++;
}
return (counter);
}
char *ft_itoa_unsigned_base(long long unsigned int n, char *base)
{
char *str;
int len;
int radix;
radix = ft_strlen(base);
len = nbrlen_unsigned_radix(n, radix);
if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
str[len] = '\0';
while (--len >= 0)
{
str[len] = base[n % radix];
n /= radix;
}
return (str);
}
|