Added env hashmap with build_env function to build envp

This commit is contained in:
Maix0 2024-05-04 19:21:56 +02:00
parent f35e986145
commit f86947a852
54 changed files with 2010 additions and 105 deletions

View file

@ -14,6 +14,15 @@ convert/str_to_u64
convert/str_to_u64_utils
convert/str_to_u8
convert/str_to_u8_utils
vec/buf_str/buf_str
vec/buf_str/buf_str_functions2
vec/buf_str/buf_str_functions3
vec/str/str
vec/str/str_functions2
vec/str/str_functions3
vec/u8/u8
vec/u8/u8_functions2
vec/u8/u8_functions3
vec/vec_buf_str
vec/vec_buf_str_functions2
vec/vec_buf_str_functions3

View file

@ -73,4 +73,10 @@ t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev);
t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
t_error (*func)(t_usize idx,
const C__KEYTYPE__ *key,
C__VALTYPE__ *val, void *ctx),
void *ctx);
#endif

View file

@ -102,9 +102,9 @@ void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
if (entry == NULL)
{
entry = mem_alloc(sizeof(t_entry_tile));
entry = mem_alloc(sizeof(t_entry_C__PREFIX__));
entry->hash_id = hashed_key;
entry->kv = (t_kv_tile){.key = key, .val = value};
entry->kv = (t_kv_C__PREFIX__){.key = key, .val = value};
entry->next = NULL;
if (prev == NULL)
hmap->buckets[hashed_key % hmap->num_buckets] = entry;

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX__.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hashmap/hashmap_C__PREFIX__.h"
t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
t_error (*func)(t_usize idx,
const C__KEYTYPE__ *key,
C__VALTYPE__ *val, void *ctx),
void *ctx)
{
t_usize bucket_id;
t_usize all_id;
t_entry_C__PREFIX__ *cur;
bucket_id = 0;
all_id = 0;
while (bucket_id < self->num_buckets)
{
cur = self->buckets[bucket_id];
while (cur != NULL)
{
if (func(all_id++, &cur->kv.key, &cur->kv.val, ctx))
return (ERROR);
cur = cur->next;
}
bucket_id++;
}
return (NO_ERROR);
}

View file

@ -27,7 +27,7 @@ C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
hmap->hfunc(&hmap->hasher, key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
entry = hashmap_get_entry_tile(hmap, hashed_key, key, &prev);
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
if (entry == NULL)
return (NULL);
return (&entry->kv.val);

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_compare.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */
/* Updated: 2024/05/04 18:37:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STR_COMPARE_H
# define STR_COMPARE_H
# include "me/types.h"
bool str_compare(t_const_str lhs, t_const_str rhs);
#endif

View file

@ -1,9 +1,9 @@
[definition.vec]
headers = ["generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"]
sources = [
"generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__",
"generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__",
"generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/vec/C__PREFIX___functions2.c__TEMPLATE__",
"generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__",
]
replace.C__TYPENAME__ = "type"
replace.C__TYPEHEADER__ = "header_include"
@ -14,8 +14,8 @@ replace.C__PREFIXUP__ = "prefix"
[definition.hashmap]
headers = ["generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"]
sources = [
"generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__",
"generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__",
]
replace.C__VALTYPE__ = "type"
replace.C__KEYTYPE__ = "type"
@ -26,8 +26,8 @@ replace.C__PREFIXUP__ = "prefix"
[definition.str_to_num]
headers = []
sources = [
"generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__",
"generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__",
]
replace.C__TYPE__ = "type"
replace.C__UNSIGNED_TYPE__ = "type"
@ -128,7 +128,7 @@ replace.C__SIGNED_TYPE__ = "false"
[[create.vec]]
sources_output = "src/vec/"
sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_u8"
replace.C__TYPEHEADER__ = ''
@ -136,7 +136,7 @@ replace.C__PREFIX__ = "u8"
replace.C__PREFIXUP__ = "U8"
[[create.vec]]
sources_output = "src/vec/"
sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_str"
replace.C__TYPEHEADER__ = ''
@ -144,7 +144,7 @@ replace.C__PREFIX__ = "str"
replace.C__PREFIXUP__ = "STR"
[[create.vec]]
sources_output = "src/vec/"
sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_buffer_str"
replace.C__TYPEHEADER__ = '#include "me/buffered_str/buf_str.h"'

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_buf_str.h"
#include <stdlib.h>
t_vec_buf_str vec_buf_str_new(t_usize capacity,
t_free_buf_str_item free_function)
{
t_vec_buf_str out;
out = (t_vec_buf_str){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_buffer_str));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element)
{
t_buffer_str *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
{
t_buffer_str *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (wanted_capacity > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
return (NO_ERROR);
}
/// Return true if the vector is empty
/// This function is safe to call with value being NULL
t_error vec_buf_str_pop(t_vec_buf_str *vec, t_buffer_str *value)
{
t_buffer_str temp_value;
t_buffer_str *ptr;
if (vec == NULL)
return (ERROR);
ptr = value;
if (vec->len == 0)
return (ERROR);
if (value == NULL)
ptr = &temp_value;
vec->len--;
*ptr = vec->buffer[vec->len];
mem_set_zero(&vec->buffer[vec->len], sizeof(t_buffer_str));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_buf_str_free(t_vec_buf_str vec)
{
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_buf_str.h"
#include <stdlib.h>
t_error vec_buf_str_find(t_vec_buf_str *vec,
bool (*fn)(const t_buffer_str *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
bool (*fn)(const t_buffer_str *),
t_usize starting_index, t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_buf_str_all(t_vec_buf_str *vec,
bool (*fn)(const t_buffer_str *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_buf_str_any(t_vec_buf_str *vec,
bool (*fn)(const t_buffer_str *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_buf_str_iter(t_vec_buf_str *vec,
void (*fn)(t_usize index, t_buffer_str *value,
void *state),
void *state)
{
t_usize idx;
if (vec == NULL || fn == NULL)
return;
idx = 0;
while (idx < vec->len)
{
fn(idx, &vec->buffer[idx], state);
idx++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_buf_str.h"
#include <stdlib.h>
t_error vec_buf_str_push_front(t_vec_buf_str *vec,
t_buffer_str element)
{
t_usize i;
if (vec->len == 0)
return (vec_buf_str_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_buf_str_reserve(vec, 3 * vec->len / 2 + 1))
return (ERROR);
while (i > 0)
{
vec->buffer[i + 1] = vec->buffer[i];
i--;
}
vec->buffer[1] = vec->buffer[0];
vec->buffer[0] = element;
vec->len++;
return (NO_ERROR);
}
t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_buffer_str *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_buf_str_pop(vec, value));
i = 0;
*value = vec->buffer[0];
vec->len--;
while (i < vec->len)
{
vec->buffer[i] = vec->buffer[i + 1];
i++;
}
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
return (NO_ERROR);
}
void vec_buf_str_reverse(t_vec_buf_str *vec)
{
t_buffer_str temporary;
t_usize i;
i = 0;
while (i < vec->len / 2)
{
temporary = vec->buffer[vec->len - 1 - i];
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
vec->buffer[i] = temporary;
i++;
}
}
t_error vec_buf_str_back(t_vec_buf_str *vec, t_buffer_str **out)
{
t_buffer_str *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
t_vec_str vec_str_new(t_usize capacity,
t_free_str_item free_function)
{
t_vec_str out;
out = (t_vec_str){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_str));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_str_push(t_vec_str *vec, t_str element)
{
t_str *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
{
t_str *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (wanted_capacity > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
return (NO_ERROR);
}
/// Return true if the vector is empty
/// This function is safe to call with value being NULL
t_error vec_str_pop(t_vec_str *vec, t_str *value)
{
t_str temp_value;
t_str *ptr;
if (vec == NULL)
return (ERROR);
ptr = value;
if (vec->len == 0)
return (ERROR);
if (value == NULL)
ptr = &temp_value;
vec->len--;
*ptr = vec->buffer[vec->len];
mem_set_zero(&vec->buffer[vec->len], sizeof(t_str));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_str_free(t_vec_str vec)
{
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
t_error vec_str_find(t_vec_str *vec,
bool (*fn)(const t_str *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_str_find_starting(t_vec_str *vec,
bool (*fn)(const t_str *),
t_usize starting_index, t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_str_all(t_vec_str *vec,
bool (*fn)(const t_str *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_str_any(t_vec_str *vec,
bool (*fn)(const t_str *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_str_iter(t_vec_str *vec,
void (*fn)(t_usize index, t_str *value,
void *state),
void *state)
{
t_usize idx;
if (vec == NULL || fn == NULL)
return;
idx = 0;
while (idx < vec->len)
{
fn(idx, &vec->buffer[idx], state);
idx++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
t_error vec_str_push_front(t_vec_str *vec,
t_str element)
{
t_usize i;
if (vec->len == 0)
return (vec_str_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_str_reserve(vec, 3 * vec->len / 2 + 1))
return (ERROR);
while (i > 0)
{
vec->buffer[i + 1] = vec->buffer[i];
i--;
}
vec->buffer[1] = vec->buffer[0];
vec->buffer[0] = element;
vec->len++;
return (NO_ERROR);
}
t_error vec_str_pop_front(t_vec_str *vec, t_str *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_str_pop(vec, value));
i = 0;
*value = vec->buffer[0];
vec->len--;
while (i < vec->len)
{
vec->buffer[i] = vec->buffer[i + 1];
i++;
}
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
return (NO_ERROR);
}
void vec_str_reverse(t_vec_str *vec)
{
t_str temporary;
t_usize i;
i = 0;
while (i < vec->len / 2)
{
temporary = vec->buffer[vec->len - 1 - i];
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
vec->buffer[i] = temporary;
i++;
}
}
t_error vec_str_back(t_vec_str *vec, t_str **out)
{
t_str *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_u8.h"
#include <stdlib.h>
t_vec_u8 vec_u8_new(t_usize capacity,
t_free_u8_item free_function)
{
t_vec_u8 out;
out = (t_vec_u8){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_u8));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
{
t_u8 *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (vec->len + 1 > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (vec->len + 1 > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
{
t_u8 *temp_buffer;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (wanted_capacity > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8));
if (temp_buffer == NULL)
return (ERROR);
mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8));
free(vec->buffer);
vec->buffer = temp_buffer;
vec->capacity = new_capacity;
}
return (NO_ERROR);
}
/// Return true if the vector is empty
/// This function is safe to call with value being NULL
t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value)
{
t_u8 temp_value;
t_u8 *ptr;
if (vec == NULL)
return (ERROR);
ptr = value;
if (vec->len == 0)
return (ERROR);
if (value == NULL)
ptr = &temp_value;
vec->len--;
*ptr = vec->buffer[vec->len];
mem_set_zero(&vec->buffer[vec->len], sizeof(t_u8));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_u8_free(t_vec_u8 vec)
{
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_u8.h"
#include <stdlib.h>
t_error vec_u8_find(t_vec_u8 *vec,
bool (*fn)(const t_u8 *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_u8_find_starting(t_vec_u8 *vec,
bool (*fn)(const t_u8 *),
t_usize starting_index, t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_u8_all(t_vec_u8 *vec,
bool (*fn)(const t_u8 *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_u8_any(t_vec_u8 *vec,
bool (*fn)(const t_u8 *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_u8_iter(t_vec_u8 *vec,
void (*fn)(t_usize index, t_u8 *value,
void *state),
void *state)
{
t_usize idx;
if (vec == NULL || fn == NULL)
return;
idx = 0;
while (idx < vec->len)
{
fn(idx, &vec->buffer[idx], state);
idx++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/types.h"
#include "me/vec/vec_u8.h"
#include <stdlib.h>
t_error vec_u8_push_front(t_vec_u8 *vec,
t_u8 element)
{
t_usize i;
if (vec->len == 0)
return (vec_u8_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_u8_reserve(vec, 3 * vec->len / 2 + 1))
return (ERROR);
while (i > 0)
{
vec->buffer[i + 1] = vec->buffer[i];
i--;
}
vec->buffer[1] = vec->buffer[0];
vec->buffer[0] = element;
vec->len++;
return (NO_ERROR);
}
t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_u8_pop(vec, value));
i = 0;
*value = vec->buffer[0];
vec->len--;
while (i < vec->len)
{
vec->buffer[i] = vec->buffer[i + 1];
i++;
}
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
return (NO_ERROR);
}
void vec_u8_reverse(t_vec_u8 *vec)
{
t_u8 temporary;
t_usize i;
i = 0;
while (i < vec->len / 2)
{
temporary = vec->buffer[vec->len - 1 - i];
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
vec->buffer[i] = temporary;
i++;
}
}
t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out)
{
t_u8 *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}

View file

@ -41,6 +41,7 @@ fs/write
gnl/get_next_line
hash/hasher
hash/hash_signed
hash/hash_str
hash/hash_unsigned
hash/sip/sip13
hash/sip/sip_utils
@ -91,6 +92,7 @@ printf/matchers
printf/printf
printf/vprintf
string/str_clone
string/str_compare
string/str_find_chr
string/str_find_rev_chr
string/str_find_str

20
stdme/src/hash/hash_str.c Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 19:17:11 by maiboyer #+# #+# */
/* Updated: 2024/05/04 19:18:26 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/hasher.h"
#include "me/string/str_len.h"
#include "me/types.h"
void hasher_write_str(t_hasher *hasher, t_str s)
{
hasher_write_bytes(hasher, (t_u8 *)s, str_len(s));
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */
/* Updated: 2024/05/04 18:37:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/str_compare.h"
// PLEASE FIX THIS FUNCTION IF NEEDED !
bool str_compare(t_const_str lhs, t_const_str rhs)
{
t_usize index;
if (lhs == NULL || rhs == NULL)
return (lhs == rhs);
index = 0;
while (lhs[index] && rhs[index] && lhs[index] == rhs[index])
index++;
return ((t_i32)(t_u8)lhs[index] - (t_i32)(t_u8)rhs[index]);
}