diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-04 21:28:21 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-04 21:28:21 +0200 |
| commit | 42316d8393d32bd88fb8e0cba6825185f78dacd0 (patch) | |
| tree | e9adbd7e5701f70ecba520bbee41d4b21852a1cb /src/dstr/ft_dstrgrow.c | |
| parent | 51b845a6a202b50966f50e166cfb11bcbdccbe33 (diff) | |
| download | libft-42316d8393d32bd88fb8e0cba6825185f78dacd0.tar.gz libft-42316d8393d32bd88fb8e0cba6825185f78dacd0.tar.bz2 libft-42316d8393d32bd88fb8e0cba6825185f78dacd0.zip | |
Added test and doc for dynamic string
Diffstat (limited to 'src/dstr/ft_dstrgrow.c')
| -rw-r--r-- | src/dstr/ft_dstrgrow.c | 14 |
1 files changed, 12 insertions, 2 deletions
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); } |
