diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/dstr/ft_dstrdestroy.c | 7 | ||||
| -rw-r--r-- | src/dstr/ft_dstrgrow.c | 14 | ||||
| -rw-r--r-- | src/dstr/ft_dstrinsert.c | 17 | ||||
| -rw-r--r-- | src/dstr/ft_dstrnew.c | 9 | ||||
| -rw-r--r-- | src/dstr/ft_dstrunwrap.c | 8 | ||||
| -rw-r--r-- | src/vec/ft_vecnew.c | 2 | ||||
| -rw-r--r-- | src/vec/ft_vecsort.c | 8 |
7 files changed, 53 insertions, 12 deletions
diff --git a/src/dstr/ft_dstrdestroy.c b/src/dstr/ft_dstrdestroy.c index 6f1a819..c76b692 100644 --- a/src/dstr/ft_dstrdestroy.c +++ b/src/dstr/ft_dstrdestroy.c @@ -6,12 +6,17 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:58:46 by charles #+# #+# */ -/* Updated: 2020/04/03 13:59:25 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:51:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Destroy a dynamic string +** \param dstr Dynamic string to destroy +*/ + void ft_dstrdestroy(t_ftdstr *dstr) { if (dstr == NULL) diff --git a/src/dstr/ft_dstrgrow.c b/src/dstr/ft_dstrgrow.c index 9ad51ea..40cad86 100644 --- a/src/dstr/ft_dstrgrow.c +++ b/src/dstr/ft_dstrgrow.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 14:17:09 by charles #+# #+# */ -/* Updated: 2020/04/03 15:09:21 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:56:26 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,28 @@ #define FT_DSTR_GROWTH_FACTOR 1.5 +/* +** \brief Grow the capacity of a dynamic string +** \param dstr Dynamic string to grow +** \param at_least Minimum capacity - 1 required +** \return Passed dynamic string or NULL on error +*/ + t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least) { size_t new_capacity; char *new_str; + if (at_least < dstr->capacity - 1) + return (dstr); new_capacity = dstr->capacity <= 1 ? 2 : dstr->capacity; - while (new_capacity < at_least) + while (at_least > new_capacity - 1) new_capacity *= FT_DSTR_GROWTH_FACTOR; if ((new_str = (char*)malloc(sizeof(char) * new_capacity)) == NULL) return (NULL); ft_memcpy(new_str, dstr->str, dstr->capacity); dstr->capacity = new_capacity; + free(dstr->str); dstr->str = new_str; return (dstr); } diff --git a/src/dstr/ft_dstrinsert.c b/src/dstr/ft_dstrinsert.c index 6a1486c..931f5d8 100644 --- a/src/dstr/ft_dstrinsert.c +++ b/src/dstr/ft_dstrinsert.c @@ -6,22 +6,29 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 14:00:44 by charles #+# #+# */ -/* Updated: 2020/04/03 15:09:52 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:21:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Insert string in dynamic string +** \param dstr Dynamic string where the string will be inserted +** \param inserted Static string to insert +** \param i Index where it should be inserted +** \return Passed dynamic string or NULL on error +*/ + t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i) { size_t inserted_len; - if (i > dstr->capacity) + if (i > dstr->length) return (NULL); inserted_len = ft_strlen(inserted); - if (dstr->capacity - dstr->length - 1 < inserted_len) - if (ft_dstrgrow(dstr, dstr->capacity + inserted_len + 1) == NULL) - return (NULL); + if (ft_dstrgrow(dstr, dstr->capacity + inserted_len) == NULL) + return (NULL); ft_memmove(dstr->str + i + inserted_len, dstr->str + i, dstr->length - i + 1); diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c index 189fc78..8ae4a64 100644 --- a/src/dstr/ft_dstrnew.c +++ b/src/dstr/ft_dstrnew.c @@ -6,12 +6,19 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:54:52 by charles #+# #+# */ -/* Updated: 2020/04/03 13:58:17 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Create a new dynamic string +** \param from Static string to create the dynamic one from +** (will be duplicated) +** \return Created dynamic string or NULL on malloc error +*/ + t_ftdstr *ft_dstrnew(char *from) { t_ftdstr *dstr; diff --git a/src/dstr/ft_dstrunwrap.c b/src/dstr/ft_dstrunwrap.c index ba62a23..5b29b5b 100644 --- a/src/dstr/ft_dstrunwrap.c +++ b/src/dstr/ft_dstrunwrap.c @@ -6,12 +6,18 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:59:35 by charles #+# #+# */ -/* Updated: 2020/04/03 14:00:36 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:58:41 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Destroy dynamic string but keep the underlying static string +** \param dstr Dynamic string to unwrap +** \return Underlying string of the dynamic one +*/ + char *ft_dstrunwrap(t_ftdstr *dstr) { char *tmp; diff --git a/src/vec/ft_vecnew.c b/src/vec/ft_vecnew.c index 8a8736a..0def9c9 100644 --- a/src/vec/ft_vecnew.c +++ b/src/vec/ft_vecnew.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:03:49 by charles #+# #+# */ -/* Updated: 2020/04/01 20:00:00 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:24:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/ft_vecsort.c b/src/vec/ft_vecsort.c index 99b1a5f..8aa5c2a 100644 --- a/src/vec/ft_vecsort.c +++ b/src/vec/ft_vecsort.c @@ -6,12 +6,18 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 15:53:23 by charles #+# #+# */ -/* Updated: 2020/04/04 15:55:21 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:30:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_vec.h" +/* +** \brief Wrapper around ft_qsort +** \param vec Vector to sort +** \param cmp Function to compare each vector element +*/ + void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp) { ft_qsort(vec->data, vec->size, sizeof(void*), cmp); |
