From: Lukas Jiriste Date: Tue, 5 Sep 2023 09:48:50 +0000 (+0200) Subject: Added functions abs, ctoa and itoa_base and changed header and Makefile accordingly. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=2545e1f7ad3efea7c219ed7dfe4fe01098992d10;p=Libft.git Added functions abs, ctoa and itoa_base and changed header and Makefile accordingly. --- diff --git a/Makefile b/Makefile index 982546f..1867830 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ RM = rm -f INCDIR = . SRCDIR = . # SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c) -SOURCES = $(shell find $(SRCDIR) -name "*.c") +SOURCES = $(shell find $(SRCDIR) -maxdepth 1 -name "*.c") OBJECTS = $(SOURCES:.c=.o) NAME = libft.a diff --git a/ft_abs.c b/ft_abs.c new file mode 100644 index 0000000..c277682 --- /dev/null +++ b/ft_abs.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:48:21 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 11:47:52 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_abs(int n) +{ + if (n < 0) + return (-n); + else + return (n); +} diff --git a/ft_ctoa.c b/ft_ctoa.c new file mode 100644 index 0000000..1574aaf --- /dev/null +++ b/ft_ctoa.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ctoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:55:17 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 09:56:02 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include "libft.h" + +char *ft_ctoa(char c) +{ + char *res; + + res = malloc(2 * sizeof(char)); + if (res == NULL) + return (res); + res[0] = c; + res[1] = '\0'; + return (res); +} diff --git a/ft_itoa_base.c b/ft_itoa_base.c new file mode 100644 index 0000000..7e0b901 --- /dev/null +++ b/ft_itoa_base.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 11:46:44 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include //intmax_t +#include "libft.h" + +static intmax_t abs_max(intmax_t n) +{ + if (n < 0) + return (-n); + else + return (n); +} + +static int size_needed(intmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +char *ft_itoa_base(intmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + if (n < 0) + res[0] = '-'; + res[size--] = '\0'; + while (n != 0) + { + res[size--] = base[abs_max(n % base_len)]; + n /= base_len; + } + return (res); +} diff --git a/libft.h b/libft.h index 61cc5cb..9b8d3b2 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/08/24 09:24:32 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 11:47:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define LIBFT_H # include +# include int ft_isalnum(int c); int ft_isalpha(int c); @@ -24,6 +25,8 @@ int ft_isspace(int c); int ft_isupper(int c); int ft_isascii(int c); +int ft_abs(int n); + void *ft_memset(void *s, int c, size_t n); void *ft_memcpy(void *dest, const void *src, size_t n); void *ft_memmove(void *dest, const void *src, size_t n); @@ -53,6 +56,7 @@ char *ft_strtrim(const char *s1, const char *set); char **ft_split(const char *s, char c); char *ft_itoa(int n); +char *ft_itoa_base(intmax_t n, const char *base); char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); void ft_striteri(char *s, void (*f)(unsigned int, char *));