From bf263810b0a47a68fae0681e6e6d2229a488c069 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 15:11:33 +0200 Subject: Added Dynamic string struct (no test, no doc) --- src/dstr/ft_dstrinsert.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/dstr/ft_dstrinsert.c (limited to 'src/dstr/ft_dstrinsert.c') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} -- cgit From 42316d8393d32bd88fb8e0cba6825185f78dacd0 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 4 Apr 2020 21:28:21 +0200 Subject: Added test and doc for dynamic string --- src/dstr/ft_dstrinsert.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src/dstr/ft_dstrinsert.c') 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); -- cgit