aboutsummaryrefslogtreecommitdiff
path: root/src/str
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-09 12:31:50 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-09 12:31:50 +0200
commit02abc030a68cb2fdd2f21c96db830ec8cb9176ad (patch)
tree0c2d67c94a3618639fc2cd29d8bc78820e41c254 /src/str
parentb5124347359833fcde33452978c62133879c6c9e (diff)
parent3a2d19df9e509d0b015c786eb02f8315ff0ad91c (diff)
downloadlibft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.gz
libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.bz2
libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.zip
Merge remote-tracking branch 'origin/minishell'
Diffstat (limited to 'src/str')
-rw-r--r--src/str/ft_atoi.c7
-rw-r--r--src/str/ft_fnmatch.c37
-rw-r--r--src/str/ft_strcasecmp.c2
-rw-r--r--src/str/ft_strcat3.c26
-rw-r--r--src/str/ft_strncasecmp.c2
-rw-r--r--src/str/ft_strncmp.c3
-rw-r--r--src/str/ft_strsep.c15
-rw-r--r--src/str/ft_strsjoin.c50
-rw-r--r--src/str/ft_strsjoinf.c29
-rw-r--r--src/str/ft_strsub.c (renamed from src/str/ft_substr.c)24
-rw-r--r--src/str/ft_strsubf.c30
-rw-r--r--src/str/ft_strtrim.c4
12 files changed, 211 insertions, 18 deletions
diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c
index d6fa5bb..b8f979d 100644
--- a/src/str/ft_atoi.c
+++ b/src/str/ft_atoi.c
@@ -6,14 +6,17 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */
-/* Updated: 2020/01/15 10:56:06 by cacharle ### ########.fr */
+/* Updated: 2020/04/04 22:34:33 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
-** Convert a string to an int
+** \brief Extract first int in a string
+** (takes as much digits has possible)
+** \param str String to convert
+** \return Extracted int
*/
int ft_atoi(const char *str)
diff --git a/src/str/ft_fnmatch.c b/src/str/ft_fnmatch.c
new file mode 100644
index 0000000..5fc35d8
--- /dev/null
+++ b/src/str/ft_fnmatch.c
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_fnmatch.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/02 23:24:16 by charles #+# #+# */
+/* Updated: 2020/04/03 00:28:46 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Search a glob pattern in a string
+** \param pattern Glob pattern '*' are interpreted as zero or more character
+** \param string String to search in
+** \return True if pattern was found, false otherwise
+*/
+
+bool ft_fnmatch(const char *pattern, const char *string)
+{
+ if (*pattern == '\0')
+ return (*string == '\0');
+ if (*string == '\0')
+ return (*pattern == '\0' || (*pattern == '*' && pattern[1] == '\0'));
+ if (*pattern == '*')
+ {
+ if (ft_fnmatch(pattern + 1, string))
+ return (true);
+ return (ft_fnmatch(pattern, string + 1));
+ }
+ if (*pattern != *string)
+ return (false);
+ return (ft_fnmatch(pattern + 1, string + 1));
+}
diff --git a/src/str/ft_strcasecmp.c b/src/str/ft_strcasecmp.c
index 044e6de..6dd86eb 100644
--- a/src/str/ft_strcasecmp.c
+++ b/src/str/ft_strcasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft_str.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strcasecmp(const char *s1, const char *s2)
{
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_strncasecmp.c b/src/str/ft_strncasecmp.c
index aafdc8c..7153237 100644
--- a/src/str/ft_strncasecmp.c
+++ b/src/str/ft_strncasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strncasecmp(const char *s1, const char *s2, size_t n)
{
diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c
index a0371e4..3e01708 100644
--- a/src/str/ft_strncmp.c
+++ b/src/str/ft_strncmp.c
@@ -6,11 +6,10 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */
-/* Updated: 2020/02/10 04:17:50 by cacharle ### ########.fr */
+/* Updated: 2020/05/09 12:29:41 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_str.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
diff --git a/src/str/ft_strsep.c b/src/str/ft_strsep.c
index 2000706..50197fb 100644
--- a/src/str/ft_strsep.c
+++ b/src/str/ft_strsep.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 04:44:11 by cacharle #+# #+# */
-/* Updated: 2020/02/10 04:51:15 by cacharle ### ########.fr */
+/* Updated: 2020/04/04 12:48:24 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,13 +15,18 @@
char *ft_strsep(char **stringp, const char *delim)
{
char *tmp;
+ char *origin;
- if (stringp == NULL || *stringp == NULL || delim == NULL)
+ if (*stringp == NULL)
return (NULL);
+ origin = *stringp;
tmp = ft_strpbrk(*stringp, delim);
if (tmp == NULL)
- return (NULL);
+ {
+ *stringp = NULL;
+ return (origin);
+ }
*tmp = '\0';
- *stringp = tmp;
- return (tmp);
+ *stringp = tmp + 1;
+ return (origin);
}
diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c
new file mode 100644
index 0000000..0923bde
--- /dev/null
+++ b/src/str/ft_strsjoin.c
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/04 14:30:08 by charles #+# #+# */
+/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Join null-terminated array of strings
+** \param strs Array of strings
+** \param delim String iterspersed between strings
+** \return Joined string or NULL on error
+** \note Empty strings are ignored
+*/
+
+char *ft_strsjoin(char **strs, char *delim)
+{
+ int i;
+ size_t join_len;
+ size_t delim_len;
+ char *join;
+
+ delim_len = ft_strlen(delim);
+ join_len = 0;
+ i = -1;
+ while (strs[++i] != NULL)
+ {
+ join_len += ft_strlen(strs[i]);
+ if (strs[i + 1] != NULL)
+ join_len += delim_len;
+ }
+ if ((join = (char*)malloc(sizeof(char) * (join_len + 1))) == NULL)
+ return (NULL);
+ join[0] = '\0';
+ i = -1;
+ while (strs[++i] != NULL)
+ {
+ ft_strcat(join, strs[i]);
+ if (*strs[i] != '\0' && strs[i + 1] != NULL)
+ ft_strcat(join, delim);
+ }
+ return (join);
+}
diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c
new file mode 100644
index 0000000..36a2892
--- /dev/null
+++ b/src/str/ft_strsjoinf.c
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strsjoinf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/04 14:27:33 by charles #+# #+# */
+/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_str.h"
+
+/*
+** \brief Join null-terminated array of strings and free the array
+** \param strs Array of strings
+** \param delim String which will be put between each string
+** \return Joined string or NULL on error
+*/
+
+char *ft_strsjoinf(char **strs, char *delim)
+{
+ char *ret;
+
+ ret = ft_strsjoin(strs, delim);
+ ft_split_destroy(strs);
+ return (ret);
+}
diff --git a/src/str/ft_substr.c b/src/str/ft_strsub.c
index 59fe3f2..c7121bd 100644
--- a/src/str/ft_substr.c
+++ b/src/str/ft_strsub.c
@@ -1,23 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* 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/05/09 12:30:39 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);
sub[len] = '\0';
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));
}