From 9ec4a33fc80ce507b0fe8c08b6d6825abb3334ae Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 20 Aug 2025 15:29:33 +0200 Subject: [PATCH] dev: Added a ft_itoa_static function to avoid malloc'ing and free'ing every frame --- libft/Makefile | 3 +- libft/includes/libft.h | 3 +- libft/src/str/ft_itoa_static.c | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 libft/src/str/ft_itoa_static.c diff --git a/libft/Makefile b/libft/Makefile index cd7a071..ab05697 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -2,7 +2,8 @@ CC=cc CFLAGS=-Wall -Wextra -Werror -g -c SOURCEFILES=src/str/ft_atoi.c src/mem/ft_bzero.c src/mem/ft_calloc.c src/cond/ft_isalnum.c \ src/cond/ft_isalpha.c src/cond/ft_isascii.c src/cond/ft_isdigit.c \ - src/cond/ft_isprint.c src/str/ft_itoa.c src/lst/ft_lstadd_back_bonus.c \ + src/cond/ft_isprint.c src/str/ft_itoa.c src/str/ft_itoa_static.c \ + src/lst/ft_lstadd_back_bonus.c \ src/lst/ft_lstadd_front_bonus.c src/lst/ft_lstclear_bonus.c \ src/lst/ft_lstdelone_bonus.c src/lst/ft_lstiter_bonus.c \ src/lst/ft_lstlast_bonus.c src/lst/ft_lstmap_bonus.c \ diff --git a/libft/includes/libft.h b/libft/includes/libft.h index e297390..034bc95 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 12:40:57 by tchampio #+# #+# */ -/* Updated: 2024/12/18 04:40:53 by tchampio ### ########.fr */ +/* Updated: 2025/08/20 15:26:09 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,7 @@ char *ft_strjoin(const char *s1, const char *s2); char *ft_strtrim(const char *s1, const char *set); char **ft_split(const char *s, char separator); char *ft_itoa(int n); +char *ft_itoa_static(int n, char *string, size_t buffersize); char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); void ft_striteri(char *s, void (*f)(unsigned int, char *)); void ft_putchar_fd(char c, int fd); diff --git a/libft/src/str/ft_itoa_static.c b/libft/src/str/ft_itoa_static.c new file mode 100644 index 0000000..5fde353 --- /dev/null +++ b/libft/src/str/ft_itoa_static.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_static.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/08/20 15:19:06 by tchampio #+# #+# */ +/* Updated: 2025/08/20 15:27:40 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/libft.h" +#include + +static size_t getnumberoffigures(int n) +{ + size_t totalsize; + long nn; + + totalsize = 0; + nn = n; + if (n == 0) + return (1); + if (nn < 0) + { + totalsize++; + nn = -nn; + } + while (nn > 0) + { + nn /= 10; + totalsize++; + } + return (totalsize); +} + +static void populate_array(char *s, int n, size_t size) +{ + int isneg; + long nn; + size_t index; + + nn = n; + if (nn < 0) + nn = -nn; + isneg = (n < 0); + index = size - 1; + while (nn > 0) + { + s[index] = (nn % 10) + '0'; + nn /= 10; + index--; + } + if (isneg == 1) + s[index] = '-'; +} + +char *ft_itoa_static(int n, char *string, size_t buffersize) +{ + size_t size; + + size = getnumberoffigures(n); + ft_bzero(string, buffersize); + if (!string) + return (NULL); + if (size > buffersize) + return (NULL); + if (n == 0) + ft_strlcpy(string, "0", 2); + populate_array(string, n, size); + return (string); +}