From 92796351f3117cb3897db4a5116d93c72187dba4 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 15 Feb 2024 17:59:03 +0100 Subject: [PATCH] first push ? --- .gitignore | 6 + Makefile | 276 +++++++++++++++++++++ include/app/best_index_to_move.h | 30 +++ include/app/best_move.h | 43 ++++ include/app/best_move_inner.h | 58 +++++ include/app/find_iter.h | 22 ++ include/app/find_place.h | 29 +++ include/app/iter_state.h | 24 ++ include/app/moves.h | 23 ++ include/app/rotate.h | 56 +++++ include/app/state.h | 73 ++++++ include/app/target.h | 21 ++ include/app/types/type_i64_bool.h | 24 ++ include/app/types/type_move.h | 31 +++ include/me/buffered_str/buf_str.h | 49 ++++ include/me/char/isalnum.h | 20 ++ include/me/char/isalpha.h | 20 ++ include/me/char/isascii.h | 20 ++ include/me/char/isdigit.h | 20 ++ include/me/char/islower.h | 20 ++ include/me/char/isprint.h | 20 ++ include/me/char/isspace.h | 20 ++ include/me/char/isupper.h | 20 ++ include/me/char/tolower.h | 20 ++ include/me/char/toupper.h | 20 ++ include/me/convert/atoi.h | 21 ++ include/me/convert/itoa.h | 20 ++ include/me/convert/str_to_numbers.h | 30 +++ include/me/fs/close.h | 20 ++ include/me/fs/open.h | 22 ++ include/me/fs/putchar_fd.h | 20 ++ include/me/fs/putendl_fd.h | 20 ++ include/me/fs/putnbr_fd.h | 20 ++ include/me/fs/putstr_fd.h | 20 ++ include/me/fs/read.h | 20 ++ include/me/fs/read_to_vec.h | 25 ++ include/me/fs/write.h | 20 ++ include/me/gnl/gnl.h | 43 ++++ include/me/hash/hasher.h | 52 ++++ include/me/hash/sip.h | 38 +++ include/me/hash/sip/sip_utils.h | 24 ++ include/me/img/qoi.h | 305 ++++++++++++++++++++++++ include/me/img/qoi/qoi_decode.h | 86 +++++++ include/me/img/qoi/qoi_encode.h | 155 ++++++++++++ include/me/img/qoi/qoi_utils.h | 56 +++++ include/me/list/list_add_back.h | 20 ++ include/me/list/list_add_front.h | 19 ++ include/me/list/list_alloc_node.h | 20 ++ include/me/list/list_free_all.h | 20 ++ include/me/list/list_free_one.h | 20 ++ include/me/list/list_get_last.h | 20 ++ include/me/list/list_iter.h | 20 ++ include/me/list/list_map.h | 20 ++ include/me/list/list_size.h | 20 ++ include/me/mem/mem_alloc.h | 20 ++ include/me/mem/mem_alloc_array.h | 20 ++ include/me/mem/mem_compare.h | 20 ++ include/me/mem/mem_copy.h | 20 ++ include/me/mem/mem_find.h | 20 ++ include/me/mem/mem_find_bytes.h | 21 ++ include/me/mem/mem_move.h | 20 ++ include/me/mem/mem_set.h | 20 ++ include/me/mem/mem_set_zero.h | 20 ++ include/me/num/u16.h | 21 ++ include/me/num/u32.h | 21 ++ include/me/num/u64.h | 24 ++ include/me/num/u8.h | 21 ++ include/me/num/usize.h | 21 ++ include/me/printf/formatter/formatter.h | 27 +++ include/me/printf/formatter/utils.h | 76 ++++++ include/me/printf/matchers/matchers.h | 52 ++++ include/me/printf/printf.h | 69 ++++++ include/me/string/str_clone.h | 20 ++ include/me/string/str_find_chr.h | 20 ++ include/me/string/str_find_rev_chr.h | 20 ++ include/me/string/str_find_str.h | 20 ++ include/me/string/str_iter.h | 20 ++ include/me/string/str_join.h | 20 ++ include/me/string/str_l_cat.h | 20 ++ include/me/string/str_l_copy.h | 20 ++ include/me/string/str_len.h | 20 ++ include/me/string/str_map.h | 20 ++ include/me/string/str_n_compare.h | 20 ++ include/me/string/str_n_find_str.h | 20 ++ include/me/string/str_split.h | 20 ++ include/me/string/str_substring.h | 20 ++ include/me/string/str_trim.h | 20 ++ include/me/types.h | 49 ++++ include/me/vec/vec_buf_str.h | 60 +++++ include/me/vec/vec_i64.h | 51 ++++ include/me/vec/vec_i64_bool.h | 60 +++++ include/me/vec/vec_u8.h | 50 ++++ include/me/vec2/vec2.h | 60 +++++ src/app/actual_main.c | 26 ++ src/app/best_index_to_move.c | 77 ++++++ src/app/best_move.c | 99 ++++++++ src/app/do_move.c | 29 +++ src/app/find_place.c | 52 ++++ src/app/iter_find.c | 57 +++++ src/app/main.c | 64 +++++ src/app/main_bonus.c | 105 ++++++++ src/app/moves.c | 71 ++++++ src/app/moves/push.c | 39 +++ src/app/moves/rev_rotate.c | 44 ++++ src/app/moves/rotate.c | 43 ++++ src/app/moves/swap.c | 47 ++++ src/app/rotate.c | 14 ++ src/app/run_with_items.c | 114 +++++++++ src/app/sort2.c | 33 +++ src/app/sort3.c | 138 +++++++++++ src/app/sort5.c | 139 +++++++++++ src/app/state.c | 91 +++++++ src/app/target.c | 24 ++ src/bonus/move1.c | 11 + src/buffered_str/mod.c | 82 +++++++ src/char/isalnum.c | 20 ++ src/char/isalpha.c | 18 ++ src/char/isascii.c | 18 ++ src/char/isdigit.c | 18 ++ src/char/islower.c | 18 ++ src/char/isprint.c | 18 ++ src/char/isspace.c | 19 ++ src/char/isupper.c | 18 ++ src/char/tolower.c | 22 ++ src/char/toupper.c | 22 ++ src/convert/atoi.c | 61 +++++ src/convert/itoa.c | 69 ++++++ src/convert/str_to_i16.c | 129 ++++++++++ src/convert/str_to_i16_utils.c | 45 ++++ src/convert/str_to_i32.c | 129 ++++++++++ src/convert/str_to_i32_utils.c | 45 ++++ src/convert/str_to_i64.c | 129 ++++++++++ src/convert/str_to_i64_utils.c | 45 ++++ src/convert/str_to_i8.c | 129 ++++++++++ src/convert/str_to_i8_utils.c | 45 ++++ src/convert/str_to_u16.c | 129 ++++++++++ src/convert/str_to_u16_utils.c | 45 ++++ src/convert/str_to_u32.c | 129 ++++++++++ src/convert/str_to_u32_utils.c | 45 ++++ src/convert/str_to_u64.c | 129 ++++++++++ src/convert/str_to_u64_utils.c | 45 ++++ src/convert/str_to_u8.c | 129 ++++++++++ src/convert/str_to_u8_utils.c | 45 ++++ src/fs/close.c | 27 +++ src/fs/open.c | 60 +++++ src/fs/putchar_fd.c | 20 ++ src/fs/putendl_fd.c | 23 ++ src/fs/putnbr_fd.c | 55 +++++ src/fs/putstr_fd.c | 22 ++ src/fs/read.c | 24 ++ src/fs/read_to_vec.c | 43 ++++ src/fs/write.c | 19 ++ src/gnl/get_next_line.c | 128 ++++++++++ src/hash/hash_signed.c | 39 +++ src/hash/hash_unsigned.c | 34 +++ src/hash/hasher.c | 29 +++ src/hash/sip/sip13.c | 31 +++ src/hash/sip/sip_utils.c | 106 ++++++++ src/hash/sip/sip_utils2.c | 68 ++++++ src/img/qoi/qoi_decode.c | 73 ++++++ src/img/qoi/qoi_encode.c | 29 +++ src/img/qoi/qoi_fs.c | 49 ++++ src/img/qoi/qoi_utils.c | 36 +++ src/list/list_add_back.c | 22 ++ src/list/list_add_front.c | 20 ++ src/list/list_alloc_node.c | 26 ++ src/list/list_free_all.c | 30 +++ src/list/list_free_one.c | 22 ++ src/list/list_get_last.c | 25 ++ src/list/list_iter.c | 22 ++ src/list/list_map.c | 40 ++++ src/list/list_size.c | 28 +++ src/mem/mem_alloc.c | 26 ++ src/mem/mem_alloc_array.c | 25 ++ src/mem/mem_compare.c | 31 +++ src/mem/mem_copy.c | 32 +++ src/mem/mem_find.c | 29 +++ src/mem/mem_find_bytes.c | 39 +++ src/mem/mem_move.c | 42 ++++ src/mem/mem_set.c | 27 +++ src/mem/mem_set_zero.c | 27 +++ src/num/u16/rotate.c | 29 +++ src/num/u32/rotate.c | 29 +++ src/num/u64/from_bytes.c | 57 +++++ src/num/u64/rotate.c | 29 +++ src/num/u8/rotate.c | 29 +++ src/num/usize/rotate.c | 29 +++ src/printf/formatter/char.c | 70 ++++++ src/printf/formatter/decimal.c | 89 +++++++ src/printf/formatter/hex.c | 67 ++++++ src/printf/formatter/oct.c | 60 +++++ src/printf/formatter/ptr.c | 74 ++++++ src/printf/formatter/unsigned_decimal.c | 76 ++++++ src/printf/formatter/utils.c | 136 +++++++++++ src/printf/formatter/utils2.c | 96 ++++++++ src/printf/formatter/utils3.c | 53 ++++ src/printf/formatter/utils_numbers.c | 36 +++ src/printf/matchers.c | 120 ++++++++++ src/printf/printf.c | 120 ++++++++++ src/printf/vprintf.c | 51 ++++ src/string/str_clone.c | 30 +++ src/string/str_find_chr.c | 29 +++ src/string/str_find_rev_chr.c | 28 +++ src/string/str_find_str.c | 56 +++++ src/string/str_iter.c | 27 +++ src/string/str_join.c | 34 +++ src/string/str_l_cat.c | 108 +++++++++ src/string/str_l_copy.c | 104 ++++++++ src/string/str_len.c | 23 ++ src/string/str_map.c | 33 +++ src/string/str_n_compare.c | 27 +++ src/string/str_n_find_str.c | 51 ++++ src/string/str_split.c | 93 ++++++++ src/string/str_substring.c | 38 +++ src/string/str_trim.c | 44 ++++ src/vec/vec_buf_str.c | 115 +++++++++ src/vec/vec_buf_str_functions2.c | 110 +++++++++ src/vec/vec_buf_str_functions3.c | 72 ++++++ src/vec/vec_i64.c | 114 +++++++++ src/vec/vec_i64_bool.c | 115 +++++++++ src/vec/vec_i64_bool_functions2.c | 109 +++++++++ src/vec/vec_i64_bool_functions3.c | 72 ++++++ src/vec/vec_i64_bool_sort.c | 41 ++++ src/vec/vec_i64_functions2.c | 106 ++++++++ src/vec/vec_i64_functions3.c | 72 ++++++ src/vec/vec_i64_sort.c | 40 ++++ src/vec/vec_u8.c | 114 +++++++++ src/vec/vec_u8_functions2.c | 106 ++++++++ src/vec/vec_u8_functions3.c | 72 ++++++ 229 files changed, 11077 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 include/app/best_index_to_move.h create mode 100644 include/app/best_move.h create mode 100644 include/app/best_move_inner.h create mode 100644 include/app/find_iter.h create mode 100644 include/app/find_place.h create mode 100644 include/app/iter_state.h create mode 100644 include/app/moves.h create mode 100644 include/app/rotate.h create mode 100644 include/app/state.h create mode 100644 include/app/target.h create mode 100644 include/app/types/type_i64_bool.h create mode 100644 include/app/types/type_move.h create mode 100644 include/me/buffered_str/buf_str.h create mode 100644 include/me/char/isalnum.h create mode 100644 include/me/char/isalpha.h create mode 100644 include/me/char/isascii.h create mode 100644 include/me/char/isdigit.h create mode 100644 include/me/char/islower.h create mode 100644 include/me/char/isprint.h create mode 100644 include/me/char/isspace.h create mode 100644 include/me/char/isupper.h create mode 100644 include/me/char/tolower.h create mode 100644 include/me/char/toupper.h create mode 100644 include/me/convert/atoi.h create mode 100644 include/me/convert/itoa.h create mode 100644 include/me/convert/str_to_numbers.h create mode 100644 include/me/fs/close.h create mode 100644 include/me/fs/open.h create mode 100644 include/me/fs/putchar_fd.h create mode 100644 include/me/fs/putendl_fd.h create mode 100644 include/me/fs/putnbr_fd.h create mode 100644 include/me/fs/putstr_fd.h create mode 100644 include/me/fs/read.h create mode 100644 include/me/fs/read_to_vec.h create mode 100644 include/me/fs/write.h create mode 100644 include/me/gnl/gnl.h create mode 100644 include/me/hash/hasher.h create mode 100644 include/me/hash/sip.h create mode 100644 include/me/hash/sip/sip_utils.h create mode 100644 include/me/img/qoi.h create mode 100644 include/me/img/qoi/qoi_decode.h create mode 100644 include/me/img/qoi/qoi_encode.h create mode 100644 include/me/img/qoi/qoi_utils.h create mode 100644 include/me/list/list_add_back.h create mode 100644 include/me/list/list_add_front.h create mode 100644 include/me/list/list_alloc_node.h create mode 100644 include/me/list/list_free_all.h create mode 100644 include/me/list/list_free_one.h create mode 100644 include/me/list/list_get_last.h create mode 100644 include/me/list/list_iter.h create mode 100644 include/me/list/list_map.h create mode 100644 include/me/list/list_size.h create mode 100644 include/me/mem/mem_alloc.h create mode 100644 include/me/mem/mem_alloc_array.h create mode 100644 include/me/mem/mem_compare.h create mode 100644 include/me/mem/mem_copy.h create mode 100644 include/me/mem/mem_find.h create mode 100644 include/me/mem/mem_find_bytes.h create mode 100644 include/me/mem/mem_move.h create mode 100644 include/me/mem/mem_set.h create mode 100644 include/me/mem/mem_set_zero.h create mode 100644 include/me/num/u16.h create mode 100644 include/me/num/u32.h create mode 100644 include/me/num/u64.h create mode 100644 include/me/num/u8.h create mode 100644 include/me/num/usize.h create mode 100644 include/me/printf/formatter/formatter.h create mode 100644 include/me/printf/formatter/utils.h create mode 100644 include/me/printf/matchers/matchers.h create mode 100644 include/me/printf/printf.h create mode 100644 include/me/string/str_clone.h create mode 100644 include/me/string/str_find_chr.h create mode 100644 include/me/string/str_find_rev_chr.h create mode 100644 include/me/string/str_find_str.h create mode 100644 include/me/string/str_iter.h create mode 100644 include/me/string/str_join.h create mode 100644 include/me/string/str_l_cat.h create mode 100644 include/me/string/str_l_copy.h create mode 100644 include/me/string/str_len.h create mode 100644 include/me/string/str_map.h create mode 100644 include/me/string/str_n_compare.h create mode 100644 include/me/string/str_n_find_str.h create mode 100644 include/me/string/str_split.h create mode 100644 include/me/string/str_substring.h create mode 100644 include/me/string/str_trim.h create mode 100644 include/me/types.h create mode 100644 include/me/vec/vec_buf_str.h create mode 100644 include/me/vec/vec_i64.h create mode 100644 include/me/vec/vec_i64_bool.h create mode 100644 include/me/vec/vec_u8.h create mode 100644 include/me/vec2/vec2.h create mode 100644 src/app/actual_main.c create mode 100644 src/app/best_index_to_move.c create mode 100644 src/app/best_move.c create mode 100644 src/app/do_move.c create mode 100644 src/app/find_place.c create mode 100644 src/app/iter_find.c create mode 100644 src/app/main.c create mode 100644 src/app/main_bonus.c create mode 100644 src/app/moves.c create mode 100644 src/app/moves/push.c create mode 100644 src/app/moves/rev_rotate.c create mode 100644 src/app/moves/rotate.c create mode 100644 src/app/moves/swap.c create mode 100644 src/app/rotate.c create mode 100644 src/app/run_with_items.c create mode 100644 src/app/sort2.c create mode 100644 src/app/sort3.c create mode 100644 src/app/sort5.c create mode 100644 src/app/state.c create mode 100644 src/app/target.c create mode 100644 src/bonus/move1.c create mode 100644 src/buffered_str/mod.c create mode 100644 src/char/isalnum.c create mode 100644 src/char/isalpha.c create mode 100644 src/char/isascii.c create mode 100644 src/char/isdigit.c create mode 100644 src/char/islower.c create mode 100644 src/char/isprint.c create mode 100644 src/char/isspace.c create mode 100644 src/char/isupper.c create mode 100644 src/char/tolower.c create mode 100644 src/char/toupper.c create mode 100644 src/convert/atoi.c create mode 100644 src/convert/itoa.c create mode 100644 src/convert/str_to_i16.c create mode 100644 src/convert/str_to_i16_utils.c create mode 100644 src/convert/str_to_i32.c create mode 100644 src/convert/str_to_i32_utils.c create mode 100644 src/convert/str_to_i64.c create mode 100644 src/convert/str_to_i64_utils.c create mode 100644 src/convert/str_to_i8.c create mode 100644 src/convert/str_to_i8_utils.c create mode 100644 src/convert/str_to_u16.c create mode 100644 src/convert/str_to_u16_utils.c create mode 100644 src/convert/str_to_u32.c create mode 100644 src/convert/str_to_u32_utils.c create mode 100644 src/convert/str_to_u64.c create mode 100644 src/convert/str_to_u64_utils.c create mode 100644 src/convert/str_to_u8.c create mode 100644 src/convert/str_to_u8_utils.c create mode 100644 src/fs/close.c create mode 100644 src/fs/open.c create mode 100644 src/fs/putchar_fd.c create mode 100644 src/fs/putendl_fd.c create mode 100644 src/fs/putnbr_fd.c create mode 100644 src/fs/putstr_fd.c create mode 100644 src/fs/read.c create mode 100644 src/fs/read_to_vec.c create mode 100644 src/fs/write.c create mode 100644 src/gnl/get_next_line.c create mode 100644 src/hash/hash_signed.c create mode 100644 src/hash/hash_unsigned.c create mode 100644 src/hash/hasher.c create mode 100644 src/hash/sip/sip13.c create mode 100644 src/hash/sip/sip_utils.c create mode 100644 src/hash/sip/sip_utils2.c create mode 100644 src/img/qoi/qoi_decode.c create mode 100644 src/img/qoi/qoi_encode.c create mode 100644 src/img/qoi/qoi_fs.c create mode 100644 src/img/qoi/qoi_utils.c create mode 100644 src/list/list_add_back.c create mode 100644 src/list/list_add_front.c create mode 100644 src/list/list_alloc_node.c create mode 100644 src/list/list_free_all.c create mode 100644 src/list/list_free_one.c create mode 100644 src/list/list_get_last.c create mode 100644 src/list/list_iter.c create mode 100644 src/list/list_map.c create mode 100644 src/list/list_size.c create mode 100644 src/mem/mem_alloc.c create mode 100644 src/mem/mem_alloc_array.c create mode 100644 src/mem/mem_compare.c create mode 100644 src/mem/mem_copy.c create mode 100644 src/mem/mem_find.c create mode 100644 src/mem/mem_find_bytes.c create mode 100644 src/mem/mem_move.c create mode 100644 src/mem/mem_set.c create mode 100644 src/mem/mem_set_zero.c create mode 100644 src/num/u16/rotate.c create mode 100644 src/num/u32/rotate.c create mode 100644 src/num/u64/from_bytes.c create mode 100644 src/num/u64/rotate.c create mode 100644 src/num/u8/rotate.c create mode 100644 src/num/usize/rotate.c create mode 100644 src/printf/formatter/char.c create mode 100644 src/printf/formatter/decimal.c create mode 100644 src/printf/formatter/hex.c create mode 100644 src/printf/formatter/oct.c create mode 100644 src/printf/formatter/ptr.c create mode 100644 src/printf/formatter/unsigned_decimal.c create mode 100644 src/printf/formatter/utils.c create mode 100644 src/printf/formatter/utils2.c create mode 100644 src/printf/formatter/utils3.c create mode 100644 src/printf/formatter/utils_numbers.c create mode 100644 src/printf/matchers.c create mode 100644 src/printf/printf.c create mode 100644 src/printf/vprintf.c create mode 100644 src/string/str_clone.c create mode 100644 src/string/str_find_chr.c create mode 100644 src/string/str_find_rev_chr.c create mode 100644 src/string/str_find_str.c create mode 100644 src/string/str_iter.c create mode 100644 src/string/str_join.c create mode 100644 src/string/str_l_cat.c create mode 100644 src/string/str_l_copy.c create mode 100644 src/string/str_len.c create mode 100644 src/string/str_map.c create mode 100644 src/string/str_n_compare.c create mode 100644 src/string/str_n_find_str.c create mode 100644 src/string/str_split.c create mode 100644 src/string/str_substring.c create mode 100644 src/string/str_trim.c create mode 100644 src/vec/vec_buf_str.c create mode 100644 src/vec/vec_buf_str_functions2.c create mode 100644 src/vec/vec_buf_str_functions3.c create mode 100644 src/vec/vec_i64.c create mode 100644 src/vec/vec_i64_bool.c create mode 100644 src/vec/vec_i64_bool_functions2.c create mode 100644 src/vec/vec_i64_bool_functions3.c create mode 100644 src/vec/vec_i64_bool_sort.c create mode 100644 src/vec/vec_i64_functions2.c create mode 100644 src/vec/vec_i64_functions3.c create mode 100644 src/vec/vec_i64_sort.c create mode 100644 src/vec/vec_u8.c create mode 100644 src/vec/vec_u8_functions2.c create mode 100644 src/vec/vec_u8_functions3.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41e62e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +build +/checker +/push_swap +*.list +*.o +*.d diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d12c11a --- /dev/null +++ b/Makefile @@ -0,0 +1,276 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: maiboyer +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # +# Updated: 2024/02/15 17:58:38 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +define_module = $(addprefix $(1)/, $(2)) + +BUILD_DIR = build +SRC_DIR = src +INCLUDE_DIR = include +LIBS_DIR = . +GENERIC_DIR = +GENERIC_INCLUDE = + +NAME = push_swap + +ifeq ($(MAKECMDGOALS), bonus) + CFLAGS += -DBONUS=1 + BUILD_DIR := $(BUILD_DIR)/bonus/ + NAME = checker +endif + +LIB_NAME ?= +TARGET = $(NAME) +CC ?= clang +CFLAGS += -Wall -Werror -Wextra -g3 -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD +BONUS_FILES = +LIBS_NAME = +SUBJECT_URL = 'https://cdn.intra.42.fr/pdf/pdf/118610/en.subject.pdf' + +GENERIC_FILES = +SRC_FILES = \ +app/actual_main \ +app/best_index_to_move \ +app/best_move \ +app/do_move \ +app/find_place \ +app/iter_find \ +app/main \ +app/main_bonus \ +app/moves \ +app/moves/push \ +app/moves/rev_rotate \ +app/moves/rotate \ +app/moves/swap \ +app/rotate \ +app/run_with_items \ +app/sort2 \ +app/sort3 \ +app/sort5 \ +app/state \ +app/target \ +bonus/move1 \ +buffered_str/mod \ +char/isalnum \ +char/isalpha \ +char/isascii \ +char/isdigit \ +char/islower \ +char/isprint \ +char/isspace \ +char/isupper \ +char/tolower \ +char/toupper \ +convert/atoi \ +convert/itoa \ +convert/str_to_i16 \ +convert/str_to_i16_utils \ +convert/str_to_i32 \ +convert/str_to_i32_utils \ +convert/str_to_i64 \ +convert/str_to_i64_utils \ +convert/str_to_i8 \ +convert/str_to_i8_utils \ +convert/str_to_u16 \ +convert/str_to_u16_utils \ +convert/str_to_u32 \ +convert/str_to_u32_utils \ +convert/str_to_u64 \ +convert/str_to_u64_utils \ +convert/str_to_u8 \ +convert/str_to_u8_utils \ +fs/close \ +fs/open \ +fs/putchar_fd \ +fs/putendl_fd \ +fs/putnbr_fd \ +fs/putstr_fd \ +fs/read \ +fs/read_to_vec \ +fs/write \ +gnl/get_next_line \ +hash/hasher \ +hash/hash_signed \ +hash/hash_unsigned \ +hash/sip/sip13 \ +hash/sip/sip_utils \ +hash/sip/sip_utils2 \ +img/qoi/qoi_decode \ +img/qoi/qoi_encode \ +img/qoi/qoi_fs \ +img/qoi/qoi_utils \ +list/list_add_back \ +list/list_add_front \ +list/list_alloc_node \ +list/list_free_all \ +list/list_free_one \ +list/list_get_last \ +list/list_iter \ +list/list_map \ +list/list_size \ +mem/mem_alloc \ +mem/mem_alloc_array \ +mem/mem_compare \ +mem/mem_copy \ +mem/mem_find \ +mem/mem_find_bytes \ +mem/mem_move \ +mem/mem_set \ +mem/mem_set_zero \ +num/u16/rotate \ +num/u32/rotate \ +num/u64/from_bytes \ +num/u64/rotate \ +num/u8/rotate \ +num/usize/rotate \ +printf/formatter/char \ +printf/formatter/decimal \ +printf/formatter/hex \ +printf/formatter/oct \ +printf/formatter/ptr \ +printf/formatter/unsigned_decimal \ +printf/formatter/utils \ +printf/formatter/utils2 \ +printf/formatter/utils3 \ +printf/formatter/utils_numbers \ +printf/matchers \ +printf/printf \ +printf/vprintf \ +string/str_clone \ +string/str_find_chr \ +string/str_find_rev_chr \ +string/str_find_str \ +string/str_iter \ +string/str_join \ +string/str_l_cat \ +string/str_l_copy \ +string/str_len \ +string/str_map \ +string/str_n_compare \ +string/str_n_find_str \ +string/str_split \ +string/str_substring \ +string/str_trim \ +vec/vec_buf_str \ +vec/vec_buf_str_functions2 \ +vec/vec_buf_str_functions3 \ +vec/vec_i64 \ +vec/vec_i64_bool \ +vec/vec_i64_bool_functions2 \ +vec/vec_i64_bool_functions3 \ +vec/vec_i64_bool_sort \ +vec/vec_i64_functions2 \ +vec/vec_i64_functions3 \ +vec/vec_i64_sort \ +vec/vec_u8 \ +vec/vec_u8_functions2 \ +vec/vec_u8_functions3 + +BONUS = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(BONUS_FILES))) +SRC = $(addsuffix .c,$(addprefix $(SRC_DIR)/,$(SRC_FILES))) \ + $(addsuffix .c,$(addprefix $(GENERIC_DIR)/,$(GENERIC_FILES))) +BONUS_OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(BONUS_FILES))) +OBJ = $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .o,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +DEPS = $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ + $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) +LIBS = $(addprefix $(LIBS_DIR)/,$(LIBS_NAME)) +INCLUDES = $(addprefix -I,$(INCLUDE_DIR) $(GENERIC_INCLUDE) $(LIBS) $(addsuffix /include,$(LIBS))) +COL_GRAY = \\e[90m +COL_WHITE = \\e[37m +COL_GREEN = \\e[32m +COL_BOLD = \\e[1m +COL_RESET = \\e[0m + + +.PHONY: remove_name clean all fclean bonus format re generate_filelist subject get_lib + +all: $(NAME) + +bonus: all + @printf \\n$(COL_GRAY)Building\ Output\ \(with\ bonus\)\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +get_lib: + @printf $(LIB_NAME)/$(NAME) + +$(NAME): $(OBJ) libs_build + @printf \\n$(COL_GRAY)Building\ Output\ $(COL_WHITE)$(COL_BOLD)%-28s$(COL_RESET)\ \ + $(NAME) + @$(CC) $(INCLUDES) $(OBJ) $(CFLAGS) -o $(NAME) + @#ar rcs $(BUILD_DIR)/$(NAME) $(OBJ) + @printf $(COL_GREEN)done$(COL_RESET)\\n + +libs_build: + @- $(foreach LIB,$(LIBS),\ + printf \\n; \ + printf $(COL_GRAY)Building\ library\ $(COL_RESET); \ + printf $(COL_WHITE)$(COL_BOLD)%-25s$(COL_RESET)\\n $(LIB); \ + make LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory all; \ + printf \\n; \ + ) + +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +$(BUILD_DIR)/%.o: $(GENERIC_DIR)/%.c + @mkdir -p $(dir $@) + @printf $(COL_GRAY)Building\ $(COL_BOLD)$(COL_WHITE)%-50s\ $(LIB_NAME)$< + @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ + @printf $(COL_RESET)$(COL_GREEN)done$(COL_RESET)\\n + +clean: + @- $(foreach LIB,$(LIBS), \ + make clean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @- $(if $(LIB_NAME),,\ + printf $(COL_WHITE)Clearing\ Artefacts\ ; \ + printf $(COL_GRAY)\%-25s$(COL_RESET)\ \($(BUILD_DIR)\); \ + rm -rf $(BUILD_DIR); \ + printf $(COL_GREEN)done$(COL_RESET)\\n; \ + ) + @echo >/dev/null + +fclean: clean + @- $(foreach LIB,$(LIBS), \ + make fclean LIB_NAME=$(LIB)/ BUILD_DIR=$(realpath $(BUILD_DIR)) -C $(LIB) --no-print-directory || true;\ + ) + @printf $(COL_WHITE)Clearing\ Output\ $(COL_GRAY)%-28s$(COL_RESET)\ \ + \($(LIB_NAME)$(NAME)\) + @rm -f $(BUILD_DIR)$(NAME) + @rm -f push_swap + @rm -f checker + @printf $(COL_GREEN)done$(COL_RESET)\\n + +re: fclean all + +generate_filelist: + @/usr/bin/env zsh -c "tree -iFf --noreport output | rg '^output/src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./gen.list + @/usr/bin/env zsh -c "tree -iFf --noreport src | rg '^src/(.*)\.c\$$' --replace '\$$1' | sort -u" > ./src.list + +format: + @zsh -c "c_formatter_42 **/*.c **/*.h" + +subject: subject.txt + @bat --plain subject.txt + +subject.txt: + @curl $(SUBJECT_URL) | pdftotext -layout -nopgbrk - subject.txt + +fuck_raphael: + @echo "Oh que oui~~~\net jte nioc" + +-include $(DEPS) + diff --git a/include/app/best_index_to_move.h b/include/app/best_index_to_move.h new file mode 100644 index 0000000..53e5ed7 --- /dev/null +++ b/include/app/best_index_to_move.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:18:43 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:20:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_INDEX_TO_MOVE_H +# define BEST_INDEX_TO_MOVE_H + +# include "app/best_move.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +struct s_best_index_to_move +{ + t_state *state; + t_vec_i64 *from; + t_vec_i64 *to; + t_iter_pos_func f; + t_usize i; +}; + +#endif /* BEST_INDEX_TO_MOVE_H */ diff --git a/include/app/best_move.h b/include/app/best_move.h new file mode 100644 index 0000000..f78fb2b --- /dev/null +++ b/include/app/best_move.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 13:30:30 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_H +# define BEST_MOVE_H + +# include "app/state.h" +# include "me/vec/vec_i64.h" + +struct s_functions +{ + void (*forward)(void *); + void (*reverse)(void *); +}; + +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); + +typedef struct s_best_move_args +{ + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; + bool print_stuff; +} t_best_move_args; + +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data); + +#endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h new file mode 100644 index 0000000..902c125 --- /dev/null +++ b/include/app/best_move_inner.h @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move_inner.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_MOVE_INNER_H +# define BEST_MOVE_INNER_H + +# include "app/best_move.h" +# include "app/rotate.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +typedef void (*t_banana_func)(void *); + +static inline t_banana_func choose_func(struct s_functions *funcs, + t_rotation rotate) +{ + if (rotate.direction == FORWARD) + return (funcs->forward); + else if (rotate.direction == REVERSE) + return (funcs->reverse); + return (NULL); +} + +static inline t_isize abs_diff(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (lhs - rhs); + else + return (rhs - lhs); +} + +static inline t_isize min(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (rhs); + else + return (lhs); +} + +static inline t_isize max(t_isize lhs, t_isize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} + +#endif /* BEST_MOVE_INNER_H */ diff --git a/include/app/find_iter.h b/include/app/find_iter.h new file mode 100644 index 0000000..ff9b460 --- /dev/null +++ b/include/app/find_iter.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:24:10 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:24:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_ITER_H +# define FIND_ITER_H + +# include "me/types.h" +# include "me/vec/vec_i64.h" + +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); + +#endif /* FIND_ITER_H */ diff --git a/include/app/find_place.h b/include/app/find_place.h new file mode 100644 index 0000000..234e7b1 --- /dev/null +++ b/include/app/find_place.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:00:27 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 22:15:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIND_PLACE_H +# define FIND_PLACE_H + +# include "app/state.h" +# include "me/types.h" + +typedef struct s_find_place_iter_state +{ + t_usize current_index; + t_usize found_index; + t_i64 to_find_elem; + +} t_find_place_iter_state; + +t_usize find_place(t_i64 elem, t_state *state); + +#endif /* FIND_PLACE_H */ diff --git a/include/app/iter_state.h b/include/app/iter_state.h new file mode 100644 index 0000000..acc14be --- /dev/null +++ b/include/app/iter_state.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:19:06 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:19:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITER_STATE_H +# define ITER_STATE_H + +# include "me/types.h" + +typedef struct s_iter_state +{ + t_usize pos; + t_i64 elem; +} t_iter_state; + +#endif /* ITER_STATE_H */ diff --git a/include/app/moves.h b/include/app/moves.h new file mode 100644 index 0000000..db9b4f7 --- /dev/null +++ b/include/app/moves.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:07:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MOVES_H +# define MOVES_H + +# include "app/state.h" +# include "app/types/type_move.h" +# include "me/types.h" + +void do_move(t_move m, t_state *s); +t_const_str get_str_for_move(t_move m); + +#endif /* MOVES_H */ diff --git a/include/app/rotate.h b/include/app/rotate.h new file mode 100644 index 0000000..f393485 --- /dev/null +++ b/include/app/rotate.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 13:33:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROTATE_H +# define ROTATE_H + +# include "me/types.h" + +enum e_rotation_direction +{ + FORWARD, + REVERSE, +}; +typedef struct s_rotation +{ + t_isize value; + t_isize ring_size; + enum e_rotation_direction direction; +} t_rotation; + +static inline t_rotation forward(t_isize by, t_isize ring_size) +{ + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = FORWARD}); +} + +static inline t_rotation reverse(t_isize by, t_isize ring_size) +{ + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = REVERSE}); +} + +static inline t_rotation flip(t_rotation rot) +{ + enum e_rotation_direction flipped; + + if (rot.direction == FORWARD) + flipped = REVERSE; + else if (rot.direction == REVERSE) + flipped = FORWARD; + else + return (rot); + return ((t_rotation){.value = rot.ring_size - rot.value, + .ring_size = rot.ring_size, .direction = flipped}); +} + +#endif /* ROTATE_H */ diff --git a/include/app/state.h b/include/app/state.h new file mode 100644 index 0000000..2261e43 --- /dev/null +++ b/include/app/state.h @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:05:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STATE_H +# define STATE_H + +# include "me/types.h" +# include "me/vec/vec_i64.h" +# include "me/vec/vec_i64_bool.h" + +# ifndef BONUS +# define BONUS 0 +# endif + +typedef struct s_state +{ + t_vec_i64_bool sorted; + t_vec_i64 stack_a; + t_vec_i64 stack_b; +} t_state; + +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); + +static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + if (s->sorted.buffer[i].value == elem) + { + s->sorted.buffer[i].active = true; + return ; + } + i++; + } +} + +static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) +{ + t_usize i; + + i = 0; + while (i < stack->len) + { + make_sorted_true_for_elem(s, stack->buffer[i++]); + } +} + +static inline void make_sorted_all_false(t_state *s) +{ + t_usize i; + + i = 0; + while (i < s->sorted.len) + { + s->sorted.buffer[i].active = false; + i++; + } +} + +#endif /* STATE_H */ diff --git a/include/app/target.h b/include/app/target.h new file mode 100644 index 0000000..204044e --- /dev/null +++ b/include/app/target.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:43 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:31:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TARGET_H +# define TARGET_H + +# include "app/rotate.h" +# include "me/types.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size); + +#endif /* TARGET_H */ diff --git a/include/app/types/type_i64_bool.h b/include/app/types/type_i64_bool.h new file mode 100644 index 0000000..48af825 --- /dev/null +++ b/include/app/types/type_i64_bool.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_i64_bool.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 19:14:52 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 19:15:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_I64_BOOL_H +# define TYPE_I64_BOOL_H + +# include "me/types.h" + +typedef struct s_i64_bool +{ + t_i64 value; + bool active; +} t_i64_bool; + +#endif /* TYPE_I64_BOOL_H */ diff --git a/include/app/types/type_move.h b/include/app/types/type_move.h new file mode 100644 index 0000000..761a82a --- /dev/null +++ b/include/app/types/type_move.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* type_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/12 21:26:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/12 21:27:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPE_MOVE_H +# define TYPE_MOVE_H + +typedef enum e_move +{ + SWAP_A = 1 << 0, + SWAP_B = 1 << 1, + SWAP_BOTH = SWAP_A | SWAP_B, + PUSH_A = 1 << 2, + PUSH_B = 1 << 3, + ROTATE_A = 1 << 4, + ROTATE_B = 1 << 5, + ROTATE_BOTH = ROTATE_A | ROTATE_B, + REVERSE_ROTATE_A = 1 << 6, + REVERSE_ROTATE_B = 1 << 7, + REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B, +} t_move; + +#endif /* TYPE_MOVE_H */ diff --git a/include/me/buffered_str/buf_str.h b/include/me/buffered_str/buf_str.h new file mode 100644 index 0000000..6bf1092 --- /dev/null +++ b/include/me/buffered_str/buf_str.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* buf_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:54:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/31 15:34:29 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUF_STR_H +# define BUF_STR_H +# include "me/types.h" + +typedef struct s_buffer_str +{ + t_str buf; + t_usize capacity; + t_usize len; +} t_buffer_str; + +bool push_str_buffer(t_buffer_str *buf, t_const_str to_push); +bool push_str_char(t_buffer_str *buf, char to_push); +void str_clear(t_buffer_str *buf); +t_buffer_str alloc_new_buffer(t_usize capacity); + +static inline void str_free(t_buffer_str buf) +{ + void free(void *); + + free(buf.buf); +} + +static inline char str_pop(t_buffer_str *buf) +{ + char c; + + c = '\0'; + if (buf->buf && buf->len) + { + c = buf->buf[buf->len - 1]; + buf->buf[buf->len - 1] = '\0'; + } + return (c); +} + +#endif diff --git a/include/me/char/isalnum.h b/include/me/char/isalnum.h new file mode 100644 index 0000000..f38c8e8 --- /dev/null +++ b/include/me/char/isalnum.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalnum.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:39 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISALNUM_H +# define ISALNUM_H + +# include "me/types.h" + +bool me_isalnum(char chr); + +#endif diff --git a/include/me/char/isalpha.h b/include/me/char/isalpha.h new file mode 100644 index 0000000..680e197 --- /dev/null +++ b/include/me/char/isalpha.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalpha.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISALPHA_H +# define ISALPHA_H + +# include "me/types.h" + +bool me_isalpha(char chr); + +#endif diff --git a/include/me/char/isascii.h b/include/me/char/isascii.h new file mode 100644 index 0000000..dfa1961 --- /dev/null +++ b/include/me/char/isascii.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isascii.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:51:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:39:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISASCII_H +# define ISASCII_H + +# include "me/types.h" + +bool me_isascii(char chr); + +#endif diff --git a/include/me/char/isdigit.h b/include/me/char/isdigit.h new file mode 100644 index 0000000..fbed927 --- /dev/null +++ b/include/me/char/isdigit.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isdigit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:41:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISDIGIT_H +# define ISDIGIT_H + +# include "me/types.h" + +bool me_isdigit(char chr); + +#endif diff --git a/include/me/char/islower.h b/include/me/char/islower.h new file mode 100644 index 0000000..de8eedf --- /dev/null +++ b/include/me/char/islower.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* islower.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:48:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISLOWER_H +# define ISLOWER_H + +# include "me/types.h" + +bool me_islower(char chr); + +#endif diff --git a/include/me/char/isprint.h b/include/me/char/isprint.h new file mode 100644 index 0000000..30c6b6f --- /dev/null +++ b/include/me/char/isprint.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isprint.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:44:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISPRINT_H +# define ISPRINT_H + +# include "me/types.h" + +bool me_isprint(char chr); + +#endif diff --git a/include/me/char/isspace.h b/include/me/char/isspace.h new file mode 100644 index 0000000..b2aa5c5 --- /dev/null +++ b/include/me/char/isspace.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isspace.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:26:25 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:02 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISSPACE_H +# define ISSPACE_H + +# include "me/types.h" + +bool me_isspace(char chr); + +#endif diff --git a/include/me/char/isupper.h b/include/me/char/isupper.h new file mode 100644 index 0000000..4c485dd --- /dev/null +++ b/include/me/char/isupper.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isupper.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:16 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ISUPPER_H +# define ISUPPER_H + +# include "me/types.h" + +bool me_isupper(char chr); + +#endif diff --git a/include/me/char/tolower.h b/include/me/char/tolower.h new file mode 100644 index 0000000..764f5a4 --- /dev/null +++ b/include/me/char/tolower.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tolower.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TOLOWER_H +# define TOLOWER_H + +# include "me/types.h" + +bool me_tolower(char chr); + +#endif diff --git a/include/me/char/toupper.h b/include/me/char/toupper.h new file mode 100644 index 0000000..0e5a3a0 --- /dev/null +++ b/include/me/char/toupper.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toupper.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:45:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TOUPPER_H +# define TOUPPER_H + +# include "me/types.h" + +bool me_toupper(char chr); + +#endif diff --git a/include/me/convert/atoi.h b/include/me/convert/atoi.h new file mode 100644 index 0000000..1cce430 --- /dev/null +++ b/include/me/convert/atoi.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* atoi.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:14:00 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 15:36:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ATOI_H +# define ATOI_H + +# include "me/types.h" + +t_i32 me_atoi(t_const_str str); +t_i64 me_atoi_64(t_const_str str); + +#endif diff --git a/include/me/convert/itoa.h b/include/me/convert/itoa.h new file mode 100644 index 0000000..a4afbf6 --- /dev/null +++ b/include/me/convert/itoa.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* itoa.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/03 21:05:46 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:48:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITOA_H +# define ITOA_H + +# include "me/types.h" + +t_str me_itoa(t_i32 nb); + +#endif diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h new file mode 100644 index 0000000..9f90ddf --- /dev/null +++ b/include/me/convert/str_to_numbers.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_numbers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:08:13 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 00:22:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_TO_NUMBERS_H +# define STR_TO_NUMBERS_H + +# include "me/types.h" + +t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); +t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); +t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); +t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); +t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); + +t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); +t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); +t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); +t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); +t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); + +#endif /* STR_TO_NUMBERS_H */ diff --git a/include/me/fs/close.h b/include/me/fs/close.h new file mode 100644 index 0000000..c764d79 --- /dev/null +++ b/include/me/fs/close.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* close.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:49:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLOSE_H +# define CLOSE_H + +# include "me/types.h" + +bool me_close(t_file file, t_i32 *error); + +#endif diff --git a/include/me/fs/open.h b/include/me/fs/open.h new file mode 100644 index 0000000..1f2f8f0 --- /dev/null +++ b/include/me/fs/open.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */ +/* Updated: 2024/01/05 16:43:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef OPEN_H +# define OPEN_H + +# include "me/types.h" + +t_error me_open(t_const_str path, bool read, bool write, t_file *file_out); +t_error me_open_truncate(t_const_str path, t_file *file_out); +t_error me_open_create(t_const_str path, t_file *file_out); + +#endif diff --git a/include/me/fs/putchar_fd.h b/include/me/fs/putchar_fd.h new file mode 100644 index 0000000..03039ac --- /dev/null +++ b/include/me/fs/putchar_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putchar_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:49:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTCHAR_FD_H +# define PUTCHAR_FD_H + +# include "me/types.h" + +void me_putchar_fd(char chr, t_file file); + +#endif diff --git a/include/me/fs/putendl_fd.h b/include/me/fs/putendl_fd.h new file mode 100644 index 0000000..0325e1e --- /dev/null +++ b/include/me/fs/putendl_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putendl_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:31:54 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTENDL_FD_H +# define PUTENDL_FD_H + +# include "me/types.h" + +void me_putendl_fd(t_str str, t_file file); + +#endif \ No newline at end of file diff --git a/include/me/fs/putnbr_fd.h b/include/me/fs/putnbr_fd.h new file mode 100644 index 0000000..933e945 --- /dev/null +++ b/include/me/fs/putnbr_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putnbr_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:32:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTNBR_FD_H +# define PUTNBR_FD_H + +# include "me/types.h" + +void me_putnbr_fd(t_i32 n, t_file file); + +#endif \ No newline at end of file diff --git a/include/me/fs/putstr_fd.h b/include/me/fs/putstr_fd.h new file mode 100644 index 0000000..5ada222 --- /dev/null +++ b/include/me/fs/putstr_fd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putstr_fd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:35:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUTSTR_FD_H +# define PUTSTR_FD_H + +# include "me/types.h" + +void me_putstr_fd(t_str str, t_file file); + +#endif diff --git a/include/me/fs/read.h b/include/me/fs/read.h new file mode 100644 index 0000000..7fc2cd7 --- /dev/null +++ b/include/me/fs/read.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:37:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef READ_H +# define READ_H + +# include "me/types.h" + +t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out); + +#endif \ No newline at end of file diff --git a/include/me/fs/read_to_vec.h b/include/me/fs/read_to_vec.h new file mode 100644 index 0000000..94375ae --- /dev/null +++ b/include/me/fs/read_to_vec.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_to_vec.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:50:37 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 18:57:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef READ_TO_VEC_H +# define READ_TO_VEC_H + +# include "me/types.h" +# include "me/vec/vec_u8.h" + +# ifndef READ_BUFFER_SIZE +# define READ_BUFFER_SIZE 4096 +# endif + +bool read_to_vec(t_const_str path, t_vec_u8 *out); + +#endif diff --git a/include/me/fs/write.h b/include/me/fs/write.h new file mode 100644 index 0000000..66ea307 --- /dev/null +++ b/include/me/fs/write.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* write.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:36:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WRITE_H +# define WRITE_H + +# include "me/types.h" + +bool me_write(t_file fd, t_u8 *buffer, t_i64 size); + +#endif diff --git a/include/me/gnl/gnl.h b/include/me/gnl/gnl.h new file mode 100644 index 0000000..2933c2d --- /dev/null +++ b/include/me/gnl/gnl.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gnl.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 14:40:15 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:21:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GNL_H +# define GNL_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 128 +# endif + +# ifndef BUFFER_LENGTH +# define BUFFER_LENGTH 512 +# endif + +# include "me/buffered_str/buf_str.h" +# include "me/types.h" + +typedef struct s_static_buffer +{ + t_file fd; + bool used; + char buf[BUFFER_SIZE + 1]; + bool init; +} t_static_buffer; + +typedef struct s_copy_flags +{ + bool error; + bool empty_read; +} t_copy_flags; + +t_buffer_str get_next_line(t_file fd, bool *error); + +#endif diff --git a/include/me/hash/hasher.h b/include/me/hash/hasher.h new file mode 100644 index 0000000..d9a9c1c --- /dev/null +++ b/include/me/hash/hasher.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hasher.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:50:39 by maix #+# #+# */ +/* Updated: 2023/12/27 16:44:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HASHER_H +# define HASHER_H + +# include "me/types.h" + +typedef void (*t_hash_bytes)(void *hasher, t_u8 *bytes, + t_usize count); +typedef t_u64 (*t_hasher_finish)(void *hasher); +typedef t_u64 (*t_hasher_reset_and_finish)(void *hasher); + +typedef struct s_hasher +{ + void *hasher; + t_hash_bytes hash_bytes; + t_hasher_finish finish; + t_hasher_reset_and_finish reset_and_finish; + +} t_hasher; + +void hasher_write_bytes(t_hasher *hasher, + t_u8 *bytes, t_usize count); + +void hasher_write_u8(t_hasher *hasher, t_u8 n); +void hasher_write_u16(t_hasher *hasher, t_u16 n); +void hasher_write_u32(t_hasher *hasher, t_u32 n); +void hasher_write_u64(t_hasher *hasher, t_u64 n); +void hasher_write_usize(t_hasher *hasher, t_usize n); + +void hasher_write_i8(t_hasher *hasher, t_i8 n); +void hasher_write_i16(t_hasher *hasher, t_i16 n); +void hasher_write_i32(t_hasher *hasher, t_i32 n); +void hasher_write_i64(t_hasher *hasher, t_i64 n); +void hasher_write_isize(t_hasher *hasher, t_isize n); + +void hasher_write_str(t_hasher *hasher, t_str str); + +t_u64 hasher_finish(t_hasher *hasher); +t_u64 hasher_reset_and_finish(t_hasher *hasher); + +#endif diff --git a/include/me/hash/sip.h b/include/me/hash/sip.h new file mode 100644 index 0000000..3fed310 --- /dev/null +++ b/include/me/hash/sip.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:33:46 by maix #+# #+# */ +/* Updated: 2023/12/11 15:10:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SIP_H +# define SIP_H +# include "me/hash/hasher.h" +# include "me/types.h" + +typedef struct s_sip_state +{ + t_u64 v0; + t_u64 v2; + t_u64 v1; + t_u64 v3; +} t_sip_state; + +typedef struct s_sip13 +{ + t_u64 k0; + t_u64 k1; + t_usize length; + t_u64 tail; + t_usize ntail; + t_sip_state state; +} t_sip13; + +t_hasher hasher_sip13_new(void); + +#endif diff --git a/include/me/hash/sip/sip_utils.h b/include/me/hash/sip/sip_utils.h new file mode 100644 index 0000000..2843533 --- /dev/null +++ b/include/me/hash/sip/sip_utils.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:34:10 by maix #+# #+# */ +/* Updated: 2023/12/27 16:48:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SIP_UTILS_H +# define SIP_UTILS_H +# include "me/hash/sip.h" +# include "me/types.h" + +void compress(t_sip_state *state); +t_sip_state create_state_with_key(t_u64 k0, t_u64 k1); +t_u64 sip13_finish(t_sip13 *hasher); +t_u64 sip13_reset_and_finish(t_sip13 *hasher); +void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count); + +#endif diff --git a/include/me/img/qoi.h b/include/me/img/qoi.h new file mode 100644 index 0000000..d679be6 --- /dev/null +++ b/include/me/img/qoi.h @@ -0,0 +1,305 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 17:39:36 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 18:35:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* + +Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org +SPDX-License-Identifier: MIT + + +QOI - The "Quite OK Image" format for fast, lossless image compression + +-- About + +QOI encodes and decodes images in a lossless format. Compared to stb_image and +stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and +20% better compression. + + +-- Synopsis + +// Define `QOI_IMPLEMENTATION` in *one* C/C++ file before including this +// library to create the implementation. + +#define QOI_IMPLEMENTATION +#include "qoi.h" + +// Encode and store an RGBA buffer to the file system. The t_qoi_desc describes +// the input pixel data. +qoi_write("image_new.qoi", rgba_pixels, &(t_qoi_desc){ + .width = 1920, + .height = 1080, + .channels = 4, + .colorspace = QOI_SRGB +}); + +// Load and decode a QOI image from the file system t_i32o a 32bbp RGBA buffer. +// The t_qoi_desc struct will be filled with the width, height, + number of channels +// and colorspace read from the file header. +t_qoi_desc desc; +void *rgba_pixels = qoi_read("image.qoi", &desc, 4); + + + +-- Documentation + +This library provides the following functions; +- qoi_read -- read and decode a QOI file +- qoi_decode -- decode the raw bytes of a QOI image from memory +- qoi_write -- encode and write a QOI file +- qoi_encode -- encode an rgba buffer t_i32o a QOI image in memory + +See the function declaration below for the signature and more information. + +If you don't want/need the qoi_read and qoi_write functions, you can define +QOI_NO_STDIO before including this library. + +This library uses malloc() and free(). To supply your own malloc implementation +you can define QOI_MALLOC and QOI_FREE before including this library. + +This library uses memset() to zero-initialize the index. To supply your own +implementation you can define QOI_ZEROARR before including this library. + + +-- Data Format + +A QOI file has a 14 byte header, followed by any number of data "chunks" and an +8-byte end marker. + +struct qoi_header_t { + char magic[4]; // magic bytes "qoif" + ut_i3232_t width; // image width in pixels (BE) + ut_i3232_t height; // sprite.height in pixels (BE) + ut_i328_t channels; // 3 = RGB, 4 = RGBA + ut_i328_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels +linear +}; + +Images are encoded row by row, left to right, top to bottom. The decoder and +encoder start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An +image is complete when all pixels specified by width * height have been covered. + +Pixels are encoded as + - a run of the previous pixel + - an index t_i32o an array of previously seen pixels + - a difference to the previous pixel value in r,g,b + - full r,g,b or r,g,b,a values + +The color channels are assumed to not be premultiplied with the alpha channel +("un-premultiplied alpha"). + +A running array[64] (zero-initialized) of previously seen pixel values is +mat_i32ained by the encoder and decoder. Each pixel that is seen by the encoder +and decoder is put t_i32o this array at the position formed by a hash function +of the color value. In the encoder, if the pixel value at the index matches the +current pixel, this index position is written to the stream as QOI_OP_INDEX. +The hash function for the index is: + + index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64 + +Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The +bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All +values encoded in these data bits have the most significant bit on the left. + +The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the +presence of an 8-bit tag first. + +The byte stream's end is marked with 7 0x00 bytes followed a single 0x01 byte. + + +The possible chunks are: + + +.- QOI_OP_INDEX ----------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----------------| +| 0 0 | index | +`-------------------------` +2-bit tag b00 +6-bit index t_i32o the color index array: 0..63 + +A valid encoder must not issue 2 or more consecutive QOI_OP_INDEX chunks to the +same index. QOI_OP_RUN should be used instead. + + +.- QOI_OP_DIFF -----------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----+-----+-----| +| 0 1 | dr | dg | db | +`-------------------------` +2-bit tag b01 +2-bit red channel difference from the previous pixel between -2..1 +2-bit green channel difference from the previous pixel between -2..1 +2-bit blue channel difference from the previous pixel between -2..1 + +The difference to the current channel values are using a wraparound operation, +so "1 - 2" will result in 255, while "255 + 1" will result in 0. + +Values are stored as unsigned t_i32egers with a bias of 2. E.g. -2 is stored as +0 (b00). 1 is stored as 3 (b11). + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_LUMA -------------------------------------. +| Byte[0] | Byte[1] | +| 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | +|-------+-----------------+-------------+-----------| +| 1 0 | green diff | dr - dg | db - dg | +`---------------------------------------------------` +2-bit tag b10 +6-bit green channel difference from the previous pixel -32..31 +4-bit red channel difference minus green channel difference -8..7 +4-bit blue channel difference minus green channel difference -8..7 + +The green channel is used to indicate the general direction of change and is +encoded in 6 bits. The red and blue channels (dr and db) base their diffs off +of the green channel difference and are encoded in 4 bits. I.e.: + dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g) + db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g) + +The difference to the current channel values are using a wraparound operation, +so "10 - 13" will result in 253, while "250 + 7" will result in 1. + +Values are stored as unsigned t_i32egers with a bias of 32 for the green channel +and a bias of 8 for the red and blue channel. + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_RUN ------------. +| Byte[0] | +| 7 6 5 4 3 2 1 0 | +|-------+-----------------| +| 1 1 | run | +`-------------------------` +2-bit tag b11 +6-bit run-length repeating the previous pixel: 1..62 + +The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64 +(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and +QOI_OP_RGBA tags. + + +.- QOI_OP_RGB ------------------------------------------. +| Byte[0] | Byte[1] | Byte[2] | Byte[3] | +| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | +|-------------------------+---------+---------+---------| +| 1 1 1 1 1 1 1 0 | red | green | blue | +`-------------------------------------------------------` +8-bit tag b11111110 +8-bit red channel value +8-bit green channel value +8-bit blue channel value + +The alpha value remains unchanged from the previous pixel. + + +.- QOI_OP_RGBA ---------------------------------------------------. +| Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] | +| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | +|-------------------------+---------+---------+---------+---------| +| 1 1 1 1 1 1 1 1 | red | green | blue | alpha | +`-----------------------------------------------------------------` +8-bit tag b11111111 +8-bit red channel value +8-bit green channel value +8-bit blue channel value +8-bit alpha channel value + +*/ + +/* ----------------------------------------------------------------------------- +Header - Public functions */ + +#ifndef QOI_H +# define QOI_H + +# include "me/mem/mem_alloc.h" +# include "me/mem/mem_set_zero.h" +# include "me/types.h" +/* A pot_i32er to a t_qoi_desc struct has to be supplied to all of qoi's +functions. It describes either the input format (for qoi_write and qoi_encode), +or is filled with the description read from the file header (for qoi_read and +qoi_decode). + +The colorspace in this t_qoi_desc is an enum where + 0 = sRGB, i.e. gamma scaled RGB channels and a linear alpha channel + 1 = all channels are linear +You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely +informative. It will be saved to the file header, but does not affect +how chunks are en-/decoded. */ + +# define QOI_SRGB 0 +# define QOI_LINEAR 1 + +typedef struct s_t_qoi_desc +{ + t_u32 width; + t_u32 height; + t_u8 channels; + t_u8 colorspace; +} t_qoi_desc; + +# ifndef QOI_NO_STDIO + +/* Encode raw RGB or RGBA pixels t_i32o a QOI image and write it to the file +system. The t_qoi_desc struct must be filled with the image width, height, +number of channels (3 = RGB, 4 = RGBA) and the colorspace. + +The function returns 0 on failure (invalid parameters, or fopen or malloc +failed) or the number of bytes written on success. */ + +t_i32 qoi_write(t_const_str filename, const void *data, + const t_qoi_desc *desc); + +/* Read and decode a QOI image from the file system. If channels is 0, the +number of channels from the file header is used. If channels is 3 or 4 the +output format will be forced t_i32o this number of channels. + +The function either returns NULL on failure (invalid data, or malloc or fopen +failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct +will be filled with the description from the file header. + +The returned pixel data should be free()d after use. */ + +void *qoi_read(t_const_str filename, t_qoi_desc *desc, + t_i32 channels); + +# endif /* QOI_NO_STDIO */ + +/* Encode raw RGB or RGBA pixels t_i32o a QOI image in memory. + +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pot_i32er to the encoded data on success. On success the out_len +is set to the size in bytes of the encoded data. + +The returned qoi data should be free()d after use. */ + +void *qoi_encode(const void *data, const t_qoi_desc *desc, + t_i32 *out_len); + +/* Decode a QOI image from memory. + +The function either returns NULL on failure (invalid parameters or malloc +failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct +is filled with the description from the file header. + +The returned pixel data should be free()d after use. */ + +void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc, + t_i32 channels); + +#endif /* QOI_H */ diff --git a/include/me/img/qoi/qoi_decode.h b/include/me/img/qoi/qoi_decode.h new file mode 100644 index 0000000..d11ebe9 --- /dev/null +++ b/include/me/img/qoi/qoi_decode.h @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_decode.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:33:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:58:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_DECODE_H +# define QOI_DECODE_H + +# include "me/img/qoi.h" +# include "me/img/qoi/qoi_utils.h" + +typedef struct s_decode_vals +{ + t_i32 px_len; + t_i32 chunks_len; + t_i32 px_pos; + t_i32 p; + t_i32 run; + t_i32 b1; + t_i32 b2; + t_i32 vg; + t_u32 header_magic; + t_qoi_rgba index[64]; + t_qoi_rgba px; + +} t_decode_vals; + +static inline void qoi_op_luma_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->b2 = bytes[vals->p++]; + vals->vg = (vals->b1 & 0x3f) - 32; + vals->px.rgba.r += vals->vg - 8 + ((vals->b2 >> 4) & 0x0f); + vals->px.rgba.g += vals->vg; + vals->px.rgba.b += vals->vg - 8 + (vals->b2 & 0x0f); +} + +static inline void qoi_op_diff_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + (void)(bytes); + vals->px.rgba.r += ((vals->b1 >> 4) & 0x03) - 2; + vals->px.rgba.g += ((vals->b1 >> 2) & 0x03) - 2; + vals->px.rgba.b += (vals->b1 & 0x03) - 2; +} + +static inline void qoi_op_rgba_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->px.rgba.r = bytes[vals->p++]; + vals->px.rgba.g = bytes[vals->p++]; + vals->px.rgba.b = bytes[vals->p++]; + vals->px.rgba.a = bytes[vals->p++]; +} + +static inline void qoi_op_rgb_decode(t_decode_vals *vals, const t_u8 *bytes) +{ + vals->px.rgba.r = bytes[vals->p++]; + vals->px.rgba.g = bytes[vals->p++]; + vals->px.rgba.b = bytes[vals->p++]; +} + +static inline void qoi_decode_inner_inner(t_decode_vals *vals, \ + const t_u8 *bytes) +{ + vals->b1 = bytes[vals->p++]; + if (vals->b1 == QOI_OP_RGB) + qoi_op_rgb_decode(vals, bytes); + else if (vals->b1 == QOI_OP_RGBA) + qoi_op_rgba_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_INDEX) + vals->px = vals->index[vals->b1]; + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_DIFF) + qoi_op_diff_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_LUMA) + qoi_op_luma_decode(vals, bytes); + else if ((vals->b1 & QOI_MASK_2) == QOI_OP_RUN) + vals->run = (vals->b1 & 0x3f); + vals->index[qoi_color_hash(vals->px) % 64] = vals->px; +} + +#endif /* QOI_DECODE_H */ diff --git a/include/me/img/qoi/qoi_encode.h b/include/me/img/qoi/qoi_encode.h new file mode 100644 index 0000000..bea2a22 --- /dev/null +++ b/include/me/img/qoi/qoi_encode.h @@ -0,0 +1,155 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_encode.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:36:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:56:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_ENCODE_H +# define QOI_ENCODE_H + +# include "me/img/qoi.h" +# include "me/img/qoi/qoi_utils.h" + +typedef struct s_encode_vals +{ + t_i32 i; + t_i32 max_size; + t_i32 p; + t_i32 run; + t_i32 px_len; + t_i32 px_end; + t_i32 px_pos; + t_i32 channels; + t_i32 index_pos; + t_qoi_rgba index[64]; + t_qoi_rgba px; + t_qoi_rgba px_prev; +} t_encode_vals; + +typedef struct s_v +{ + t_i8 r; + t_i8 g; + t_i8 b; + t_i8 g_r; + t_i8 g_b; +} t_v; + +static inline void qoi_encode_if_thingy1(t_encode_vals *vals, + t_v *v, t_u8 *bytes) +{ + v->r = vals->px.rgba.r - vals->px_prev.rgba.r; + v->g = vals->px.rgba.g - vals->px_prev.rgba.g; + v->b = vals->px.rgba.b - vals->px_prev.rgba.b; + v->g_r = v->r - v->g; + v->g_b = v->b - v->g; + if (v->r > -3 && v->r < 2 && v->g > -3 && v->g < 2 && v->b > -3 + && v->b < 2) + bytes[vals->p++] = QOI_OP_DIFF | (v->r + 2) << 4 | (v->g + + 2) << 2 | (v->b + 2); + else if (v->g_r > -9 && v->g_r < 8 && v->g > -33 && v->g < 32 + && v->g_b > -9 && v->g_b < 8) + { + bytes[vals->p++] = QOI_OP_LUMA | (v->g + 32); + bytes[vals->p++] = (v->g_r + 8) << 4 | (v->g_b + 8); + } + else + { + bytes[vals->p++] = QOI_OP_RGB; + bytes[vals->p++] = vals->px.rgba.r; + bytes[vals->p++] = vals->px.rgba.g; + bytes[vals->p++] = vals->px.rgba.b; + } +} + +static inline void qoi_encode_inner_inner_inner(t_encode_vals *vals, + t_v *v, t_u8 *bytes) +{ + if (vals->run > 0) + { + bytes[vals->p++] = QOI_OP_RUN | (vals->run - 1); + vals->run = 0; + } + vals->index_pos = qoi_color_hash(vals->px) % 64; + if (vals->index[vals->index_pos].v == vals->px.v) + bytes[vals->p++] = QOI_OP_INDEX | vals->index_pos; + else + { + vals->index[vals->index_pos] = vals->px; + if (vals->px.rgba.a == vals->px_prev.rgba.a) + qoi_encode_if_thingy1(vals, v, bytes); + else + { + bytes[vals->p++] = QOI_OP_RGBA; + bytes[vals->p++] = vals->px.rgba.r; + bytes[vals->p++] = vals->px.rgba.g; + bytes[vals->p++] = vals->px.rgba.b; + bytes[vals->p++] = vals->px.rgba.a; + } + } +} + +static inline void qoi_encode_inner_inner(t_encode_vals *vals, + const t_u8 *pixels, t_u8 *bytes) +{ + t_v v; + + v = (t_v){0}; + while (vals->px_pos < vals->px_len) + { + vals->px.rgba.r = pixels[vals->px_pos + 0]; + vals->px.rgba.g = pixels[vals->px_pos + 1]; + vals->px.rgba.b = pixels[vals->px_pos + 2]; + if (vals->channels == 4) + vals->px.rgba.a = pixels[vals->px_pos + 3]; + if (vals->px.v == vals->px_prev.v) + { + vals->run++; + if (vals->run == 62 || vals->px_pos == vals->px_end) + { + bytes[vals->p++] = QOI_OP_RUN | (vals->run - 1); + vals->run = 0; + } + } + else + qoi_encode_inner_inner_inner(vals, &v, bytes); + vals->px_prev = vals->px; + vals->px_pos += vals->channels; + } +} + +static inline void *qoi_encode_inner(t_encode_vals *vals, + const t_qoi_desc *desc, + const t_u8 *pixels, t_i32 *out_len) +{ + t_u8 *bytes; + + bytes = (t_u8 *)mem_alloc(vals->max_size); + if (!bytes) + return (NULL); + qoi_write_32(bytes, &vals->p, QOI_MAGIC); + qoi_write_32(bytes, &vals->p, desc->width); + qoi_write_32(bytes, &vals->p, desc->height); + bytes[vals->p++] = desc->channels; + bytes[vals->p++] = desc->colorspace; + mem_set_zero(vals->index, sizeof(vals->index)); + vals->px_prev.rgba.a = 255; + vals->px = vals->px_prev; + vals->px_len = desc->width * desc->height * desc->channels; + vals->px_end = vals->px_len - desc->channels; + vals->channels = desc->channels; + qoi_encode_inner_inner(vals, pixels, bytes); + vals->i = 0; + while (vals->i < (t_i32) sizeof(t_u8[8])) + bytes[vals->p++] = ((t_u8[8]){0, 0, 0, 0, 0, 0, 0, 1})[vals->i++]; + *out_len = vals->p; + return (bytes); +} + +#endif /* QOI_ENCODE_H */ diff --git a/include/me/img/qoi/qoi_utils.h b/include/me/img/qoi/qoi_utils.h new file mode 100644 index 0000000..543296b --- /dev/null +++ b/include/me/img/qoi/qoi_utils.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:59:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:33:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QOI_UTILS_H +# define QOI_UTILS_H + +# include "me/img/qoi.h" +# include "me/types.h" + +# define QOI_OP_INDEX 0x00 /* 00xxxxxx */ +# define QOI_OP_DIFF 0x40 /* 01xxxxxx */ +# define QOI_OP_LUMA 0x80 /* 10xxxxxx */ +# define QOI_OP_RUN 0xc0 /* 11xxxxxx */ +# define QOI_OP_RGB 0xfe /* 11111110 */ +# define QOI_OP_RGBA 0xff /* 11111111 */ +# define QOI_MASK_2 0xc0 /* 11000000 */ +# define QOI_MAGIC 0x716f6966u +//(((t_u32)'q') << 24 | ((t_u32)'o') << 16 | ((t_u32)'i') << 8 | ((t_u32)'f')) +# define QOI_HEADER_SIZE 14 +/* 2GB is the max file size that this implementation can safely handle. We guard +against anything larger than that, assuming the worst case with 5 bytes per +pixel, rounded down to a nice clean value. 400 million pixels ought to be +enough for anybody. */ +# define QOI_PIXELS_MAX 400000000u + +typedef union u_qoi_rgba +{ + struct s_qoi_rgba + { + t_u8 r; + t_u8 g; + t_u8 b; + t_u8 a; + } rgba; + t_u32 v; +} t_qoi_rgba; + +void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v); + +t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p); + +static inline t_u32 qoi_color_hash(t_qoi_rgba c) +{ + return (c.rgba.r * 3 + c.rgba.g * 5 + c.rgba.b * 7 + c.rgba.a * 11); +} + +#endif /* QOI_UTILS_H */ diff --git a/include/me/list/list_add_back.h b/include/me/list/list_add_back.h new file mode 100644 index 0000000..97dd2ef --- /dev/null +++ b/include/me/list/list_add_back.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_back.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:27:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ADD_BACK_H +# define LIST_ADD_BACK_H + +# include "me/types.h" + +void list_add_back(t_list **list, t_list *new); + +#endif diff --git a/include/me/list/list_add_front.h b/include/me/list/list_add_front.h new file mode 100644 index 0000000..8cc7fbc --- /dev/null +++ b/include/me/list/list_add_front.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_front.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ADD_FRONT_H +# define LIST_ADD_FRONT_H + +# include "me/types.h" + +void list_add_front(t_list **lst, t_list *new); +#endif \ No newline at end of file diff --git a/include/me/list/list_alloc_node.h b/include/me/list/list_alloc_node.h new file mode 100644 index 0000000..a84fb05 --- /dev/null +++ b/include/me/list/list_alloc_node.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_alloc_node.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:30 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ALLOC_NODE_H +# define LIST_ALLOC_NODE_H + +# include "me/types.h" + +t_list *list_alloc_node(void *content); + +#endif diff --git a/include/me/list/list_free_all.h b/include/me/list/list_free_all.h new file mode 100644 index 0000000..151eb5f --- /dev/null +++ b/include/me/list/list_free_all.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_all.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:28:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_FREE_ALL_H +# define LIST_FREE_ALL_H + +# include "me/types.h" + +void list_free_all(t_list **lst, void (*del)(void *)); + +#endif diff --git a/include/me/list/list_free_one.h b/include/me/list/list_free_one.h new file mode 100644 index 0000000..cac9897 --- /dev/null +++ b/include/me/list/list_free_one.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_one.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_FREE_ONE_H +# define LIST_FREE_ONE_H + +# include "me/types.h" + +void list_free_one(t_list *lst, void (*del)(void *)); + +#endif diff --git a/include/me/list/list_get_last.h b/include/me/list/list_get_last.h new file mode 100644 index 0000000..b0172ee --- /dev/null +++ b/include/me/list/list_get_last.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_get_last.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_GET_LAST_H +# define LIST_GET_LAST_H + +# include "me/types.h" + +t_list *list_get_last(t_list *list); + +#endif \ No newline at end of file diff --git a/include/me/list/list_iter.h b/include/me/list/list_iter.h new file mode 100644 index 0000000..1dc5c0b --- /dev/null +++ b/include/me/list/list_iter.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:29:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_ITER_H +# define LIST_ITER_H + +# include "me/types.h" + +void list_iter(t_list *list, void (*f)(void *)); + +#endif diff --git a/include/me/list/list_map.h b/include/me/list/list_map.h new file mode 100644 index 0000000..996f672 --- /dev/null +++ b/include/me/list/list_map.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:30:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_MAP_H +# define LIST_MAP_H + +# include "me/types.h" + +t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +#endif \ No newline at end of file diff --git a/include/me/list/list_size.h b/include/me/list/list_size.h new file mode 100644 index 0000000..758131e --- /dev/null +++ b/include/me/list/list_size.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_size.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:30:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_SIZE_H +# define LIST_SIZE_H + +# include "me/types.h" + +t_usize list_size(t_list *lst); + +#endif diff --git a/include/me/mem/mem_alloc.h b/include/me/mem/mem_alloc.h new file mode 100644 index 0000000..1afc6fb --- /dev/null +++ b/include/me/mem/mem_alloc.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:41:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_ALLOC_H +# define MEM_ALLOC_H + +# include "me/types.h" + +void *mem_alloc(t_usize size); + +#endif diff --git a/include/me/mem/mem_alloc_array.h b/include/me/mem/mem_alloc_array.h new file mode 100644 index 0000000..2979ef3 --- /dev/null +++ b/include/me/mem/mem_alloc_array.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc_array.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:19:45 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_ALLOC_ARRAY_H +# define MEM_ALLOC_ARRAY_H + +# include "me/types.h" + +void *mem_alloc_array(t_usize item_count, t_usize item_size); + +#endif diff --git a/include/me/mem/mem_compare.h b/include/me/mem/mem_compare.h new file mode 100644 index 0000000..1ae3c3e --- /dev/null +++ b/include/me/mem/mem_compare.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_compare.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:21:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_COMPARE_H +# define MEM_COMPARE_H + +# include "me/types.h" + +t_i32 mem_compare(const void *lhs, const void *rhs, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_copy.h b/include/me/mem/mem_copy.h new file mode 100644 index 0000000..59d7124 --- /dev/null +++ b/include/me/mem/mem_copy.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_copy.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:21:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_COPY_H +# define MEM_COPY_H + +# include "me/types.h" + +void *mem_copy(void *destination, const void *source, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_find.h b/include/me/mem/mem_find.h new file mode 100644 index 0000000..048d844 --- /dev/null +++ b/include/me/mem/mem_find.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_FIND_H +# define MEM_FIND_H + +# include "me/types.h" + +void *mem_find(void *buf, t_u8 find, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_find_bytes.h b/include/me/mem/mem_find_bytes.h new file mode 100644 index 0000000..5f220b6 --- /dev/null +++ b/include/me/mem/mem_find_bytes.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find_bytes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 18:24:04 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_FIND_BYTES_H +# define MEM_FIND_BYTES_H + +# include "me/types.h" + +void *mem_find_bytes(void *buf, t_u8 *find, t_usize find_count, + t_usize count); + +#endif diff --git a/include/me/mem/mem_move.h b/include/me/mem/mem_move.h new file mode 100644 index 0000000..b2bce61 --- /dev/null +++ b/include/me/mem/mem_move.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_MOVE_H +# define MEM_MOVE_H + +# include "me/types.h" + +void *mem_move(void *destination, const void *source, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_set.h b/include/me/mem/mem_set.h new file mode 100644 index 0000000..c6702a1 --- /dev/null +++ b/include/me/mem/mem_set.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:25:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_SET_H +# define MEM_SET_H + +# include "me/types.h" + +void mem_set(void *buf, t_u8 byte, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/mem/mem_set_zero.h b/include/me/mem/mem_set_zero.h new file mode 100644 index 0000000..dc38393 --- /dev/null +++ b/include/me/mem/mem_set_zero.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set_zero.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:58:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:24:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_SET_ZERO_H +# define MEM_SET_ZERO_H + +# include "me/types.h" + +void mem_set_zero(void *buf, t_usize count); + +#endif \ No newline at end of file diff --git a/include/me/num/u16.h b/include/me/num/u16.h new file mode 100644 index 0000000..e2791fd --- /dev/null +++ b/include/me/num/u16.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u16.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:16:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U16_H +# define U16_H + +# include "me/types.h" + +t_u16 u16_rotate_left(t_u16 n, t_usize by); +t_u16 u16_rotate_right(t_u16 n, t_usize by); + +#endif diff --git a/include/me/num/u32.h b/include/me/num/u32.h new file mode 100644 index 0000000..4eab4f8 --- /dev/null +++ b/include/me/num/u32.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u32.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U32_H +# define U32_H + +# include "me/types.h" + +t_u32 u32_rotate_left(t_u32 n, t_usize by); +t_u32 u32_rotate_right(t_u32 n, t_usize by); + +#endif diff --git a/include/me/num/u64.h b/include/me/num/u64.h new file mode 100644 index 0000000..5ff2137 --- /dev/null +++ b/include/me/num/u64.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:59:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U64_H +# define U64_H + +# include "me/types.h" + +t_u64 u64_rotate_left(t_u64 n, t_usize by); +t_u64 u64_rotate_right(t_u64 n, t_usize by); + +t_u64 u64_from_7bytes(t_u8 *bytes, t_usize start, t_usize len); +t_u64 u64_from_bytes(t_u8 *bytes, t_usize len); + +#endif diff --git a/include/me/num/u8.h b/include/me/num/u8.h new file mode 100644 index 0000000..fb254ae --- /dev/null +++ b/include/me/num/u8.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u8.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U8_H +# define U8_H + +# include "me/types.h" + +t_u8 u8_rotate_left(t_u8 n, t_usize by); +t_u8 u8_rotate_right(t_u8 n, t_usize by); + +#endif diff --git a/include/me/num/usize.h b/include/me/num/usize.h new file mode 100644 index 0000000..2c9cc6b --- /dev/null +++ b/include/me/num/usize.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* usize.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:17:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef USIZE_H +# define USIZE_H + +# include "me/types.h" + +t_usize usize_rotate_left(t_usize n, t_usize by); +t_usize usize_rotate_right(t_usize n, t_usize by); + +#endif diff --git a/include/me/printf/formatter/formatter.h b/include/me/printf/formatter/formatter.h new file mode 100644 index 0000000..67346c5 --- /dev/null +++ b/include/me/printf/formatter/formatter.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* formatter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */ +/* Updated: 2023/11/18 19:11:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORMATTER_H +# define FORMATTER_H +# include "me/printf/printf.h" +# include "me/types.h" + +void printf_x_low(t_printf_arg data, t_printf_func f); +void printf_x_up(t_printf_arg data, t_printf_func f); +void printf_o(t_printf_arg data, t_printf_func f); +void printf_d(t_printf_arg data, t_printf_func f); +void printf_u(t_printf_arg data, t_printf_func f); +void printf_c(t_printf_arg data, t_printf_func f); +void printf_s(t_printf_arg data, t_printf_func f); +void printf_p(t_printf_arg data, t_printf_func f); + +#endif diff --git a/include/me/printf/formatter/utils.h b/include/me/printf/formatter/utils.h new file mode 100644 index 0000000..150d5aa --- /dev/null +++ b/include/me/printf/formatter/utils.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:24:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H + +# include "me/printf/matchers/matchers.h" +# include "me/printf/printf.h" +# include "me/types.h" +# include + +typedef struct s_prec_strs +{ + t_str *out; + t_str *pretty; + bool free_out; +} t_prec_strs; + +typedef struct s_pad_and_stuff_args +{ + t_usize fill_zero; + t_usize fill; + t_usize len; + t_usize pretty_len; + t_usize sign_len; + t_str pretty; + t_str str; + t_str sign; + bool allow_zero_fill; + +} t_pad_and_stuff_args; +typedef struct s_pad_inner_args +{ + void *p_args; + t_usize fmt_len; + t_printf_func f; + va_list *arguments; + t_matcher_list *matchers; +} t_pad_inner_args; + +void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, + t_printf_arg *d); +void print_with_func(t_pad_and_stuff_args *a, t_printf_arg *d, + t_printf_func f, t_const_str t); +void pad_and_stuff(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f); +void handle_prec_and_align(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg); +bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, + t_usize *nxt, t_printf_arg *c_arg); +void set_params2(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg); +bool set_params(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg); +void ret_reset(t_usize *c_idx, t_usize *nxt, t_const_str fmt); +t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt, + t_pad_inner_args extra); +void pad_inner(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_pad_inner_args extra); +void advance_atoi(t_const_str fmt, t_usize *idx); +void me_printf_str_inner(t_const_str fmt, t_printf_func f, + va_list *arguments, void *p_args); +void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f); +void handle_weird_precision_stuff(t_printf_arg *data, + t_prec_strs strs, t_usize value); +#endif diff --git a/include/me/printf/matchers/matchers.h b/include/me/printf/matchers/matchers.h new file mode 100644 index 0000000..7dc4239 --- /dev/null +++ b/include/me/printf/matchers/matchers.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* matchers.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */ +/* Updated: 2023/11/18 18:10:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MATCHERS_H +# define MATCHERS_H + +# include "me/printf/printf.h" +# include "me/types.h" +# include +# define PRINTF_BUFFER_CHUNK 20 + +typedef struct s_matcher_tmp +{ + char chr_val; + t_i64 i64_val; + t_u64 u64_val; +} t_matcher_tmp_val; + +typedef void (*t_matcher_func)(t_printf_arg data, + t_printf_func f); +typedef struct s_matcher +{ + t_const_str matcher; + t_usize matcher_len; + t_printf_type arg_type; + t_matcher_func f; +} t_matcher; + +typedef struct s_matcher_list +{ + t_matcher data[PRINTF_BUFFER_CHUNK]; + struct s_matcher_list *next; +} t_matcher_list; + +t_matcher_list *get_matchers(void); +bool insert_matcher(t_matcher matcher); +t_matcher *find_matcher(t_const_str fmt, + t_matcher_list *matchers, t_usize *c_idx); +void call_matcher(t_matcher *matcher, + t_printf_arg matcher_arguments, va_list args, + t_printf_func f); + +#endif diff --git a/include/me/printf/printf.h b/include/me/printf/printf.h new file mode 100644 index 0000000..f12bbd6 --- /dev/null +++ b/include/me/printf/printf.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:06:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRINTF_H + +# define PRINTF_H +# include "me/types.h" +# include + +typedef struct s_fprintf_arg +{ + t_usize total_print; + t_file fd; +} t_fprintf_arg; + +typedef enum e_printf_flags +{ + PRECISION = 1 << 1, + ALIGN = 1 << 2, + ZERO_ALIGN = 1 << 3, + SIGN = 1 << 4, +} t_printf_flags; + +typedef enum e_printf_type +{ + CHAR = 1 << 0, + STR = 1 << 1, + U64 = 1 << 2, + I64 = 1 << 3, + VOID_PTR = 1 << 4, + I32 = 1 << 5, + U32 = 1 << 6, +} t_printf_type; + +typedef struct s_printf_extra_args +{ + t_u64 precision; + t_u64 align; + bool left_align; + bool space_align; + bool pretty; +} t_printf_extra_args; + +typedef struct s_printf_args +{ + void *argument; + void *p_args; + t_printf_extra_args extra; + t_printf_flags flags; +} t_printf_arg; + +typedef void (*t_printf_func)(t_const_str to_write, + t_usize to_write_len, void *p_args); + +t_usize me_printf(t_const_str fmt, ...); +t_usize me_eprintf(t_const_str fmt, ...); +t_usize me_vprintf(t_const_str fmt, va_list *args); +t_usize me_veprintf(t_const_str fmt, va_list *args); + +#endif diff --git a/include/me/string/str_clone.h b/include/me/string/str_clone.h new file mode 100644 index 0000000..a152316 --- /dev/null +++ b/include/me/string/str_clone.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_clone.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:30:19 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_CLONE_H +# define STR_CLONE_H + +# include "me/types.h" + +t_str str_clone(t_const_str source); + +#endif diff --git a/include/me/string/str_find_chr.h b/include/me/string/str_find_chr.h new file mode 100644 index 0000000..0c583d6 --- /dev/null +++ b/include/me/string/str_find_chr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_chr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:07:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_CHR_H +# define STR_FIND_CHR_H + +# include "me/types.h" + +char *str_find_chr(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_find_rev_chr.h b/include/me/string/str_find_rev_chr.h new file mode 100644 index 0000000..5dcd107 --- /dev/null +++ b/include/me/string/str_find_rev_chr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_rev_chr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:07:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_REV_CHR_H +# define STR_FIND_REV_CHR_H + +# include "me/types.h" + +char *str_find_rev_chr(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_find_str.h b/include/me/string/str_find_str.h new file mode 100644 index 0000000..30bd878 --- /dev/null +++ b/include/me/string/str_find_str.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_FIND_STR_H +# define STR_FIND_STR_H + +# include "me/types.h" + +const char *str_find_str(t_const_str str, t_const_str to_find); + +#endif diff --git a/include/me/string/str_iter.h b/include/me/string/str_iter.h new file mode 100644 index 0000000..e9bf1f5 --- /dev/null +++ b/include/me/string/str_iter.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_iter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:08:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_ITER_H +# define STR_ITER_H + +# include "me/types.h" + +void str_iter(t_str s, void (*f)(t_usize, char *)); + +#endif \ No newline at end of file diff --git a/include/me/string/str_join.h b/include/me/string/str_join.h new file mode 100644 index 0000000..408eb2c --- /dev/null +++ b/include/me/string/str_join.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_join.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:02:58 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:08:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_JOIN_H +# define STR_JOIN_H + +# include "me/types.h" + +t_str str_join(t_const_str s1, t_const_str s2); + +#endif \ No newline at end of file diff --git a/include/me/string/str_l_cat.h b/include/me/string/str_l_cat.h new file mode 100644 index 0000000..fa752cb --- /dev/null +++ b/include/me/string/str_l_cat.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_cat.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:09:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_L_CAT_H +# define STR_L_CAT_H + +# include "me/types.h" + +t_usize str_l_cat(t_str dest, t_const_str src, t_usize buffer_size); + +#endif diff --git a/include/me/string/str_l_copy.h b/include/me/string/str_l_copy.h new file mode 100644 index 0000000..a75fbb4 --- /dev/null +++ b/include/me/string/str_l_copy.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_copy.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:09:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_L_COPY_H +# define STR_L_COPY_H + +# include "me/types.h" + +t_usize str_l_copy(t_str dest, t_const_str src, t_usize buffer_size); + +#endif \ No newline at end of file diff --git a/include/me/string/str_len.h b/include/me/string/str_len.h new file mode 100644 index 0000000..a1dddf3 --- /dev/null +++ b/include/me/string/str_len.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_len.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 16:08:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_LEN_H +# define STR_LEN_H + +# include "me/types.h" + +t_usize str_len(t_const_str str); + +#endif diff --git a/include/me/string/str_map.h b/include/me/string/str_map.h new file mode 100644 index 0000000..a64e3d2 --- /dev/null +++ b/include/me/string/str_map.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:11:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_MAP_H +# define STR_MAP_H + +# include "me/types.h" + +t_str str_map(t_const_str s, char (*f)(t_usize, char)); + +#endif \ No newline at end of file diff --git a/include/me/string/str_n_compare.h b/include/me/string/str_n_compare.h new file mode 100644 index 0000000..c07c04f --- /dev/null +++ b/include/me/string/str_n_compare.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_compare.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:17:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_N_COMPARE_H +# define STR_N_COMPARE_H + +# include "me/types.h" + +t_i32 str_n_compare(t_const_str lhs, t_const_str rhs, t_usize n); + +#endif \ No newline at end of file diff --git a/include/me/string/str_n_find_str.h b/include/me/string/str_n_find_str.h new file mode 100644 index 0000000..eea578b --- /dev/null +++ b/include/me/string/str_n_find_str.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_find_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_N_FIND_STR_H +# define STR_N_FIND_STR_H + +# include "me/types.h" + +const char *str_n_find_str(t_const_str str, t_const_str to_find, t_usize len); + +#endif diff --git a/include/me/string/str_split.h b/include/me/string/str_split.h new file mode 100644 index 0000000..578f1dd --- /dev/null +++ b/include/me/string/str_split.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_split.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:18:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_SPLIT_H +# define STR_SPLIT_H + +# include "me/types.h" + +t_str *str_split(t_const_str str, char chr); + +#endif \ No newline at end of file diff --git a/include/me/string/str_substring.h b/include/me/string/str_substring.h new file mode 100644 index 0000000..da9cb41 --- /dev/null +++ b/include/me/string/str_substring.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_substring.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 22:42:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:18:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_SUBSTRING_H +# define STR_SUBSTRING_H + +# include "me/types.h" + +t_str str_substring(t_const_str str, t_usize start, t_usize len); + +#endif \ No newline at end of file diff --git a/include/me/string/str_trim.h b/include/me/string/str_trim.h new file mode 100644 index 0000000..e7682d3 --- /dev/null +++ b/include/me/string/str_trim.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_trim.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:43:42 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:19:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_TRIM_H +# define STR_TRIM_H + +# include "me/types.h" + +t_str str_trim(t_const_str str, t_const_str charset); + +#endif \ No newline at end of file diff --git a/include/me/types.h b/include/me/types.h new file mode 100644 index 0000000..55a0360 --- /dev/null +++ b/include/me/types.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */ +/* Updated: 2024/01/05 00:08:41 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TYPES_H +# define TYPES_H + +# include +# include +# include + +typedef char *t_str; +typedef const char *t_const_str; + +typedef unsigned char t_u8; +typedef char t_i8; +typedef unsigned short t_u16; +typedef short t_i16; +typedef int t_i32; +typedef unsigned int t_u32; +typedef unsigned long long t_u64; +typedef long long t_i64; +typedef ssize_t t_isize; +typedef size_t t_usize; + +typedef float t_f32; +typedef double t_f64; + +typedef int t_file; +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +typedef bool t_error; + +# define ERROR 1 +# define NO_ERROR 0 + +#endif diff --git a/include/me/vec/vec_buf_str.h b/include/me/vec/vec_buf_str.h new file mode 100644 index 0000000..083cb24 --- /dev/null +++ b/include/me/vec/vec_buf_str.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_BUF_STR_H +# define VEC_BUF_STR_H + +# include "me/buffered_str/buf_str.h" +# include "me/types.h" + +typedef bool (*t_vec_buf_str_sort_fn)(t_buffer_str *, + t_buffer_str *); +typedef void (*t_free_buf_str_item)(t_buffer_str); + +typedef struct s_vec_buf_str +{ + t_free_buf_str_item free_func; + t_usize len; + t_usize capacity; + t_buffer_str *buffer; +} t_vec_buf_str; + +t_vec_buf_str vec_buf_str_new(t_usize capacity, + t_free_buf_str_item free_function); +t_error vec_buf_str_push(t_vec_buf_str *vec, + t_buffer_str element); +t_error vec_buf_str_push_front(t_vec_buf_str *vec, + t_buffer_str element); +t_error vec_buf_str_pop(t_vec_buf_str *vec, + t_buffer_str *value); +t_error vec_buf_str_pop_front(t_vec_buf_str *vec, + t_buffer_str *value); +void vec_buf_str_free(t_vec_buf_str vec); +t_error vec_buf_str_reserve(t_vec_buf_str *vec, + t_usize wanted_capacity); +t_error vec_buf_str_find(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), t_usize *index); +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_error vec_buf_str_all(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result); +t_error vec_buf_str_any(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result); +void vec_buf_str_iter(t_vec_buf_str *vec, + void (*fn)(t_usize index, t_buffer_str *value, + void *state), void *state); +void vec_buf_str_reverse(t_vec_buf_str *vec); +void vec_buf_str_sort(t_vec_buf_str *vec, + t_vec_buf_str_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_i64.h b/include/me/vec/vec_i64.h new file mode 100644 index 0000000..8a8f200 --- /dev/null +++ b/include/me/vec/vec_i64.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_H +# define VEC_I64_H + +# include "me/types.h" + +typedef bool (*t_vec_i64_sort_fn)(t_i64 *, t_i64 *); +typedef void (*t_free_i64_item)(t_i64); + +typedef struct s_vec_i64 +{ + t_free_i64_item free_func; + t_usize len; + t_usize capacity; + t_i64 *buffer; +} t_vec_i64; + +t_vec_i64 vec_i64_new(t_usize capacity, + t_free_i64_item free_function); +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element); +t_error vec_i64_pop(t_vec_i64 *vec, t_i64 *value); +t_error vec_i64_pop_front(t_vec_i64 *vec, t_i64 *value); +void vec_i64_free(t_vec_i64 vec); +t_error vec_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity); +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + t_usize *index); +t_error vec_i64_find_starting(t_vec_i64 *vec, + bool (*fn)(const t_i64 *), t_usize starting_index, + t_usize *index); +t_error vec_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +t_error vec_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + bool *result); +void vec_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, + t_i64 *value, void *state), void *state); +void vec_i64_reverse(t_vec_i64 *vec); +void vec_i64_sort(t_vec_i64 *vec, t_vec_i64_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_i64_bool.h b/include/me/vec/vec_i64_bool.h new file mode 100644 index 0000000..42f456a --- /dev/null +++ b/include/me/vec/vec_i64_bool.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_I64_BOOL_H +# define VEC_I64_BOOL_H + +# include "app/types/type_i64_bool.h" +# include "me/types.h" + +typedef bool (*t_vec_i64_bool_sort_fn)(t_i64_bool *, + t_i64_bool *); +typedef void (*t_free_i64_bool_item)(t_i64_bool); + +typedef struct s_vec_i64_bool +{ + t_free_i64_bool_item free_func; + t_usize len; + t_usize capacity; + t_i64_bool *buffer; +} t_vec_i64_bool; + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function); +t_error vec_i64_bool_push(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, + t_i64_bool element); +t_error vec_i64_bool_pop(t_vec_i64_bool *vec, + t_i64_bool *value); +t_error vec_i64_bool_pop_front(t_vec_i64_bool *vec, + t_i64_bool *value); +void vec_i64_bool_free(t_vec_i64_bool vec); +t_error vec_i64_bool_reserve(t_vec_i64_bool *vec, + t_usize wanted_capacity); +t_error vec_i64_bool_find(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), t_usize *index); +t_error vec_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), + t_usize starting_index, t_usize *index); +t_error vec_i64_bool_all(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +t_error vec_i64_bool_any(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), bool *result); +void vec_i64_bool_iter(t_vec_i64_bool *vec, + void (*fn)(t_usize index, t_i64_bool *value, + void *state), void *state); +void vec_i64_bool_reverse(t_vec_i64_bool *vec); +void vec_i64_bool_sort(t_vec_i64_bool *vec, + t_vec_i64_bool_sort_fn is_sorted); + +#endif diff --git a/include/me/vec/vec_u8.h b/include/me/vec/vec_u8.h new file mode 100644 index 0000000..fe8ec03 --- /dev/null +++ b/include/me/vec/vec_u8.h @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_U8_H +# define VEC_U8_H + +# include "me/types.h" + +typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *); +typedef void (*t_free_u8_item)(t_u8); + +typedef struct s_vec_u8 +{ + t_free_u8_item free_func; + t_usize len; + t_usize capacity; + t_u8 *buffer; +} t_vec_u8; + +t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function); +t_error vec_u8_push(t_vec_u8 *vec, t_u8 element); +t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element); +t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value); +t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value); +void vec_u8_free(t_vec_u8 vec); +t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity); +t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + t_usize *index); +t_error vec_u8_find_starting(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), t_usize starting_index, + t_usize *index); +t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + bool *result); +t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), + bool *result); +void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, + t_u8 *value, void *state), void *state); +void vec_u8_reverse(t_vec_u8 *vec); +void vec_u8_sort(t_vec_u8 *vec, t_vec_u8_sort_fn is_sorted); + +#endif diff --git a/include/me/vec2/vec2.h b/include/me/vec2/vec2.h new file mode 100644 index 0000000..81a0e52 --- /dev/null +++ b/include/me/vec2/vec2.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec2.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 14:54:39 by maiboyer #+# #+# */ +/* Updated: 2023/12/31 15:07:54 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC2_H +# define VEC2_H + +# include "me/types.h" + +typedef struct s_vi2d +{ + t_i32 x; + t_i32 y; +} t_vi2d; + +typedef struct s_vu2d +{ + t_u32 x; + t_u32 y; +} t_vu2d; + +typedef struct s_vf2d +{ + t_f32 x; + t_f32 y; +} t_vf2d; + +static inline t_vf2d vf2d(t_f32 x, t_f32 y) +{ + return ((t_vf2d){.x = x, .y = y}); +} + +static inline t_vi2d vi2d(t_i32 x, t_i32 y) +{ + return ((t_vi2d){.x = x, .y = y}); +} + +static inline t_vu2d vu2d(t_u32 x, t_u32 y) +{ + return ((t_vu2d){.x = x, .y = y}); +} + +static inline t_vi2d vi2d_add(t_vi2d lhs, t_vi2d rhs) +{ + return ((t_vi2d){.x = lhs.x + rhs.x, .y = lhs.y + rhs.y}); +} + +static inline t_vi2d vi2d_sub(t_vi2d lhs, t_vi2d rhs) +{ + return ((t_vi2d){.x = lhs.x - rhs.x, .y = lhs.y - rhs.y}); +} +#endif /* VEC2_H */ diff --git a/src/app/actual_main.c b/src/app/actual_main.c new file mode 100644 index 0000000..6804f8b --- /dev/null +++ b/src/app/actual_main.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* actual_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:10:39 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:26:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" + +int main_normal(t_i32 argc, t_str argv[]); +int main_checker(t_i32 argc, t_str argv[]); + +int main(t_i32 argc, t_str argv[]) +{ + if (BONUS) + return (main_checker(argc, argv)); + return (main_normal(argc, argv)); +} diff --git a/src/app/best_index_to_move.c b/src/app/best_index_to_move.c new file mode 100644 index 0000000..1b37701 --- /dev/null +++ b/src/app/best_index_to_move.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:14:54 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:21:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_index_to_move.h" +#include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +static void inc(void *s) +{ + t_usize *p; + + p = (t_usize *)s; + *p += 1; +} + +static inline t_usize count_move_for_index(struct s_best_index_to_move d) +{ + t_usize tmp; + + tmp = 0; + run_func_with_best_rotate_for_item(d.state, + (t_best_move_args){ + .index = d.i, + .args = &tmp, + .iter_func = d.f, + .from = d.from, + .to = d.to, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); + return (tmp); +} + +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f) +{ + t_usize min_val; + t_usize min_pos; + t_usize i; + t_usize tmp; + + if (from->len == 0) + return (0); + i = 0; + min_val = ~0; + min_pos = 0; + while (i < from->len) + { + tmp = count_move_for_index((struct s_best_index_to_move){state, from, + to, f, i}); + if (tmp < min_val) + { + min_val = tmp; + min_pos = i; + } + i++; + } + return (min_pos); +} diff --git a/src/app/best_move.c b/src/app/best_move.c new file mode 100644 index 0000000..2bbbfdf --- /dev/null +++ b/src/app/best_move.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/best_move_inner.h" +#include "app/find_iter.h" +#include "app/find_place.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include + +static inline t_isize move_count(t_rotation lhs, t_rotation rhs) +{ + if (lhs.direction == rhs.direction) + return (max(lhs.value, rhs.value)); + else + return (abs_diff(lhs.value, rhs.value)); +} + +void find_least_move(t_rotation *main, t_rotation *other) +{ + t_isize main_fliped; + t_isize other_fliped; + t_isize none_fliped; + t_rotation tmp; + t_isize minimum; + + none_fliped = move_count(*main, *other); + main_fliped = move_count(flip(*main), *other); + other_fliped = move_count(*main, flip(*other)); + minimum = min(none_fliped, min(main_fliped, other_fliped)); + if (minimum == none_fliped) + return ; + else if (minimum == main_fliped) + { + tmp = flip(*main); + *main = tmp; + } + else if (minimum == other_fliped) + { + tmp = flip(*other); + *other = tmp; + } +} + +static inline void func(t_best_move_args *data, t_rotation *main, + t_rotation *other) +{ + t_isize i; + + i = 0; + while (i++ < min(main->value, other->value)) + choose_func(&data->both, *main)(data->args); + i = 0; + if (main->value > other->value) + while (i++ < main->value - other->value) + choose_func(&data->main, *main)(data->args); + else + while (i++ < other->value - main->value) + choose_func(&data->other, *other)(data->args); +} + +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data) +{ + t_isize target_index; + t_rotation main; + t_rotation other; + t_isize i; + + target_index = (find_place(data.from->buffer[data.index], state) + + (data.to->len - data.iter_func(data.to))) % max(data.to->len, 1); + main = target(0, data.index, data.from->len); + other = target(target_index, 0, data.to->len); + find_least_move(&main, &other); + if (main.direction == other.direction) + func(&data, &main, &other); + else + { + i = 0; + while (i++ < main.value) + choose_func(&data.main, main)(data.args); + i = 0; + while (i++ < other.value) + choose_func(&data.other, other)(data.args); + } +} diff --git a/src/app/do_move.c b/src/app/do_move.c new file mode 100644 index 0000000..96b06b1 --- /dev/null +++ b/src/app/do_move.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* do_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:25:57 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 22:54:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/moves.h" +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); diff --git a/src/app/find_place.c b/src/app/find_place.c new file mode 100644 index 0000000..1a3efd6 --- /dev/null +++ b/src/app/find_place.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_place.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:06:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/types/type_i64_bool.h" +#include "me/vec/vec_i64_bool.h" + +static void find_place_iter(t_usize index, t_i64_bool *elem, + t_find_place_iter_state *state) +{ + (void)(index); + if (!(elem->active || elem->value == state->to_find_elem)) + return ; + if (elem->value == state->to_find_elem) + state->found_index = state->current_index; + state->current_index++; +} + +t_usize find_place(t_i64 elem, t_state *state) +{ + t_find_place_iter_state iter_state; + + iter_state.current_index = 0; + iter_state.found_index = 0; + iter_state.to_find_elem = elem; + vec_i64_bool_iter(&state->sorted, (void (*)())find_place_iter, &iter_state); + return (iter_state.found_index); +} + +/* +state + .sorted + .iter() + .copied() + .filter(|&(e, active)| active || elem == e) + .enumerate() + .find(|(_, (e, _))| *e == elem) + .map(|(i, _)| i) + .unwrap_or_else(|| { + println!("why..."); + 0 + }) +*/ diff --git a/src/app/iter_find.c b/src/app/iter_find.c new file mode 100644 index 0000000..bd6d1cf --- /dev/null +++ b/src/app/iter_find.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 14:17:53 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:23:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/iter_state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem < state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state) +{ + if (*elem > state->elem) + { + state->elem = *elem; + state->pos = index; + } +} + +t_usize min_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_min, &state); + return (state.pos); +} + +t_usize max_iter_zero_pos(t_vec_i64 *vec) +{ + t_iter_state state; + + if (vec->len == 0) + return (0); + state.pos = 0; + state.elem = vec->buffer[0]; + vec_i64_iter(vec, (void (*)())iter_max, &state); + return (state.pos); +} diff --git a/src/app/main.c b/src/app/main.c new file mode 100644 index 0000000..e234e3e --- /dev/null +++ b/src/app/main.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include +#include + +#if BONUS == 1 + +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); +bool is_sorted(t_vec_i64 *v); +void run_with_items(t_state *state); + +int main_normal(t_i32 argc, t_str argv[]) +{ + t_state state; + + (void)(argc--, argv++); + state = parses_arguments(argc, argv); + if (is_sorted(&state.stack_a)) + return (free_state(state), 0); + if (state.stack_a.len == 0) + (free_state(state), exit(0)); + else if (state.stack_a.len == 2) + sort_2(&state); + else if (state.stack_a.len == 3) + sort_3(&state); + else if (state.stack_a.len == 5) + sort_5(&state); + else + run_with_items(&state); + free_state(state); + return (0); +} diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c new file mode 100644 index 0000000..4de3a02 --- /dev/null +++ b/src/app/main_bonus.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/vec/vec_i64.h" + +#if BONUS + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +bool is_sorted(t_vec_i64 *v); + +bool does_match_strings(t_const_str in, t_const_str s1, t_const_str s2) +{ + bool res; + + res = (str_n_compare(in, s1, 4) == 0); + if (s2 != NULL) + res = (res || (str_n_compare(in, s2, 4) == 0)); + return (res); +} + +t_error handle_operation(t_buffer_str s, t_state *state) +{ + t_usize op; + + op = 0; + if (s.buf == NULL) + return (ERROR); + if (does_match_strings(s.buf, "pa", NULL)) + push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "pb", NULL)) + push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "sa", "ss")) + swap_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "sb", "ss")) + swap_inner(&state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "ra", "rr")) + rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rb", "rr")) + rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rra", "rrr")) + rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (does_match_strings(s.buf, "rrb", "rrr")) + rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (op == 0) + return (ERROR); + return (NO_ERROR); +} + +void handle_end(t_state state, t_buffer_str s) +{ + str_free(s); + if (is_sorted(&state.stack_a) && state.stack_b.len == 0) + me_printf("OK\n"); + else + me_printf("KO\n"); + free_state(state); +} + +int main_checker(t_i32 argc, t_str argv[]) +{ + t_state state; + t_buffer_str s; + bool err; + + (void)(argc--, argv++); + s.buf = ""; + err = false; + state = parses_arguments(argc, argv); + while (!err) + { + s = get_next_line(0, &err); + if (err) + break ; + if (s.len != 0 && s.buf[s.len - 1] == '\n') + str_pop(&s); + if (s.buf[0] == '\0') + continue ; + if (handle_operation(s, &state)) + return (me_printf("KO\n"), str_free(s), free_state(state), 1); + if (s.buf != NULL) + str_free(s); + } + handle_end(state, s); + return (0); +} + +#endif diff --git a/src/app/moves.c b/src/app/moves.c new file mode 100644 index 0000000..1eef164 --- /dev/null +++ b/src/app/moves.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:17:26 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 18:58:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/moves.h" +#include "app/state.h" +#include "app/types/type_move.h" + +void push_a(t_state *s); +void push_b(t_state *s); +void swap_a(t_state *s); +void swap_b(t_state *s); +void rotate_a(t_state *s); +void rotate_b(t_state *s); +void rev_rotate_a(t_state *s); +void rev_rotate_b(t_state *s); + +void do_move(t_move m, t_state *s) +{ + if (m & PUSH_A) + push_a(s); + if (m & PUSH_B) + push_b(s); + if (m & SWAP_A) + swap_a(s); + if (m & SWAP_B) + swap_b(s); + if (m & ROTATE_A) + rotate_a(s); + if (m & ROTATE_B) + rotate_b(s); + if (m & REVERSE_ROTATE_A) + rev_rotate_a(s); + if (m & REVERSE_ROTATE_B) + rev_rotate_b(s); +} + +t_const_str get_str_for_move(t_move m) +{ + if (m & PUSH_A) + return ("pa"); + if (m & PUSH_B) + return ("pb"); + if (m & SWAP_A && m & SWAP_B) + return ("ss"); + if (m & SWAP_A) + return ("sa"); + if (m & SWAP_B) + return ("sb"); + if (m & ROTATE_A && m & ROTATE_B) + return ("rr"); + if (m & ROTATE_A) + return ("ra"); + if (m & ROTATE_B) + return ("rb"); + if (m & REVERSE_ROTATE_A && m & REVERSE_ROTATE_B) + return ("rrr"); + if (m & REVERSE_ROTATE_A) + return ("rra"); + if (m & REVERSE_ROTATE_B) + return ("rrb"); + return (""); +} diff --git a/src/app/moves/push.c b/src/app/moves/push.c new file mode 100644 index 0000000..dc53491 --- /dev/null +++ b/src/app/moves/push.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:15:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/vec/vec_i64.h" + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (from->len == 0) + return ; + vec_i64_pop_front(from, &e); + vec_i64_push_front(to, e); + if (print) + me_printf("%s\n", print); +} + +void push_a(t_state *s) +{ + push_inner(&s->stack_a, &s->stack_b, "Push A", "pa"); +} + +void push_b(t_state *s) +{ + push_inner(&s->stack_b, &s->stack_a, "Push B", "pb"); +} diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c new file mode 100644 index 0000000..f630ea0 --- /dev/null +++ b/src/app/moves/rev_rotate.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rev_rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/10 18:39:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" + +void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop(stack, &e); + vec_i64_push_front(stack, e); + if (print) + me_printf("%s\n", print); +} + +void rev_rotate_a(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate A", "rra"); +} + +void rev_rotate_b(t_state *s) +{ + rev_rotate_inner(&s->stack_b, "RevRotate B", "rrb"); +} + +void rev_rotate_both(t_state *s) +{ + rev_rotate_inner(&s->stack_a, "RevRotate Both", NULL); + rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr"); +} diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c new file mode 100644 index 0000000..86cd2aa --- /dev/null +++ b/src/app/moves/rotate.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/10 18:38:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" + +void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +{ + t_i64 e; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop_front(stack, &e); + vec_i64_push(stack, e); + if (print) + me_printf("%s\n", print); +} + +void rotate_a(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate A", "ra"); +} + +void rotate_b(t_state *s) +{ + rotate_inner(&s->stack_b, "Rotate B", "rb"); +} + +void rotate_both(t_state *s) +{ + rotate_inner(&s->stack_a, "Rotate Both", NULL); + rotate_inner(&s->stack_b, "Rotate Both", "rr"); +} diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c new file mode 100644 index 0000000..934cedd --- /dev/null +++ b/src/app/moves/swap.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:40:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/vec/vec_i64.h" + +void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) +{ + t_i64 first; + t_i64 second; + + (void)(tag); + if (stack->len < 2) + return ; + vec_i64_pop_front(stack, &first); + vec_i64_pop_front(stack, &second); + vec_i64_push_front(stack, first); + vec_i64_push_front(stack, second); + if (print) + me_printf("%s\n", print); +} + +void swap_a(t_state *s) +{ + swap_inner(&s->stack_a, "Swap A", "sa"); +} + +void swap_b(t_state *s) +{ + swap_inner(&s->stack_b, "Swap B", "sb"); +} + +void swap_both(t_state *s) +{ + swap_inner(&s->stack_a, "Swap Both", NULL); + swap_inner(&s->stack_b, "Swap Both", "ss"); +} diff --git a/src/app/rotate.c b/src/app/rotate.c new file mode 100644 index 0000000..33c6652 --- /dev/null +++ b/src/app/rotate.c @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 18:59:56 by maiboyer #+# #+# */ +/* Updated: 2024/01/29 20:04:18 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/rotate.h" +#include "me/types.h" diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c new file mode 100644 index 0000000..45ed8df --- /dev/null +++ b/src/app/run_with_items.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* run_with_items.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:22:15 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/best_move.h" +#include "app/find_iter.h" +#include "app/rotate.h" +#include "app/state.h" +#include "app/target.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f); + +static inline struct s_functions funcs( void (*f)(void *), void (*r)(void *)) +{ + return ((struct s_functions){.forward = f, .reverse = r}); +} + +static inline void banana_first(t_state *state) +{ + t_usize idx; + + idx = best_index_to_move(state, &state->stack_a, &state->stack_b, + &max_iter_zero_pos); + run_func_with_best_rotate_for_item(state, + (t_best_move_args){ + .index = idx, .iter_func = &max_iter_zero_pos, .from = \ +&state->stack_a, .to = &state->stack_b, .args = state, .both = \ +funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_a, rev_rotate_a), \ +.other = funcs(rotate_b, rev_rotate_b), }); + push_b(state); + make_sorted_true_from_stack(state, &state->stack_b); +} + +bool is_sorted(t_vec_i64 *v) +{ + t_usize i; + + i = 1; + while (i < v->len) + { + if (v->buffer[i - 1] > v->buffer[i]) + return (false); + i++; + } + return (true); +} + +static inline void banana_second(t_state *state) +{ + t_usize idx; + + idx = best_index_to_move(state, &state->stack_b, &state->stack_a, + &min_iter_zero_pos); + run_func_with_best_rotate_for_item(state, (t_best_move_args){ + .index = idx, .from = &state->stack_b, \ +.to = &state->stack_a, .iter_func = &min_iter_zero_pos, .args = state, \ +.both = funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_b, \ +rev_rotate_b), .other = funcs(rotate_a, rev_rotate_a), }); + push_a(state); + make_sorted_true_from_stack(state, &state->stack_a); +} + +void run_with_items(t_state *state) +{ + t_rotation rot; + + if (is_sorted(&state->stack_a)) + return ; + while (state->stack_a.len > state->stack_b.len) + banana_first(state); + while (state->stack_a.len != 0) + push_b(state); + vec_i64_bool_reverse(&state->sorted); + make_sorted_all_false(state); + while (state->stack_b.len != 0) + banana_second(state); + rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len); + if (rot.value > flip(rot).value) + rot = flip(rot); + while (rot.value > 0) + { + if (rot.direction == FORWARD) + rotate_a(state); + else + rev_rotate_a(state); + rot.value--; + } +} diff --git a/src/app/sort2.c b/src/app/sort2.c new file mode 100644 index 0000000..a82f46c --- /dev/null +++ b/src/app/sort2.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 16:20:35 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +void sort_2(t_state *state) +{ + if (state->stack_a.buffer[0] > state->stack_a.buffer[1]) + swap_a(state); +} diff --git a/src/app/sort3.c b/src/app/sort3.c new file mode 100644 index 0000000..080bfe4 --- /dev/null +++ b/src/app/sort3.c @@ -0,0 +1,138 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 17:53:02 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); + +/* +fn sort_three(state: &mut State, selector: StackSelector, min_first: bool) { + macro_rules! stack { + () => { + match selector { + StackSelector::B => &mut state.b, + StackSelector::A => &mut state.a, + } + }; + } + let [swap, rotate, rev_rotate] = match selector { + StackSelector::A => [sa, ra, rra], + StackSelector::B => [sb, rb, rrb], + }; + match stack!().len() { + 2 => { + let func = match min_first { + true => PartialOrd::gt, + false => PartialOrd::lt, + }; + + if func(&stack!()[0].clone(), &stack!()[1]) { + swap(state); + } + return ; + } + 3 => {} + 0 | 1 | 4.. => return, + } + let mut c = stack!().clone(); + macro_rules! comb { + ($i1:literal, $i2:literal, $i3:literal) => { + &[c[$i1 - 1], c[$i2 - 1], c[$i3 - 1]] + }; + } + c.make_contiguous().sort_unstable(); + if min_first { + c.make_contiguous().reverse(); + } + stack!().make_contiguous(); + + if (stack!() == comb![1, 2, 3] / * abc * /) +{ + swap(state); + rev_rotate(state); +} +else if (stack!() == comb ![ 1, 3, 2 ] / * acb * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 2, 3, 1 ] / * bca * /) +{ + swap(state); +} +else if (stack !() == comb ![ 2, 1, 3 ] / * bac * /) +{ + rotate(state); +} +else if (stack !() == comb ![ 3, 2, 1 ] / * cba * /) +{ +} +else if (stack !() == comb ![ 3, 1, 2 ] / * cab * /) +{ + rotate(state); + swap(state); + rev_rotate(state); +} +} +*/ +// 2 3 1 +bool sort_i64(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs <= *rhs); +} + +void sort_3_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_3(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64); + vec_i64_reverse(&why_do_i_do_this); + sort_3_inner(state, state->stack_a.buffer, array); +} diff --git a/src/app/sort5.c b/src/app/sort5.c new file mode 100644 index 0000000..8c23853 --- /dev/null +++ b/src/app/sort5.c @@ -0,0 +1,139 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 15:50:10 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:50:53 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/find_place.h" +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include "me/mem/mem_compare.h" +#include "me/mem/mem_copy.h" + +void push_a(void *s); +void push_b(void *s); +void swap_a(void *s); +void swap_b(void *s); +void swap_both(void *s); +void rotate_a(void *s); +void rotate_b(void *s); +void rotate_both(void *s); +void rev_rotate_a(void *s); +void rev_rotate_b(void *s); +void rev_rotate_both(void *s); +void sort_2(t_state *state); + +bool sort_i64(t_i64 *lhs, t_i64 *rhs); + +bool sort_i64_other_way(t_i64 *lhs, t_i64 *rhs) +{ + return (*lhs >= *rhs); +} + +void sort_5_inner(t_state *state, t_i64 data[3], t_i64 sorted[3]) +{ + if ((data[0] == sorted[0]) && (data[1] == sorted[1]) + && (data[2] == sorted[2])) + (swap_a(state), rev_rotate_a(state)); + else if ((data[0] == sorted[0]) && (data[1] == sorted[2]) + && (data[2] == sorted[1])) + rotate_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[2]) + && (data[2] == sorted[0])) + swap_a(state); + else if ((data[0] == sorted[1]) && (data[1] == sorted[0]) + && (data[2] == sorted[2])) + rev_rotate_a(state); + else if ((data[0] == sorted[2]) && (data[1] == sorted[0]) + && (data[2] == sorted[1])) + (rotate_a(state), swap_a(state), rev_rotate_a(state)); +} + +void sort_5_inner2(t_state *state) +{ + t_usize index; + + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); + index = find_place(state->stack_b.buffer[0], state); + if (index == 0) + push_a(state); + else if (index == 1) + (push_a(state), swap_a(state)); + else if (index == 2) + (rotate_a(state), push_a(state), swap_a(state), rev_rotate_a(state)); + else if (index == 3) + (rev_rotate_a(state), push_a(state), rotate_a(state), rotate_a(state)); + else if (index == 4) + (push_a(state), rotate_a(state)); + make_sorted_true_from_stack(state, &state->stack_a); +} + +bool sort_5_specialized(t_state *state, t_i64 cur[5]) +{ + t_i64 target[5]; + t_vec_i64 why_do_i_do_this; + + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 5; + why_do_i_do_this.capacity = 5; + why_do_i_do_this.buffer = target; + mem_copy(target, state->stack_a.buffer, sizeof(t_i64) * 5); + vec_i64_sort(&why_do_i_do_this, sort_i64); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (me_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); + if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (me_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) + return (me_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + true); + if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ + target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) + return (me_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (false); +} + +void sort_5(t_state *state) +{ + t_i64 array[3]; + t_vec_i64 why_do_i_do_this; + + if (sort_5_specialized(state, state->stack_a.buffer)) + return ; + push_b(state); + push_b(state); + if (state->stack_b.buffer[0] >= state->stack_a.buffer[1]) + swap_b(state); + why_do_i_do_this.free_func = NULL; + why_do_i_do_this.len = 3; + why_do_i_do_this.capacity = 3; + why_do_i_do_this.buffer = array; + array[0] = state->stack_a.buffer[0]; + array[1] = state->stack_a.buffer[1]; + array[2] = state->stack_a.buffer[2]; + vec_i64_sort(&why_do_i_do_this, sort_i64_other_way); + sort_5_inner(state, state->stack_a.buffer, array); + sort_5_inner2(state); +} diff --git a/src/app/state.c b/src/app/state.c new file mode 100644 index 0000000..7d5e2dc --- /dev/null +++ b/src/app/state.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "app/types/type_i64_bool.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" +#include "me/vec/vec_i64.h" +#include "me/vec/vec_i64_bool.h" +#include +#include + +#if BONUS == 1 + +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +{ + return (lhs->value <= rhs->value); +} + +void free_state(t_state state) +{ + vec_i64_free(state.stack_a); + vec_i64_free(state.stack_b); + vec_i64_bool_free(state.sorted); +} + +bool duplicate_check(t_state *state) +{ + t_i64 last; + t_usize index; + + if (state->sorted.len == 0) + return (false); + index = 1; + last = state->sorted.buffer[0].value; + while (index < state->sorted.len) + { + if (last == state->sorted.buffer[index].value) + return (true); + last = state->sorted.buffer[index++].value; + } + return (false); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state state; + t_i32 atoi; + t_usize i; + + state.stack_a = vec_i64_new(count, NULL); + state.stack_b = vec_i64_new(count, NULL); + state.sorted = vec_i64_bool_new(count, NULL); + i = 0; + while (i < count) + { + if (str_to_i32(nums[i], 10, &atoi)) + (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); + vec_i64_push(&state.stack_a, atoi); + vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, + .active = false}); + i++; + } + vec_i64_bool_sort(&state.sorted, sort_i64_bool); + if (duplicate_check(&state)) + (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); + return (state); +} diff --git a/src/app/target.c b/src/app/target.c new file mode 100644 index 0000000..1444454 --- /dev/null +++ b/src/app/target.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* target.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:30:27 by maiboyer #+# #+# */ +/* Updated: 2024/02/02 22:40:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/rotate.h" +#include "app/target.h" + +t_rotation target(t_usize from, t_usize to, t_usize ring_size) +{ + if (ring_size == 0) + ring_size++; + if (from > to) + return (reverse(from - to, ring_size)); + else + return (forward(to - from, ring_size)); +} diff --git a/src/bonus/move1.c b/src/bonus/move1.c new file mode 100644 index 0000000..6ca0754 --- /dev/null +++ b/src/bonus/move1.c @@ -0,0 +1,11 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 19:15:21 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 19:15:21 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ diff --git a/src/buffered_str/mod.c b/src/buffered_str/mod.c new file mode 100644 index 0000000..f408679 --- /dev/null +++ b/src/buffered_str/mod.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mod.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */ +/* Updated: 2023/12/26 19:54:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_set_zero.h" +#include "me/string/str_l_cat.h" +#include "me/string/str_l_copy.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include + +bool push_str_buffer(t_buffer_str *buf, t_const_str to_push) +{ + t_usize to_push_len; + t_str temp_buffer; + t_usize new_capacity; + + if (buf == NULL || to_push == NULL) + return (true); + to_push_len = str_len(to_push); + while (buf->len + to_push_len + 2 > buf->capacity) + { + new_capacity = (buf->capacity * 3) / 2 + 1; + temp_buffer = mem_alloc(new_capacity); + if (temp_buffer == NULL) + return (true); + str_l_copy(temp_buffer, buf->buf, new_capacity); + free(buf->buf); + buf->buf = temp_buffer; + buf->capacity = new_capacity; + } + buf->len += to_push_len; + str_l_cat(buf->buf, to_push, buf->capacity); + return (false); +} + +bool push_str_char(t_buffer_str *buf, char to_push) +{ + char push_str[2]; + + push_str[0] = to_push; + push_str[1] = 0; + return (push_str_buffer(buf, push_str)); +} + +void str_clear(t_buffer_str *buf) +{ + mem_set_zero(buf->buf, buf->capacity); + buf->len = 0; + return ; +} + +t_buffer_str alloc_new_buffer(t_usize capacity) +{ + t_buffer_str out; + t_str buf; + + if (capacity == 0) + capacity = 16; + buf = mem_alloc(sizeof(char) * capacity); + if (buf == NULL) + { + out.buf = NULL; + out.capacity = 0; + out.len = 0; + return (out); + } + out.buf = buf; + out.capacity = capacity; + out.len = 0; + return (out); +} diff --git a/src/char/isalnum.c b/src/char/isalnum.c new file mode 100644 index 0000000..c4ebcfc --- /dev/null +++ b/src/char/isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/09 19:38:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalnum.h" +#include "me/char/isalpha.h" +#include "me/char/isdigit.h" + +bool me_isalnum(char chr) +{ + return (me_isalpha(chr) || me_isdigit(chr)); +} diff --git a/src/char/isalpha.c b/src/char/isalpha.c new file mode 100644 index 0000000..bffddf0 --- /dev/null +++ b/src/char/isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 04:01:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" + +bool me_isalpha(char chr) +{ + return (('z' >= chr && chr >= 'a') || ('Z' >= chr && chr >= 'A')); +} diff --git a/src/char/isascii.c b/src/char/isascii.c new file mode 100644 index 0000000..208a84f --- /dev/null +++ b/src/char/isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 17:51:01 by maiboyer #+# #+# */ +/* Updated: 2023/11/09 18:25:14 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isascii.h" + +bool me_isascii(char chr) +{ + return (0 <= chr && chr <= 127); +} diff --git a/src/char/isdigit.c b/src/char/isdigit.c new file mode 100644 index 0000000..f75d219 --- /dev/null +++ b/src/char/isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:59:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isdigit.h" + +bool me_isdigit(char chr) +{ + return (chr >= '0' && chr <= '9'); +} diff --git a/src/char/islower.c b/src/char/islower.c new file mode 100644 index 0000000..384e88c --- /dev/null +++ b/src/char/islower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:58:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/islower.h" + +bool me_islower(char chr) +{ + return (chr >= 'a' && chr <= 'z'); +} diff --git a/src/char/isprint.c b/src/char/isprint.c new file mode 100644 index 0000000..f45c9ee --- /dev/null +++ b/src/char/isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:42:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isprint.h" + +bool me_isprint(char chr) +{ + return (chr >= ' ' && chr <= '~'); +} diff --git a/src/char/isspace.c b/src/char/isspace.c new file mode 100644 index 0000000..1757f34 --- /dev/null +++ b/src/char/isspace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:26:25 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 02:39:24 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isspace.h" + +bool me_isspace(char chr) +{ + return (chr == ' ' || chr == '\f' || chr == '\n' || chr == '\r' + || chr == '\t' || chr == '\v'); +} diff --git a/src/char/isupper.c b/src/char/isupper.c new file mode 100644 index 0000000..405cb09 --- /dev/null +++ b/src/char/isupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 04:01:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isupper.h" + +bool me_isupper(char chr) +{ + return ('Z' >= chr && chr >= 'A'); +} diff --git a/src/char/tolower.c b/src/char/tolower.c new file mode 100644 index 0000000..4ee817c --- /dev/null +++ b/src/char/tolower.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:56:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isupper.h" +#include "me/char/tolower.h" + +bool me_tolower(char chr) +{ + if (me_isupper(chr)) + return (chr + ('a' - 'A')); + else + return (chr); +} diff --git a/src/char/toupper.c b/src/char/toupper.c new file mode 100644 index 0000000..9888b7e --- /dev/null +++ b/src/char/toupper.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */ +/* Updated: 2023/11/04 16:51:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/islower.h" +#include "me/char/toupper.h" + +bool me_toupper(char chr) +{ + if (me_islower(chr)) + return (chr - ('a' - 'A')); + else + return (chr); +} diff --git a/src/convert/atoi.c b/src/convert/atoi.c new file mode 100644 index 0000000..076e6ef --- /dev/null +++ b/src/convert/atoi.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:14:00 by maiboyer #+# #+# */ +/* Updated: 2024/01/11 15:37:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isdigit.h" +#include "me/char/isspace.h" +#include "me/convert/atoi.h" + +t_i32 me_atoi(t_const_str str) +{ + t_u64 out; + t_u64 sign; + t_usize i; + + out = 0; + i = 0; + sign = 1; + while (me_isspace(str[i])) + i++; + if (str[i] == '+' || str[i] == '-') + if (str[i++] == '-') + sign = -1; + while (me_isdigit(str[i])) + { + out *= 10; + out += str[i] - '0'; + i++; + } + return ((t_i32)(out * sign)); +} + +t_i64 me_atoi_64(t_const_str str) +{ + t_u64 out; + t_u64 sign; + t_usize i; + + out = 0; + i = 0; + sign = 1; + while (me_isspace(str[i])) + i++; + if (str[i] == '+' || str[i] == '-') + if (str[i++] == '-') + sign = -1; + while (me_isdigit(str[i])) + { + out *= 10; + out += str[i] - '0'; + i++; + } + return ((t_i64)(out * sign)); +} diff --git a/src/convert/itoa.c b/src/convert/itoa.c new file mode 100644 index 0000000..426d84c --- /dev/null +++ b/src/convert/itoa.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/03 21:05:46 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 14:56:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/convert/itoa.h" +#include "me/mem/mem_set.h" +#include "me/string/str_clone.h" +#include + +static void me_itoa_inner(t_u64 nb, t_str out) +{ + t_i32 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 1000000000; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +t_str me_itoa(t_i32 nb) +{ + char out[12]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, 12); + if (nb < 0) + { + out[0] = '-'; + me_itoa_inner(-n, out + 1); + } + else if (nb == 0) + out[0] = '0'; + else + me_itoa_inner(n, out); + return (str_clone(out)); +} +/*R +int main(void) +{ + me_putnbr(-2147483648); + write(1, "\n", 1); + me_putnbr(0); + write(1, "\n", 1); + me_putnbr(12345); + return (0); +} +R*/ diff --git a/src/convert/str_to_i16.c b/src/convert/str_to_i16.c new file mode 100644 index 0000000..b1d2bc8 --- /dev/null +++ b/src/convert/str_to_i16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i16 digit, t_u8 op, t_i16 *result) +{ + t_i16 rhs; + t_i16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i16 *out) +{ + t_u32 digit; + t_i16 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i16(t_const_str s, t_u32 radix, t_i16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i16_utils.c b/src/convert/str_to_i16_utils.c new file mode 100644 index 0000000..4ecbbe2 --- /dev/null +++ b/src/convert/str_to_i16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i16_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if (rhs > 0 && (lhs > 32767 - rhs)) + return (ERROR); + *out = (t_i16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < \ + -32768 + rhs))) + return (ERROR); + *out = (t_i16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out) +{ + t_i16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i32.c b/src/convert/str_to_i32.c new file mode 100644 index 0000000..4956029 --- /dev/null +++ b/src/convert/str_to_i32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i32 digit, t_u8 op, t_i32 *result) +{ + t_i32 rhs; + t_i32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i32 *out) +{ + t_u32 digit; + t_i32 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i32(t_const_str s, t_u32 radix, t_i32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i32_utils.c b/src/convert/str_to_i32_utils.c new file mode 100644 index 0000000..5b6f5fd --- /dev/null +++ b/src/convert/str_to_i32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i32_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:56:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if (rhs > 0 && (lhs > 2147483647 - rhs)) + return (ERROR); + *out = (t_i32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < \ + -2147483648 + rhs))) + return (ERROR); + *out = (t_i32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out) +{ + t_i32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i64.c b/src/convert/str_to_i64.c new file mode 100644 index 0000000..56a48cc --- /dev/null +++ b/src/convert/str_to_i64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i64 digit, t_u8 op, t_i64 *result) +{ + t_i64 rhs; + t_i64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i64 *out) +{ + t_u32 digit; + t_i64 result; + + result = 0ll; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i64(t_const_str s, t_u32 radix, t_i64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i64_utils.c b/src/convert/str_to_i64_utils.c new file mode 100644 index 0000000..1dd268d --- /dev/null +++ b/src/convert/str_to_i64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 18:55:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if (rhs > 0 && (lhs > 9223372036854775807ll - rhs)) + return (ERROR); + *out = (t_i64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < \ + -(~9223372036854775807ll + 1) + rhs))) + return (ERROR); + *out = (t_i64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out) +{ + t_i64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_i8.c b/src/convert/str_to_i8.c new file mode 100644 index 0000000..80f91a0 --- /dev/null +++ b/src/convert/str_to_i8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_i8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_i8 digit, t_u8 op, t_i8 *result) +{ + t_i8 rhs; + t_i8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_i8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_i8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_i8 *out) +{ + t_u32 digit; + t_i8 result; + + result = 0; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_i8(t_const_str s, t_u32 radix, t_i8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && true) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, true, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_i8_utils.c b/src/convert/str_to_i8_utils.c new file mode 100644 index 0000000..2073dd6 --- /dev/null +++ b/src/convert/str_to_i8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if (rhs > 0 && (lhs > 127 - rhs)) + return (ERROR); + *out = (t_i8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + if ((((rhs & (1 << (sizeof(t_i8) - 1)) || rhs == 0) || !true) && (lhs < -128 + + rhs))) + return (ERROR); + *out = (t_i8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out) +{ + t_i8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u16.c b/src/convert/str_to_u16.c new file mode 100644 index 0000000..7c5bbaa --- /dev/null +++ b/src/convert/str_to_u16.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u16) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u16 digit, t_u8 op, t_u16 *result) +{ + t_u16 rhs; + t_u16 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u16(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u16(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u16 *out) +{ + t_u32 digit; + t_u16 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u16(t_const_str s, t_u32 radix, t_u16 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u16_utils.c b/src/convert/str_to_u16_utils.c new file mode 100644 index 0000000..85fd7b2 --- /dev/null +++ b/src/convert/str_to_u16_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if (rhs > 0 && (lhs > 65535u - rhs)) + return (ERROR); + *out = (t_u16)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + if ((((rhs & (1 << (sizeof(t_u16) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u16)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out) +{ + t_u16 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u32.c b/src/convert/str_to_u32.c new file mode 100644 index 0000000..1b625d1 --- /dev/null +++ b/src/convert/str_to_u32.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u32) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u32 digit, t_u8 op, t_u32 *result) +{ + t_u32 rhs; + t_u32 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u32(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u32(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u32 *out) +{ + t_u32 digit; + t_u32 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u32(t_const_str s, t_u32 radix, t_u32 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u32_utils.c b/src/convert/str_to_u32_utils.c new file mode 100644 index 0000000..3a929d5 --- /dev/null +++ b/src/convert/str_to_u32_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if (rhs > 0 && (lhs > 4294967295u - rhs)) + return (ERROR); + *out = (t_u32)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + if ((((rhs & (1 << (sizeof(t_u32) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u32)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out) +{ + t_u32 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u64.c b/src/convert/str_to_u64.c new file mode 100644 index 0000000..b744be8 --- /dev/null +++ b/src/convert/str_to_u64.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u64) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u64 digit, t_u8 op, t_u64 *result) +{ + t_u64 rhs; + t_u64 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u64(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u64(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u64 *out) +{ + t_u32 digit; + t_u64 result; + + result = 0llu; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u64(t_const_str s, t_u32 radix, t_u64 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u64_utils.c b/src/convert/str_to_u64_utils.c new file mode 100644 index 0000000..f2bb77f --- /dev/null +++ b/src/convert/str_to_u64_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if (rhs > 0 && (lhs > 18446744073709551615llu - rhs)) + return (ERROR); + *out = (t_u64)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + if ((((rhs & (1 << (sizeof(t_u64) - 1)) || rhs == 0) || !false) + && (lhs < 0llu + rhs))) + return (ERROR); + *out = (t_u64)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out) +{ + t_u64 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/convert/str_to_u8.c b/src/convert/str_to_u8.c new file mode 100644 index 0000000..8fac9fa --- /dev/null +++ b/src/convert/str_to_u8.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/string/str_len.h" +#include "me/types.h" + +#define OP_ADD 0b0001 +#define OP_SUB 0b0010 +#define OP_MUL 0b0100 +#define OP_CHK 0b1000 + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out); + +static inline bool can_not_overflow(t_u32 radix, bool is_signed_type, + t_usize digits_len) +{ + return (radix <= 16 && digits_len <= sizeof(t_u8) * 2 + - (t_usize)is_signed_type); +} + +static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out) +{ + t_u32 digit; + + if (radix < 2 || radix > 36) + return (ERROR); + digit = ascii - '0'; + if (radix > 10 && digit >= 10) + { + if (!me_isalpha(ascii)) + return (ERROR); + digit = ((ascii | 0b100000) - 'a') + 10; + } + if (digit >= radix) + return (ERROR); + *out = digit; + return (NO_ERROR); +} + +static inline t_error do_operation(t_u8 digit, t_u8 op, t_u8 *result) +{ + t_u8 rhs; + t_u8 res; + + rhs = *result; + res = *result; + if (op & OP_CHK) + { + if (op & OP_MUL && checked_mul_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_ADD && checked_add_u8(rhs, digit, &res)) + return (ERROR); + else if (op & OP_SUB && checked_sub_u8(rhs, digit, &res)) + return (ERROR); + } + else + { + if (op & OP_MUL) + res = rhs * digit; + else if (op & OP_ADD) + res = rhs + digit; + else if (op & OP_SUB) + res = rhs - digit; + } + *result = res; + return (NO_ERROR); +} + +static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op, + t_u8 *out) +{ + t_u32 digit; + t_u8 result; + + result = 0u; + while (*s) + { + if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result)) + return (ERROR); + if (to_digit(*s, radix, &digit)) + return (ERROR); + if (do_operation(digit, op, &result)) + return (ERROR); + s++; + } + if (out) + *out = result; + return (NO_ERROR); +} + +t_error str_to_u8(t_const_str s, t_u32 radix, t_u8 *out) +{ + t_usize digits_len; + bool is_positive; + t_u8 op; + + if (radix < 2 || radix > 36) + return (ERROR); + digits_len = str_len(s); + is_positive = true; + if (digits_len == 0) + return (ERROR); + if ((s[0] == '-' || s[0] == '+') && s[1] == '\0') + return (ERROR); + else if (s[0] == '+') + is_positive = (s++, digits_len--, true); + else if (s[0] == '-' && false) + is_positive = (s++, digits_len--, false); + if (is_positive) + op = OP_ADD; + else + op = OP_SUB; + if (!can_not_overflow(radix, false, digits_len)) + op |= OP_CHK; + return (loop_inner(s, radix, op, out)); +} diff --git a/src/convert/str_to_u8_utils.c b/src/convert/str_to_u8_utils.c new file mode 100644 index 0000000..14a4a04 --- /dev/null +++ b/src/convert/str_to_u8_utils.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_to_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */ +/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/char/isalpha.h" +#include "me/convert/str_to_numbers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" + +t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if (rhs > 0 && (lhs > 128u - rhs)) + return (ERROR); + *out = (t_u8)(lhs + rhs); + return (NO_ERROR); +} + +t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + if ((((rhs & (1 << (sizeof(t_u8) - 1)) || rhs == 0) || !false) && (lhs < 0u + + rhs))) + return (ERROR); + *out = (t_u8)(lhs - rhs); + return (NO_ERROR); +} + +t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out) +{ + t_u8 mul; + + mul = lhs * rhs; + if (lhs != 0 && mul / lhs != rhs) + return (ERROR); + *out = mul; + return (NO_ERROR); +} diff --git a/src/fs/close.c b/src/fs/close.c new file mode 100644 index 0000000..1b4ef62 --- /dev/null +++ b/src/fs/close.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* close.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/10 19:05:48 by maix ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/close.h" +#include "me/types.h" +#include + +bool me_close(t_file file, t_i32 *error) +{ + t_i32 res; + bool out; + + res = close(file); + out = res != 0; + if (res && error != NULL) + *error = res; + return (out); +} diff --git a/src/fs/open.c b/src/fs/open.c new file mode 100644 index 0000000..96d6200 --- /dev/null +++ b/src/fs/open.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 18:19:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/open.h" +#include + +t_error me_open(t_const_str path, bool read, bool write, t_file *file_out) +{ + t_file out; + int flags; + + flags = 0; + if (read && write) + flags = O_RDWR; + else if (read) + flags = O_RDONLY; + else if (write) + flags = O_WRONLY; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} + +t_error me_open_truncate(t_const_str path, t_file *file_out) +{ + t_file out; + int flags; + + unlink(path); + flags = O_WRONLY | O_CREAT | O_TRUNC; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} + +t_error me_open_create(t_const_str path, t_file *file_out) +{ + t_file out; + int flags; + + flags = O_WRONLY | O_CREAT | O_APPEND; + out = open(path, flags, 0666); + if (out < 0) + return (ERROR); + *file_out = out; + return (NO_ERROR); +} diff --git a/src/fs/putchar_fd.c b/src/fs/putchar_fd.c new file mode 100644 index 0000000..2a10051 --- /dev/null +++ b/src/fs/putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 13:22:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putchar_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putchar_fd(char chr, t_file file) +{ + me_write(file, (t_u8 *)&chr, 1); +} diff --git a/src/fs/putendl_fd.c b/src/fs/putendl_fd.c new file mode 100644 index 0000000..ba94ff5 --- /dev/null +++ b/src/fs/putendl_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 16:23:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putstr_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putendl_fd(t_str str, t_file file) +{ + if (str == NULL) + return ; + me_write(file, (t_u8 *)str, str_len(str)); + me_write(file, (t_u8 *)"\n", 1); +} diff --git a/src/fs/putnbr_fd.c b/src/fs/putnbr_fd.c new file mode 100644 index 0000000..62143ad --- /dev/null +++ b/src/fs/putnbr_fd.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */ +/* Updated: 2023/11/08 13:11:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putnbr_fd.h" +#include "me/fs/write.h" +#include "me/mem/mem_set.h" + +static void me_inner(t_u64 nb, t_str out, t_usize *idx) +{ + bool need_print; + t_u64 modulus; + char c; + + modulus = 1000000000; + need_print = false; + while (modulus) + { + c = (char)(nb / modulus); + if (c != 0 || need_print) + { + out[(*idx)++] = c + '0'; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +void me_putnbr_fd(t_i32 n, t_file file) +{ + t_usize idx; + t_u64 nb; + char out[15]; + + nb = (t_u64)n; + idx = 0; + if (nb < 0) + { + out[idx++] = '-'; + nb = -nb; + } + if (nb == 0) + out[idx++] = '0'; + me_inner(nb, out, &idx); + me_write(file, (t_u8 *)out, idx); +} diff --git a/src/fs/putstr_fd.c b/src/fs/putstr_fd.c new file mode 100644 index 0000000..065b490 --- /dev/null +++ b/src/fs/putstr_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */ +/* Updated: 2023/11/10 16:23:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/putstr_fd.h" +#include "me/fs/write.h" +#include "me/string/str_len.h" + +void me_putstr_fd(t_str str, t_file file) +{ + if (str == NULL) + return ; + me_write(file, (t_u8 *)str, str_len(str)); +} diff --git a/src/fs/read.c b/src/fs/read.c new file mode 100644 index 0000000..6635d6b --- /dev/null +++ b/src/fs/read.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:08:10 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/read.h" +#include + +t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out) +{ + ssize_t out; + + out = read(fd, buffer, buffer_max); + if (out == 0 && buffer_max != 0 && eof_out != NULL) + *eof_out = true; + return (out); +} diff --git a/src/fs/read_to_vec.c b/src/fs/read_to_vec.c new file mode 100644 index 0000000..b04f316 --- /dev/null +++ b/src/fs/read_to_vec.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_to_vec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 18:15:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/open.h" +#include "me/fs/read.h" +#include "me/mem/mem_copy.h" +#include "me/vec/vec_u8.h" + +#define READ_BUFFER_SIZE 4096 + +bool read_to_vec(t_const_str path, t_vec_u8 *out) +{ + t_u8 temp_buffer[READ_BUFFER_SIZE]; + t_isize read_amount; + t_file f; + bool eof; + t_usize current_size; + + eof = false; + current_size = 0; + if (out == NULL || me_open(path, true, false, &f)) + return (true); + *out = vec_u8_new(READ_BUFFER_SIZE, NULL); + while (!eof) + { + read_amount = me_read(f, temp_buffer, READ_BUFFER_SIZE, &eof); + if (read_amount < 0) + return (true); + vec_u8_reserve(out, current_size + (t_usize)read_amount); + mem_copy(&out->buffer[out->len], temp_buffer, (t_usize)read_amount); + out->len += (t_usize)read_amount; + } + return (false); +} diff --git a/src/fs/write.c b/src/fs/write.c new file mode 100644 index 0000000..a51e5fe --- /dev/null +++ b/src/fs/write.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* write.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */ +/* Updated: 2023/11/03 15:44:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/write.h" +#include + +bool me_write(t_file fd, t_u8 *buffer, t_i64 size) +{ + return (write(fd, buffer, size) < 0); +} diff --git a/src/gnl/get_next_line.c b/src/gnl/get_next_line.c new file mode 100644 index 0000000..74ec66f --- /dev/null +++ b/src/gnl/get_next_line.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 17:38:21 by maix #+# #+# */ +/* Updated: 2023/12/11 19:10:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_move.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include + +static t_static_buffer *get_next_line_buffer(t_file fd) +{ + t_usize index; + static t_static_buffer bufs[BUFFER_LENGTH] = {0}; + + index = 0; + while (index < BUFFER_LENGTH && (bufs[index].fd != fd && bufs[index].used)) + index++; + if (index == BUFFER_LENGTH) + return (NULL); + bufs[index].fd = fd; + bufs[index].used = true; + return (&bufs[index]); +} + +static bool copy_next_line_into_buffer(t_file fd, t_buffer_str *out, + char *temp_buffer, t_usize amount) +{ + char *buf; + char *newline; + bool got_newline; + t_usize other_part_len; + + buf = get_next_line_buffer(fd)->buf; + newline = buf; + while (*newline != '\n' && *newline && newline < buf + amount) + newline++; + got_newline = *newline == '\n'; + other_part_len = amount - (t_usize)(newline - buf + !got_newline); + if (amount < (t_usize)(newline - buf + !got_newline)) + other_part_len = 0; + mem_move(temp_buffer, buf, (t_usize)(newline - buf + got_newline)); + buf[(t_usize)(newline - buf)] = 0; + temp_buffer[(t_usize)(newline - buf + got_newline)] = 0; + mem_move(buf, newline + 1, other_part_len); + push_str_buffer(out, temp_buffer); + buf[amount - (t_usize)(newline - buf + got_newline)] = 0; + return (got_newline); +} + +static bool read_and_copy(t_file fd, t_buffer_str *out, char *tmp, + t_copy_flags *flags) +{ + t_isize amount; + t_static_buffer *buf; + + buf = get_next_line_buffer(fd); + amount = read(fd, &buf->buf, BUFFER_SIZE); + flags->error = amount < 0; + if (flags->error) + return (true); + buf->init = true; + if ((t_isize)amount < (t_isize)BUFFER_SIZE) + { + copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount); + flags->empty_read = (amount == 0 && out->len == 0); + return (true); + } + buf->buf[(t_usize)amount] = 0; + return (copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount)); +} + +static bool handle_leftovers(t_file fd, char *temp_buffer, t_buffer_str *buf) +{ + t_static_buffer *static_buffer; + + static_buffer = get_next_line_buffer(fd); + if (static_buffer->init) + { + if (copy_next_line_into_buffer(fd, buf, temp_buffer, + str_len(static_buffer->buf))) + { + free(temp_buffer); + return (true); + } + } + return (false); +} + +t_buffer_str get_next_line(t_file fd, bool *error) +{ + t_buffer_str buf; + char *temp_buffer; + t_copy_flags flags; + + *error = false; + if (fd < 0 || BUFFER_SIZE <= 0) + return (*error = true, (t_buffer_str){0}); + flags = (t_copy_flags){ + .error = false, + .empty_read = false, + }; + temp_buffer = mem_alloc(sizeof(char) * (BUFFER_SIZE + 2)); + buf = alloc_new_buffer(32); + if (handle_leftovers(fd, temp_buffer, &buf)) + return (buf); + while (!read_and_copy(fd, &buf, temp_buffer, &flags) && !flags.empty_read) + ; + free(temp_buffer); + if (flags.error || flags.empty_read) + { + free(buf.buf); + return (*error = true, (t_buffer_str){0}); + } + return (buf); +} diff --git a/src/hash/hash_signed.c b/src/hash/hash_signed.c new file mode 100644 index 0000000..28fd330 --- /dev/null +++ b/src/hash/hash_signed.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash_signed.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:26:06 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 17:27:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +void hasher_write_i8(t_hasher *hasher, t_i8 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_i16(t_hasher *hasher, t_i16 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 2); +} + +void hasher_write_i32(t_hasher *hasher, t_i32 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_i64(t_hasher *hasher, t_i64 n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, 1); +} + +void hasher_write_isize(t_hasher *hasher, t_isize n) +{ + hasher_write_bytes(hasher, (t_u8 *)&n, sizeof(t_isize)); +} diff --git a/src/hash/hash_unsigned.c b/src/hash/hash_unsigned.c new file mode 100644 index 0000000..2e4ed86 --- /dev/null +++ b/src/hash/hash_unsigned.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash_unsigned.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:25:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:37:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +void hasher_write_u8(t_hasher *hasher, t_u8 byte) +{ + hasher->hash_bytes(hasher->hasher, &byte, 1); +} + +void hasher_write_u16(t_hasher *hasher, t_u16 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 2); +} + +void hasher_write_u32(t_hasher *hasher, t_u32 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 4); +} + +void hasher_write_u64(t_hasher *hasher, t_u64 byte) +{ + hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 8); +} diff --git a/src/hash/hasher.c b/src/hash/hasher.c new file mode 100644 index 0000000..8e544e1 --- /dev/null +++ b/src/hash/hasher.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hasher.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 15:58:12 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:44:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/types.h" + +t_u64 hasher_finish(t_hasher *hasher) +{ + return (hasher->finish(hasher->hasher)); +} + +t_u64 hasher_reset_and_finish(t_hasher *hasher) +{ + return (hasher->reset_and_finish(hasher->hasher)); +} + +void hasher_write_bytes(t_hasher *hasher, t_u8 *bytes, t_usize count) +{ + hasher->hash_bytes(hasher->hasher, bytes, count); +} diff --git a/src/hash/sip/sip13.c b/src/hash/sip/sip13.c new file mode 100644 index 0000000..d9ab515 --- /dev/null +++ b/src/hash/sip/sip13.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip13.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 19:32:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:48:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip.h" +#include "me/hash/sip/sip_utils.h" +#include "me/mem/mem_alloc.h" + +t_hasher hasher_sip13_new(void) +{ + t_hasher out; + t_sip13 *inner; + + inner = mem_alloc(sizeof(t_sip13)); + inner->state = create_state_with_key(0, 0); + inner->k0 = 0; + inner->k1 = 0; + out.hasher = inner; + out.hash_bytes = (t_hash_bytes)sip13_write_bytes; + out.finish = (t_hasher_finish)sip13_finish; + out.reset_and_finish = (t_hasher_reset_and_finish)sip13_reset_and_finish; + return (out); +} diff --git a/src/hash/sip/sip_utils.c b/src/hash/sip/sip_utils.c new file mode 100644 index 0000000..aed4a33 --- /dev/null +++ b/src/hash/sip/sip_utils.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 20:02:12 by maix #+# #+# */ +/* Updated: 2023/12/11 19:09:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip.h" +#include "me/hash/sip/sip_utils.h" +#include "me/num/u64.h" +#include "me/num/usize.h" +#include +#include + +static t_usize me_min(t_usize lhs, t_usize rhs) +{ + if (lhs < rhs) + return (lhs); + return (rhs); +} + +static t_usize handle_remaining(t_sip13 *self, t_u8 *msg, t_usize count, + bool *exit_early) +{ + t_usize needed; + + needed = 0; + *exit_early = false; + if (self->ntail != 0) + { + needed = 8 - self->ntail; + self->tail |= u64_from_bytes(msg, me_min(count, needed)) << (8 \ + * self->ntail); + if (count < needed) + { + self->ntail += count; + *exit_early = true; + return (needed); + } + else + { + self->state.v3 ^= self->tail; + compress(&self->state); + self->state.v0 ^= self->tail; + self->ntail = 0; + } + } + return (needed); +} + +t_usize read_u8_to_u64(t_u8 p[]) +{ + return (((t_u64)((p)[0])) | ((t_u64)((p)[1]) << 8) | \ + ((t_u64)((p)[2]) << 16) | ((t_u64)((p)[3]) << 24) | \ + ((t_u64)((p)[4]) << 32) | ((t_u64)((p)[5]) << 40) | \ + ((t_u64)((p)[6]) << 48) | ((t_u64)((p)[7]) << 56)); +} + +void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count) +{ + bool exit_early; + t_usize needed; + t_usize left; + t_usize i; + t_u64 mi; + + self->length += count; + needed = handle_remaining(self, msg, count, &exit_early); + if (exit_early) + return ; + count = count - needed; + left = count & 0x7; + i = needed; + while (i < count - left) + { + mi = read_u8_to_u64(msg + i); + self->state.v3 ^= mi; + compress(&self->state); + self->state.v0 ^= mi; + i += 8; + } + self->tail = u64_from_7bytes(msg, i, left); + self->ntail = left; +} + +t_u64 sip13_finish(t_sip13 *self) +{ + t_sip_state state; + t_u64 b; + + state = self->state; + b = (((t_u64)self->length & 0xff) << 56) | self->tail; + state.v3 ^= b; + compress(&state); + state.v0 ^= b; + state.v2 ^= 0xff; + compress(&state); + compress(&state); + free(self); + return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3); +} diff --git a/src/hash/sip/sip_utils2.c b/src/hash/sip/sip_utils2.c new file mode 100644 index 0000000..4575821 --- /dev/null +++ b/src/hash/sip/sip_utils2.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sip_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:29:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/27 16:52:17 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip/sip_utils.h" +#include "me/num/usize.h" + +void compress(t_sip_state *state) +{ + state->v0 = state->v0 + state->v1; + state->v1 = usize_rotate_left(state->v1, 13); + state->v1 ^= state->v0; + state->v0 = usize_rotate_left(state->v0, 32); + state->v2 = state->v2 + state->v3; + state->v3 = usize_rotate_left(state->v3, 16); + state->v3 ^= state->v2; + state->v0 = state->v0 + state->v3; + state->v3 = usize_rotate_left(state->v3, 21); + state->v3 ^= state->v0; + state->v2 = state->v2 + state->v1; + state->v1 = usize_rotate_left(state->v1, 17); + state->v1 ^= state->v2; + state->v2 = usize_rotate_left(state->v2, 32); +} + +t_sip_state create_state_with_key(t_u64 k0, t_u64 k1) +{ + t_sip_state state; + + state = (t_sip_state){.v0 = 0, .v1 = 0, .v2 = 0, .v3 = 0}; + state.v0 = k0 ^ 0x736f6d6570736575; + state.v1 = k1 ^ 0x646f72616e646f6d; + state.v2 = k0 ^ 0x6c7967656e657261; + state.v3 = k1 ^ 0x7465646279746573; + return (state); +} + +t_u64 sip13_reset_and_finish(t_sip13 *self) +{ + t_sip_state state; + t_u64 b; + t_u64 ret; + + state = self->state; + b = (((t_u64)self->length & 0xff) << 56) | self->tail; + state.v3 ^= b; + compress(&state); + state.v0 ^= b; + state.v2 ^= 0xff; + compress(&state); + compress(&state); + ret = (state.v0 ^ state.v1 ^ state.v2 ^ state.v3); + self->length = 0; + self->state.v0 = self->k0 ^ 0x736f6d6570736575; + self->state.v1 = self->k1 ^ 0x646f72616e646f6d; + self->state.v2 = self->k0 ^ 0x6c7967656e657261; + self->state.v3 = self->k1 ^ 0x7465646279746573; + self->ntail = 0; + return (ret); +} diff --git a/src/img/qoi/qoi_decode.c b/src/img/qoi/qoi_decode.c new file mode 100644 index 0000000..971f8bc --- /dev/null +++ b/src/img/qoi/qoi_decode.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_decode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:31:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:35:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi.h" +#include "me/img/qoi/qoi_utils.h" +#include "me/img/qoi/qoi_decode.h" + +static inline void *qoi_decode_inner(t_decode_vals *vals, const t_u8 *bytes, + t_i32 channels, t_i32 size) +{ + t_u8 *pixels; + + pixels = (t_u8 *)mem_alloc(vals->px_len); + if (!pixels) + return (NULL); + mem_set_zero(vals->index, sizeof(vals->index)); + vals->px.v = 0; + vals->px.rgba.a = 255; + vals->chunks_len = size - (t_i32) sizeof(t_u8[8]); + vals->px_pos = 0; + while (vals->px_pos < vals->px_len) + { + if (vals->run > 0) + vals->run--; + else if (vals->p < vals->chunks_len) + qoi_decode_inner_inner(vals, bytes); + pixels[vals->px_pos + 0] = vals->px.rgba.r; + pixels[vals->px_pos + 1] = vals->px.rgba.g; + pixels[vals->px_pos + 2] = vals->px.rgba.b; + if (channels == 4) + pixels[vals->px_pos + 3] = vals->px.rgba.a; + vals->px_pos += channels; + } + return (pixels); +} + +void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc, + t_i32 channels) +{ + const t_u8 *bytes; + t_decode_vals vals; + + vals.p = 0; + vals.run = 0; + if (data == NULL || desc == NULL || (channels != 0 && channels != 3 + && channels != 4) || size < QOI_HEADER_SIZE + + (t_i32) sizeof(t_u8[8])) + return (NULL); + bytes = (const t_u8 *)data; + vals.header_magic = qoi_read_32(bytes, &vals.p); + desc->width = qoi_read_32(bytes, &vals.p); + desc->height = qoi_read_32(bytes, &vals.p); + desc->channels = bytes[vals.p++]; + desc->colorspace = bytes[vals.p++]; + if (desc->width == 0 || desc->height == 0 || desc->channels < 3 + || desc->channels > 4 || desc->colorspace > 1 + || vals.header_magic != QOI_MAGIC || desc->height >= QOI_PIXELS_MAX + / desc->width) + return (NULL); + if (channels == 0) + channels = desc->channels; + vals.px_len = desc->width * desc->height * channels; + return (qoi_decode_inner(&vals, bytes, channels, size)); +} diff --git a/src/img/qoi/qoi_encode.c b/src/img/qoi/qoi_encode.c new file mode 100644 index 0000000..137bc96 --- /dev/null +++ b/src/img/qoi/qoi_encode.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_encode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/25 22:38:42 by maiboyer #+# #+# */ +/* Updated: 2023/12/25 22:56:06 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi.h" +#include "me/img/qoi/qoi_encode.h" +#include "me/img/qoi/qoi_utils.h" + +void *qoi_encode(const void *data, const t_qoi_desc *desc, t_i32 *out_len) +{ + t_encode_vals vals; + + if (data == NULL || out_len == NULL || desc == NULL || desc->width == 0 + || desc->height == 0 || desc->channels < 3 || desc->channels > 4 + || desc->colorspace > 1 || desc->height >= QOI_PIXELS_MAX / desc->width) + return (NULL); + vals = (t_encode_vals){0}; + vals.max_size = desc->width * desc->height * (desc->channels + 1) + + QOI_HEADER_SIZE + sizeof(t_u8[8]); + return (qoi_encode_inner(&vals, desc, (const t_u8 *)data, out_len)); +} diff --git a/src/img/qoi/qoi_fs.c b/src/img/qoi/qoi_fs.c new file mode 100644 index 0000000..9be0545 --- /dev/null +++ b/src/img/qoi/qoi_fs.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_fs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 19:18:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/fs/close.h" +#include "me/fs/open.h" +#include "me/fs/read_to_vec.h" +#include "me/fs/write.h" +#include "me/img/qoi.h" +#include + +t_i32 qoi_write(t_const_str filename, const void *data, + const t_qoi_desc *desc) +{ + t_file f; + void *encoded; + t_i32 size; + + if (me_open(filename, false, true, &f)) + return (0); + encoded = qoi_encode(data, desc, &size); + if (!encoded) + return (me_close(f, NULL), 0); + if (me_write(f, encoded, size)) + return (me_close(f, NULL), 0); + me_close(f, NULL); + free(encoded); + return (size); +} + +void *qoi_read(t_const_str filename, t_qoi_desc *desc, t_i32 channels) +{ + t_vec_u8 out; + void *pixels; + + if (read_to_vec(filename, &out)) + return (NULL); + pixels = qoi_decode(out.buffer, out.len, desc, channels); + vec_u8_free(out); + return (pixels); +} diff --git a/src/img/qoi/qoi_utils.c b/src/img/qoi/qoi_utils.c new file mode 100644 index 0000000..7eb494b --- /dev/null +++ b/src/img/qoi/qoi_utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qoi_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/24 19:02:03 by maiboyer #+# #+# */ +/* Updated: 2023/12/24 19:02:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/img/qoi/qoi_utils.h" +#include "me/types.h" + +void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v) +{ + bytes[(*p)++] = (0xff000000 & v) >> 24; + bytes[(*p)++] = (0x00ff0000 & v) >> 16; + bytes[(*p)++] = (0x0000ff00 & v) >> 8; + bytes[(*p)++] = (0x000000ff & v); +} + +t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p) +{ + t_u32 a; + t_u32 b; + t_u32 c; + t_u32 d; + + a = bytes[(*p)++]; + b = bytes[(*p)++]; + c = bytes[(*p)++]; + d = bytes[(*p)++]; + return (a << 24 | b << 16 | c << 8 | d); +} diff --git a/src/list/list_add_back.c b/src/list/list_add_back.c new file mode 100644 index 0000000..0c8bea4 --- /dev/null +++ b/src/list/list_add_back.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:02:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_add_back.h" +#include "me/list/list_get_last.h" + +void list_add_back(t_list **list, t_list *new) +{ + if (*list) + list_get_last(*list)->next = new; + else + *list = new; +} diff --git a/src/list/list_add_front.c b/src/list/list_add_front.c new file mode 100644 index 0000000..5b1244f --- /dev/null +++ b/src/list/list_add_front.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_add_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:02:50 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_add_front.h" +#include "me/list/list_alloc_node.h" + +void list_add_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; +} diff --git a/src/list/list_alloc_node.c b/src/list/list_alloc_node.c new file mode 100644 index 0000000..7e93cdc --- /dev/null +++ b/src/list/list_alloc_node.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_alloc_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:13:05 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_alloc_node.h" +#include "me/mem/mem_alloc.h" + +t_list *list_alloc_node(void *content) +{ + t_list *out; + + out = mem_alloc(sizeof(t_list) * 1); + if (out == NULL) + return (NULL); + out->content = content; + out->next = NULL; + return (out); +} diff --git a/src/list/list_free_all.c b/src/list/list_free_all.c new file mode 100644 index 0000000..af358b9 --- /dev/null +++ b/src/list/list_free_all.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_free_all.h" +#include + +void list_free_all(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (lst == NULL || del == NULL) + return ; + while (*lst) + { + del((*lst)->content); + tmp = *lst; + *lst = (*lst)->next; + free(tmp); + } + *lst = NULL; +} diff --git a/src/list/list_free_one.c b/src/list/list_free_one.c new file mode 100644 index 0000000..df44299 --- /dev/null +++ b/src/list/list_free_one.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_free_one.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_free_one.h" +#include + +void list_free_one(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL || del == NULL) + return ; + del(lst->content); + free(lst); +} diff --git a/src/list/list_get_last.c b/src/list/list_get_last.c new file mode 100644 index 0000000..789796c --- /dev/null +++ b/src/list/list_get_last.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_get_last.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:49 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_get_last.h" + +t_list *list_get_last(t_list *list) +{ + t_list *head; + + head = list; + if (head == NULL) + return (NULL); + while (head->next) + head = head->next; + return (head); +} diff --git a/src/list/list_iter.c b/src/list/list_iter.c new file mode 100644 index 0000000..e1d6c6e --- /dev/null +++ b/src/list/list_iter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_iter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:03:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_iter.h" + +void list_iter(t_list *list, void (*f)(void *)) +{ + while (list) + { + f(list->content); + list = list->next; + } +} diff --git a/src/list/list_map.c b/src/list/list_map.c new file mode 100644 index 0000000..0be2735 --- /dev/null +++ b/src/list/list_map.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:05:20 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_alloc_node.h" +#include "me/list/list_free_all.h" +#include "me/list/list_map.h" +#include + +t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + void *data; + t_list new; + t_list *cursor; + + new = (t_list){.next = NULL, .content = NULL}; + cursor = &new; + while (lst) + { + data = f(lst->content); + cursor->next = list_alloc_node(data); + if (cursor->next == NULL) + { + del(data); + list_free_all(&new.next, del); + return (NULL); + } + cursor = cursor->next; + lst = lst->next; + } + return (new.next); +} diff --git a/src/list/list_size.c b/src/list/list_size.c new file mode 100644 index 0000000..58efc4a --- /dev/null +++ b/src/list/list_size.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:05:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/list/list_size.h" + +t_usize list_size(t_list *lst) +{ + t_list *head; + t_usize idx; + + head = lst; + idx = 0; + while (head) + { + head = head->next; + idx++; + } + return (idx); +} diff --git a/src/mem/mem_alloc.c b/src/mem/mem_alloc.c new file mode 100644 index 0000000..a52c40a --- /dev/null +++ b/src/mem/mem_alloc.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:14:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include + +void *mem_alloc(t_usize size) +{ + void *out; + size_t i; + + i = 0; + out = malloc(size); + while (out && i < size) + ((t_u8 *)out)[i++] = 0; + return (out); +} diff --git a/src/mem/mem_alloc_array.c b/src/mem/mem_alloc_array.c new file mode 100644 index 0000000..b17ce7f --- /dev/null +++ b/src/mem/mem_alloc_array.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_alloc_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:14:47 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_alloc_array.h" +#include + +void *mem_alloc_array(t_usize item_count, t_usize item_size) +{ + t_usize multiplied; + + multiplied = item_count * item_size; + if (multiplied == 0 || multiplied / item_count != item_size) + return (NULL); + return (mem_alloc(multiplied)); +} diff --git a/src/mem/mem_compare.c b/src/mem/mem_compare.c new file mode 100644 index 0000000..76d9d5b --- /dev/null +++ b/src/mem/mem_compare.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:00:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_compare.h" + +t_i32 mem_compare(const void *lhs, const void *rhs, t_usize count) +{ + t_usize i; + t_u8 *lhs_; + t_u8 *rhs_; + + i = 0; + lhs_ = (t_u8 *)lhs; + rhs_ = (t_u8 *)rhs; + while (i < count) + { + if (lhs_[i] - rhs_[i]) + return ((t_i32)(lhs_[i] - rhs_[i])); + i++; + } + return (0); +} diff --git a/src/mem/mem_copy.c b/src/mem/mem_copy.c new file mode 100644 index 0000000..bbd7298 --- /dev/null +++ b/src/mem/mem_copy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_copy.h" + +void *mem_copy(void *destination, const void *source, t_usize count) +{ + t_usize i; + t_u8 *dst; + t_u8 *src; + + i = 0; + dst = (t_u8 *)destination; + src = (t_u8 *)source; + if (dst == src) + return (destination); + while (i < count) + { + dst[i] = src[i]; + i++; + } + return (destination); +} diff --git a/src/mem/mem_find.c b/src/mem/mem_find.c new file mode 100644 index 0000000..e38303e --- /dev/null +++ b/src/mem/mem_find.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:36 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_find.h" + +void *mem_find(void *buf, t_u8 find, t_usize count) +{ + t_usize i; + t_u8 *buf_bytes; + + i = 0; + buf_bytes = (t_u8 *)buf; + while (i < count) + { + if (buf_bytes[i] == find) + return ((void *)&buf_bytes[i]); + i++; + } + return (NULL); +} diff --git a/src/mem/mem_find_bytes.c b/src/mem/mem_find_bytes.c new file mode 100644 index 0000000..59eb784 --- /dev/null +++ b/src/mem/mem_find_bytes.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_find_bytes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2024/01/06 17:14:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_find_bytes.h" +#include "stdio.h" + +void *mem_find_bytes(void *buf, t_u8 *find, t_usize find_len, t_usize count) +{ + t_usize i; + t_usize j; + t_u8 *buf_bytes; + + i = 0; + printf("inside\n"); + buf_bytes = (t_u8 *)buf; + while (i < count) + { + j = 0; + while (j < find_len && i + j < count) + { + if (buf_bytes[i + j] != find[j]) + break ; + j++; + } + if (j == find_len) + return ((void *)&buf_bytes[i]); + i++; + } + return (NULL); +} diff --git a/src/mem/mem_move.c b/src/mem/mem_move.c new file mode 100644 index 0000000..03ebde3 --- /dev/null +++ b/src/mem/mem_move.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:43 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_move.h" + +void *mem_move(void *destination, const void *source, t_usize count) +{ + t_usize i; + t_u8 *dst; + t_u8 *src; + + i = 0; + dst = (t_u8 *)destination; + src = (t_u8 *)source; + if (dst < src) + { + while (i < count) + { + dst[i] = src[i]; + i++; + } + } + else if (dst > src) + { + i = count; + while (i > 0) + { + i--; + dst[i] = src[i]; + } + } + return (destination); +} diff --git a/src/mem/mem_set.c b/src/mem/mem_set.c new file mode 100644 index 0000000..106c736 --- /dev/null +++ b/src/mem/mem_set.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 18:15:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" + +void mem_set(void *buf, t_u8 fill_by, t_usize count) +{ + t_usize i; + t_u8 *buf_bytes; + + i = 0; + buf_bytes = (t_u8 *)buf; + while (i < count) + { + buf_bytes[i] = fill_by; + i++; + } +} diff --git a/src/mem/mem_set_zero.c b/src/mem/mem_set_zero.c new file mode 100644 index 0000000..b12e07d --- /dev/null +++ b/src/mem/mem_set_zero.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_set_zero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:58:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 15:01:57 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set_zero.h" + +void mem_set_zero(void *buf, t_usize count) +{ + t_u8 *buffer; + t_usize index; + + index = 0; + buffer = (t_u8 *)buf; + while (index < count) + { + buffer[index] = 0; + index++; + } +} diff --git a/src/num/u16/rotate.c b/src/num/u16/rotate.c new file mode 100644 index 0000000..6d1a3f8 --- /dev/null +++ b/src/num/u16/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u16.h" + +t_u16 u16_rotate_left(t_u16 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u16 u16_rotate_right(t_u16 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u32/rotate.c b/src/num/u32/rotate.c new file mode 100644 index 0000000..2a7e5c2 --- /dev/null +++ b/src/num/u32/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u32.h" + +t_u32 u32_rotate_left(t_u32 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u32 u32_rotate_right(t_u32 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u64/from_bytes.c b/src/num/u64/from_bytes.c new file mode 100644 index 0000000..70b162f --- /dev/null +++ b/src/num/u64/from_bytes.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* from_bytes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:30:56 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:09:52 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u64.h" + +t_u64 u64_from_7bytes(t_u8 *msg, t_usize start, t_usize len) +{ + t_usize i; + t_usize out; + t_u64 temp_val; + + i = 0; + out = 0; + if (i + 3 < len) + { + temp_val = (t_u64)(msg[start + i]); + temp_val |= (t_u64)(msg[start + i + 1]) << (8 * 1); + temp_val |= (t_u64)(msg[start + i + 2]) << (8 * 2); + temp_val |= (t_u64)(msg[start + i + 3]) << (8 * 3); + out = temp_val; + i += 4; + } + if (i + 1 < len) + { + temp_val = ((t_u64)(msg[start + i + 1]) << (8 * 1)) | (t_u64)(msg[start \ + + i]); + out |= temp_val << (i * 8); + i += 2; + } + if (i++ < len) + out |= (t_u64)(msg[start + i - 1]) << ((i - 1) * 8); + return (out); +} + +t_u64 u64_from_bytes(t_u8 *bytes, t_usize len) +{ + t_u64 out; + t_usize i; + + i = 0; + out = 0; + while (i < len && i < 8) + { + out |= ((t_u64)(bytes[i])) << (8 * i); + i++; + } + return (out); +} diff --git a/src/num/u64/rotate.c b/src/num/u64/rotate.c new file mode 100644 index 0000000..561d553 --- /dev/null +++ b/src/num/u64/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:24:16 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u64.h" + +t_u64 u64_rotate_left(t_u64 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u64 u64_rotate_right(t_u64 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/u8/rotate.c b/src/num/u8/rotate.c new file mode 100644 index 0000000..45f0fd8 --- /dev/null +++ b/src/num/u8/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/u8.h" + +t_u8 u8_rotate_left(t_u8 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_u8 u8_rotate_right(t_u8 value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/num/usize/rotate.c b/src/num/usize/rotate.c new file mode 100644 index 0000000..7ad365d --- /dev/null +++ b/src/num/usize/rotate.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/usize.h" + +t_usize usize_rotate_left(t_usize value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value << by) | (value >> (sizeof(value) * 8 - by))); +} + +t_usize usize_rotate_right(t_usize value, t_usize by) +{ + by &= sizeof(value) * 8 - 1; + if (by == 0) + return (value); + return ((value >> by) | (value << (sizeof(value) * 8 - by))); +} diff --git a/src/printf/formatter/char.c b/src/printf/formatter/char.c new file mode 100644 index 0000000..dcbe742 --- /dev/null +++ b/src/printf/formatter/char.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:18:48 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/string/str_substring.h" +#include + +void printf_c(t_printf_arg data, t_printf_func f) +{ + char value[2]; + t_str start_num; + + value[0] = *(char *)data.argument; + value[1] = 0; + start_num = &value[0]; + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = 1, .pretty = "", .pretty_len = 0, \ +.str = start_num, .allow_zero_fill = false, .sign = NULL, \ +.sign_len = 0, }, data, f); +} + +t_usize printf_s_inner(t_str *value, t_printf_arg *data, t_printf_func *f) +{ + t_usize len; + + if (*value == NULL) + { + if (data->flags & (PRECISION) && data->extra.precision < 6) + *value = ""; + else + *value = "(null)"; + } + len = str_len(*value); + if (data->flags & (PRECISION) && len > data->extra.precision) + len = data->extra.precision; + if (data->flags & (PRECISION) && len < data->extra.precision) + data->extra.precision = len; + (void)(f); + return (len); +} + +void printf_s(t_printf_arg data, t_printf_func f) +{ + t_str value; + t_str start_num; + t_usize len; + + value = (t_str)data.argument; + len = printf_s_inner(&value, &data, &f); + start_num = str_substring(value, 0, len); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = len, .pretty = "", .pretty_len = 0, \ +.str = start_num, .allow_zero_fill = false, .sign = NULL, \ +.sign_len = 0, }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/decimal.c b/src/printf/formatter/decimal.c new file mode 100644 index 0000000..b99b5e3 --- /dev/null +++ b/src/printf/formatter/decimal.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* decimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ +/* Updated: 2023/12/11 19:19:27 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_clone.h" +#include "me/string/str_len.h" +#include +#include +#define INT_INLINE_BUF 21 + +static void itoa_inner(t_u64 nb, t_str out) +{ + t_u64 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 1000000000000000000; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +static t_str itoa_64(t_i64 nb) +{ + char out[INT_INLINE_BUF]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, INT_INLINE_BUF); + if (nb < 0) + itoa_inner(-n, out); + else if (nb == 0) + out[0] = '0'; + else + itoa_inner(n, out); + return (str_clone(out)); +} + +void printf_d(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + t_str start_num; + t_str sign; + t_str void_write; + + value = *(t_i64 *)data.argument; + if ((t_u64)value & ((t_u64)1 << 31)) + sign = "-"; + else + sign = "+"; + if (!(data.flags & SIGN) && !((t_u64)value & ((t_u64)1 << 31))) + sign = ""; + if (!(data.flags & SIGN) && data.extra.space_align + && !((t_u64)value & ((t_u64)1 << 31))) + sign = " "; + data.flags |= SIGN; + start_num = itoa_64(value); + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = true, .pretty = &void_write}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .sign = sign, .pretty = NULL, .len = \ +str_len(start_num), .pretty_len = 0, .str = start_num, .allow_zero_fill \ + = true, .sign_len = str_len(sign), }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/hex.c b/src/printf/formatter/hex.c new file mode 100644 index 0000000..9e91150 --- /dev/null +++ b/src/printf/formatter/hex.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:19:03 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#define HEX_INLINE_BUF 17 + +static void fill_hex(t_str out_buf, t_u64 num, t_const_str base) +{ + t_usize i; + + mem_set(out_buf, 0, HEX_INLINE_BUF); + out_buf[HEX_INLINE_BUF - 1] = 0; + i = 0; + while (i < HEX_INLINE_BUF - 1) + { + out_buf[HEX_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)]; + i++; + } +} + +static void printf_x_common(t_printf_arg data, t_printf_func f, + t_const_str pretty, t_const_str base) +{ + t_u64 value; + char inline_buffer[HEX_INLINE_BUF]; + t_str start_num; + + value = *(t_u64 *)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + if (value) + fill_hex(inline_buffer, value, base); + start_num = &inline_buffer[0]; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + if (value == 0) + data.extra.pretty = false; + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = false, .pretty = (t_str *)&pretty}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = (t_str)pretty, \ +.pretty_len = 2, .str = start_num, .allow_zero_fill = true, .sign = NULL, \ +.sign_len = 0, }, data, f); +} + +void printf_x_up(t_printf_arg data, t_printf_func f) +{ + printf_x_common(data, f, "0X", "0123456789ABCDEF"); +} + +void printf_x_low(t_printf_arg data, t_printf_func f) +{ + printf_x_common(data, f, "0x", "0123456789abcdef"); +} diff --git a/src/printf/formatter/oct.c b/src/printf/formatter/oct.c new file mode 100644 index 0000000..a6befc2 --- /dev/null +++ b/src/printf/formatter/oct.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* oct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:19:18 by maix #+# #+# */ +/* Updated: 2023/12/11 19:17:23 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include +#define OCT_INLINE_BUF 23 + +static void fill_oct(t_str out_buf, t_u64 num, t_str base) +{ + t_usize i; + + mem_set(out_buf, 0, OCT_INLINE_BUF); + out_buf[OCT_INLINE_BUF - 1] = 0; + i = 0; + while (i < OCT_INLINE_BUF - 1) + { + out_buf[OCT_INLINE_BUF - i - 2] = base[(num >> (3 * i) & 7)]; + i++; + } +} + +void printf_o(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + char inline_buffer[OCT_INLINE_BUF]; + t_str start_num; + t_str pretty; + + value = *(t_u64 *)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + pretty = "0o"; + if (value) + fill_oct(inline_buffer, value, "01234567"); + start_num = &inline_buffer[0]; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + if (!value && data.extra.precision == 0 && (data.flags & PRECISION)) + { + start_num = ""; + pretty = ""; + } + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = pretty, \ +.pretty_len = str_len(pretty), .str = start_num, .allow_zero_fill = true, \ +.sign = NULL, .sign_len = 0, }, data, f); +} diff --git a/src/printf/formatter/ptr.c b/src/printf/formatter/ptr.c new file mode 100644 index 0000000..9f169eb --- /dev/null +++ b/src/printf/formatter/ptr.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:20:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#define PTR_INLINE_BUF 17 + +static void fill_hex(t_str out_buf, t_u64 num, t_str base) +{ + t_usize i; + + mem_set(out_buf, 0, PTR_INLINE_BUF); + out_buf[PTR_INLINE_BUF - 1] = 0; + i = 0; + while (i < PTR_INLINE_BUF - 1) + { + out_buf[PTR_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)]; + i++; + } +} + +static void set_values_for_nil(t_usize value, t_str *out, t_printf_arg *data, + t_printf_func f) +{ + (void)(f); + if (value) + { + data->flags &= ~PRECISION; + data->extra.precision = 0; + data->extra.pretty = true; + } + else + { + *out = "(nil)"; + data->extra.precision = 0; + data->extra.pretty = false; + data->flags &= ~(ZERO_ALIGN | PRECISION); + data->flags |= ALIGN; + } +} + +void printf_p(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + char inline_buffer[PTR_INLINE_BUF + 1]; + t_str start_num; + + value = (t_u64)data.argument; + inline_buffer[0] = '0'; + inline_buffer[1] = '\0'; + if (value) + fill_hex(inline_buffer, value, "0123456789abcdef"); + start_num = &inline_buffer[0]; + inline_buffer[PTR_INLINE_BUF] = 0; + while (start_num[1] != '\0' && start_num[0] == '0') + start_num++; + set_values_for_nil(value, &start_num, &data, f); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = "0x", \ +.pretty_len = 2, .str = start_num, .allow_zero_fill = value != 0, \ +.sign = NULL, .sign_len = 0, }, data, f); +} diff --git a/src/printf/formatter/unsigned_decimal.c b/src/printf/formatter/unsigned_decimal.c new file mode 100644 index 0000000..5d44f4e --- /dev/null +++ b/src/printf/formatter/unsigned_decimal.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unsigned_decimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maix +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/18 01:44:35 by maix #+# #+# */ +/* Updated: 2023/12/11 19:19:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_set.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/string/str_clone.h" +#include "me/string/str_len.h" +#include +#include +#define UINT_INLINE_BUF 21 + +static void itoa_inner(t_u64 nb, t_str out) +{ + t_u64 modulus; + bool need_print; + char c; + t_usize idx; + + need_print = false; + modulus = 10000000000000000000u; + idx = 0; + while (modulus) + { + c = (char)(nb / modulus) + '0'; + if (c != '0' || need_print) + { + out[idx++] = c; + need_print = true; + } + nb = nb % modulus; + modulus /= 10; + } +} + +static t_str itoa_u64(t_u64 nb) +{ + char out[UINT_INLINE_BUF]; + t_u64 n; + + n = (t_u64)nb; + mem_set(out, 0, UINT_INLINE_BUF); + if (nb == 0) + out[0] = '0'; + else + itoa_inner(n, out); + return (str_clone(out)); +} + +void printf_u(t_printf_arg data, t_printf_func f) +{ + t_u64 value; + t_str start_num; + t_str void_write; + + value = *(t_u64 *)data.argument; + start_num = itoa_u64(value); + handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num, + .free_out = true, .pretty = &void_write}, value); + pad_and_stuff( + (t_pad_and_stuff_args){\ +.fill_zero = 0, .fill = 0, .len = str_len(start_num), \ +.pretty = NULL, .pretty_len = 0, .str = start_num, \ +.allow_zero_fill = true, .sign = NULL, .sign_len = 0, }, data, f); + free(start_num); +} diff --git a/src/printf/formatter/utils.c b/src/printf/formatter/utils.c new file mode 100644 index 0000000..f8dd664 --- /dev/null +++ b/src/printf/formatter/utils.c @@ -0,0 +1,136 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:20:07 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/convert/atoi.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_find_chr.h" +#include "me/string/str_substring.h" +#include "me/types.h" +#include +#include + +bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg) +{ + t_i32 atoi_res; + + atoi_res = me_atoi(&fmt[*c_idx]); + if (atoi_res < 0) + { + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); + return (false); + } + advance_atoi(fmt, c_idx); + c_arg->extra.align = (t_u64)atoi_res; + handle_prec_and_align(fmt, c_idx, c_arg); + atoi_res = atoi(&fmt[*c_idx]); + if (atoi_res < 0) + { + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); + return (false); + } + advance_atoi(fmt, c_idx); + c_arg->extra.precision = (t_u64)atoi_res; + return (true); +} + +static void set_flags_if_needed(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg) +{ + if (fmt[*c_idx] == ' ') + { + (*c_idx)++; + c_arg->extra.space_align = true; + } + if (fmt[*c_idx] == '#') + { + (*c_idx)++; + c_arg->extra.pretty = true; + } + if (fmt[*c_idx] == '+') + { + (*c_idx)++; + c_arg->flags |= SIGN; + } + if (fmt[*c_idx] == '-') + { + (*c_idx)++; + c_arg->extra.left_align = true; + } + if (fmt[*c_idx] == '0') + { + (*c_idx)++; + c_arg->flags |= ZERO_ALIGN; + } +} + +bool set_params(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_printf_arg *c_arg) +{ + t_usize b_idx; + + b_idx = ~0; + while (b_idx != *c_idx) + { + b_idx = *c_idx; + set_flags_if_needed(fmt, c_idx, c_arg); + } + return (handle_atoi_stuff(fmt, c_idx, nxt, c_arg)); +} + +t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt, + t_pad_inner_args extra) +{ + t_str truc; + + truc = str_substring(fmt, *c_idx, *nxt - *c_idx); + extra.f(truc, *nxt - *c_idx, extra.p_args); + free(truc); + *c_idx = *nxt + 1; + return ((t_printf_arg){ + .p_args = extra.p_args, + .argument = extra.arguments, + }); +} + +void pad_inner(t_const_str fmt, t_usize *c_idx, t_usize *nxt, + t_pad_inner_args extra) +{ + t_printf_arg c_arg; + t_matcher *matcher; + + c_arg = print_substr(c_idx, nxt, fmt, extra); + if (fmt[*c_idx] == '%') + { + (*c_idx)++; + extra.f("%", 1, extra.p_args); + } + else + { + if (!set_params(fmt, c_idx, nxt, &c_arg)) + return (ret_reset(c_idx, nxt, fmt)); + matcher = find_matcher(fmt, extra.matchers, c_idx); + if (matcher == NULL) + return (ret_reset(c_idx, nxt, fmt)); + call_matcher(matcher, c_arg, *extra.arguments, extra.f); + } + *nxt = (t_usize)(str_find_chr(fmt + *c_idx, '%')); + if (*nxt == 0) + return (*nxt = extra.fmt_len, (void)extra.f(fmt + *c_idx, extra.fmt_len + - *c_idx, extra.p_args)); + *nxt = *nxt - (t_usize)fmt; +} diff --git a/src/printf/formatter/utils2.c b/src/printf/formatter/utils2.c new file mode 100644 index 0000000..1537211 --- /dev/null +++ b/src/printf/formatter/utils2.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:48:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/char/isdigit.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include "me/types.h" + +void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d) +{ + t_isize fill_zero; + t_isize fill; + + fill_zero = 0; + fill = 0; + if (d->flags & ZERO_ALIGN && a->len < d->extra.align) + fill_zero = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty + - a->sign_len; + if (d->flags & PRECISION && a->len < d->extra.precision) + fill_zero = d->extra.precision - a->len; + if (d->flags & (ALIGN) && a->len < d->extra.align) + fill = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty + - a->sign_len - fill_zero; + if (fill_zero < 0) + fill_zero = 0; + if (fill < 0) + fill = 0; + if (fill_zero < 0) + fill_zero = 0; + if (fill < 0) + fill = 0; + a->fill_zero = fill_zero; + a->fill = fill; +} + +void print_with_func(t_pad_and_stuff_args *a, t_printf_arg *d, + t_printf_func f, t_const_str t) +{ + f(t, 1, d->p_args); + if (t[0] == ' ' && t[1] == 0 && a->fill) + a->fill--; + if (t[0] == '0' && t[1] == 0 && a->fill_zero) + a->fill_zero--; +} + +void pad_and_stuff(t_pad_and_stuff_args a, t_printf_arg d, t_printf_func f) +{ + set_var_for_pad_and_stuff(&a, &d); + if (!(d.extra.left_align || d.extra.precision) && d.flags & (ZERO_ALIGN)) + { + print_sign_if_needed(a, d, f); + while (a.allow_zero_fill && a.fill_zero > 0) + print_with_func(&a, &d, f, "0"); + } + else + { + while (!(d.extra.left_align) && d.flags & (ALIGN) && a.fill > 0) + print_with_func(&a, &d, f, " "); + print_sign_if_needed(a, d, f); + if (d.extra.pretty) + f(a.pretty, a.pretty_len, d.p_args); + } + while (a.allow_zero_fill && a.fill_zero > 0) + print_with_func(&a, &d, f, "0"); + f(a.str, a.len, d.p_args); + while (d.extra.left_align && a.fill > 0) + print_with_func(&a, &d, f, " "); +} + +void handle_prec_and_align(t_const_str fmt, t_usize *c_idx, + t_printf_arg *c_arg) +{ + if (c_arg->extra.align && !(c_arg->flags & ZERO_ALIGN)) + c_arg->flags |= ALIGN; + if (fmt[*c_idx] == '.') + { + (*c_idx)++; + c_arg->flags |= PRECISION; + } +} + +void advance_atoi(t_const_str fmt, t_usize *idx) +{ + while (me_isdigit(fmt[*idx])) + (*idx)++; +} diff --git a/src/printf/formatter/utils3.c b/src/printf/formatter/utils3.c new file mode 100644 index 0000000..1b03f1e --- /dev/null +++ b/src/printf/formatter/utils3.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:06:15 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:21:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/string/str_find_chr.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include + +void ret_reset(t_usize *c_idx, t_usize *nxt, t_const_str fmt) +{ + *c_idx = *nxt; + *nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt); +} + +void me_printf_str_inner(t_const_str fmt, t_printf_func f, + va_list *arguments, void *p_args) +{ + t_usize c_idx; + t_usize nxt; + t_usize fmt_len; + + c_idx = 0; + fmt_len = str_len(fmt); + nxt = (t_usize)(str_find_chr(fmt, '%')); + if (nxt == 0) + return (f(fmt, fmt_len, p_args)); + nxt = nxt - (t_usize)fmt; + while (nxt < fmt_len) + { + pad_inner(fmt, &c_idx, &nxt, \ + ((t_pad_inner_args){\ + .p_args = p_args, .fmt_len = fmt_len, .f = f, \ + .arguments = arguments, .matchers = get_matchers(), })); + } +} + +void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d, + t_printf_func f) +{ + if (d.flags & (SIGN) && a.sign != NULL) + f(a.sign, a.sign_len, d.p_args); +} diff --git a/src/printf/formatter/utils_numbers.c b/src/printf/formatter/utils_numbers.c new file mode 100644 index 0000000..58e5095 --- /dev/null +++ b/src/printf/formatter/utils_numbers.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_numbers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */ +/* Updated: 2023/12/01 21:49:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/printf.h" +#include + +void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs, + t_usize value) +{ + if (!value && data->extra.precision == 0 && (data->flags & PRECISION)) + { + data->flags &= (~ZERO_ALIGN); + data->flags |= ALIGN; + if (strs.free_out) + *strs.out = (free(*strs.out), (t_str)mem_alloc_array(1, 1)); + else + *strs.out = ""; + *strs.pretty = ""; + } + else if ((data->flags & PRECISION) && !data->extra.left_align) + { + data->flags &= (~ZERO_ALIGN); + data->flags |= ALIGN; + } +} diff --git a/src/printf/matchers.c b/src/printf/matchers.c new file mode 100644 index 0000000..d122dac --- /dev/null +++ b/src/printf/matchers.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* matchers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 19:11:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_compare.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include +#include +#include + +t_matcher_list *get_matchers(void) +{ + static t_matcher_list printf_matchers = (t_matcher_list){ + .data = { + {.matcher = "o", .matcher_len = 1, .arg_type = U64, .f = &printf_o}, + {.matcher = "c", .matcher_len = 1, .arg_type = CHAR, .f = &printf_c}, + {.matcher = "s", .matcher_len = 1, .arg_type = STR, .f = &printf_s}, + {.matcher = "p", .matcher_len = 1, .arg_type = VOID_PTR, .f = &printf_p}, + {.matcher = "d", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, + {.matcher = "i", .matcher_len = 1, .arg_type = I32, .f = &printf_d}, + {.matcher = "u", .matcher_len = 1, .arg_type = U32, .f = &printf_u}, + {.matcher = "x", .matcher_len = 1, .arg_type = U32, .f = &printf_x_low}, + {.matcher = "X", .matcher_len = 1, .arg_type = U32, .f = &printf_x_up}, + }, + .next = NULL, + }; + + return (&printf_matchers); +} + +bool insert_matcher(t_matcher matcher) +{ + t_matcher_list *matchers; + t_usize i; + + matchers = get_matchers(); + while (matchers) + { + i = 0; + while (i < PRINTF_BUFFER_CHUNK) + { + if (matchers->data[i].f == NULL) + { + (*matchers).data[i] = matcher; + return (true); + } + } + matchers->next = malloc(sizeof(t_matcher_list) * 1); + } + return (false); +} + +t_matcher *find_matcher(t_const_str fmt, t_matcher_list *matchers, + t_usize *c_idx) +{ + t_usize matcher_index; + t_matcher *matcher; + + while (matchers) + { + matcher_index = 0; + while (matcher_index < PRINTF_BUFFER_CHUNK) + { + matcher = &matchers->data[matcher_index]; + if (matcher->f) + { + if (!mem_compare(&fmt[*c_idx], matcher->matcher, + matcher->matcher_len)) + { + *c_idx += matcher->matcher_len; + return (matcher); + } + } + matcher_index++; + } + matchers = matchers->next; + } + return (NULL); +} + +// FIGURE OUT HOW TO MAKE I64 WORK +void call_matcher(t_matcher *matcher, t_printf_arg matcher_arguments, + va_list args, t_printf_func f) +{ + t_matcher_tmp_val vals; + + matcher_arguments.argument = NULL; + if (matcher->arg_type == CHAR) + { + vals.chr_val = (char)va_arg(args, int); + matcher_arguments.argument = (void *)&vals.chr_val; + } + if (matcher->arg_type == U32) + vals.u64_val = va_arg(args, t_u32); + if (matcher->arg_type == U64) + vals.u64_val = va_arg(args, t_u64); + if (matcher->arg_type == I64) + vals.i64_val = va_arg(args, t_i64); + if (matcher->arg_type == I32) + vals.i64_val = va_arg(args, t_i32); + if (matcher->arg_type == I32 || matcher->arg_type == I64) + matcher_arguments.argument = (void *)&vals.i64_val; + if (matcher->arg_type == U32 || matcher->arg_type == U64) + matcher_arguments.argument = (void *)&vals.u64_val; + if (matcher->arg_type == VOID_PTR) + matcher_arguments.argument = (void *)va_arg(args, void *); + if (matcher->arg_type == STR) + matcher_arguments.argument = (void *)va_arg(args, t_str); + matcher->f(matcher_arguments, f); +} diff --git a/src/printf/printf.c b/src/printf/printf.c new file mode 100644 index 0000000..935b3cd --- /dev/null +++ b/src/printf/printf.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 14:58:10 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/fs/write.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include +#include + +// p_args is an t_buffer_str; +static void me_printf_add_to_string(t_const_str to_write, t_usize to_write_len, + void *p_args) +{ + t_buffer_str *out_buf; + + out_buf = (t_buffer_str *)p_args; + (void)(to_write_len); + push_str_buffer(out_buf, to_write); +} + +t_str me_printf_str(t_const_str fmt, va_list *arguments) +{ + t_buffer_str out; + + out = alloc_new_buffer(str_len(fmt)); + if (out.buf == NULL) + { + return (NULL); + } + me_printf_str_inner(fmt, &me_printf_add_to_string, arguments, (void *)&out); + return (out.buf); +} + +void me_printf_write(t_const_str to_write, t_usize to_write_len, + void *p_args) +{ + t_fprintf_arg *arg; + + arg = (t_fprintf_arg *)p_args; + me_write(arg->fd, (t_u8 *)to_write, to_write_len); + arg->total_print += to_write_len; +} + +t_usize me_printf(t_const_str fmt, ...) +{ + va_list args; + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 1, + .total_print = 0, + }; + va_start(args, fmt); + me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); + va_end(args); + return (passthru.total_print); +} + +t_usize me_eprintf(t_const_str fmt, ...) +{ + va_list args; + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 2, + .total_print = 0, + }; + va_start(args, fmt); + me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru); + va_end(args); + return (passthru.total_print); +} + +/* +t_usize me_printf(t_const_str fmt, ...) +{ + va_list args; + t_str str; + t_usize len; + + va_start(args, fmt); + str = me_printf_str(fmt, &args); + va_end(args); + len = str_len(str); + write(1, str, len); + free(str); + return (len); +} + +t_usize me_eprintf(t_const_str fmt, ...) +{ + va_list args; + t_str str; + t_usize len; + + va_start(args, fmt); + str = me_printf_str(fmt, &args); + va_end(args); + len = str_len(str); + write(2, str, len); + free(str); + return (len); +} +*/ diff --git a/src/printf/vprintf.c b/src/printf/vprintf.c new file mode 100644 index 0000000..6cf3bc0 --- /dev/null +++ b/src/printf/vprintf.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:00:39 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/buffered_str/buf_str.h" +#include "me/fs/write.h" +#include "me/printf/formatter/formatter.h" +#include "me/printf/formatter/utils.h" +#include "me/printf/matchers/matchers.h" +#include "me/printf/printf.h" +#include "me/string/str_len.h" +#include "me/types.h" +#include +#include +#include +#include + +void me_printf_write(t_const_str to_write, t_usize to_write_len, + void *p_args); + +t_usize me_vprintf(t_const_str fmt, va_list *args) +{ + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 1, + .total_print = 0, + }; + me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); + return (passthru.total_print); +} + +t_usize me_veprintf(t_const_str fmt, va_list *args) +{ + t_fprintf_arg passthru; + + passthru = (t_fprintf_arg){ + .fd = 2, + .total_print = 0, + }; + me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru); + return (passthru.total_print); +} diff --git a/src/string/str_clone.c b/src/string/str_clone.c new file mode 100644 index 0000000..70fd942 --- /dev/null +++ b/src/string/str_clone.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_clone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/string/str_find_chr.c b/src/string/str_find_chr.c new file mode 100644 index 0000000..565ead1 --- /dev/null +++ b/src/string/str_find_chr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/string/str_find_rev_chr.c b/src/string/str_find_rev_chr.c new file mode 100644 index 0000000..1930873 --- /dev/null +++ b/src/string/str_find_rev_chr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_rev_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:47:47 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; + + index = str_len((t_str)str); + while (index >= 0) + { + if (str[index] == chr) + return ((char *)&str[index]); + index--; + } + return (NULL); +} diff --git a/src/string/str_find_str.c b/src/string/str_find_str.c new file mode 100644 index 0000000..4afa489 --- /dev/null +++ b/src/string/str_find_str.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_find_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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*/ diff --git a/src/string/str_iter.c b/src/string/str_iter.c new file mode 100644 index 0000000..dc9abb0 --- /dev/null +++ b/src/string/str_iter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_iter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++; + } +} diff --git a/src/string/str_join.c b/src/string/str_join.c new file mode 100644 index 0000000..5aacfd9 --- /dev/null +++ b/src/string/str_join.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_join.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/string/str_l_cat.c b/src/string/str_l_cat.c new file mode 100644 index 0000000..a606722 --- /dev/null +++ b/src/string/str_l_cat.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_cat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:51:34 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/*R + +//CFLAGS="-lbsd" + +#include +#include +#include + +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 +#include +#include + +#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*/ diff --git a/src/string/str_l_copy.c b/src/string/str_l_copy.c new file mode 100644 index 0000000..e4bb6bc --- /dev/null +++ b/src/string/str_l_copy.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_l_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:51:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/*R + +//CFLAGS="-lbsd" + +#include +#include +#include + +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 +#include +#include + +#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*/ diff --git a/src/string/str_len.c b/src/string/str_len.c new file mode 100644 index 0000000..1feb088 --- /dev/null +++ b/src/string/str_len.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 14:52:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_len.h" + +t_usize str_len(t_const_str str) +{ + t_usize out; + + out = 0; + while (str[out]) + out++; + return (out); +} diff --git a/src/string/str_map.c b/src/string/str_map.c new file mode 100644 index 0000000..2e65a86 --- /dev/null +++ b/src/string/str_map.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/string/str_n_compare.c b/src/string/str_n_compare.c new file mode 100644 index 0000000..5c39ac5 --- /dev/null +++ b/src/string/str_n_compare.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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]); +} diff --git a/src/string/str_n_find_str.c b/src/string/str_n_find_str.c new file mode 100644 index 0000000..321af97 --- /dev/null +++ b/src/string/str_n_find_str.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_n_find_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 16:53:21 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) && len == 0) + 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); +} diff --git a/src/string/str_split.c b/src/string/str_split.c new file mode 100644 index 0000000..d533e60 --- /dev/null +++ b/src/string/str_split.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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)); +} diff --git a/src/string/str_substring.c b/src/string/str_substring.c new file mode 100644 index 0000000..790bd31 --- /dev/null +++ b/src/string/str_substring.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_substring.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/string/str_trim.c b/src/string/str_trim.c new file mode 100644 index 0000000..9320f10 --- /dev/null +++ b/src/string/str_trim.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_trim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/vec/vec_buf_str.c b/src/vec/vec_buf_str.c new file mode 100644 index 0000000..281433a --- /dev/null +++ b/src/vec/vec_buf_str.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/vec/vec_buf_str_functions2.c b/src/vec/vec_buf_str_functions2.c new file mode 100644 index 0000000..6bee346 --- /dev/null +++ b/src/vec/vec_buf_str_functions2.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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++; + } +} diff --git a/src/vec/vec_buf_str_functions3.c b/src/vec/vec_buf_str_functions3.c new file mode 100644 index 0000000..2b51c44 --- /dev/null +++ b/src/vec/vec_buf_str_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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++; + } +} diff --git a/src/vec/vec_i64.c b/src/vec/vec_i64.c new file mode 100644 index 0000000..1a24dec --- /dev/null +++ b/src/vec/vec_i64.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64.h" +#include + +t_vec_i64 vec_i64_new(t_usize capacity, t_free_i64_item free_function) +{ + t_vec_i64 out; + + out = (t_vec_i64){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_push(t_vec_i64 *vec, t_i64 element) +{ + t_i64 *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_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + 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_i64_reserve(t_vec_i64 *vec, t_usize wanted_capacity) +{ + t_i64 *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_i64)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64)); + 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_i64_pop(t_vec_i64 *vec, t_i64 *value) +{ + t_i64 temp_value; + t_i64 *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_i64)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_free(t_vec_i64 vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_i64_bool.c b/src/vec/vec_i64_bool.c new file mode 100644 index 0000000..af10724 --- /dev/null +++ b/src/vec/vec_i64_bool.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64_bool.h" +#include + +t_vec_i64_bool vec_i64_bool_new(t_usize capacity, + t_free_i64_bool_item free_function) +{ + t_vec_i64_bool out; + + out = (t_vec_i64_bool){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_i64_bool)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_i64_bool_push(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_i64_bool *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_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + 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_i64_bool_reserve(t_vec_i64_bool *vec, t_usize wanted_capacity) +{ + t_i64_bool *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_i64_bool)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_i64_bool)); + 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_i64_bool_pop(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_i64_bool temp_value; + t_i64_bool *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_i64_bool)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_i64_bool_free(t_vec_i64_bool vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/src/vec/vec_i64_bool_functions2.c b/src/vec/vec_i64_bool_functions2.c new file mode 100644 index 0000000..01927c2 --- /dev/null +++ b/src/vec/vec_i64_bool_functions2.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64_bool.h" +#include + +t_error vec_i64_bool_find(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + 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_i64_bool_find_starting(t_vec_i64_bool *vec, + bool (*fn)(const t_i64_bool *), 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_i64_bool_all(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + 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_i64_bool_any(t_vec_i64_bool *vec, bool (*fn)(const t_i64_bool *), + 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_i64_bool_iter(t_vec_i64_bool *vec, void (*fn)(t_usize index, + t_i64_bool *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++; + } +} diff --git a/src/vec/vec_i64_bool_functions3.c b/src/vec/vec_i64_bool_functions3.c new file mode 100644 index 0000000..ee82947 --- /dev/null +++ b/src/vec/vec_i64_bool_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64_bool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64_bool.h" +#include + +t_error vec_i64_bool_push_front(t_vec_i64_bool *vec, t_i64_bool element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_bool_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_bool_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_i64_bool_pop_front(t_vec_i64_bool *vec, t_i64_bool *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_bool_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_i64_bool_reverse(t_vec_i64_bool *vec) +{ + t_i64_bool 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++; + } +} diff --git a/src/vec/vec_i64_bool_sort.c b/src/vec/vec_i64_bool_sort.c new file mode 100644 index 0000000..edbdfc1 --- /dev/null +++ b/src/vec/vec_i64_bool_sort.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64_bool.h" + +void vec_i64_bool_sort(t_vec_i64_bool *v, + t_vec_i64_bool_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64_bool tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} diff --git a/src/vec/vec_i64_functions2.c b/src/vec/vec_i64_functions2.c new file mode 100644 index 0000000..d7180bc --- /dev/null +++ b/src/vec/vec_i64_functions2.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64.h" +#include + +t_error vec_i64_find(t_vec_i64 *vec, bool (*fn)(const t_i64 *), 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_i64_find_starting(t_vec_i64 *vec, bool (*fn)(const t_i64 *), + 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_i64_all(t_vec_i64 *vec, bool (*fn)(const t_i64 *), 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_i64_any(t_vec_i64 *vec, bool (*fn)(const t_i64 *), 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_i64_iter(t_vec_i64 *vec, void (*fn)(t_usize index, t_i64 *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++; + } +} diff --git a/src/vec/vec_i64_functions3.c b/src/vec/vec_i64_functions3.c new file mode 100644 index 0000000..653eee8 --- /dev/null +++ b/src/vec/vec_i64_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_i64.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_i64.h" +#include + +t_error vec_i64_push_front(t_vec_i64 *vec, t_i64 element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_i64_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && vec_i64_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_i64_pop_front(t_vec_i64 *vec, t_i64 *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_i64_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_i64_reverse(t_vec_i64 *vec) +{ + t_i64 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++; + } +} diff --git a/src/vec/vec_i64_sort.c b/src/vec/vec_i64_sort.c new file mode 100644 index 0000000..d32b66b --- /dev/null +++ b/src/vec/vec_i64_sort.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ +/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_i64.h" + +void vec_i64_sort(t_vec_i64 *v, t_vec_i64_sort_fn is_sorted_fn) +{ + t_usize sorted_part; + t_usize i; + t_i64 tmp; + + if (v == NULL) + return ; + sorted_part = v->len; + while (sorted_part > 0) + { + i = 0; + while (i < sorted_part - 1) + { + if (!is_sorted_fn(&v->buffer[i], &v->buffer[i + 1])) + { + tmp = v->buffer[i]; + v->buffer[i] = v->buffer[i + 1]; + v->buffer[i + 1] = tmp; + } + i++; + } + sorted_part--; + } +} diff --git a/src/vec/vec_u8.c b/src/vec/vec_u8.c new file mode 100644 index 0000000..74ff9a0 --- /dev/null +++ b/src/vec/vec_u8.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/vec/vec_u8_functions2.c b/src/vec/vec_u8_functions2.c new file mode 100644 index 0000000..3358623 --- /dev/null +++ b/src/vec/vec_u8_functions2.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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++; + } +} diff --git a/src/vec/vec_u8_functions3.c b/src/vec/vec_u8_functions3.c new file mode 100644 index 0000000..3167ba2 --- /dev/null +++ b/src/vec/vec_u8_functions3.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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++; + } +}