mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-05-14 00:08:47 +02:00
「🔨」 fix(libft): replaced libft by a better one :D
This commit is contained in:
40
libft/src/str/XtoX/ft_atoi.c
Normal file
40
libft/src/str/XtoX/ft_atoi.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
|
||||
/* Updated: 2023/11/13 19:29:30 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int r;
|
||||
int i;
|
||||
int s;
|
||||
|
||||
r = 0;
|
||||
i = 0;
|
||||
s = 1;
|
||||
if (!str)
|
||||
return (0);
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
if (str[i] == '-' || str[i] == '+')
|
||||
{
|
||||
if (str[i] == '-')
|
||||
s *= -1;
|
||||
i++;
|
||||
}
|
||||
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
|
||||
{
|
||||
r = (r * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
return (r * s);
|
||||
}
|
40
libft/src/str/XtoX/ft_atol.c
Normal file
40
libft/src/str/XtoX/ft_atol.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atol.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/18 00:39:13 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
long long ft_atol(const char *str)
|
||||
{
|
||||
long long r;
|
||||
int i;
|
||||
char s;
|
||||
|
||||
r = 0;
|
||||
i = 0;
|
||||
s = 1;
|
||||
if (!str)
|
||||
return (0);
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
if (str[i] == '-' || str[i] == '+')
|
||||
{
|
||||
if (str[i] == '-')
|
||||
s *= -1;
|
||||
i++;
|
||||
}
|
||||
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
|
||||
{
|
||||
r = (r * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
return (r * s);
|
||||
}
|
89
libft/src/str/XtoX/ft_itoa.c
Normal file
89
libft/src/str/XtoX/ft_itoa.c
Normal file
@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
|
||||
/* Updated: 2023/11/11 11:41:53 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int n_size(int n)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = 0;
|
||||
if (n <= 0)
|
||||
{
|
||||
s++;
|
||||
n *= -1;
|
||||
}
|
||||
while (n >= 1)
|
||||
{
|
||||
s++;
|
||||
n /= 10;
|
||||
}
|
||||
return (s);
|
||||
}
|
||||
|
||||
static size_t pwten(int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while (n)
|
||||
{
|
||||
i *= 10;
|
||||
n--;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static char *itoa_rec(char *r, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (n > 9)
|
||||
{
|
||||
r[0] = n / (pwten(n_size(n) - 1)) + '0';
|
||||
i = 1;
|
||||
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
|
||||
{
|
||||
r[i] = '0';
|
||||
i++;
|
||||
}
|
||||
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
|
||||
n % pwten(n_size(n) - 1));
|
||||
return (r);
|
||||
}
|
||||
r[0] = n + '0';
|
||||
return (r);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *r;
|
||||
|
||||
if (n == -2147483648)
|
||||
return (ft_strdup("-2147483648"));
|
||||
r = ft_calloc(sizeof(char), n_size(n) + 1);
|
||||
if (!r)
|
||||
return (NULL);
|
||||
if (n == 0)
|
||||
{
|
||||
r[0] = '0';
|
||||
return (r);
|
||||
}
|
||||
if (n < 0)
|
||||
{
|
||||
r[0] = '-';
|
||||
itoa_rec(&r[1], n * -1);
|
||||
return (r);
|
||||
}
|
||||
itoa_rec(r, n);
|
||||
return (r);
|
||||
}
|
89
libft/src/str/XtoX/ft_ltoa.c
Normal file
89
libft/src/str/XtoX/ft_ltoa.c
Normal file
@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ltoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/12 00:52:12 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int n_size(int n)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = 0;
|
||||
if (n <= 0)
|
||||
{
|
||||
s++;
|
||||
n *= -1;
|
||||
}
|
||||
while (n >= 1)
|
||||
{
|
||||
s++;
|
||||
n /= 10;
|
||||
}
|
||||
return (s);
|
||||
}
|
||||
|
||||
static size_t pwten(int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while (n)
|
||||
{
|
||||
i *= 10;
|
||||
n--;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static char *itoa_rec(char *r, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (n > 9)
|
||||
{
|
||||
r[0] = n / (pwten(n_size(n) - 1)) + '0';
|
||||
i = 1;
|
||||
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
|
||||
{
|
||||
r[i] = '0';
|
||||
i++;
|
||||
}
|
||||
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
|
||||
n % pwten(n_size(n) - 1));
|
||||
return (r);
|
||||
}
|
||||
r[0] = n + '0';
|
||||
return (r);
|
||||
}
|
||||
|
||||
char *ft_ltoa(long long n)
|
||||
{
|
||||
char *r;
|
||||
|
||||
if (n == -9223372036854775807)
|
||||
return (ft_strdup("-9223372036854775807"));
|
||||
r = ft_calloc(sizeof(char), n_size(n) + 1);
|
||||
if (!r)
|
||||
return (NULL);
|
||||
if (n == 0)
|
||||
{
|
||||
r[0] = '0';
|
||||
return (r);
|
||||
}
|
||||
if (n < 0)
|
||||
{
|
||||
r[0] = '-';
|
||||
itoa_rec(&r[1], n * -1);
|
||||
return (r);
|
||||
}
|
||||
itoa_rec(r, n);
|
||||
return (r);
|
||||
}
|
Reference in New Issue
Block a user