removing the libft of rparodi

This commit is contained in:
Raphael 2024-11-08 19:37:30 +01:00
parent 0391323626
commit be6038dcc8
523 changed files with 724 additions and 3336 deletions

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:19:44 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:57:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:13:03 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:57:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_isalpha(int c)
{
return (ft_islower(c) || ft_isupper(c));
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:23:34 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:57:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:27:37 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:56:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ishexdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:48:46 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:56:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_ishexdigit(int c)
{
return (ft_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_islower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:14:15 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:56:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_islower(int c)
{
return (c >= 'a' && c <= 'z');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isoctdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:52:06 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:56:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isoctdigit(int c)
{
return (c >= '0' && c <= '7');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:31:52 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:58:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
return (c >= ' ' && c <= '~');
}

View file

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 11:53:46 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:58:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isspace(int c)
{
return (c == ' ' || c == '\f' || c == '\n' \
|| c == '\r' || c == '\t' || c == '\v');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:42:20 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:34:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isupper(int c)
{
return (c >= 'A' && c <= 'Z');
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */
/* Updated: 2023/11/09 10:03:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
return ;
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:42:46 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:17:51 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_tolower(int c)
{
if (ft_isupper(c))
c += 32;
return (c);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:43:00 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:18:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_toupper(int c)
{
if (ft_islower(c))
c -= 32;
return (c);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_apply_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 09:47:17 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:08:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include <stddef.h>
void ft_apply_2d(void **array, t_data_apply f)
{
size_t i;
i = 0;
while (array[i])
f(array[i++]);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 17:10:05 by bgoulard #+# #+# */
/* Updated: 2024/06/25 17:40:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void *ft_arena_alloc(size_t size)
{
return (ft_narena_alloc(size, 0));
}
void *ft_arena_realloc(void *ptr, size_t size)
{
return (ft_narena_realloc(ptr, size, 0));
}
void *ft_arena_calloc(size_t count, size_t size)
{
return (ft_narena_calloc(count, size, 0));
}
void ft_arena_free(void *ptr)
{
ft_narena_free(ptr, 0);
}

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:05:47 by bgoulard #+# #+# */
/* Updated: 2024/06/26 19:48:45 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static void byte_bzero(char *s, size_t n)
{
while (n--)
{
*s = 0;
s++;
}
}
static void word_bzero(size_t *s, size_t *n)
{
size_t word_count;
size_t i;
word_count = *n / sizeof(size_t);
i = 0;
while (i < word_count)
{
s[i] = 0;
i++;
}
*n %= sizeof(size_t);
}
void ft_bzero(void *s, size_t n)
{
if (!n || !s)
return ;
if (n < sizeof(size_t))
return (byte_bzero(s, n));
byte_bzero(s, (size_t)s % sizeof(size_t));
n -= (size_t)s % sizeof(size_t);
s += (size_t)s % sizeof(size_t);
word_bzero(s, &n);
byte_bzero(s, n);
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:28:30 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:20:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdint.h>
#include <stdlib.h>
#include "ft_string.h"
void *ft_calloc(size_t nmemb, size_t weight)
{
void *ret;
size_t tot;
if (nmemb == 0 || weight == 0)
return (ft_malloc(0));
if (nmemb > SIZE_MAX / weight)
return (NULL);
tot = nmemb * weight;
ret = ft_malloc(tot);
if (!ret)
return (NULL);
return (ft_memset(ret, 0, tot));
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fd_to_buff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 19:52:02 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:46:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <unistd.h>
#include "ft_string.h"
#include "ft_string_types.h"
char *ft_fd_to_buff(int fd)
{
char buff[BUFFER_SIZE + 1];
char *file;
char *prev;
ssize_t ret;
if (fd == -1)
return (NULL);
file = NULL;
ret = read(fd, buff, BUFFER_SIZE);
if (ret == -1)
return (NULL);
buff[ret] = '\0';
file = ft_strdup(buff);
while (ret == BUFFER_SIZE)
{
ret = read(fd, buff, BUFFER_SIZE);
if (ret == -1)
return (ft_free((void **)&file), NULL);
buff[ret] = '\0';
prev = file;
file = ft_strjoin(file, buff);
ft_free((void **)&prev);
}
return (file);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 10:18:13 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:26:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
void ft_free(void **ptr)
{
if (!ptr || !*ptr)
return ;
free(*ptr);
*ptr = NULL;
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:32:28 by bgoulard #+# #+# */
/* Updated: 2024/05/28 23:47:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
void ft_free_2d(void **arr)
{
ft_apply_2d(arr, free);
free(arr);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_len_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:41:42 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:22:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_len_2d(const void *const *array)
{
size_t len;
len = 0;
while (array[len])
len++;
return (len);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_malloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/19 18:20:32 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:20:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_string.h"
void *ft_malloc(size_t size)
{
return (malloc(size));
}

View file

@ -0,0 +1,120 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:15:12 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:19:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static void *sword_memchr(const void *s, int c, size_t n)
{
char *casted;
size_t i;
const char target = (char)c;
casted = (char *)s;
i = 0;
while (n != i)
{
if (casted[i++] == target)
return (casted + i - 1);
}
return (NULL);
}
/*
** we check if the size of size_t is 8 bytes by checkning the platform
** we are on
** if yes we use the 1st function else we are on the 32 bit platform
** so use the second version
*/
#ifdef __x86_64__
static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight,
char c)
{
*target = (size_t)c;
*target |= *target << 8;
*target |= *target << 16;
*target |= *target << 32;
*rp_one = 0x0101010101010101;
*rp_eight = 0x8080808080808080;
}
#else
static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight,
char c)
{
*target = (size_t)c;
*target |= *target << 8;
*target |= *target << 16;
*rp_one = 0x0101010101010101;
*rp_eight = 0x8080808080808080;
}
#endif
/*
** *casted ^ target : XOR between the target and the casted value to leave a
** segment of bits where if the target is present, the bits will be 0.
** ((*casted ^ target) - 0x0101010101010101) : XOR result - 0x0101010101010101
** remove 1 to the said bits. if target is there the result will be FF.
** ~((casted ^ target) : XOR result inverted aka 0x00 -> 0xFF and 0xFF -> 0x00
** Remember if target is present on xor segment the res is 0 so ~ will be 0xFF
** we effectively have a mask where the target is present.
** ((*casted ^ target) - repeated_1) & ~((*casted ^ target) :
** XOR result - repeated_1 & ~XOR result
** xor result gets us to 0 where the target is
** -repeated_1 will get us to FF where the target is
** ~XOR result will get us to FF where the target is
** & between the two will get us to FF where the target is
** to filter the match we only keep the 8th bit of the result
** if the 8th bit is different from 0 we have a match in the section.
** when match send to sword_memchr to find the exact position of the target.
**
** For more infos check ft_strlen.c it is the same principle but simpler
** as we are looking directly for a 0x00.
*/
static void *bword_memchr(const void *s, int c, size_t n)
{
size_t *casted;
size_t target;
size_t rp_one;
size_t rp_eight;
casted = (size_t *)s;
constr_bword(&target, &rp_one, &rp_eight, c);
while (n > sizeof(size_t))
{
if ((((*casted ^ target) - rp_one) & ~((*casted ^ target) & rp_eight)))
return (sword_memchr(casted, c, sizeof(size_t)));
casted++;
n -= sizeof(size_t);
}
if (n)
return (sword_memchr(casted, c, n));
return (NULL);
}
void *ft_memchr(const void *s, int c, size_t n)
{
char *res;
if (n < sizeof(n))
return (sword_memchr(s, c, n));
res = sword_memchr(s, c, (size_t)s % sizeof(size_t));
if (res)
return (res);
res = (char *)s + ((size_t)s % sizeof(size_t));
n -= (size_t)s % sizeof(size_t);
return (bword_memchr(res, c, n));
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:11:32 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:59:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t index;
index = 0;
if (s1 == s2 || n == 0)
return (0);
while (index != n && ((const unsigned char *)s1)[index] == \
((const unsigned char *)s2)[index])
index++;
if (index == n)
index--;
return (((unsigned char *)s1)[index] - ((unsigned char *)s2)[index]);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 13:48:06 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:57:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
return (ft_memmove(dest, src, n));
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/27 18:14:50 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:15:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_string.h"
#include <stddef.h>
#include <stdlib.h>
void **ft_memmap(void **src, size_t nb_e, t_data_tr f)
{
void **dst;
size_t i;
if (!src || !f || !nb_e)
return (NULL);
dst = ft_malloc((nb_e + 1) * sizeof(void *));
if (!dst)
return (NULL);
dst[nb_e] = NULL;
i = 0;
while (i < nb_e)
{
dst[i] = f(src[i]);
i++;
}
return (dst);
}

View file

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:14:49 by bgoulard #+# #+# */
/* Updated: 2024/07/11 11:59:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdio.h>
static void *byte_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
if (dest > src)
while (n--)
((unsigned char *)dest)[n] = ((unsigned char *)src)[n];
else
{
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
}
return (dest);
}
static void word_memcpy(void *dest, const void *src, size_t *n)
{
size_t *casted_dest;
size_t *casted_src;
size_t word_count;
size_t i;
if (*n < sizeof(size_t))
return ;
casted_dest = (size_t *)dest;
casted_src = (size_t *)src;
word_count = *n / sizeof(size_t);
i = 0;
if (dest < src)
{
while (i++ < word_count)
casted_dest[i - 1] = casted_src[i - 1];
}
else
{
i = word_count;
while (i--)
casted_dest[i] = casted_src[i];
}
*n %= sizeof(size_t);
}
/*
* If src is not aligned then we copy byte by byte otherwise
* the compiler will need to align each time we pull from src
*/
static void *ft_memmove_forward(void *dest, const void *src, size_t n)
{
const void *ret = dest;
size_t align_offset;
size_t prev_n;
if ((size_t)src % sizeof(size_t))
return (byte_memcpy(dest, src, n));
align_offset = (size_t)dest % sizeof(size_t);
byte_memcpy(dest, src, align_offset);
n -= align_offset;
src += align_offset;
dest += align_offset;
prev_n = n;
word_memcpy(dest, src, &n);
dest += prev_n - n;
src += prev_n - n;
if (n)
byte_memcpy(dest, src, n);
return ((void *)ret);
}
static void *ft_memmove_backward(void *dest, const void *src, size_t n)
{
const void *ret = dest;
size_t align_offset;
if ((size_t)src % sizeof(size_t))
return (byte_memcpy(dest, src, n));
align_offset = (size_t)(dest + n) % sizeof(size_t);
n -= align_offset;
byte_memcpy(dest + n, src + n, align_offset);
word_memcpy(dest, src, &n);
if (n)
byte_memcpy(dest, src, n);
return ((void *)ret);
}
void *ft_memmove(void *dest, const void *src, size_t n)
{
const void *ret = dest;
if (dest == src || !n || !dest || !src)
return ((void *)ret);
if (n < sizeof(size_t))
return (byte_memcpy(dest, src, n));
if (dest < src)
return (ft_memmove_forward(dest, src, n));
return (ft_memmove_backward(dest, src, n));
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:07:20 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:48:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memset(void *s, int c, size_t n)
{
char *casted;
casted = (char *)s;
if (!n)
return (s);
while (n--)
{
*casted = (char)c;
casted++;
}
return (s);
}

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_narena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 17:41:16 by bgoulard #+# #+# */
/* Updated: 2024/06/30 18:06:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_list_types.h"
#include "ft_math.h"
#include <stddef.h>
#include <stdlib.h>
#define FT_NARENAS_MAX 16
#define FT_AREA_BLOCK_SIZE 4096
typedef struct s_arena
{
void *block_origin;
void *block_current;
void *block_end;
} t_arena;
t_list *ft_arena_get(int n)
{
static t_list arena_array[FT_NARENAS_MAX] = {0};
if (n < 0 || n >= FT_NARENAS_MAX)
return (NULL);
return (&arena_array[n]);
}
bool ft_arena_create_handler(t_list *ar_ptr, size_t size)
{
t_arena *arena;
size = ft_align_2(size + sizeof(t_arena), FT_AREA_BLOCK_SIZE);
arena = malloc(size);
if (!arena)
return (false);
arena->block_origin = arena + 1;
arena->block_current = arena->block_origin;
arena->block_end = (void *)arena + size;
ar_ptr->data = arena;
return (true);
}
void *ft_arena_alloc(size_t size, int ar_nb)
{
t_list *ar_list;
t_arena *arena;
void *ret;
size = ft_align_2(size, FT_AREA_BLOCK_SIZE);
ar_list = ft_arena_get(ar_nb);
if (!ar_list)
return (NULL);
arena = ft_ll_end(ar_list)->data;
if (!arena && ft_arena_create_handler(ar_list, size) == false)
return (NULL);
// create new block handler - if fail ret null
if (arena->block_origin == NULL || \
arena->block_current + size > arena->block_end)
{
// allocate a new block
;
}
ret = arena->block_current;
arena->block_current += size;
return (ret);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_qsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 11:31:15 by bgoulard #+# #+# */
/* Updated: 2023/12/30 12:37:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void ft_qsort(void *array, size_t nmb, size_t size, \
int (*cmp)(const void *, const void *))
{
size_t i;
size_t j;
size_t pivot;
if (nmb < 2)
return ;
i = 0;
j = nmb - 1;
pivot = nmb / 2;
while (i < j)
{
while (cmp(array + (i * size), array + (pivot * size)) < 0)
i++;
while (cmp(array + (j * size), array + (pivot * size)) > 0)
j--;
if (i < j)
{
ft_swap(array + (i * size), array + (j * size));
if (i == pivot || j == pivot)
pivot = (i == pivot) * i + (j == pivot) * j;
}
}
ft_qsort(array, pivot, size, cmp);
ft_qsort(array + ((pivot + 1) * size), nmb - pivot - 1, size, cmp);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:27:54 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
// direct call to ft_malloc as ft_bzero is not needed
// >> we are copying the content of ptr to new
void *ft_realloc(void *ptr, size_t sizeNew, size_t sizeOld)
{
void *new;
if (sizeNew == sizeOld)
return (ptr);
new = ft_malloc(sizeNew);
if (!new)
return (NULL);
if (sizeNew < sizeOld)
ft_memcpy(new, ptr, sizeNew);
else
ft_memcpy(new, ptr, sizeOld);
free(ptr);
return (new);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 11:34:29 by bgoulard #+# #+# */
/* Updated: 2023/12/12 11:39:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
void ft_swap(void **a, void **b)
{
void *tmp;
tmp = *a;
*a = *b;
*b = tmp;
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atof.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 11:09:02 by bgoulard #+# #+# */
/* Updated: 2024/05/26 16:37:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
static int loc_forward(const char *str, int *i)
{
bool neg;
neg = false;
*i = 0;
while (ft_isspace(str[*i]) || str[*i] == '-' || str[*i] == '+' || \
ft_isdigit(str[*i]))
{
if (str[*i] == '-')
neg = true;
(*i)++;
}
return (neg);
}
double ft_atof(const char *str)
{
int whole;
bool neg;
double decimal;
int i;
int j;
j = 1;
decimal = 0;
whole = ft_atoi(str);
neg = loc_forward(str, &i);
if (str[i] == '.')
{
i++;
while (ft_isdigit(str[i]))
{
decimal = decimal * 10 + str[i] - '0';
i++;
j *= 10;
}
}
if (neg == true)
decimal *= -1;
return ((double)whole + (double)decimal / j);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:05:24 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:36:45 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
int ft_atoi(const char *str)
{
int ret;
int sign;
size_t offset;
ret = 0;
offset = 0;
sign = 0;
while (ft_isspace(str[offset]))
offset++;
while (str[offset] == '+' || str[offset] == '-')
{
if (str[offset] == '-')
sign++;
offset++;
}
while (ft_isdigit(str[offset]))
ret = ret * 10 + str[offset++] - '0';
if (sign % 2 == 1)
return (-ret);
return (ret);
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:52:58 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:54:39 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
static int ft_is_valid(const char *base)
{
int i;
int j;
i = 0;
if (ft_strlen(base) < 2)
return (0);
while (base[i])
{
if (base[i] == '+' || base[i] == '-' || ft_isspace(base[i]))
return (0);
j = i + 1;
while (base[j])
if (base[i] == base[j++])
return (0);
i++;
}
return (1);
}
int ft_atoi_base(const char *str, const char *base)
{
const size_t base_len = ft_strlen(base);
int i;
int nb;
int sign;
i = 0;
nb = 0;
sign = 1;
if (!ft_is_valid(base))
return (0);
while (ft_isspace(str[i]))
i++;
while (str[i] == '+' || str[i] == '-')
if (str[i++] == '-')
sign *= -1;
while (str[i] && ft_strchr(base, str[i]))
nb = nb * base_len + ft_strchr(base, str[i++]) - base;
return (nb * sign);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:20:28 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:37:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_itoa(int nbr)
{
return (ft_itoa_base(nbr, "0123456789"));
}

View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 15:03:11 by bgoulard #+# #+# */
/* Updated: 2024/06/13 17:53:23 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_math.h"
#include <stdlib.h>
#include "ft_char.h"
static int ft_is_valid(const char *base)
{
int i;
int j;
i = 0;
if (ft_strlen(base) < 2)
return (0);
while (base[i])
{
if (base[i] == '+' || base[i] == '-' || ft_isspace(base[i]))
return (0);
j = i + 1;
while (base[j])
if (base[i] == base[j++])
return (0);
i++;
}
return (1);
}
static int loc_getlen(long long nbr, size_t blen)
{
size_t nbr_len;
nbr_len = 0;
if (nbr == 0)
return (1);
if (nbr < 0)
{
nbr *= -1;
nbr_len++;
}
nbr_len += ft_llogof(nbr, blen) + 1;
return (nbr_len);
}
char *ft_itoa_base(int nbr, const char *base)
{
char *ret;
size_t off;
long long srcnbr;
const size_t blen = ft_strlen(base);
const int nbrlen = loc_getlen((long long)nbr, blen);
srcnbr = (long long)nbr;
if (!ft_is_valid(base))
return (NULL);
ret = ft_calloc(sizeof(char), (nbrlen + 1));
if (!ret)
return (NULL);
if (srcnbr <= 0)
{
ret[0] = (srcnbr == 0) * '0' + (srcnbr < 0) * '-';
srcnbr *= -1;
}
off = 0;
while (srcnbr)
{
ret[nbrlen - ++off] = base[srcnbr % blen];
srcnbr /= blen;
}
return (ret);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_perror.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 14:09:15 by bgoulard #+# #+# */
/* Updated: 2024/07/08 14:36:23 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void ft_perror(const char *s)
{
if (s)
{
ft_putstr_fd(s, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
}
ft_putstr_fd(ft_strerror(errno), STDERR_FILENO);
ft_putstr_fd("\n", STDERR_FILENO);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:48:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <unistd.h>
int ft_putendl_fd(const char *s, int fd)
{
int ret;
if (!s)
return (-1);
ret = ft_putstr_fd(s, fd);
if (ret < 0)
return (ret);
if (write(fd, "\n", 1) < 0)
return (-1);
return (ret + 1);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:08:01 by bgoulard #+# #+# */
/* Updated: 2024/07/19 21:28:41 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include <unistd.h>
#include "ft_string.h"
int ft_putnbr_fd(int nb, int fd)
{
char result[13] = {0};
int it;
int neg;
neg = 0;
it = 12;
if (nb == INT_MIN)
return (ft_putstr_fd("-2147483648", fd));
if (nb < 0)
{
neg = 1;
nb = -nb;
}
while (nb >= 10)
{
result[--it] = nb % 10 + '0';
nb /= 10;
}
result[--it] = nb + '0';
if (neg)
result[--it] = '-';
return (ft_putstr_fd(result + it, fd));
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:57:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <unistd.h>
int ft_putstr_fd(const char *s, int fd)
{
if (!s)
return (-1);
return (write(fd, s, ft_strlen(s)));
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_shift_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/14 17:10:50 by bgoulard #+# #+# */
/* Updated: 2024/01/01 17:14:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
const char *ft_shift_args(const char **args[], int *index)
{
const char *arg;
if (*index <= 0 || !*args || !**args)
return (NULL);
(*index)--;
arg = **args;
(*args)++;
return (arg);
}

View file

@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 21:48:50 by bgoulard #+# #+# */
/* Updated: 2024/07/06 15:48:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static size_t loc_get_nbwords(const char *str, char delim)
{
size_t ret;
size_t i;
size_t inword;
ret = 0;
i = 0;
inword = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i++] == delim)
{
if (inword == 1)
ret++;
inword = 0;
}
else
inword = 1;
}
if (inword == 1)
ret++;
return (ret);
}
static char *loc_cpy_til(const char *str, char delim, size_t *offset_str)
{
size_t len;
char *ret;
len = 0;
while (str[*offset_str + len] != delim && str[*offset_str + len])
len++;
if (len == 0)
return (NULL);
ret = ft_calloc(sizeof(char), (len + 1));
if (!ret)
return (NULL);
len = 0;
while (str[*offset_str + len] != delim && str[*offset_str + len])
{
ret[len] = str[*offset_str + len];
len++;
}
*offset_str += len;
return (ret);
}
char **ft_split(const char *str, char delim)
{
size_t offset_str;
size_t offset_words;
char **words;
words = ft_calloc(sizeof(char *), (loc_get_nbwords(str, delim) + 1));
if (!words)
return (words);
offset_words = 0;
offset_str = 0;
while (str && str[offset_str] == delim && delim != 0)
offset_str++;
while (str && str[offset_str])
{
words[offset_words++] = loc_cpy_til(str, delim, &offset_str);
if (!words[offset_words - 1])
return (ft_free_2d((void **)words), NULL);
while (str[offset_str] == delim && str[offset_str])
offset_str++;
}
return (words);
}

View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_splits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 21:48:50 by bgoulard #+# #+# */
/* Updated: 2024/07/06 15:51:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static size_t loc_get_nbwords(const char *str, const char *delim)
{
size_t ret;
size_t i;
size_t inword;
ret = 0;
i = 0;
inword = 0;
if (!str)
return (0);
while (str[i])
{
if (ft_strchr(delim, str[i++]))
{
if (inword == 1)
ret++;
inword = 0;
}
else
inword = 1;
}
if (inword == 1)
ret++;
return (ret);
}
char **ft_splits(const char *str, const char *delim)
{
char *str_cpy;
char *s;
size_t offset_words;
char **words;
words = ft_calloc(sizeof(char *), (loc_get_nbwords(str, delim) + 1));
if (!words)
return (NULL);
offset_words = 0;
str_cpy = ft_strdup(str);
s = ft_strtok(str_cpy, delim);
while (s)
{
words[offset_words++] = ft_strdup(s);
if (!words[offset_words - 1])
return (ft_free_2d((void **)words), NULL);
s = ft_strtok(NULL, delim);
}
free(str_cpy);
return (words);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:52:54 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:42:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
bool ft_str_isalnum(const char *str)
{
if (!str)
return (false);
return (ft_str_isvalid(str, ft_isalnum));
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:55:40 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:42:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
bool ft_str_isalpha(const char *str)
{
int i;
if (!str)
return (false);
i = 0;
while (str[i])
{
if (!ft_isalpha(str[i]))
return (false);
i++;
}
if (i == 0)
return (false);
return (true);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isbool.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:37:46 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdbool.h>
bool ft_str_isbool(const char *str)
{
if (!str)
return (false);
if (ft_strcmp(str, "true") == 0 || ft_strcmp(str, "false") == 0 || \
ft_strcmp(str, "0") == 0 || ft_strcmp(str, "1") == 0)
return (true);
return (false);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 13:39:43 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:40:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
#include <stdbool.h>
bool ft_str_isdigit(const char *str)
{
if (!str)
return (false);
return (ft_str_isvalid(str, ft_isdigit));
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isdouble.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:42:00 by bgoulard #+# #+# */
/* Updated: 2024/05/31 18:29:01 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
bool ft_str_isdouble(const char *str)
{
return (ft_str_isfloat(str));
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:45:19 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:40:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdbool.h>
#include "ft_char.h"
// no value check like isint due to the fact that it's a floating point
bool ft_str_isfloat(const char *str)
{
size_t i;
bool dot;
if (!str)
return (false);
i = 0;
dot = false;
if (str[i] == '-')
i++;
while (str[i])
{
if (str[i] == '.' && !dot)
dot = true;
else if (!ft_isdigit(str[i]))
return (false);
i++;
}
if (i == 0 || (i == 1 && (str[0] == '.' || str[0] == '-')) || \
(i == 2 && str[0] == '-' && str[1] == '.') || \
i > 20)
return (false);
return (true);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_ishex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:46:22 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:40:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
bool ft_str_ishex(const char *str)
{
if (!str)
return (false);
if (ft_strstart_with(str, "0X") || ft_strstart_with(str, "0x"))
str += 2;
return (ft_str_isvalid(str, ft_ishexdigit));
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:47:57 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
#include <stdbool.h>
// 0 success, !0 failure
bool ft_str_isint(const char *str)
{
size_t i;
if (!str)
return (false);
i = 0;
if (str[i] == '-')
i++;
while (str[i])
{
if (!ft_isdigit(str[i]))
return (false);
i++;
}
if ((i == 10 && ft_strcmp(str, "2147483647") > 0) || i > 11 || \
(i == 11 && ft_strcmp(str, "-2147483648") > 0))
return (false);
if (i == 0 || (i == 1 && (str[0] == '-')))
return (false);
return (true);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_islong.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:50:51 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdbool.h>
#include <stddef.h>
#include "ft_string.h"
#include "ft_char.h"
bool ft_str_islong(const char *str)
{
size_t i;
i = 0;
if (!str)
return (false);
if (str[i] == '-')
i++;
while (str[i])
{
if (!ft_isdigit(str[i]))
return (false);
i++;
}
if ((i == 19 && ft_strcmp(str, "9223372036854775807") > 0) || (i == 20
&& ft_strcmp(str, "-9223372036854775808") > 0) || i > 20)
return (false);
if (i == 0 || (i == 1 && (str[0] == '-')))
return (false);
return (true);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:56:04 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
#include <stdbool.h>
bool ft_str_isnum(const char *str)
{
int i;
if (!str)
return (false);
i = 0;
if (str[i] == '-' || str[i] == '+')
i++;
return (ft_str_isvalid(str + i, ft_isdigit));
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isoct.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:49:53 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:40 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
#include "ft_string.h"
bool ft_str_isoct(const char *str)
{
if (!str)
return (false);
if (!ft_strstart_with(str, "0o") && !ft_strstart_with(str, "0O"))
return (false);
return (ft_str_isvalid(str + 2, ft_isoctdigit));
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_isvalid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:53:23 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:41:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdbool.h>
#include <stddef.h>
bool ft_str_isvalid(const char *str, int (*is_type)(int))
{
size_t i;
if (!str)
return (false);
i = 0;
while (str[i])
{
if (!is_type(str[i]))
return (false);
i++;
}
if (i == 0)
return (false);
return (true);
}

View file

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 18:35:51 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
static bool setup(char **res, const char *targ, size_t targ_len, size_t sub_len)
{
char *occurence;
size_t nb_occurence;
occurence = ft_strnstr(*res, targ, ft_strlen(*res));
nb_occurence = 0;
while (occurence)
{
occurence = ft_strnstr(occurence + 1, targ, ft_strlen(occurence + 1));
nb_occurence++;
}
*res = ft_malloc(ft_strlen(*res) + (sub_len - targ_len) * nb_occurence + 1);
if (!*res)
return (false);
return (true);
}
char *ft_str_replace(const char *str, const char *target, \
const char *substitute)
{
char *res;
char *ret;
char *occurence;
const size_t target_len = ft_strlen(target);
const size_t substitute_len = ft_strlen(substitute);
occurence = ft_strnstr(str, target, ft_strlen(str));
if (!occurence)
return (ft_strdup(str));
res = (char *)str;
if (!setup(&res, target, target_len, substitute_len))
return (NULL);
ret = res;
while (occurence)
{
ft_memcpy(res, str, occurence - str);
res += occurence - str;
ft_memcpy(res, substitute, substitute_len);
res += substitute_len;
str = occurence + target_len;
occurence = ft_strnstr(str, target, ft_strlen(str));
}
return (ft_strlcpy(res, str, ft_strlen(str) + 1), ret);
}
char *ft_str_replace_chr(char *str, char to_replace, char replace_by)
{
char *ptr;
ptr = str;
str = ft_strchr(str, to_replace);
while (str && *str)
{
*str = replace_by;
str++;
str = ft_strchr(str, to_replace);
}
return (ptr);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strappend_c.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/10 12:02:19 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:59:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_strappend_c(char **str, char c)
{
size_t len;
char *prev;
if (!str)
return (NULL);
len = 0;
if (*str)
len = ft_strlen(*str);
prev = *str;
*str = ft_realloc(*str, len + 2, len);
if (!*str)
return (*str = prev, NULL);
(*str)[len] = c;
(*str)[len + 1] = '\0';
return (*str);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:46:36 by bgoulard #+# #+# */
/* Updated: 2024/06/13 17:54:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strchr(const char *str, int c)
{
char *ret;
int i;
const char target = (char)c;
ret = NULL;
i = 0;
while (str[i] && !ret)
{
if (str[i] == target)
ret = (char *)str + i;
i++;
}
if (!str[i] && target == 0)
return ((char *)str + i);
return ((char *)ret);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strclen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 11:00:55 by bgoulard #+# #+# */
/* Updated: 2024/05/18 20:03:48 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_strclen(char *str, char c)
{
size_t i;
i = 0;
while (str[i] && str[i] != c)
i++;
return (i);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 11:05:16 by bgoulard #+# #+# */
/* Updated: 2024/05/18 19:45:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_math.h"
int ft_strcmp(const char *s1, const char *s2)
{
return (ft_strncmp(s1, s2, ft_max(ft_strlen(s1), ft_strlen(s2))));
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcnb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 10:58:46 by bgoulard #+# #+# */
/* Updated: 2024/05/18 20:04:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_strcnb(char *str, char c)
{
size_t i;
size_t nb;
i = 0;
nb = 0;
if (!str)
return (0);
while (str[i])
if (str[i++] == c)
nb++;
if (c == '\0')
nb++;
return (nb);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcspn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 23:02:45 by bgoulard #+# #+# */
/* Updated: 2024/05/10 09:11:18 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
size_t ft_strcspn(const char *str, const char *charset)
{
size_t i;
i = 0;
while (str[i])
{
if (ft_strchr(charset, str[i]))
return (i);
i++;
}
return (i);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:33:24 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:52:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_strdup(const char *strsrc)
{
return (ft_strndup(strsrc, ft_strlen(strsrc)));
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strend_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 14:05:34 by bgoulard #+# #+# */
/* Updated: 2024/05/18 20:05:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_strend_with(const char *str, const char *end)
{
int i;
int j;
i = ft_strlen(str) - 1;
j = ft_strlen(end) - 1;
if (j > i)
return (0);
while (j >= 0)
{
if (str[i] != end[j])
return (0);
i--;
j--;
}
return (1);
}

View file

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strerror.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 11:43:23 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:19:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static const char *block_1(int errnum)
{
static const char *str[] = {\
"Success", "Operation not permitted", \
"No such file or directory", "No such process", "Interrupted system call", \
"Input/output error", "No such device or address", \
"Argument list too long", "Exec format error", "Bad file descriptor", \
"No child processes", "Resource temporarily unavailable", \
"Cannot allocate memory", "Permission denied", "Bad address", \
"Block device required", "Device or resource busy", "File exists", \
"Invalid cross-device link", "No such device", "Not a directory", \
"Is a directory", "Invalid argument", "Too many open files in system", \
"Too many open files", "Inappropriate ioctl for device", "Text file busy", \
"File too large", "No space left on device", "Illegal seek", \
"Read-only file system", "Too many links", "Broken pipe", \
"Numerical argument out of domain", "Numerical result out of range", \
"Resource deadlock avoided", "File name too long", "No locks available", \
"Function not implemented", "Directory not empty", \
"Too many levels of symbolic links", "Unknown error 41", \
"No message of desired type", "Identifier removed", \
"Channel number out of range", "Level 2 not synchronized", \
"Level 3 halted", "Level 3 reset", "Link number out of range", \
"Protocol driver not attached", "No CSI structure available"};
return (str[errnum]);
}
static const char *block_2(int errnum)
{
static const char *str[] = {\
"Level 2 halted", "Invalid exchange", "Invalid request descriptor", \
"Exchange full", "No anode", "Invalid request code", \
"Invalid slot", "Unknown error 58", \
"Bad font file format", "Device not a stream", "No data available", \
"Timer expired", "Out of streams resources", \
"Machine is not on the network", "Package not installed", \
"Object is remote", "Link has been severed", "Advertise error", \
"Srmount error", "Communication error on send", "Protocol error", \
"Multihop attempted", "RFS specific error", "Bad message", \
"Value too large for defined data type", "Name not unique on network", \
"File descriptor in bad state", "Remote address changed", \
"Can not access a needed shared library", \
"Accessing a corrupted shared library", ".lib section in a.out corrupted", \
"Attempting to link in too many shared libraries", \
"Cannot exec a shared library directly", \
"Invalid or incomplete multibyte or wide character", \
"Interrupted system call should be restarted", "Streams pipe error", \
"Too many users", "Socket operation on non-socket", \
"Destination address required", "Message too long", \
"Protocol wrong type for socket", "Protocol not available"};
return (str[errnum - 51]);
}
static const char *block_3(int errnum)
{
static const char *str[] = {\
"Protocol not supported", "Socket type not supported", \
"Operation not supported", "Protocol family not supported", \
"Address family not supported by protocol", "Address already in use", \
"Cannot assign requested address", "Network is down", \
"Network is unreachable", "Network dropped connection on reset", \
"Software caused connection abort", "Connection reset by peer", \
"No buffer space available", "Transport endpoint is already connected", \
"Transport endpoint is not connected", \
"Cannot send after transport endpoint shutdown", \
"Too many references: cannot splice", "Connection timed out", \
"Connection refused", "Host is down", "No route to host", \
"Operation already in progress", "Operation now in progress", \
"Stale file handle", "Structure needs cleaning", \
"Not a XENIX named type file", "No XENIX semaphores available", \
"Is a named type file", "Remote I/O error", "Disk quota exceeded", \
"No medium found", "Wrong medium type", "Operation canceled", \
"Required key not available", "Key has expired", "Key has been revoked", \
"Key was rejected by service", "Owner died", "State not recoverable", \
"Operation not possible due to RF-kill", "Memory page has hardware error"};
return (str[errnum - 93]);
}
const char *ft_strerror(int errnum)
{
if (errnum > 133)
return (NULL);
if (errnum < 51)
return (block_1(errnum));
if (errnum < 93)
return (block_2(errnum));
return (block_3(errnum));
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:36:23 by bgoulard #+# #+# */
/* Updated: 2023/11/12 09:59:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
unsigned int index;
if (!s || !f)
return ;
index = 0;
while (s[index])
{
f(index, (char *)s + index);
index++;
}
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:55:53 by bgoulard #+# #+# */
/* Updated: 2023/12/05 10:27:52 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
char *ft_strjoin(char const *s1, char const *s2)
{
size_t tot;
size_t off;
char *ret;
tot = 0;
if (s1)
tot += ft_strlen(s1);
if (s2)
tot += ft_strlen(s2);
ret = ft_calloc(sizeof(char), (tot + 1));
if (!ret)
return (NULL);
off = 0;
tot = 0;
while (s1 && s1[off])
{
ret[off] = s1[off];
off++;
}
while (s2 && s2[tot])
ret[off++] = s2[tot++];
return (ret);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:11:59 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:15:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len;
size_t src_len;
size_t i;
if (!size || !dst)
return (0);
if (!src)
return (ft_strlen(dst));
dst_len = ft_strlen(dst);
src_len = ft_strlen(src);
if (dst_len >= size)
return (size + src_len);
i = 0;
while (src[i] && dst_len + i < size - 1)
{
dst[dst_len + i] = src[i];
i++;
}
dst[dst_len + i] = 0;
return (dst_len + src_len);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:21:14 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:39:45 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t src_len;
size_t dst_len;
if (!dst)
return (size);
if (!src)
return (ft_strlen(dst));
if (!size)
return (ft_strlen(src));
src_len = ft_strlen(src);
dst_len = 0;
while (dst_len < size - 1 && src[dst_len])
{
dst[dst_len] = src[dst_len];
dst_len++;
}
dst[dst_len] = 0;
return (src_len);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:46:04 by bgoulard #+# #+# */
/* Updated: 2023/11/10 12:51:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
size_t ft_strlen(const char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:39:40 by bgoulard #+# #+# */
/* Updated: 2023/12/04 15:33:02 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_string.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *ret;
unsigned int index;
index = 0;
if (!f || !s)
return (NULL);
ret = ft_calloc(sizeof(char), (ft_strlen(s) + 1));
if (!ret)
return (ret);
while (s[index++])
ret[index - 1] = f(index - 1, s[index - 1]);
return (ret);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:56:17 by bgoulard #+# #+# */
/* Updated: 2023/12/13 08:25:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stddef.h>
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
const unsigned char *str1 = (const unsigned char *)s1;
const unsigned char *str2 = (const unsigned char *)s2;
size_t index;
index = 0;
if (n == 0)
return (0);
while (str1[index] == str2[index] && str1[index] && index < n - 1)
index++;
return (str1[index] - str2[index]);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 18:16:47 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:53:50 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_strndup(const char *str, size_t n)
{
char *ret;
size_t index;
ret = ft_calloc(sizeof(char), (n + 1));
if (!ret)
return (ret);
index = 0;
while (index < n && str[index])
{
ret[index] = str[index];
index++;
}
return (ret);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:55:09 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:09:52 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_strnstr(const char *big, const char *small, size_t n)
{
size_t len_small;
const size_t len_big = n;
if (!big || !small || n == 0)
return (NULL);
len_small = ft_strlen(small);
while (n-- > len_small && *big)
if (ft_strncmp(big++, small, len_small) == 0)
return ((char *)big - 1);
if (len_big >= len_small && ft_strncmp(big, small, len_small) == 0)
return ((char *)big);
return (NULL);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:46:36 by bgoulard #+# #+# */
/* Updated: 2024/06/13 17:54:51 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strrchr(const char *str, int c)
{
char *ret;
int i;
const char target = (char)c;
ret = NULL;
i = 0;
while (str[i])
{
if (str[i] == target)
ret = (char *)str + i;
i++;
}
if (str[i] == target)
return ((char *)str + i);
return (ret);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strspn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 23:02:45 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:02:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
size_t ft_strspn(const char *str, const char *charset)
{
size_t i;
i = 0;
while (str[i])
{
if (!ft_strchr(charset, str[i]))
return (i);
i++;
}
return (i);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstart_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 14:07:26 by bgoulard #+# #+# */
/* Updated: 2024/05/18 20:05:26 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_strstart_with(const char *str, const char *start)
{
if (ft_strlen(start) > ft_strlen(str))
return (0);
return (ft_strncmp(str, start, ft_strlen(start)) == 0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtok.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 13:19:26 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:52:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_strtok(char *str, const char *token)
{
static char *ptr;
char *ret;
if (str)
ptr = str;
while (ptr && ft_strchr(token, *ptr) && *ptr)
ptr++;
ret = ptr;
while (ptr && !ft_strchr(token, *ptr))
ptr++;
if (ptr && *ptr)
{
*ptr = 0;
ptr++;
}
else
ptr = 0;
return (ret);
}

View file

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 21:03:17 by bgoulard #+# #+# */
/* Updated: 2023/12/05 10:28:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static int loc_strchr(const char *str, int c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (-1);
}
static size_t loc_strtrim_size(size_t *offset, char const *s1,
char const *set)
{
size_t ret_size;
ret_size = ft_strlen(s1);
*offset = 0;
while (ret_size > 0 && loc_strchr(set, s1[ret_size - 1]) != -1)
ret_size--;
while (ret_size && s1[*offset] && loc_strchr(set, s1[*offset]) != -1)
{
(*offset)++;
ret_size--;
}
return (ret_size);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *ret;
size_t ret_size;
size_t offset;
size_t offset_ret;
if (!s1)
return (NULL);
if (!set)
return (ft_strdup(s1));
offset_ret = 0;
ret_size = loc_strtrim_size(&offset, s1, set);
if (ret_size == 0)
return (ft_strdup(""));
ret = ft_calloc(sizeof(char), (ret_size + 1));
if (!ret)
return (ret);
while (s1[offset] && ret_size--)
ret[offset_ret++] = s1[offset++];
offset--;
while (loc_strchr(set, s1[offset--]) != -1)
ret[offset_ret--] = 0;
return (ret);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:46:31 by bgoulard #+# #+# */
/* Updated: 2023/12/05 10:28:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *res;
size_t i;
if (!s)
return (NULL);
if (start >= ft_strlen(s))
return (ft_strdup(""));
i = ft_strlen(s + start);
if (len < i)
i = len;
res = ft_calloc(sizeof(char), (i + 1));
if (!res)
return (res);
i = 0;
while (s[start + i] && i < len)
{
res[i] = s[start + i];
i++;
}
return (res);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 14:54:49 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:04:37 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static int loc_getlen(unsigned long long nbr)
{
size_t nbr_len;
nbr_len = 0;
if (nbr == 0)
return (1);
while (nbr)
{
nbr_len++;
nbr /= 10;
}
return (nbr_len);
}
char *ft_utoa(unsigned int nbr)
{
char *ret;
size_t off;
unsigned long long srcnbr;
const int nbrlen = loc_getlen((unsigned long long)nbr);
srcnbr = (unsigned long long)nbr;
ret = ft_calloc(sizeof(char), (nbrlen + 1));
if (!ret)
return (NULL);
if (srcnbr == 0)
ret[0] = '0';
off = 0;
while (srcnbr)
{
ret[nbrlen - ++off] = "0123456789"[srcnbr % 10];
srcnbr /= 10;
}
return (ret);
}

View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 11:02:12 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string_types.h"
#include "ft_string.h"
static int ft_strchr_index(const char *from, char c)
{
int i;
i = 0;
if (!from)
return (-1);
while (from[i] && from[i] != c)
i++;
if (from[i] == c)
return (i);
return (-1);
}
static int mvstr(char *str, int nb, char delim)
{
size_t off;
if (ft_strchr_index(str, delim) != -1)
nb++;
off = 0;
while (off != BUFFER_SIZE)
{
if (off + nb < BUFFER_SIZE && str[off + nb])
str[off] = str[off + nb];
else
str[off] = 0;
off++;
}
return (0);
}
static int split_from(char **dst, char *from, int pos, char delim)
{
char *ret;
size_t or_len;
size_t len;
size_t i;
or_len = 0;
if (*dst)
or_len += ft_strlen(*dst);
len = or_len + pos + 1;
ret = ft_malloc(sizeof(char) * (len + 1));
if (!ret)
return (-1);
ret[len] = 0;
i = 0;
while (i != len)
{
if (i < or_len)
ret[i] = (*dst)[i];
else
ret[i] = from[i - or_len];
i++;
}
if (*dst)
free(*dst);
return (*dst = ret, (mvstr(from, pos, delim) || 0));
}
char *get_next_line(int fd)
{
static char loc_buff[1024][BUFFER_SIZE + 1] = {0};
char *ret;
int rep;
ret = NULL;
while (ft_strchr_index(loc_buff[fd], '\n') == -1)
{
if (loc_buff[fd][0] && \
split_from(&ret, loc_buff[fd], ft_strlen(loc_buff[fd]), '\n'))
return (NULL);
rep = read(fd, loc_buff[fd], BUFFER_SIZE);
if (rep <= 0)
return (ret);
loc_buff[fd][rep] = 0;
if (rep != BUFFER_SIZE)
break ;
}
rep = ft_strchr_index(loc_buff[fd], '\n');
if (rep == -1)
rep = ft_strlen(loc_buff[fd]);
if (split_from(&ret, loc_buff[fd], rep, '\n'))
return (NULL);
return (ret);
}

View file

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_append.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 22:24:43 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:45:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_append(t_string *str, const char *append)
{
size_t len;
len = ft_strlen(append) + 1;
if (str->length + len >= str->capacity)
{
if (ft_string_resize(str, str->capacity + len) == -1)
return (-1);
}
ft_memcpy(str->str + str->length, append, len);
str->length += len - 1;
return (0);
}
int ft_string_append_n(t_string *str, const char *append, size_t n)
{
if (str->length + n + 1 >= str->capacity)
{
if (ft_string_resize(str, str->capacity + n + 1) == -1)
return (-1);
}
ft_memcpy(str->str + str->length, append, n);
str->length += n;
str->str[str->length] = '\0';
return (0);
}
int ft_string_append_c(t_string *str, char c)
{
if (str->length + 2 >= str->capacity)
{
if (ft_string_resize(str, str->capacity + 2) == -1)
return (-1);
}
str->str[str->length] = c;
str->str[str->length + 1] = '\0';
str->length += 1;
return (0);
}
int ft_string_append_s(t_string *str, t_string *append)
{
if (str->length + append->length + 1 >= str->capacity)
{
if (ft_string_resize(str, str->capacity + append->length + 1) == -1)
return (-1);
}
ft_memcpy(str->str + str->length, append->str, append->length + 1);
str->length += append->length;
str->str[str->length] = '\0';
return (0);
}
int ft_string_append_s_n(t_string *str, t_string *append, size_t n)
{
if (str->length + n + 1 >= str->capacity)
{
if (ft_string_resize(str, str->capacity + n + 1) == -1)
return (-1);
}
ft_memcpy(str->str + str->length, append->str, n);
str->length += n;
str->str[str->length] = '\0';
return (0);
}

View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_chr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:51:16 by bgoulard #+# #+# */
/* Updated: 2024/06/02 10:38:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_string_chr(const t_string *str, char c)
{
size_t i;
i = 0;
while (i < str->length)
{
if (str->str[i] == c)
return (str->str + i);
i++;
}
if (c == '\0')
return (str->str + i);
return (NULL);
}
char *ft_string_rchr(const t_string *str, char c)
{
size_t i;
i = str->length;
while (i > 0)
{
if (str->str[i] == c)
return (str->str + i);
i--;
}
if (str->str[i] == c)
return (str->str + i);
return (NULL);
}
ssize_t ft_string_offset(const t_string *str, char c)
{
char *ptr;
ptr = ft_string_chr(str, c);
if (ptr)
return (ptr - str->str);
return (-1);
}
ssize_t ft_string_roffset(const t_string *str, char c)
{
char *ptr;
ptr = ft_string_rchr(str, c);
if (ptr)
return (ptr - str->str);
return (-1);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:20:00 by bgoulard #+# #+# */
/* Updated: 2023/12/09 17:20:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void ft_string_clear(t_string *str)
{
ft_bzero(str->str, str->length);
str->length = 0;
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_cmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:50:20 by bgoulard #+# #+# */
/* Updated: 2024/06/02 19:27:34 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_cmp(const t_string *str, const char *cmp)
{
return (ft_strcmp(str->str, cmp));
}
int ft_string_ncmp(const t_string *str, const char *cmp, size_t n)
{
return (ft_strncmp(str->str, cmp, n));
}
int ft_string_cmpstr(const t_string *str, const t_string *cmp)
{
if (str->length != cmp->length)
return (str->length - cmp->length);
return (ft_strcmp(str->str, cmp->str));
}
int ft_string_ncmpstr(const t_string *str, const t_string *cmp, size_t n)
{
if (n < str->length && n < cmp->length)
return (ft_strncmp(str->str, cmp->str, n));
if (str->length != cmp->length)
return (str->length - cmp->length);
return (ft_strcmp(str->str, cmp->str));
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:20:41 by bgoulard #+# #+# */
/* Updated: 2024/06/23 17:45:07 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void ft_string_destroy(t_string **str)
{
if (!str || !*str)
return ;
free((*str)->str);
free(*str);
*str = NULL;
}

View file

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_from.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:14:14 by bgoulard #+# #+# */
/* Updated: 2024/06/02 19:40:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_math.h"
// +1 for the null terminator
t_string *ft_string_from(const char *str)
{
t_string *new;
size_t len;
if (!str)
return (ft_string_new(0));
len = ft_strlen(str);
new = ft_string_new(len + 1);
if (!new)
return (NULL);
ft_memcpy(new->str, str, len + 1);
new->length = len;
return (new);
}
t_string *ft_string_from_n(const char *str, size_t n)
{
t_string *new;
new = ft_string_new(n + 1);
if (!new)
return (NULL);
if (n == 0 || !str)
return (new);
ft_memcpy(new->str, str, n);
new->str[n] = '\0';
new->length = n;
return (new);
}
t_string *ft_string_from_c(char c)
{
t_string *new;
new = ft_string_new(2);
if (!new)
return (NULL);
new->str[0] = c;
new->str[1] = '\0';
new->length = 1;
return (new);
}
t_string *ft_string_from_s(const t_string *str)
{
t_string *new;
if (!str)
return (ft_string_new(0));
new = ft_string_new(str->length + 1);
if (!new)
return (NULL);
ft_memcpy(new->str, str->str, str->length + 1);
new->length = str->length;
return (new);
}
t_string *ft_string_from_s_n(const t_string *str, size_t n)
{
t_string *new;
size_t len;
if (!str || n == 0)
return (ft_string_new(0));
len = ft_min(n, str->length);
new = ft_string_new(len + 1);
if (!new)
return (NULL);
ft_memcpy(new->str, str->str, len);
new->str[len] = '\0';
new->length = len;
return (new);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:50:23 by bgoulard #+# #+# */
/* Updated: 2024/05/31 10:33:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
const char *ft_string_get(const t_string *str)
{
return (str->str);
}
size_t ft_string_len(const t_string *str)
{
return (str->length);
}
size_t ft_string_cap(const t_string *str)
{
return (str->capacity);
}

View file

@ -0,0 +1,133 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:22:33 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:46:39 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_insert(t_string *str, const char *insert, size_t index)
{
size_t insert_len;
size_t new_len;
char *new_str;
insert_len = ft_strlen(insert);
if (index > str->length)
index = str->length;
new_len = str->length + insert_len;
new_str = ft_calloc(new_len + 1, sizeof(char));
if (!new_str)
return (0);
ft_memcpy(new_str, str->str, index);
ft_memcpy(new_str + index, insert, insert_len);
ft_memcpy(new_str + index + insert_len, str->str + index, str->length
- index);
ft_free((void **)&str->str);
str->str = new_str;
if (index == str->length)
str->str[new_len] = '\0';
str->length = new_len;
str->capacity += insert_len + 1;
return (1);
}
int ft_string_insert_n(t_string *str, const char *insert, size_t index, size_t n)
{
size_t new_len;
char *new_str;
if (index > str->length)
index = str->length;
new_len = str->length + n;
new_str = ft_calloc(new_len + 1, sizeof(char));
if (!new_str)
return (0);
ft_memcpy(new_str, str->str, index);
ft_memcpy(new_str + index, insert, n);
ft_memcpy(new_str + index + n, str->str + index, str->length - index);
ft_free((void **)&str->str);
str->str = new_str;
if (index == str->length)
str->str[new_len] = '\0';
str->length = new_len;
str->capacity = new_len + 1;
return (1);
}
int ft_string_insert_c(t_string *str, char insert, size_t index)
{
size_t new_len;
char *new_str;
if (index > str->length)
index = str->length;
new_len = str->length + 1;
new_str = ft_calloc(new_len + 1, sizeof(char));
if (!new_str)
return (0);
ft_memcpy(new_str, str->str, index);
new_str[index] = insert;
ft_memcpy(new_str + index + 1, str->str + index, str->length - index);
ft_free((void **)&str->str);
str->str = new_str;
if (index == str->length)
str->str[new_len] = '\0';
str->length = new_len;
str->capacity = new_len + 1;
return (1);
}
int ft_string_insert_s(t_string *str, t_string *insert, size_t index)
{
size_t new_len;
char *new_str;
if (index > str->length)
index = str->length;
new_len = str->length + insert->length;
new_str = ft_calloc(new_len + 1, sizeof(char));
if (!new_str)
return (0);
ft_memcpy(new_str, str->str, index);
ft_memcpy(new_str + index, insert->str, insert->length);
ft_memcpy(new_str + index + insert->length, str->str + index, str->length
- index);
ft_free((void **)&str->str);
str->str = new_str;
if (index == str->length)
str->str[new_len] = '\0';
str->length = new_len;
str->capacity = new_len + 1;
return (1);
}
int ft_string_insert_s_n(t_string *str, t_string *insert, size_t index,
size_t n)
{
size_t new_len;
char *new_str;
if (index > str->length)
index = str->length;
new_len = str->length + n;
new_str = ft_calloc(new_len + 1, sizeof(char));
if (!new_str)
return (0);
ft_memcpy(new_str, str->str, index);
ft_memcpy(new_str + index, insert->str, n);
ft_memcpy(new_str + index + n, str->str + index, str->length - index);
ft_free((void **)&str->str);
str->str = new_str;
if (index == str->length)
str->str[new_len] = '\0';
str->length = new_len;
str->capacity = new_len + 1;
return (1);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:00:19 by bgoulard #+# #+# */
/* Updated: 2024/06/02 10:22:47 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
t_string *ft_string_new(size_t capacity)
{
t_string *new;
new = ft_calloc(1, sizeof(t_string));
if (!new)
return (NULL);
if (capacity < T_STRING_BUFF)
capacity = T_STRING_BUFF;
new->str = ft_calloc(capacity, sizeof(char));
if (!new->str)
return (ft_free((void **)&new), NULL);
new->capacity = capacity;
new->length = 0;
return (new);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/07 03:49:59 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:46:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string_types.h"
#include <unistd.h>
int ft_string_put(const t_string *str, int fd)
{
int ret;
if (fd < 0 || !str || (!str->str && str->length != 0))
return (-1);
ret = write(fd, str->str, str->length);
return (ret);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:53:23 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:58:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_replace(t_string *str, const char *to_replace, const char *replace_by)
{
char *tmp;
char *tmp2;
tmp = (char *)ft_string_get(str);
if (!tmp)
return (0);
tmp2 = ft_str_replace(tmp, to_replace, replace_by);
if (!tmp2)
return (0);
ft_string_set_inplace(str, tmp2);
return (1);
}
int ft_string_replace_chr(t_string *str, char to_replace, char replace_by)
{
size_t i;
size_t nb_rep;
if (!str->str)
return (0);
i = 0;
nb_rep = 0;
while (i < str->length)
{
if (str->str[i] == to_replace)
{
str->str[i] = replace_by;
nb_rep++;
}
i++;
}
if (nb_rep == 0)
return (0);
return (1);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_reserve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:26:02 by bgoulard #+# #+# */
/* Updated: 2023/12/30 12:27:08 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_reserve(t_string *str, size_t size)
{
char *new;
if (size <= str->capacity)
return (0);
new = ft_realloc(str->str, str->capacity, size);
if (!new)
return (-1);
str->str = new;
str->capacity = size;
return (0);
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_resize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:26:28 by bgoulard #+# #+# */
/* Updated: 2024/01/05 02:46:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_resize(t_string *str, size_t size)
{
char *new;
new = ft_realloc(str->str, size, str->capacity);
if (!new)
return (-1);
str->str = new;
str->capacity = size;
return (0);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 18:17:04 by bgoulard #+# #+# */
/* Updated: 2024/06/01 12:35:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_set(t_string *str, const char *new_str)
{
return (ft_string_set_n(str, new_str, ft_strlen(new_str) + 1));
}
int ft_string_set_n(t_string *str, const char *new_str, size_t n)
{
size_t new_len;
new_len = ft_strlen(new_str) + 1;
if (new_len > n)
new_len = n;
if (new_len > str->capacity)
{
str->str = ft_realloc(str->str, new_len, str->capacity);
if (!str->str)
return (0);
str->capacity = new_len;
}
ft_memcpy(str->str, new_str, new_len - 1);
str->str[new_len - 1] = '\0';
str->length = new_len - 1;
return (1);
}
int ft_string_set_inplace(t_string *str, char *new_str)
{
size_t new_len;
new_len = ft_strlen(new_str);
str->length = new_len;
if (str->str)
free(str->str);
str->str = new_str;
str->capacity = new_len + 1;
return (1);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_shrink.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:31:09 by bgoulard #+# #+# */
/* Updated: 2024/06/03 10:56:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int ft_string_shrink(t_string *str)
{
char *new;
if (str->capacity <= str->length + 1)
return (0);
new = ft_realloc(str->str, str->length + 1, str->capacity);
if (!new)
return (-1);
str->str = new;
str->capacity = str->length + 1;
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:42:41 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:58:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
t_string *ft_string_substr(t_string *str, size_t start, size_t len)
{
t_string *new;
if (start > str->length)
return (NULL);
if (len > str->length - start)
len = str->length - start;
new = ft_string_new(len);
if (!new)
return (NULL);
if (ft_string_cap(new) != len)
return (ft_string_destroy(&new), NULL);
ft_memcpy(new->str, str->str + start, len);
new->length = len - 1;
new->str[new->length] = '\0';
return (new);
}

Some files were not shown because too many files have changed in this diff Show more