Added <num>_to_str functions

This commit is contained in:
Maieul BOYER 2024-07-16 19:38:27 +02:00
parent aafd056f49
commit 698d1088e2
No known key found for this signature in database
21 changed files with 963 additions and 60 deletions

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error i16_to_str_base_prefix(t_i16 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.i16 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u16 = ~value.u16 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error i16_to_str_base(t_i16 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (i16_to_str_base_prefix(val, base, "", out));
}
t_error i16_to_str(t_i16 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (i16_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error i32_to_str_base_prefix(t_i32 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.i32 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u32 = ~value.u32 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error i32_to_str_base(t_i32 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (i32_to_str_base_prefix(val, base, "", out));
}
t_error i32_to_str(t_i32 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (i32_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error i64_to_str_base_prefix(t_i64 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.i64 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u64 = ~value.u64 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error i64_to_str_base(t_i64 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (i64_to_str_base_prefix(val, base, "", out));
}
t_error i64_to_str(t_i64 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (i64_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error i8_to_str_base_prefix(t_i8 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.i8 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u8 = ~value.u8 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error i8_to_str_base(t_i8 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (i8_to_str_base_prefix(val, base, "", out));
}
t_error i8_to_str(t_i8 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (i8_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error u16_to_str_base_prefix(t_u16 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.u16 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u16 = ~value.u16 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error u16_to_str_base(t_u16 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (u16_to_str_base_prefix(val, base, "", out));
}
t_error u16_to_str(t_u16 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (u16_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error u32_to_str_base_prefix(t_u32 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.u32 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u32 = ~value.u32 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error u32_to_str_base(t_u32 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (u32_to_str_base_prefix(val, base, "", out));
}
t_error u32_to_str(t_u32 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (u32_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error u64_to_str_base_prefix(t_u64 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.u64 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u64 = ~value.u64 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error u64_to_str_base(t_u64 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (u64_to_str_base_prefix(val, base, "", out));
}
t_error u64_to_str(t_u64 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (u64_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_i64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/char/char.h"
#include "me/convert/numbers_to_str.h"
#include "me/str/str.h"
#include "me/types.h"
t_error _format_u64(t_num_str args, t_str *out);
t_error u8_to_str_base_prefix(t_u8 val, t_str base, t_str prefix,
t_str *out)
{
union u_nums value;
bool is_nonnegative;
if (out == NULL || base == NULL || prefix == NULL)
return (ERROR);
value.u8 = val;
is_nonnegative = val < 0;
if (is_nonnegative)
value.u8 = ~value.u8 + 1;
return (_format_u64((t_num_str){.value = value.u64, \
.is_nonnegative = is_nonnegative, \
.base = base, \
.prefix = prefix}, \
out));
}
t_error u8_to_str_base(t_u8 val, t_str base, t_str *out)
{
if (out == NULL || base == NULL)
return (ERROR);
return (u8_to_str_base_prefix(val, base, "", out));
}
t_error u8_to_str(t_u8 val, t_str *out)
{
if (out == NULL)
return (ERROR);
return (u8_to_str_base_prefix(val, "0123456789", "", out));
}

View file

@ -10,11 +10,8 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_buf_str.h"
#include <stdlib.h>
@ -45,7 +42,7 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_string element)
/// Return true in case of an error
t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
{
size_t new_capacity;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
@ -54,7 +51,8 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_string));
vec->buffer =
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_string));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -83,6 +81,8 @@ t_error vec_buf_str_pop(t_vec_buf_str *vec, t_string *value)
/// This function is safe to call with `free_elem` being NULL
void vec_buf_str_free(t_vec_buf_str vec)
{
if (vec.buffer == NULL)
return;
if (vec.free_func)
{
while (vec.len)

View file

@ -27,7 +27,7 @@ t_error vec_buf_str_find(t_vec_buf_str *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_string *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_string *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_buf_str_all(t_vec_buf_str *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_string *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_buf_str_any(t_vec_buf_str *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_string *)&vec->buffer[idx]))
*result = true;
idx++;
}

View file

@ -10,11 +10,8 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_str.h"
#include <stdlib.h>
@ -45,7 +42,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element)
/// Return true in case of an error
t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
{
size_t new_capacity;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
@ -54,7 +51,8 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->buffer =
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_str));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -83,6 +81,8 @@ t_error vec_str_pop(t_vec_str *vec, t_str *value)
/// This function is safe to call with `free_elem` being NULL
void vec_str_free(t_vec_str vec)
{
if (vec.buffer == NULL)
return;
if (vec.free_func)
{
while (vec.len)

View file

@ -27,7 +27,7 @@ t_error vec_str_find(t_vec_str *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_str_find_starting(t_vec_str *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_str_all(t_vec_str *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_str *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_str_any(t_vec_str *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
*result = true;
idx++;
}

View file

@ -10,11 +10,8 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_u8.h"
#include <stdlib.h>
@ -45,7 +42,7 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element)
/// Return true in case of an error
t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
{
size_t new_capacity;
size_t new_capacity;
if (vec == NULL)
return (ERROR);
@ -54,7 +51,8 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity)
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->buffer =
mem_realloc_array(vec->buffer, new_capacity, sizeof(t_u8));
vec->capacity = new_capacity;
}
return (NO_ERROR);
@ -83,6 +81,8 @@ t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value)
/// This function is safe to call with `free_elem` being NULL
void vec_u8_free(t_vec_u8 vec)
{
if (vec.buffer == NULL)
return;
if (vec.free_func)
{
while (vec.len)

View file

@ -27,7 +27,7 @@ t_error vec_u8_find(t_vec_u8 *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_u8 *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_u8_find_starting(t_vec_u8 *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_u8 *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_u8_all(t_vec_u8 *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_u8 *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_u8_any(t_vec_u8 *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_u8 *)&vec->buffer[idx]))
*result = true;
idx++;
}