From 1b57b42b77161ddf0ef0c6c8fdfa0aa1fed97675 Mon Sep 17 00:00:00 2001 From: Francisco jesus Gata valdes Date: Thu, 4 Mar 2021 15:32:19 +0100 Subject: [PATCH] Libft updated --- ft_atol.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ ft_memccpy.c | 2 +- ft_memchr.c | 4 ++-- ft_putnbr_fd.c | 29 ++++++++++++++++------------- ft_split.c | 2 +- ft_strtrim.c | 37 +++++++------------------------------ 6 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 ft_atol.c diff --git a/ft_atol.c b/ft_atol.c new file mode 100644 index 0000000..d3d6b5d --- /dev/null +++ b/ft_atol.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgata-va +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/ft_memccpy.c b/ft_memccpy.c index fd9eb72..d05c3fa 100644 --- a/ft_memccpy.c +++ b/ft_memccpy.c @@ -6,7 +6,7 @@ /* By: fgata-va +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/ft_memchr.c b/ft_memchr.c index 7d603c6..ac4bd40 100644 --- a/ft_memchr.c +++ b/ft_memchr.c @@ -6,7 +6,7 @@ /* By: fgata-va +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index cba142f..5d97b7c 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -6,7 +6,7 @@ /* By: fgata-va +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); } diff --git a/ft_split.c b/ft_split.c index 9e1843c..d4f4b5c 100644 --- a/ft_split.c +++ b/ft_split.c @@ -6,7 +6,7 @@ /* By: fgata-va +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/ft_strtrim.c b/ft_strtrim.c index 57bbc37..bce3931 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -6,7 +6,7 @@ /* By: fgata-va +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); @@ -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) @@ -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); }