From 3a2d19df9e509d0b015c786eb02f8315ff0ad91c Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 5 Apr 2020 14:05:43 +0200 Subject: Renamed ft_substr to ft_strsub, Added ft_strsubf, ft_strcat3 --- src/str/ft_strcat3.c | 26 ++++++++++++++++++++++++++ src/str/ft_strsub.c | 39 +++++++++++++++++++++++++++++++++++++++ src/str/ft_strsubf.c | 30 ++++++++++++++++++++++++++++++ src/str/ft_strtrim.c | 4 ++-- src/str/ft_substr.c | 26 -------------------------- 5 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 src/str/ft_strcat3.c create mode 100644 src/str/ft_strsub.c create mode 100644 src/str/ft_strsubf.c delete mode 100644 src/str/ft_substr.c (limited to 'src') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_strsub.c b/src/str/ft_strsub.c new file mode 100644 index 0000000..77bffb2 --- /dev/null +++ b/src/str/ft_strsub.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ +/* Updated: 2020/04/05 13:47:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \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; + 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); + 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)); } diff --git a/src/str/ft_substr.c b/src/str/ft_substr.c deleted file mode 100644 index ad9c706..0000000 --- a/src/str/ft_substr.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_substr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:44:42 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_substr(char const *s, unsigned int start, size_t len) -{ - char *sub; - - if (s == NULL) - return (NULL); - if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) - return (NULL); - if (start > ft_strlen(s)) - return (sub); - return (ft_strncpy(sub, s + start, len)); -} -- cgit