Compare commits

...

2 Commits

Author SHA1 Message Date
hugogogo
413e149003 adding labs 2026-05-07 11:46:03 +02:00
hugogogo
36379f56b0 adding itoa_static and ft_nbrlen 2026-05-07 03:06:16 +02:00
7 changed files with 123 additions and 4 deletions

View File

@@ -37,6 +37,7 @@ SRCS = ft_memset.c \
ft_tolower.c \
\
ft_strlen.c \
ft_nbrlen.c \
ft_strchr.c \
ft_strrchr.c \
ft_strchrset.c \
@@ -75,6 +76,7 @@ SRCS = ft_memset.c \
ft_atoll_superscript.c \
ft_atof.c \
ft_itoa.c \
ft_itoa_static.c \
ft_utoa.c \
ft_atoibase.c \
\
@@ -120,6 +122,7 @@ SRCS = ft_memset.c \
pf_utils.c \
\
ft_abs.c \
ft_labs.c \
ft_fabs.c \
ft_greater.c \
ft_smaller.c \

View File

@@ -27,6 +27,7 @@ void *ft_memmove(void *dst, const void *src, size_t len);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
size_t ft_strlen(const char *str);
size_t ft_nbrlen(int nbr);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isdigit_superscript(const char *input, int *size);
@@ -60,6 +61,7 @@ size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(long int n);
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len);
char *ft_utoa(unsigned long int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
@@ -122,6 +124,7 @@ void ft_putnbrendl(int n);
void ft_putnbrendl_fd(int n, int fd);
char *ft_concat_free(char *str1, char *str2);
int ft_abs(int n);
long int ft_labs(long int n);
double ft_fabs(double n);
int ft_greater(int a, int b);
int ft_smaller(int a, int b);

80
srcs/ft_itoa_static.c Normal file
View File

@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
/* Updated: 2020/02/19 15:44:04 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/**
* Converts a long integer to its string representation and stores it in a pre-allocated buffer.
*
* @param nbr The long integer to convert.
* @param buffer The buffer to store the resulting string. Must be pre-allocated.
* @param buff_len The size of the buffer.
* Must be at least as large as the length of the number (including sign).
*
* @return The buffer (if successful), or NULL if the buffer is too small or invalid.
*/
#include "libft.h"
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len)
{
int is_negative;
const char *min_str;
size_t min_len;
size_t i;
if (buffer == NULL || buff_len == 0)
{
return NULL; // Invalid buffer or length
}
// Handle LONG_MIN separately
if (nbr == LONG_MIN)
{
min_str = "-9223372036854775808";
min_len = ft_strlen(min_str);
if (buff_len < min_len)
{
return NULL; // Buffer too small for LONG_MIN
}
i = 0;
while (i < min_len)
{
buffer[i] = min_str[i];
i++;
}
buffer[min_len] = '\0'; // Null-terminate
return buffer;
}
// Determine if the number is negative
is_negative = nbr < 0;
nbr = ft_labs(nbr);
// Write the null terminator at the end
buffer[buff_len - 1] = '\0';
// Write the digits in reverse order
i = buff_len - 2;
do
{
buffer[i] = (nbr % 10) + '0';
nbr /= 10;
i--;
} while (nbr != 0);
// Add the negative sign if needed
if (is_negative)
{
buffer[0] = '-';
}
return buffer;
}

8
srcs/ft_labs.c Normal file
View File

@@ -0,0 +1,8 @@
#include "libft.h"
long int ft_labs(long int n)
{
if (n < 0)
n *= -1;
return (n);
}

18
srcs/ft_nbrlen.c Normal file
View File

@@ -0,0 +1,18 @@
#include "libft.h"
// return length of int
// eg. 123 -> 3, -123 -> 4
size_t ft_nbrlen(int nbr)
{
int len;
int is_negative;
is_negative = nbr < 0;
len = (nbr < 0) ? 2 : 1;
while (nbr /= 10)
len++;
return len + is_negative;
}

View File

@@ -15,12 +15,19 @@
size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n)
{
size_t total_len;
size_t i;
// Calculate total length needed
size_t total_len = 0;
for (size_t i = 0; i < n; i++)
total_len = 0;
i = 0;
while (i < n)
{
total_len += ft_strlen(srcs[i]);
ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
i++;
}
ft_printf("{dst_size:%li,srcs[0]:%s,srcs[1]:%s,total_len:%i}", dst_size, srcs[0], srcs[1], total_len); // debug
// Check if buffer is large enough (include space for '\0')
if (total_len + 1 > dst_size)

View File

@@ -11,12 +11,12 @@
/* ************************************************************************** */
/*
** return length of of string
** return length of of string, excluding the terminal '\0'
*/
#include "libft.h"
size_t ft_strlen(const char *str)
size_t ft_strlen(const char *str)
{
size_t i;