Skip to content

Commit

Permalink
Libft updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco jesus Gata valdes committed Mar 4, 2021
1 parent bb900c3 commit 1b57b42
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
47 changes: 47 additions & 0 deletions ft_atol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/16 18:23:02 by fgata-va #+# #+# */
/* Updated: 2021/02/17 00:08:43 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

static long ft_save_long(int i, const char *str, int negative)
{
long long nbr;

nbr = 0;
while (ft_isdigit(str[i]))
{
nbr = (nbr * 10) + (str[i] - '0');
if ((nbr > 9223372036854775807 || nbr < -9223372036854775807)
&& negative > 0)
return (9223372036854775807);
i++;
}
return (nbr * negative);
}

long ft_atol(const char *str)
{
int i;
int negative;

i = 0;
negative = 1;
while (ft_strchr("\t\n\v\f\r ", str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i])
negative *= -1;
i++;
}
return (ft_save_long(i, str, negative));
}
2 changes: 1 addition & 1 deletion ft_memccpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/05 11:28:44 by fgata-va #+# #+# */
/* Updated: 2020/03/24 19:52:04 by fgata ### ########.fr */
/* Updated: 2020/02/07 16:30:51 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 2 additions & 2 deletions ft_memchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/05 13:57:46 by fgata-va #+# #+# */
/* Updated: 2020/03/24 19:52:54 by fgata ### ########.fr */
/* Updated: 2020/02/07 16:31:12 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@ void *ft_memchr(const void *s, int c, size_t n)
while (i < n)
{
if (((unsigned char *)s)[i] == (unsigned char)c)
return ((void *)s + i);
return (((void *)s + i));
i++;
}
return (NULL);
Expand Down
29 changes: 16 additions & 13 deletions ft_putnbr_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 16:36:21 by fgata-va #+# #+# */
/* Updated: 2020/03/24 19:54:03 by fgata ### ########.fr */
/* Updated: 2020/02/07 16:31:48 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,21 +16,24 @@ void ft_putnbr_fd(int n, int fd)
{
char c;

if (n < 0)
if (n <= 2147483647 && n > -2147483648)
{
ft_putchar_fd('-', fd);
if (n == -2147483648)
if (n < 0)
{
ft_putchar_fd('2', fd);
ft_putchar_fd('-', fd);
if (n == -2147483648)
{
ft_putchar_fd('2', fd);
n *= -1;
n = n % 1000000000;
}
n *= -1;
n = n % 1000000000;
}
n *= -1;
}
if (n > 9)
{
ft_putnbr_fd(n / 10, fd);
if (n > 9)
{
ft_putnbr_fd(n / 10, fd);
}
c = (n % 10) + '0';
ft_putchar_fd(c, fd);
}
c = (n % 10) + '0';
ft_putchar_fd(c, fd);
}
2 changes: 1 addition & 1 deletion ft_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 12:54:07 by fgata-va #+# #+# */
/* Updated: 2020/03/24 19:55:41 by fgata ### ########.fr */
/* Updated: 2020/11/24 21:35:20 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
37 changes: 7 additions & 30 deletions ft_strtrim.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fgata-va <fgata-va@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 16:52:37 by fgata-va #+# #+# */
/* Updated: 2019/11/19 13:13:50 by fgata-va ### ########.fr */
/* Updated: 2021/02/16 14:35:44 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,24 +15,12 @@
static unsigned int ft_getstart(char const *s1, char const *set)
{
unsigned int i;
unsigned int j;
unsigned int encountered;

i = 0;
j = 0;
encountered = 0;
while (s1[i] != '\0')
{
while (set[j] != '\0')
{
if (s1[i] == set[j])
encountered = 1;
j++;
}
if (encountered == 0)
if (!(ft_strchr(set, s1[i])))
return (i);
j = 0;
encountered = 0;
i++;
}
return (i);
Expand All @@ -41,27 +29,17 @@ static unsigned int ft_getstart(char const *s1, char const *set)
static size_t ft_getlen(char const *s1, char const *set, unsigned int s)
{
unsigned int i;
unsigned int j;
unsigned int encountered;

i = ft_strlen(s1) - 1;
j = 0;
encountered = 0;
while (i > s)
if (i == s)
return (1);
while (i >= s)
{
while (set[j] != '\0')
{
if (s1[i] == set[j])
encountered = 1;
j++;
}
if (encountered == 0)
if (!(ft_strchr(set, s1[i])))
return (i - s + 1);
j = 0;
encountered = 0;
i--;
}
return (0);
return (i);
}

char *ft_strtrim(char const *s1, char const *set)
Expand All @@ -77,6 +55,5 @@ char *ft_strtrim(char const *s1, char const *set)
str = ft_substr(s1, start, len);
if (str == NULL)
return (NULL);
str[ft_strlen(str)] = '\0';
return (str);
}

0 comments on commit 1b57b42

Please sign in to comment.