Started from buttom go to the sky

This commit is contained in:
Raphaël 2024-04-28 19:59:01 +02:00
parent 96215449bd
commit f811e55dea
4781 changed files with 10121 additions and 1743 deletions

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_clone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */
/* Updated: 2023/12/09 18:15:57 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/string/str_clone.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_len.h"
#include <stdlib.h>
t_str str_clone(t_const_str source)
{
t_str res;
t_usize len;
len = str_len(source) + 1;
res = mem_alloc(sizeof(unsigned char) * len);
if (res == NULL)
return (NULL);
str_l_copy(res, source, len);
return (res);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_find_chr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:47:43 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_find_chr.h"
char *str_find_chr(t_const_str str, char chr)
{
t_usize index;
index = 0;
while (str[index])
{
if (str[index] == chr)
return ((char *)&str[index]);
index++;
}
if (str[index] == chr)
return ((char *)&str[index]);
return (NULL);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_find_rev_chr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */
/* Updated: 2024/04/28 19:42:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_find_rev_chr.h"
#include "me/string/str_len.h"
char *str_find_rev_chr(t_const_str str, char chr)
{
t_usize index;
if (str == NULL)
return (NULL);
index = str_len((t_str)str);
while (index >= 0)
{
if (str[index] == chr)
return ((char *)&str[index]);
index--;
}
return (NULL);
}

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_find_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:53:53 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_n_find_str.h"
const char *str_find_str(t_const_str str, t_const_str to_find)
{
t_str needle;
t_str haystack;
if (*to_find == '\0')
return (str);
while (*str)
{
haystack = (t_str)str;
needle = (t_str)to_find;
while (*haystack && *haystack == *needle)
{
haystack++;
needle++;
}
if (*needle == '\0')
return (str);
str++;
}
return (NULL);
}
/*R
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 3)
return (2);
printf("HAYSTACK = '%s'\n", argv[1]);
printf(" NEEDLE = '%s'\n", argv[2]);
printf("libc: %p : '%s'\n", str_find_str(argv[1], argv[2]),
str_find_str(argv[1],
argv[2]));
printf(" ft : %p : '%s'\n",
str_find_str(argv[1], argv[2]),
str_find_str(argv[1], argv[2]));
}
R*/

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_iter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:49:47 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_iter.h"
void str_iter(t_str s, void (*f)(t_usize, char *))
{
t_usize idx;
if (s == NULL)
return ;
idx = 0;
while (s[idx])
{
f(idx, &s[idx]);
idx++;
}
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_join.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:02:58 by maiboyer #+# #+# */
/* Updated: 2023/12/09 18:16:19 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/string/str_join.h"
#include "me/string/str_l_cat.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_len.h"
#include <stdlib.h>
t_str str_join(t_const_str s1, t_const_str s2)
{
t_str out;
t_usize buf_size;
if (s1 == NULL || s2 == NULL)
return (NULL);
buf_size = str_len(s1) + str_len(s2) + 1;
out = mem_alloc(sizeof(char) * buf_size);
if (out == NULL)
return (NULL);
str_l_copy(out, s1, buf_size);
str_l_cat(out, s2, buf_size);
return (out);
}

View file

@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_l_cat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:51:34 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
/*R
//CFLAGS="-lbsd"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
R*/
#include "me/string/str_l_cat.h"
#include "me/string/str_len.h"
t_usize str_l_cat(t_str dest, t_const_str src, t_usize buffer_size)
{
t_usize dest_len;
t_usize src_len;
t_usize i;
if (buffer_size == 0 && (dest == NULL || src == NULL))
return (0);
dest_len = str_len(dest);
src_len = str_len(src);
if (dest_len >= buffer_size)
return (buffer_size + src_len);
i = 0;
while (src[i] && dest_len + i < buffer_size - 1)
{
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
return (dest_len + src_len);
}
/*R
#include <bsd/string.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 64
int main(void) {
char* dest_ft;
char* dest_libc;
char* to_cat = "banane";
unsigned int res_libc = 0;
unsigned int res_ft = 0;
int i;
for (int v = 0; v <= SIZE; v++)
for (int j = 0; j < SIZE; j++)
{
dest_ft = malloc(SIZE);
dest_libc = malloc(SIZE);
i = 0;
while (i < SIZE)
{
dest_ft[i] = 'X';
dest_libc[i] = 'X';
i++;
}
dest_ft[SIZE - j - 1] = 0;
dest_libc[SIZE - j - 1] = 0;
res_libc = str_l_cat(dest_libc, to_cat, SIZE - v);
res_ft = str_l_cat(dest_ft, to_cat, SIZE - v);
int k;
k = 0;
while (k < SIZE && dest_libc[k] == dest_ft[k])
k++;
if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc
|| k != SIZE)
{
printf("----------v[%d]-j[%d]---------\n", v, j);
printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc);
printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft);
printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n",
i,
dest_libc[j], dest_ft[j]);
printf(" ft :");
for (int x = 0; x < SIZE; x++)
printf("%02X", dest_ft[x]);
printf("\n");
printf("libc:");
for (int x = 0; x < SIZE; x++)
printf("%02X", dest_libc[x]);
printf("\n");
printf("\n");
}
free(dest_libc);
free(dest_ft);
}
}
R*/

View file

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_l_copy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:51:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
/*R
//CFLAGS="-lbsd"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
R*/
#include "me/string/str_l_copy.h"
#include "me/string/str_len.h"
t_usize str_l_copy(t_str dest, t_const_str src, t_usize buffer_size)
{
t_usize src_len;
t_usize i;
src_len = str_len(src);
i = 0;
if (buffer_size == 0)
return (src_len);
while (src[i] && i < buffer_size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (src_len);
}
/*R
#include <bsd/string.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 64
int main(void) {
char* dest_ft;
char* dest_libc;
char* to_cat = "banane";
unsigned int res_libc = 0;
unsigned int res_ft = 0;
int i;
for (int v = 0; v <= SIZE; v++)
for (int j = 0; j < SIZE; j++)
{
dest_ft = malloc(SIZE);
dest_libc = malloc(SIZE);
i = 0;
while (i < SIZE)
{
dest_ft[i] = 'X';
dest_libc[i] = 'X';
i++;
}
dest_ft[SIZE - j - 1] = 0;
dest_libc[SIZE - j - 1] = 0;
res_libc = str_l_cat(dest_libc, to_cat, SIZE - v);
res_ft = str_l_cat(dest_ft, to_cat, SIZE - v);
int k;
k = 0;
while (k < SIZE && dest_libc[k] == dest_ft[k])
k++;
if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc
|| k != SIZE)
{
printf("----------v[%d]-j[%d]---------\n", v, j);
printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc);
printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft);
printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n",
i,
dest_libc[j], dest_ft[j]);
printf(" ft :");
for (int x = 0; x < SIZE; x++)
printf("%02X", dest_ft[x]);
printf("\n");
printf("libc:");
for (int x = 0; x < SIZE; x++)
printf("%02X", dest_libc[x]);
printf("\n");
printf("\n");
}
free(dest_libc);
free(dest_ft);
}
}
R*/

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */
/* Updated: 2024/03/08 12:49:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_len.h"
t_usize str_len(t_const_str str)
{
t_usize out;
if (str == NULL)
return (0);
out = 0;
while (str[out])
out++;
return (out);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:52:34 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_clone.h"
#include "me/string/str_map.h"
t_str str_map(t_const_str s, char (*f)(t_usize, char))
{
t_str out;
t_usize idx;
if (f == NULL || s == NULL)
return (NULL);
out = str_clone((t_str)s);
if (out == NULL)
return (NULL);
idx = 0;
while (s[idx])
{
out[idx] = f(idx, s[idx]);
idx++;
}
return (out);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_n_compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */
/* Updated: 2023/12/09 14:52:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_n_compare.h"
// PLEASE FIX THIS FUNCTION IF NEEDED !
t_i32 str_n_compare(t_const_str lhs, t_const_str rhs, t_usize n)
{
t_usize index;
index = 0;
if (n == 0)
return (0);
while (lhs[index] && rhs[index] && lhs[index] == rhs[index] && index < n
- 1)
index++;
return ((t_i32)(t_u8)lhs[index] - (t_i32)(t_u8)rhs[index]);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_n_find_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */
/* Updated: 2024/04/28 19:40:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_len.h"
#include "me/string/str_n_find_str.h"
static t_str local_get_end_of_search(t_usize len, t_str str)
{
t_usize out_len;
out_len = str_len(str);
if (len > out_len)
len = out_len;
return (str + len);
}
const char *str_n_find_str(t_const_str str, t_const_str to_find, t_usize len)
{
t_str needle;
t_str haystack;
t_str end_of_search;
if ((str == NULL || to_find == NULL))
return (NULL);
if (*to_find == '\0')
return ((t_str)str);
end_of_search = local_get_end_of_search(len, (t_str)str);
while (*str)
{
haystack = (t_str)str;
needle = (t_str)to_find;
while (haystack < end_of_search && *haystack && *haystack == *needle)
{
haystack++;
needle++;
}
if (*needle == '\0')
return (str);
str++;
}
return (NULL);
}

View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:52:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/mem/mem_alloc_array.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_split.h"
#include <stdlib.h>
static t_usize local_count_words(t_const_str str, char chr);
static t_str *local_split_inner(t_const_str str, char chr, t_str *out);
static t_str *local_split_freeall(t_str **to_free);
static t_usize local_count_words(t_const_str str, char chr)
{
t_usize i;
t_usize out;
out = 0;
i = 0;
while (str[i])
{
while (str[i] && str[i] == chr)
i++;
if (str[i] == 0)
return (out);
out++;
while (str[i] && str[i] != chr)
i++;
}
return (out);
}
static t_str *local_split_freeall(t_str **to_free)
{
while (*to_free)
free(*(to_free++));
return (NULL);
}
static t_str *local_split_inner(t_const_str str, char chr, t_str *out)
{
t_usize str_i;
t_usize sub_i;
t_usize ptr_i;
str_i = 0;
ptr_i = 0;
while (str[str_i])
{
while (str[str_i] && str[str_i] == chr)
str_i++;
if (str[str_i] == 0)
break ;
sub_i = 0;
while (str[str_i + sub_i] && str[str_i + sub_i] != chr)
sub_i++;
out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1));
if (out[ptr_i] == NULL)
return (local_split_freeall(&out));
str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1);
str_i += sub_i;
}
out[ptr_i] = NULL;
return (out);
}
t_str *str_split(t_const_str str, char chr)
{
t_usize ptr_len;
t_str *out;
if (str == NULL || *str == 0)
{
out = mem_alloc(sizeof(t_str) * 1);
*out = NULL;
return (out);
}
ptr_len = local_count_words(str, chr);
out = mem_alloc_array(sizeof(t_str), (ptr_len + 1));
if (out == NULL)
return (NULL);
return (local_split_inner(str, chr, out));
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_substring.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 22:42:55 by maiboyer #+# #+# */
/* Updated: 2023/12/09 16:52:34 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_len.h"
#include "me/string/str_substring.h"
#include <stdlib.h>
t_str str_substring(t_const_str str, t_usize start, t_usize len)
{
t_usize len_str;
t_usize len_str_substring;
t_str out;
if (str == NULL)
return (NULL);
len_str = str_len(str);
if (start >= len_str)
return (mem_alloc(1));
len_str_substring = len_str - start + 1;
if (len_str_substring > len)
len_str_substring = len + 1;
out = mem_alloc(sizeof(char) * len_str_substring);
if (out == NULL)
return (NULL);
str_l_copy(out, &str[start], len_str_substring);
return (out);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_trim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:43:42 by maiboyer #+# #+# */
/* Updated: 2023/12/09 18:16:31 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc.h"
#include "me/string/str_find_chr.h"
#include "me/string/str_l_copy.h"
#include "me/string/str_len.h"
#include "me/string/str_trim.h"
#include <stdlib.h>
t_str str_trim(t_const_str str, t_const_str charset)
{
t_usize start_idx;
t_usize end_idx;
t_usize buf_size;
t_str out;
if (str == NULL || charset == NULL)
return (0);
start_idx = 0;
while (str[start_idx] && str_find_chr((t_str)charset,
str[start_idx]) != NULL)
start_idx++;
end_idx = str_len((t_str)str);
while (end_idx > 0 && str_find_chr((t_str)charset, str[end_idx]) != NULL)
end_idx--;
buf_size = end_idx - start_idx + 2;
if (end_idx == 0)
buf_size = 1;
out = mem_alloc(sizeof(char) * buf_size);
if (out == NULL)
return (NULL);
str_l_copy(out, &((t_str)str)[start_idx], buf_size);
return (out);
}