Norminette 'static' functions

This commit is contained in:
Maieul BOYER 2024-07-16 19:42:30 +02:00
parent 698d1088e2
commit 74336f37a3
No known key found for this signature in database
2 changed files with 86 additions and 73 deletions

View file

@ -1,4 +1,3 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
@ -7,58 +6,59 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 17:18:47 by maiboyer #+# #+# */ /* Created: 2024/07/16 17:18:47 by maiboyer #+# #+# */
/* Updated: 2024/07/16 17:19:31 by maiboyer ### ########.fr */ /* Updated: 2024/07/16 19:41:42 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef NUMBERS_TO_STR_H #ifndef NUMBERS_TO_STR_H
#define NUMBERS_TO_STR_H # define NUMBERS_TO_STR_H
#include "me/types.h" # include "me/types.h"
typedef struct s_num_str t_num_str; typedef struct s_num_str t_num_str;
typedef struct s_num_str_state t_num_str_state; typedef struct s_num_str_state t_num_str_state;
struct s_num_str struct s_num_str
{ {
t_u64 value; t_u64 value;
bool is_nonnegative; bool is_nonnegative;
t_str base; t_str base;
t_str prefix; t_str prefix;
}; };
struct s_num_str_state struct s_num_str_state
{ {
bool print; bool print;
bool zero; bool zero;
char buffer[40]; char buffer[40];
t_str base; t_str base;
t_u64 base_len; t_u64 base_len;
t_u64 modulus; t_u64 modulus;
t_u64 value; t_u64 value;
t_usize idx; t_usize idx;
}; };
union u_nums { union u_nums
t_u64 u64; {
t_i64 i64; t_u64 u64;
t_u32 u32; t_i64 i64;
t_i32 i32; t_u32 u32;
t_u16 u16; t_i32 i32;
t_i16 i16; t_u16 u16;
t_u8 u8; t_i16 i16;
t_i8 i8; t_u8 u8;
t_i8 i8;
}; };
/// @brief Convert a signed 8-bit integer to a string with a base and a prefix. /// @brief Convert a signed 8-bit integer to a string with a base + a prefix.
/// @param val The signed 8-bit integer to convert. /// @param val The signed 8-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i8_to_str_base_prefix(t_i8 val, t_str base, t_str prefix, t_str *out); t_error i8_to_str_base_prefix(t_i8 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a signed 8-bit integer to a string with a base /// @brief Convert a signed 8-bit integer to a string with a base
/// @param val The signed 8-bit integer to convert. /// @param val The signed 8-bit integer to convert.
@ -66,22 +66,24 @@ t_error i8_to_str_base_prefix(t_i8 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i8_to_str_base(t_i8 val, t_str base, t_str *out); t_error i8_to_str_base(t_i8 val, t_str base,
t_str *out);
/// @brief Convert a signed 8-bit integer to a string. /// @brief Convert a signed 8-bit integer to a string.
/// @param val The signed 8-bit integer to convert. /// @param val The signed 8-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error i8_to_str(t_i8 val, t_str *out); t_error i8_to_str(t_i8 val, t_str *out);
/// @brief Convert a unsigned 8-bit integer to a string with a base and a prefix. /// @brief Convert a unsigned 8-bit integer to a string with a base + a prefix.
/// @param val The unsigned 8-bit integer to convert. /// @param val The unsigned 8-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u8_to_str_base_prefix(t_u8 val, t_str base, t_str prefix, t_str *out); t_error u8_to_str_base_prefix(t_u8 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a unsigned 8-bit integer to a string with a base /// @brief Convert a unsigned 8-bit integer to a string with a base
/// @param val The unsigned 8-bit integer to convert. /// @param val The unsigned 8-bit integer to convert.
@ -89,22 +91,24 @@ t_error u8_to_str_base_prefix(t_u8 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u8_to_str_base(t_u8 val, t_str base, t_str *out); t_error u8_to_str_base(t_u8 val, t_str base,
t_str *out);
/// @brief Convert a unsigned 8-bit integer to a string. /// @brief Convert a unsigned 8-bit integer to a string.
/// @param val The unsigned 8-bit integer to convert. /// @param val The unsigned 8-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error u8_to_str(t_u8 val, t_str *out); t_error u8_to_str(t_u8 val, t_str *out);
/// @brief Convert a signed 16-bit integer to a string with a base and a prefix. /// @brief Convert a signed 16-bit integer to a string with a base + a prefix.
/// @param val The signed 16-bit integer to convert. /// @param val The signed 16-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i16_to_str_base_prefix(t_i16 val, t_str base, t_str prefix, t_str *out); t_error i16_to_str_base_prefix(t_i16 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a signed 16-bit integer to a string with a base /// @brief Convert a signed 16-bit integer to a string with a base
/// @param val The signed 16-bit integer to convert. /// @param val The signed 16-bit integer to convert.
@ -112,22 +116,24 @@ t_error i16_to_str_base_prefix(t_i16 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i16_to_str_base(t_i16 val, t_str base, t_str *out); t_error i16_to_str_base(t_i16 val, t_str base,
t_str *out);
/// @brief Convert a signed 16-bit integer to a string. /// @brief Convert a signed 16-bit integer to a string.
/// @param val The signed 16-bit integer to convert. /// @param val The signed 16-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error i16_to_str(t_i16 val, t_str *out); t_error i16_to_str(t_i16 val, t_str *out);
/// @brief Convert a unsigned 16-bit integer to a string with a base and a prefix. /// @brief Convert a unsigned 16-bit integer to a string with a base + a prefix.
/// @param val The unsigned 16-bit integer to convert. /// @param val The unsigned 16-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u16_to_str_base_prefix(t_u16 val, t_str base, t_str prefix, t_str *out); t_error u16_to_str_base_prefix(t_u16 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a unsigned 16-bit integer to a string with a base /// @brief Convert a unsigned 16-bit integer to a string with a base
/// @param val The unsigned 16-bit integer to convert. /// @param val The unsigned 16-bit integer to convert.
@ -135,22 +141,24 @@ t_error u16_to_str_base_prefix(t_u16 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u16_to_str_base(t_u16 val, t_str base, t_str *out); t_error u16_to_str_base(t_u16 val, t_str base,
t_str *out);
/// @brief Convert a unsigned 16-bit integer to a string. /// @brief Convert a unsigned 16-bit integer to a string.
/// @param val The unsigned 16-bit integer to convert. /// @param val The unsigned 16-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error u16_to_str(t_u16 val, t_str *out); t_error u16_to_str(t_u16 val, t_str *out);
/// @brief Convert a signed 32-bit integer to a string with a base and a prefix. /// @brief Convert a signed 32-bit integer to a string with a base + a prefix.
/// @param val The signed 32-bit integer to convert. /// @param val The signed 32-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i32_to_str_base_prefix(t_i32 val, t_str base, t_str prefix, t_str *out); t_error i32_to_str_base_prefix(t_i32 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a signed 32-bit integer to a string with a base /// @brief Convert a signed 32-bit integer to a string with a base
/// @param val The signed 32-bit integer to convert. /// @param val The signed 32-bit integer to convert.
@ -158,22 +166,24 @@ t_error i32_to_str_base_prefix(t_i32 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i32_to_str_base(t_i32 val, t_str base, t_str *out); t_error i32_to_str_base(t_i32 val, t_str base,
t_str *out);
/// @brief Convert a signed 32-bit integer to a string. /// @brief Convert a signed 32-bit integer to a string.
/// @param val The signed 32-bit integer to convert. /// @param val The signed 32-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error i32_to_str(t_i32 val, t_str *out); t_error i32_to_str(t_i32 val, t_str *out);
/// @brief Convert a unsigned 32-bit integer to a string with a base and a prefix. /// @brief Convert a unsigned 32-bit integer to a string with a base + a prefix.
/// @param val The unsigned 32-bit integer to convert. /// @param val The unsigned 32-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u32_to_str_base_prefix(t_u32 val, t_str base, t_str prefix, t_str *out); t_error u32_to_str_base_prefix(t_u32 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a unsigned 32-bit integer to a string with a base /// @brief Convert a unsigned 32-bit integer to a string with a base
/// @param val The unsigned 32-bit integer to convert. /// @param val The unsigned 32-bit integer to convert.
@ -181,13 +191,14 @@ t_error u32_to_str_base_prefix(t_u32 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u32_to_str_base(t_u32 val, t_str base, t_str *out); t_error u32_to_str_base(t_u32 val, t_str base,
t_str *out);
/// @brief Convert a unsigned 32-bit integer to a string. /// @brief Convert a unsigned 32-bit integer to a string.
/// @param val The unsigned 32-bit integer to convert. /// @param val The unsigned 32-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error u32_to_str(t_u32 val, t_str *out); t_error u32_to_str(t_u32 val, t_str *out);
/// @brief Convert a signed 64-bit integer to a string with a base and a prefix. /// @brief Convert a signed 64-bit integer to a string with a base and a prefix.
/// @param val The signed 64-bit integer to convert. /// @param val The signed 64-bit integer to convert.
@ -196,7 +207,8 @@ t_error u32_to_str(t_u32 val, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i64_to_str_base_prefix(t_i64 val, t_str base, t_str prefix, t_str *out); t_error i64_to_str_base_prefix(t_i64 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a signed 64-bit integer to a string with a base /// @brief Convert a signed 64-bit integer to a string with a base
/// @param val The signed 64-bit integer to convert. /// @param val The signed 64-bit integer to convert.
@ -204,22 +216,24 @@ t_error i64_to_str_base_prefix(t_i64 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error i64_to_str_base(t_i64 val, t_str base, t_str *out); t_error i64_to_str_base(t_i64 val, t_str base,
t_str *out);
/// @brief Convert a signed 64-bit integer to a string. /// @brief Convert a signed 64-bit integer to a string.
/// @param val The signed 64-bit integer to convert. /// @param val The signed 64-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error i64_to_str(t_i64 val, t_str *out); t_error i64_to_str(t_i64 val, t_str *out);
/// @brief Convert a unsigned 64-bit integer to a string with a base and a prefix. /// @brief Convert a unsigned 64-bit integer to a string with a base + a prefix.
/// @param val The unsigned 64-bit integer to convert. /// @param val The unsigned 64-bit integer to convert.
/// @param base The base to use for the conversion. /// @param base The base to use for the conversion.
/// @param prefix The prefix to use for the conversion. /// @param prefix The prefix to use for the conversion.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u64_to_str_base_prefix(t_u64 val, t_str base, t_str prefix, t_str *out); t_error u64_to_str_base_prefix(t_u64 val, t_str base,
t_str prefix, t_str *out);
/// @brief Convert a unsigned 64-bit integer to a string with a base /// @brief Convert a unsigned 64-bit integer to a string with a base
/// @param val The unsigned 64-bit integer to convert. /// @param val The unsigned 64-bit integer to convert.
@ -227,13 +241,13 @@ t_error u64_to_str_base_prefix(t_u64 val, t_str base, t_str prefix, t_str *out);
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
/// @note The base must be a string of at least 2 characters and no duplicates. /// @note The base must be a string of at least 2 characters and no duplicates.
t_error u64_to_str_base(t_u64 val, t_str base, t_str *out); t_error u64_to_str_base(t_u64 val, t_str base,
t_str *out);
/// @brief Convert a unsigned 64-bit integer to a string. /// @brief Convert a unsigned 64-bit integer to a string.
/// @param val The unsigned 64-bit integer to convert. /// @param val The unsigned 64-bit integer to convert.
/// @param out The output string. /// @param out The output string.
/// @return True in case of error, false otherwise. /// @return True in case of error, false otherwise.
t_error u64_to_str(t_u64 val, t_str *out); t_error u64_to_str(t_u64 val, t_str *out);
#endif /* NUMBERS_TO_STR_H */ #endif /* NUMBERS_TO_STR_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 18:15:57 by maiboyer #+# #+# */ /* Created: 2024/07/16 18:15:57 by maiboyer #+# #+# */
/* Updated: 2024/07/16 18:19:42 by maiboyer ### ########.fr */ /* Updated: 2024/07/16 19:39:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,10 +16,10 @@
#include "me/types.h" #include "me/types.h"
#include <stdint.h> #include <stdint.h>
static t_error _check_base(t_str base) static t_error _check_base(t_str base)
{ {
t_usize i; t_usize i;
t_usize j; t_usize j;
i = 0; i = 0;
if (base == NULL) if (base == NULL)
@ -37,7 +37,7 @@ static t_error _check_base(t_str base)
return (NO_ERROR); return (NO_ERROR);
} }
static void _set_modulus(t_num_str_state *s) static void _set_modulus(t_num_str_state *s)
{ {
s->modulus = 0; s->modulus = 0;
s->base_len = str_len(s->base); s->base_len = str_len(s->base);
@ -45,7 +45,7 @@ static void _set_modulus(t_num_str_state *s)
s->modulus += s->base_len; s->modulus += s->base_len;
} }
static char _get_char(t_u64 value, t_num_str_state *s) static char _get_char(t_u64 value, t_num_str_state *s)
{ {
if (value == 0) if (value == 0)
s->zero = true; s->zero = true;
@ -54,9 +54,9 @@ static char _get_char(t_u64 value, t_num_str_state *s)
return (s->base[value]); return (s->base[value]);
} }
static t_error _format_u64_base_innner(t_num_str_state *s) static t_error _format_u64_base_innner(t_num_str_state *s)
{ {
char c; char c;
if (s->value == 0) if (s->value == 0)
return (s->buffer[s->idx++] = _get_char(0, s), NO_ERROR); return (s->buffer[s->idx++] = _get_char(0, s), NO_ERROR);
@ -74,9 +74,9 @@ static t_error _format_u64_base_innner(t_num_str_state *s)
return (NO_ERROR); return (NO_ERROR);
} }
t_error _format_u64(t_num_str args, t_str *out) t_error _format_u64(t_num_str args, t_str *out)
{ {
t_num_str_state s; t_num_str_state s;
t_str res; t_str res;
if (_check_base(args.base)) if (_check_base(args.base))
@ -96,4 +96,3 @@ t_error _format_u64(t_num_str args, t_str *out)
return (ERROR); return (ERROR);
return (*out = res, NO_ERROR); return (*out = res, NO_ERROR);
} }