aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_strsub.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/str/ft_strsub.c
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/str/ft_strsub.c')
-rw-r--r--src/str/ft_strsub.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/str/ft_strsub.c b/src/str/ft_strsub.c
new file mode 100644
index 0000000..c7121bd
--- /dev/null
+++ b/src/str/ft_strsub.c
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsub.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */
+/* Updated: 2020/05/09 12:30:39 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';
+ if (start > ft_strlen(s))
+ return (sub);
+ return (ft_strncpy(sub, s + start, len));
+}