aboutsummaryrefslogtreecommitdiff
path: root/src/dstr/ft_dstrgrow.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-09 12:31:50 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-09 12:31:50 +0200
commit02abc030a68cb2fdd2f21c96db830ec8cb9176ad (patch)
tree0c2d67c94a3618639fc2cd29d8bc78820e41c254 /src/dstr/ft_dstrgrow.c
parentb5124347359833fcde33452978c62133879c6c9e (diff)
parent3a2d19df9e509d0b015c786eb02f8315ff0ad91c (diff)
downloadlibft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.gz
libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.bz2
libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.zip
Merge remote-tracking branch 'origin/minishell'
Diffstat (limited to 'src/dstr/ft_dstrgrow.c')
-rw-r--r--src/dstr/ft_dstrgrow.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/dstr/ft_dstrgrow.c b/src/dstr/ft_dstrgrow.c
new file mode 100644
index 0000000..40cad86
--- /dev/null
+++ b/src/dstr/ft_dstrgrow.c
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_dstrgrow.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/03 14:17:09 by charles #+# #+# */
+/* Updated: 2020/04/04 19:56:26 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_dstr.h"
+
+#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 (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);
+}