diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /42sh/src/utils/libstring.h | |
Diffstat (limited to '42sh/src/utils/libstring.h')
| -rw-r--r-- | 42sh/src/utils/libstring.h | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/42sh/src/utils/libstring.h b/42sh/src/utils/libstring.h new file mode 100644 index 0000000..e30097c --- /dev/null +++ b/42sh/src/utils/libstring.h @@ -0,0 +1,87 @@ +#ifndef LIBSTRING_H +#define LIBSTRING_H + +#include <stdbool.h> +#include <string.h> + +/** + * @brief Represents the default capacity of the string + */ +#define BASE_STRING_SIZE 8 + +#define STRINGS_ARE_EQUAL(A, B) (strcmp(A, B) == 0) + +/** + * @brief Represents a dynamically allocated string. + * + * This struct manages a dynamically allocated null-terminated string + * along with its length and capacity. + */ +struct string +{ + size_t length; /**< The current length of the string (excluding the null + terminator). */ + size_t capacity; /**< The allocated capacity of the string (always a + multiple of `BASE_STRING_SIZE`). */ + char *data; /**< A pointer to the null-terminated character data. */ +}; + +/** + * @brief Creates a heap-allocated copy of the given string. + * + * The capacity is always a multiple of `BASE_STRING_SIZE`. If `s` is + * heap-allocated, the caller must free it later. The resulting string struct + * and data are freed when `string_free` is called. + * + * @param s A null-terminated string that can be null. + * @return A pointer to a newly heap-allocated `struct string`. + */ +struct string *string_create(char *s); + +/** + * @brief Frees the `struct string` and its associated data. + * + * This function frees the struct `str` and its heap-allocated null-terminated + * character data. + * + * @param str A pointer to the `struct string` to be freed. + */ +void string_free(struct string *str); + +/** + * @brief Pushes a single character into the string. + * + * Resizes the string's data and capacity if needed. + * + * @param str A pointer to the `struct string` where the character will be + * added. + * @param c The character to push into the string. + * @return `true` if the push succeeds, `false` otherwise. + */ +bool string_pushc(struct string *str, char c); + +/** + * @brief Pushes a null-terminated string into the string. + * + * @param str A pointer to the `struct string` where the string will be added. + * @param s A null-terminated string to push into `str`. + * @return `true` if the operation succeeds, `false` otherwise. + */ +bool string_pushstr(struct string *str, char *s); + +/** + * @brief Concatenates two `struct string` objects and frees the source string. + * + * The `src` struct and its data are freed after concatenation. + * + * @param dst A pointer to the destination `struct string` to which `src` will + * be concatenated. + * @param src A pointer to the source `struct string` that will be concatenated + * and freed. + * @return `true` if the concatenation succeeds, `false` otherwise. + */ +bool string_catenate(struct string *dst, struct string *src); + +struct string *string_deepcopy(struct string *str); + +#endif /* ! LIBSTRING_H */ |
