aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/str/ft_strcat3.c26
-rw-r--r--src/str/ft_strsub.c (renamed from src/str/ft_substr.c)27
-rw-r--r--src/str/ft_strsubf.c30
-rw-r--r--src/str/ft_strtrim.c4
4 files changed, 78 insertions, 9 deletions
diff --git a/src/str/ft_strcat3.c b/src/str/ft_strcat3.c
new file mode 100644
index 0000000..1f7c5df
--- /dev/null
+++ b/src/str/ft_strcat3.c
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strcat3.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/05 12:53:05 by charles #+# #+# */
+/* Updated: 2020/04/05 12:55:49 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Wrapper around ft_strcat to concatenate 3 strings
+** \param dest Destination of the concatenation
+** \param src1 First concatenation
+** \param src2 Second concatenation
+** \return Pointer to destination
+*/
+
+char *ft_strcat3(char *dest, const char *src1, const char *src2)
+{
+ return (ft_strcat(ft_strcat(dest, src1), src2));
+}
diff --git a/src/str/ft_substr.c b/src/str/ft_strsub.c
index ad9c706..77bffb2 100644
--- a/src/str/ft_substr.c
+++ b/src/str/ft_strsub.c
@@ -1,26 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_substr.c :+: :+: :+: */
+/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */
-/* Updated: 2020/02/14 03:44:42 by cacharle ### ########.fr */
+/* Updated: 2020/04/05 13:47:55 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
+#include "libft_str.h"
-char *ft_substr(char const *s, unsigned int start, size_t len)
+/*
+** \brief Extract a substring from a string
+** \param s String to extract from
+** \param start Starting index of the substring
+** \param len Substring length
+** \return The created substring or NULL on error
+*/
+
+char *ft_strsub(char const *s, size_t start, size_t len)
{
- char *sub;
+ char *sub;
+ size_t s_len;
if (s == NULL)
return (NULL);
+ s_len = ft_strlen(s);
+ if (start > s_len)
+ return (NULL);
+ if (start + len > s_len)
+ len = s_len - start;
if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
- if (start > ft_strlen(s))
- return (sub);
+ sub[len] = '\0';
return (ft_strncpy(sub, s + start, len));
}
diff --git a/src/str/ft_strsubf.c b/src/str/ft_strsubf.c
new file mode 100644
index 0000000..dc49ba5
--- /dev/null
+++ b/src/str/ft_strsubf.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsubf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/05 13:48:13 by charles #+# #+# */
+/* Updated: 2020/04/05 13:51:47 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Wrapper around ft_substr which free the original string
+** \param s String to extract from (will be free)
+** \param start Starting index of the substring
+** \param len Substring length
+** \return The created substring or NULL on error
+*/
+
+char *ft_strsubf(char const *s, size_t start, size_t len)
+{
+ char *ret;
+
+ ret = ft_strsub(s, start, len);
+ free((void*)s);
+ return (ret);
+}
diff --git a/src/str/ft_strtrim.c b/src/str/ft_strtrim.c
index aa48826..fa9b192 100644
--- a/src/str/ft_strtrim.c
+++ b/src/str/ft_strtrim.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:24:16 by cacharle #+# #+# */
-/* Updated: 2019/11/20 03:52:58 by cacharle ### ########.fr */
+/* Updated: 2020/04/05 13:50:43 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -27,5 +27,5 @@ char *ft_strtrim(char const *s1, char const *set)
while (s1[start + len - 1]
&& ft_strchr(set, s1[start + len - 1]) != NULL)
len--;
- return (ft_substr(s1, start, len));
+ return (ft_strsub(s1, start, len));
}