diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-15 11:40:48 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-04 04:09:19 +0100 |
| commit | 014d876b691b5c0e84faa98bd308a855269e4ae4 (patch) | |
| tree | 8c26213ab8426f906fa143f904ab0f6a39e22013 | |
| parent | 435fec311c0c5173ae99c32b8f4e6431bd1e43d2 (diff) | |
| download | libft-014d876b691b5c0e84faa98bd308a855269e4ae4.tar.gz libft-014d876b691b5c0e84faa98bd308a855269e4ae4.tar.bz2 libft-014d876b691b5c0e84faa98bd308a855269e4ae4.zip | |
Added ft_strtol, ft_isspace, ft_todigit, ft_strict_atoi, modified ft_atoi to use ft_strtol
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | ft_atoi.c | 41 | ||||
| -rw-r--r-- | ft_isspace.c | 19 | ||||
| -rw-r--r-- | ft_strtol.c | 78 | ||||
| -rw-r--r-- | libft.h | 7 |
5 files changed, 108 insertions, 41 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 15:45:53 by cacharle #+# #+# # -# Updated: 2020/02/04 03:32:29 by cacharle ### ########.fr # +# Updated: 2020/02/04 04:08:01 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -28,7 +28,7 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c f ft_strncat.c ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \ ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c ft_tolower.c \ ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strcount.c ft_memswap.c ft_qsort.c \ - ft_abs.c + ft_abs.c ft_strtol.c ft_isspace.c OBJ = $(SRC:.c=.o) INCLUDE = libft.h @@ -6,48 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ -/* Updated: 2019/10/19 10:50:39 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:04:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#define MIN_INT -2147483648 -#define MAX_INT 2147483647 - -static int precheck(const char **str) -{ - int is_negative; - - while (**str == ' ' || **str == '\t' || **str == '\n' - || **str == '\v' || **str == '\f' || **str == '\r') - (*str)++; - is_negative = 0; - if (**str == '-' || **str == '+') - { - if (**str == '-') - is_negative = 1; - (*str)++; - } - return (is_negative); -} +#include "libft.h" int ft_atoi(const char *str) { - unsigned int nb; - int i; - int is_negative; - - is_negative = precheck(&str); - i = 0; - nb = 0; - while (str[i] >= '0' && str[i] <= '9') - { - if (!is_negative && nb > (unsigned int)MAX_INT) - return (-1); - else if (nb > (unsigned int)MIN_INT) - return (0); - nb *= 10; - nb += str[i] - '0'; - i++; - } - return ((int)(is_negative ? -nb : nb)); + return ((int)ft_strtol(str, (char**)NULL, 10)); } diff --git a/ft_isspace.c b/ft_isspace.c new file mode 100644 index 0000000..18b6dba --- /dev/null +++ b/ft_isspace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 11:33:36 by cacharle #+# #+# */ +/* Updated: 2020/01/15 11:35:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + return (c == ' ' || c == '\t' || c == '\n' + || c == '\v' || c == '\f' || c == '\r'); +} diff --git a/ft_strtol.c b/ft_strtol.c new file mode 100644 index 0000000..447f1af --- /dev/null +++ b/ft_strtol.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */ +/* Updated: 2020/01/15 11:39:34 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#define STRTOL_STD_BASE "0123456789abcdefghijklmnopqrstuvwxyz" + +static int strtol_handle_base(int base, const char **str) +{ + if (base != 16 && base != 0) + return (base); + if (base == 16 && **str == '0' && (*str)[1] == 'x') + { + *str += 2; + return (base); + } + if (**str == '0') + { + (*str)++; + if (**str == 'x') + { + (*str)++; + return (16); + } + else + return (8); + } + return (10); +} + +static long errno_return(int err) +{ + errno = err; + return (0); +} + +/* +** If there is no digits doesn't put str in endptr like the original, +** instead it puts the address of the char after spaces and sign. +** Too much lines and annoyance, I can't be bothered. +*/ + +long ft_strtol(const char *str, char **endptr, int base) +{ + t_bool is_negative; + long long nb; + char base_str[37]; + + if (base > 36) + return (errno_return(EINVAL)); + while (ft_isspace(*str)) + str++; + is_negative = *str == '-' ? TRUE : FALSE; + if (*str == '-' || *str == '+') + str++; + base = strtol_handle_base(base, &str); + ft_strncpy(base_str, STRTOL_STD_BASE, base); + nb = 0; + while (ft_strchr(base_str, *str) != NULL) + { + nb *= base; + nb += ft_strchr(base_str, ft_tolower(*str)) - base_str; + if (((long)nb ^ (long)(nb / base)) < 0) + return (errno_return(ERANGE)); + } + if (endptr != NULL) + *endptr = (char*)str; + return (is_negative ? -nb : nb); +} @@ -6,13 +6,14 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */ -/* Updated: 2020/02/04 03:33:44 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 04:08:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFT_H # define LIBFT_H +# include <errno.h> # include <string.h> # define TRUE 1 @@ -62,6 +63,7 @@ int ft_isdigit(int c); int ft_isalnum(int c); int ft_isascii(int c); int ft_isprint(int c); +int ft_isspace(int c); int ft_toupper(int c); int ft_tolower(int c); void *ft_calloc(size_t count, size_t size); @@ -90,6 +92,9 @@ void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); +long ft_strtol(const char *str, char **endptr, int base); + + /* ** bonus */ |
