aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libft_dstr.h35
-rw-r--r--src/dstr/ft_dstrdestroy.c21
-rw-r--r--src/dstr/ft_dstrgrow.c31
-rw-r--r--src/dstr/ft_dstrinsert.c31
-rw-r--r--src/dstr/ft_dstrnew.c25
-rw-r--r--src/dstr/ft_dstrunwrap.c22
6 files changed, 165 insertions, 0 deletions
diff --git a/include/libft_dstr.h b/include/libft_dstr.h
new file mode 100644
index 0000000..13cb4e3
--- /dev/null
+++ b/include/libft_dstr.h
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* libft_dstr.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/03 10:39:51 by charles #+# #+# */
+/* Updated: 2020/04/03 15:10:54 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef LIBFT_DSTR_H
+# define LIBFT_DSTR_H
+
+# include <stdlib.h>
+# include "libft_def.h"
+# include "libft_str.h"
+# include "libft_mem.h"
+
+typedef struct s_ftdstr
+{
+ char *str;
+ size_t length;
+ size_t capacity;
+} t_ftdstr;
+
+t_ftdstr *ft_dstrnew(char *from);
+void ft_dstrdestroy(t_ftdstr *dstr);
+t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least);
+char *ft_dstrunwrap(t_ftdstr *dstr);
+t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i);
+// t_ftdstr *ft_dstrreplace(t_ftdstr *dstr, char *from, char *to);
+
+#endif
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <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);
+}
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}