aboutsummaryrefslogtreecommitdiff
path: root/src/dstr/ft_dstrinsert.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dstr/ft_dstrinsert.c')
-rw-r--r--src/dstr/ft_dstrinsert.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/dstr/ft_dstrinsert.c b/src/dstr/ft_dstrinsert.c
new file mode 100644
index 0000000..6a1486c
--- /dev/null
+++ b/src/dstr/ft_dstrinsert.c
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_dstrinsert.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* 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 */
+/* */
+/* ************************************************************************** */
+
+#include "libft_dstr.h"
+
+t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i)
+{
+ size_t inserted_len;
+
+ if (i > dstr->capacity)
+ 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);
+ ft_memmove(dstr->str + i + inserted_len,
+ dstr->str + i,
+ dstr->length - i + 1);
+ ft_memcpy(dstr->str + i, inserted, inserted_len);
+ dstr->length += inserted_len;
+ return (dstr);
+}