mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
dev: Added a ft_itoa_static function to avoid malloc'ing and free'ing every frame
This commit is contained in:
parent
e8fac75779
commit
9ec4a33fc8
3 changed files with 77 additions and 2 deletions
|
|
@ -2,7 +2,8 @@ CC=cc
|
||||||
CFLAGS=-Wall -Wextra -Werror -g -c
|
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 \
|
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_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_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_lstdelone_bonus.c src/lst/ft_lstiter_bonus.c \
|
||||||
src/lst/ft_lstlast_bonus.c src/lst/ft_lstmap_bonus.c \
|
src/lst/ft_lstlast_bonus.c src/lst/ft_lstmap_bonus.c \
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/14 12:40:57 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_strtrim(const char *s1, const char *set);
|
||||||
char **ft_split(const char *s, char separator);
|
char **ft_split(const char *s, char separator);
|
||||||
char *ft_itoa(int n);
|
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));
|
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
|
||||||
void ft_striteri(char *s, void (*f)(unsigned int, char *));
|
void ft_striteri(char *s, void (*f)(unsigned int, char *));
|
||||||
void ft_putchar_fd(char c, int fd);
|
void ft_putchar_fd(char c, int fd);
|
||||||
|
|
|
||||||
73
libft/src/str/ft_itoa_static.c
Normal file
73
libft/src/str/ft_itoa_static.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa_static.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/20 15:19:06 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/20 15:27:40 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/libft.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue