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_dstrdestroy.c | 21 +++++++++++++++++++++ src/dstr/ft_dstrgrow.c | 31 +++++++++++++++++++++++++++++++ src/dstr/ft_dstrinsert.c | 31 +++++++++++++++++++++++++++++++ src/dstr/ft_dstrnew.c | 25 +++++++++++++++++++++++++ src/dstr/ft_dstrunwrap.c | 22 ++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 src/dstr/ft_dstrdestroy.c create mode 100644 src/dstr/ft_dstrgrow.c create mode 100644 src/dstr/ft_dstrinsert.c create mode 100644 src/dstr/ft_dstrnew.c create mode 100644 src/dstr/ft_dstrunwrap.c (limited to 'src/dstr') diff --git a/src/dstr/ft_dstrdestroy.c b/src/dstr/ft_dstrdestroy.c new file mode 100644 index 0000000..6f1a819 --- /dev/null +++ b/src/dstr/ft_dstrdestroy.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:58:46 by charles #+# #+# */ +/* Updated: 2020/04/03 13:59:25 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +void ft_dstrdestroy(t_ftdstr *dstr) +{ + if (dstr == NULL) + return ; + free(dstr->str); + free(dstr); +} diff --git a/src/dstr/ft_dstrgrow.c b/src/dstr/ft_dstrgrow.c new file mode 100644 index 0000000..9ad51ea --- /dev/null +++ b/src/dstr/ft_dstrgrow.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:17:09 by charles #+# #+# */ +/* Updated: 2020/04/03 15:09:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +#define FT_DSTR_GROWTH_FACTOR 1.5 + +t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least) +{ + size_t new_capacity; + char *new_str; + + new_capacity = dstr->capacity <= 1 ? 2 : dstr->capacity; + while (new_capacity < at_least) + 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; + dstr->str = new_str; + return (dstr); +} 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); +} diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c new file mode 100644 index 0000000..189fc78 --- /dev/null +++ b/src/dstr/ft_dstrnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:54:52 by charles #+# #+# */ +/* Updated: 2020/04/03 13:58:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +t_ftdstr *ft_dstrnew(char *from) +{ + t_ftdstr *dstr; + + if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || + (dstr->str = ft_strdup(from)) == NULL) + return (NULL); + dstr->length = ft_strlen(from); + dstr->capacity = dstr->length + 1; + return (dstr); +} diff --git a/src/dstr/ft_dstrunwrap.c b/src/dstr/ft_dstrunwrap.c new file mode 100644 index 0000000..ba62a23 --- /dev/null +++ b/src/dstr/ft_dstrunwrap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrunwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:59:35 by charles #+# #+# */ +/* Updated: 2020/04/03 14:00:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +char *ft_dstrunwrap(t_ftdstr *dstr) +{ + char *tmp; + + tmp = dstr->str; + free(dstr); + return (tmp); +} -- 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_dstrdestroy.c | 7 ++++++- src/dstr/ft_dstrgrow.c | 14 ++++++++++++-- src/dstr/ft_dstrinsert.c | 17 ++++++++++++----- src/dstr/ft_dstrnew.c | 9 ++++++++- src/dstr/ft_dstrunwrap.c | 8 +++++++- 5 files changed, 45 insertions(+), 10 deletions(-) (limited to 'src/dstr') 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; -- cgit From 944a236443bacf531085b346d06907b24e3739b1 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 5 Apr 2020 01:11:50 +0200 Subject: Added ft_dstrerase, ft_dstrsubstitute --- src/dstr/ft_dstrerase.c | 35 +++++++++++++++++++++++++++++++++++ src/dstr/ft_dstrsubstitute.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/dstr/ft_dstrerase.c create mode 100644 src/dstr/ft_dstrsubstitute.c (limited to 'src/dstr') diff --git a/src/dstr/ft_dstrerase.c b/src/dstr/ft_dstrerase.c new file mode 100644 index 0000000..3d4732b --- /dev/null +++ b/src/dstr/ft_dstrerase.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrerase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:39:12 by charles #+# #+# */ +/* Updated: 2020/04/05 01:11:07 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Erase part of a dynamic string +** \param dstr Dynamic string to erase from +** \param start Erase starting index +** \param len Number of character to erase +*/ + +void ft_dstrerase(t_ftdstr *dstr, size_t start, size_t len) +{ + if (start > dstr->length) + return ; + if (start + len > dstr->length) + len = dstr->length - start; + ft_memmove( + dstr->str + start, + dstr->str + start + len, + dstr->length - start - len + ); + dstr->length -= len; + dstr->str[dstr->length] = '\0'; +} diff --git a/src/dstr/ft_dstrsubstitute.c b/src/dstr/ft_dstrsubstitute.c new file mode 100644 index 0000000..84adc29 --- /dev/null +++ b/src/dstr/ft_dstrsubstitute.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrsubstitute.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:22:55 by charles #+# #+# */ +/* Updated: 2020/04/05 00:38:40 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Substitute part of a dynamic string for an other string +** \param dstr Dynamic string to substitute in +** \param replacement Replacement text +** \param start Substitution start index +** \param len Substitution length +*/ + +t_ftdstr *ft_dstrsubstitute( + t_ftdstr *dstr, + char *replacement, + size_t start, + size_t len +) +{ + ft_dstrerase(dstr, start, len); + return (ft_dstrinsert(dstr, replacement, start)); +} -- cgit