Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements, Bug fixes, New Features #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ BLUE = \033[1;34m
WHITE = \033[0m

### ### RULES ### ###
all: $(OBJ_D) $(NAME)
all: $(OBJ_D)
@$(MAKE) -j $(NAME)

$(NAME): $(SRCOBJ)
@ar -rcs $(NAME) $^
Expand Down
10 changes: 5 additions & 5 deletions ft_atod.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ double ft_atod(char *nbr)
double res;
int i;
int j;
int vz;
int sign;

j = 1;
vz = 1;
sign = 1;
if (!nbr)
return (0);
res = ft_atoi(nbr);
res = ft_atol(nbr);
if (is_negativ(nbr))
vz = -1;
sign = -1;
while (*nbr && *nbr != '.')
nbr++;
if (*nbr == '.')
nbr++;
i = ft_strlen(nbr);
while (i-- > 0)
j *= 10;
res += (double)ft_atoi(nbr) * vz / j;
res += (double)ft_atol(nbr) * sign / j;
return (res);
}
18 changes: 9 additions & 9 deletions ft_atoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@

int ft_atoi(const char *str)
{
int vz;
unsigned int c;
int sign;
unsigned int nbr;

vz = 1;
c = 0;
sign = 1;
nbr = 0;
while (ft_isspace(*str))
str++;
if (*str == '-')
{
str++;
vz = -1;
sign = -1;
}
else if (*str == '+')
str++;
if (!ft_isdigit(*str))
return (0);
while (ft_isdigit(*str))
{
c = c * 10 + *(str++) - '0';
if (c > INT32_MAX && vz == -1)
nbr = nbr * 10 + *(str++) - '0';
if (nbr > INT32_MAX && sign == -1)
return (0);
else if (c > (INT32_MAX - 1) && vz == 1)
else if (nbr > (INT32_MAX - 1) && sign == 1)
return (-1);
}
return ((int)(c * vz));
return ((int)(nbr * sign));
}
18 changes: 9 additions & 9 deletions ft_atol.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@

long ft_atol(char *str)
{
long vz;
unsigned long c;
long sign;
unsigned long nbr;

vz = 1;
c = 0;
sign = 1;
nbr = 0;
while (ft_isspace(*str))
str++;
if (*str == '-')
{
str++;
vz = -1;
sign = -1;
}
else if (*str == '+')
str++;
if (!ft_strisdigit(str))
return (0);
while (ft_isdigit(*str))
{
c = c * 10 + *(str++) - '0';
if (c > INT64_MAX && vz == -1)
nbr = nbr * 10 + *(str++) - '0';
if (nbr > INT64_MAX && sign == -1)
return (0);
else if (c > (INT64_MAX - 1) && vz == 1)
else if (nbr > (INT64_MAX - 1) && sign == 1)
return (-1);
}
return ((long)(c * vz));
return ((long)(nbr * sign));
}
12 changes: 7 additions & 5 deletions ft_first_word.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@

/*Returns a malloced string to first occurens of c in str from beginning*/
/*or if rev = 1 from the end*/
char *ft_first_word(const char *str, char c, int rev)
char *ft_first_word(const char *str, char limiter, int rev)
{
int i;
int len;
char *ret;

i = 0;
len = ft_strlen(str);
if (str == NULL)
return (NULL);
if (c == '\0')
c = ' ';
len = ft_strlen(str);
if (limiter == '\0')
limiter = ' ';
if (rev != 1)
rev = 0;
while (str[i] != '\0' && str[i] != c)
while (str[i] != '\0' && str[i] != limiter)
i++;
if (rev == 0)
ret = ft_calloc(i + 1, sizeof(char));
else
ret = ft_calloc(len - i + 1, sizeof(char));
if (ret == NULL)
return (NULL);
if (rev == 0)
ft_memcpy(ret, &str[0], i);
else
Expand Down
34 changes: 17 additions & 17 deletions ft_itoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ static int ft_count(int c)
return (n);
}

char *ft_itoa(int n)
char *ft_itoa(int nbr)
{
char *p;
char *str;
int i;
int vz;
int sign;

i = ft_count(n);
vz = 0;
if (n < 0)
vz = 1;
p = malloc(i + vz +1);
if (p == NULL)
i = ft_count(nbr);
sign = 0;
if (nbr < 0)
sign = 1;
str = malloc(i + sign + 1);
if (str == NULL)
return (NULL);
p[i + vz] = '\0';
if (vz == 1)
p[0] = '-';
str[i + sign] = '\0';
if (sign == 1)
str[0] = '-';
while (i > 0)
{
if (vz == 1)
p[i + vz - 1] = (n % 10) * -1 + '0';
if (sign == 1)
str[i + sign - 1] = (nbr % 10) * -1 + '0';
else
p[i + vz - 1] = n % 10 + '0';
n = n / 10;
str[i + sign - 1] = nbr % 10 + '0';
nbr = nbr / 10;
i--;
}
return (p);
return (str);
}
14 changes: 8 additions & 6 deletions ft_last_word.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@

/*Returns a malloced string to last occurens of c in str from end*/
/*or if rev = 1 from start to the lim*/
char *ft_last_word(const char *str, char c, int rev)
char *ft_last_word(const char *str, char limiter, int rev)
{
int i;
int len;
char *ret;

len = ft_strlen(str);
i = len;
if (str == NULL)
return (NULL);
if (c == '\0')
c = ' ';
len = ft_strlen(str);
i = len;
if (limiter == '\0')
limiter = ' ';
if (rev != 1)
rev = 0;
while (i >= 0 && str[i] != c)
while (i >= 0 && str[i] != limiter)
i--;
if (rev == 0)
ret = ft_calloc(len - i + 1, sizeof(char));
else
ret = ft_calloc(i, sizeof(char));
if (ret == NULL)
return (NULL);
if (rev == 0)
ft_memcpy(ret, &str[i + 1], len - i);
else
Expand Down
42 changes: 21 additions & 21 deletions ft_ltoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@

#include "libft.h"

static long ft_countdigits(long c)
static long ft_countdigits(long nbr)
{
long n;

n = 0;
if (c == 0)
if (nbr == 0)
return (1);
while (c != 0)
while (nbr != 0)
{
c = c / 10;
nbr = nbr / 10;
n++;
}
return (n);
}

char *ft_ltoa(long n)
char *ft_ltoa(long nbr)
{
char *p;
char *str;
long i;
long vz;
long sign;

i = ft_countdigits(n);
vz = 0;
if (n < 0)
vz = 1;
p = malloc(i + vz +1);
if (p == NULL)
i = ft_countdigits(nbr);
sign = 0;
if (nbr < 0)
sign = 1;
str = malloc(i + sign + 1);
if (str == NULL)
return (NULL);
p[i + vz] = '\0';
if (vz == 1)
p[0] = '-';
str[i + sign] = '\0';
if (sign == 1)
str[0] = '-';
while (i > 0)
{
if (vz == 1)
p[i + vz - 1] = (n % 10) * -1 + '0';
if (sign == 1)
str[i + sign - 1] = (nbr % 10) * -1 + '0';
else
p[i + vz - 1] = n % 10 + '0';
n = n / 10;
str[i + sign - 1] = nbr % 10 + '0';
nbr = nbr / 10;
i--;
}
return (p);
return (str);
}
4 changes: 1 addition & 3 deletions ft_memmove.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void ft_move(char *a, char *b, size_t v)
{
while (v > 0)
{
a[v -1] = b[v -1];
a[v - 1] = b[v - 1];
v--;
}
}
Expand All @@ -43,8 +43,6 @@ void *ft_memmove(void *dst, const void *src, size_t len)
d = (char *)dst;
s = (char *)src;
if (len > 0 && (dst != NULL || src != NULL))
{
ft_move(d, s, len);
}
return (dst);
}
Loading