diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-15 10:04:09 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-15 10:04:09 +0100 |
| commit | 1e9b077e0d9f3c50d7d518bf6ea7f08e7f5ec590 (patch) | |
| tree | 21d906cd9f96c58af572ce3bb6784d1d8fca18d2 /src/str | |
| parent | 49ad59b0e773e92e93fb69ede889c781ca53e680 (diff) | |
| download | libft-1e9b077e0d9f3c50d7d518bf6ea7f08e7f5ec590.tar.gz libft-1e9b077e0d9f3c50d7d518bf6ea7f08e7f5ec590.tar.bz2 libft-1e9b077e0d9f3c50d7d518bf6ea7f08e7f5ec590.zip | |
splited src in category, more generic makefile
Diffstat (limited to 'src/str')
33 files changed, 908 insertions, 0 deletions
diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c new file mode 100644 index 0000000..0ad6022 --- /dev/null +++ b/src/str/ft_atoi.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ +/* Updated: 2020/01/15 09:26:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#define MIN_INT -2147483648 +#define MAX_INT 2147483647 + +int ft_atoi(const char *str) +{ + unsigned int nb; + int i; + int is_negative; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + if (*str == '-' || *str == '+') + if (*str++ == '-') + is_negative = 1; + i = 0; + nb = 0; + while (ft_isdigit(str[i])) + { + if (!is_negative && nb > (unsigned int)MAX_INT) + return (-1); + else if (nb > (unsigned int)MIN_INT) + return (0); + nb *= 10; + nb += str[i++] & 0x0F; + } + return ((int)(is_negative ? -nb : nb)); +} diff --git a/src/str/ft_itoa.c b/src/str/ft_itoa.c new file mode 100644 index 0000000..166e278 --- /dev/null +++ b/src/str/ft_itoa.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:13:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + char *str; + int len; + unsigned int u_nbr; + + len = n < 0 || n == 0 ? 1 : 0; + u_nbr = n < 0 ? -n : n; + while (u_nbr > 0) + { + u_nbr /= 10; + len++; + } + if ((str = ft_strnew(len)) == NULL) + return (NULL); + u_nbr = n < 0 ? -n : n; + if (n < 0) + str[0] = '-'; + while (--len >= (n < 0 ? 1 : 0)) + { + str[len] = (u_nbr % 10) | 0x30; + u_nbr /= 10; + } + return (str); +} diff --git a/src/str/ft_split.c b/src/str/ft_split.c new file mode 100644 index 0000000..6fb5964 --- /dev/null +++ b/src/str/ft_split.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t count_segment(char const *s, char c) +{ + size_t counter; + int i; + + counter = 0; + i = 0; + while (s[i]) + { + if (s[i] == c) + { + i++; + continue ; + } + counter++; + while (s[i] && s[i] != c) + i++; + } + return (counter); +} + +static void *destroy_strs(char **strs) +{ + if (strs == NULL) + return (NULL); + while (*strs != NULL) + free(*strs++); + free(strs); + return (NULL); +} + +char **ft_split(char const *s, char c) +{ + char **strs; + size_t tab_counter; + size_t i; + size_t j; + + if (s == NULL) + return (NULL); + tab_counter = count_segment(s, c); + if ((strs = (char**)malloc(sizeof(char*) * (tab_counter + 1))) == NULL) + return (NULL); + tab_counter = 0; + j = -1; + while (s[++j]) + { + if (s[j] == c) + continue ; + i = 0; + while (s[j + i] && s[j + i] != c) + i++; + if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) + return (destroy_strs(strs)); + j += i - 1; + } + strs[tab_counter] = NULL; + return (strs); +} diff --git a/src/str/ft_strcat.c b/src/str/ft_strcat.c new file mode 100644 index 0000000..d5bc7e0 --- /dev/null +++ b/src/str/ft_strcat.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:09:41 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:03:38 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *dest, const char *src) +{ + ft_memcpy(dest + ft_strlen(dest), src, ft_strlen(src) + 1); + return (dest); +} diff --git a/src/str/ft_strchr.c b/src/str/ft_strchr.c new file mode 100644 index 0000000..50bfc0a --- /dev/null +++ b/src/str/ft_strchr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:14:47 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:04:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + return (ft_memchr(s, c, ft_strlen(s) + 1)); +} diff --git a/src/str/ft_strclr.c b/src/str/ft_strclr.c new file mode 100644 index 0000000..7e412fe --- /dev/null +++ b/src/str/ft_strclr.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:15:18 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:11:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + if (s == NULL) + return ; + ft_bzero(s, ft_strlen(s)); +} diff --git a/src/str/ft_strcmp.c b/src/str/ft_strcmp.c new file mode 100644 index 0000000..1978286 --- /dev/null +++ b/src/str/ft_strcmp.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:16:07 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:58:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + return (ft_memcmp(s1, s2, ft_strlen(s1) + 1)); +} diff --git a/src/str/ft_strcount.c b/src/str/ft_strcount.c new file mode 100644 index 0000000..87e756d --- /dev/null +++ b/src/str/ft_strcount.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcount.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/15 09:17:48 by cacharle #+# #+# */ +/* Updated: 2019/11/15 09:19:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcount(char *str, char c) +{ + int counter; + + if (c == '\0') + return (1); + counter = 0; + while (*str) + if (*str++ == c) + counter++; + return (counter); +} diff --git a/src/str/ft_strcpy.c b/src/str/ft_strcpy.c new file mode 100644 index 0000000..9677b24 --- /dev/null +++ b/src/str/ft_strcpy.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:38:36 by cacharle #+# #+# */ +/* Updated: 2019/11/20 23:25:57 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dest, const char *src) +{ + return (ft_memcpy(dest, src, ft_strlen(src) + 1)); +} diff --git a/src/str/ft_strdel.c b/src/str/ft_strdel.c new file mode 100644 index 0000000..05cf064 --- /dev/null +++ b/src/str/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:39:14 by cacharle #+# #+# */ +/* Updated: 2019/11/20 01:58:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + ft_memdel((void*)as); +} diff --git a/src/str/ft_strdup.c b/src/str/ft_strdup.c new file mode 100644 index 0000000..65a6ac6 --- /dev/null +++ b/src/str/ft_strdup.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:18:07 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:13:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *clone; + + if ((clone = ft_strnew(ft_strlen(s))) == NULL) + return (NULL); + return (ft_strcpy(clone, s)); +} diff --git a/src/str/ft_strequ.c b/src/str/ft_strequ.c new file mode 100644 index 0000000..75ccb81 --- /dev/null +++ b/src/str/ft_strequ.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:18:34 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:00:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + if (s1 == NULL || s2 == NULL) + return (0); + return (ft_strcmp(s1, s2) == 0); +} diff --git a/src/str/ft_striter.c b/src/str/ft_striter.c new file mode 100644 index 0000000..f410d24 --- /dev/null +++ b/src/str/ft_striter.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:38:16 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:01:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + if (s == NULL || f == NULL) + return ; + while (*s) + (*f)(s++); +} diff --git a/src/str/ft_striteri.c b/src/str/ft_striteri.c new file mode 100644 index 0000000..05f15d4 --- /dev/null +++ b/src/str/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:33:09 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:01:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i]) + { + (*f)(i, &s[i]); + i++; + } +} diff --git a/src/str/ft_strjoin.c b/src/str/ft_strjoin.c new file mode 100644 index 0000000..2bc4908 --- /dev/null +++ b/src/str/ft_strjoin.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:35:26 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:20 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *joined; + + if (s1 == NULL || s2 == NULL) + return (NULL); + if ((joined = ft_strnew(ft_strlen(s1) + ft_strlen(s2))) == NULL) + return (NULL); + return (ft_strcat(ft_strcpy(joined, s1), s2)); +} diff --git a/src/str/ft_strjoin_free.c b/src/str/ft_strjoin_free.c new file mode 100644 index 0000000..4050b77 --- /dev/null +++ b/src/str/ft_strjoin_free.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/05 21:32:48 by cacharle #+# #+# */ +/* Updated: 2019/11/14 10:07:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "libft.h" + +char *ft_strjoin_free(char const *s1, char const *s2, int free_nb) +{ + char *joined; + + if (s1 == NULL || s2 == NULL || free_nb < 0 || free_nb > 2) + return (NULL); + if ((joined = ft_strjoin(s1, s2)) == NULL) + return (NULL); + if (free_nb == 1) + free((void*)s1); + if (free_nb == 2) + { + free((void*)s1); + free((void*)s2); + } + return (joined); +} diff --git a/src/str/ft_strjoin_free_snd.c b/src/str/ft_strjoin_free_snd.c new file mode 100644 index 0000000..0503211 --- /dev/null +++ b/src/str/ft_strjoin_free_snd.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin_free_snd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/05 22:12:56 by cacharle #+# #+# */ +/* Updated: 2019/11/14 10:07:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "libft.h" + +char *ft_strjoin_free_snd(char const *s1, char const *s2) +{ + char *joined; + + if (s1 == NULL || s2 == NULL) + return (NULL); + if ((joined = ft_strjoin(s1, s2)) == NULL) + return (NULL); + free((void*)s2); + return (joined); +} diff --git a/src/str/ft_strlcat.c b/src/str/ft_strlcat.c new file mode 100644 index 0000000..ce7fa0b --- /dev/null +++ b/src/str/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:31:37 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:31:08 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t i; + size_t j; + size_t dst_len; + size_t src_len; + + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + if (size <= dst_len) + return (src_len + size); + i = 0; + j = dst_len; + while (src[i] && j < size - 1) + dst[j++] = src[i++]; + dst[j] = '\0'; + return (dst_len + src_len); +} diff --git a/src/str/ft_strlcpy.c b/src/str/ft_strlcpy.c new file mode 100644 index 0000000..6afb8f5 --- /dev/null +++ b/src/str/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 12:28:47 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:31:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + unsigned int i; + + if (dst == NULL || src == NULL) + return (0); + if (size == 0) + return (ft_strlen(src)); + i = -1; + while (++i < size - 1 && src[i] != '\0') + dst[i] = src[i]; + dst[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/src/str/ft_strlen.c b/src/str/ft_strlen.c new file mode 100644 index 0000000..0e0a47c --- /dev/null +++ b/src/str/ft_strlen.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:45:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + unsigned long int *ptr; + const char *cpy; + + ptr = (unsigned long int*)s; + while (TRUE) + { + cpy = (const char*)ptr++; + if (cpy[0] == '\0') + return (cpy - s); + if (cpy[1] == '\0') + return (cpy + 1 - s); + if (cpy[2] == '\0') + return (cpy + 2 - s); + if (cpy[3] == '\0') + return (cpy + 3 - s); + if (cpy[4] == '\0') + return (cpy + 4 - s); + if (cpy[5] == '\0') + return (cpy + 5 - s); + if (cpy[6] == '\0') + return (cpy + 6 - s); + if (cpy[7] == '\0') + return (cpy + 7 - s); + } +} diff --git a/src/str/ft_strmap.c b/src/str/ft_strmap.c new file mode 100644 index 0000000..61d16f1 --- /dev/null +++ b/src/str/ft_strmap.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:29:52 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + size_t i; + size_t len; + char *mapped; + + if (s == NULL || f == NULL) + return (NULL); + len = ft_strlen(s); + if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + i = 0; + while (i < len) + { + mapped[i] = (*f)(s[i]); + i++; + } + mapped[i] = '\0'; + return (mapped); +} diff --git a/src/str/ft_strmapi.c b/src/str/ft_strmapi.c new file mode 100644 index 0000000..71d77e4 --- /dev/null +++ b/src/str/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:29:32 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:15 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char *s, char (*f)(unsigned int, char)) +{ + size_t i; + size_t len; + char *mapped; + + if (s == NULL || f == NULL) + return (NULL); + len = ft_strlen(s); + if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + i = 0; + while (i < len) + { + mapped[i] = (*f)((unsigned int)i, s[i]); + i++; + } + mapped[i] = '\0'; + return (mapped); +} diff --git a/src/str/ft_strncat.c b/src/str/ft_strncat.c new file mode 100644 index 0000000..d68db0a --- /dev/null +++ b/src/str/ft_strncat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:28:37 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:33:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *dest, const char *src, size_t n) +{ + size_t i; + size_t j; + + i = ft_strlen(dest); + j = 0; + while (j < n && src[j]) + { + dest[i + j] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +} diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c new file mode 100644 index 0000000..cd303fc --- /dev/null +++ b/src/str/ft_strncmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */ +/* Updated: 2019/11/21 01:56:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while (i + 1 < n && s1[i] == s2[i] && s1[i]) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/src/str/ft_strncpy.c b/src/str/ft_strncpy.c new file mode 100644 index 0000000..a0cfb4c --- /dev/null +++ b/src/str/ft_strncpy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:26:59 by cacharle #+# #+# */ +/* Updated: 2019/11/21 02:49:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dest, const char *src, size_t n) +{ + size_t len; + + len = ft_strlen(src); + ft_memcpy(dest, src, MIN(n, len)); + if (len < n) + ft_bzero(dest + len, n - len); + return (dest); +} diff --git a/src/str/ft_strndup.c b/src/str/ft_strndup.c new file mode 100644 index 0000000..0683dae --- /dev/null +++ b/src/str/ft_strndup.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/25 03:28:52 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:15:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strndup(const char *s1, size_t n) +{ + char *clone; + + if ((clone = ft_strnew(n)) == NULL) + return (NULL); + return (ft_strncpy(clone, s1, n)); +} diff --git a/src/str/ft_strnequ.c b/src/str/ft_strnequ.c new file mode 100644 index 0000000..e242ee7 --- /dev/null +++ b/src/str/ft_strnequ.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:30:27 by cacharle #+# #+# */ +/* Updated: 2019/11/20 02:00:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + if (s1 == NULL || s2 == NULL) + return (0); + return (ft_strncmp(s1, s2, n) == 0); +} diff --git a/src/str/ft_strnew.c b/src/str/ft_strnew.c new file mode 100644 index 0000000..1bca6d5 --- /dev/null +++ b/src/str/ft_strnew.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:17:34 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:16:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + return ((char*)ft_calloc(size + 1, sizeof(char))); +} diff --git a/src/str/ft_strnstr.c b/src/str/ft_strnstr.c new file mode 100644 index 0000000..4995637 --- /dev/null +++ b/src/str/ft_strnstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:25:13 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:58:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t needle_len; + + needle_len = ft_strlen(needle); + if (needle_len == 0) + return ((char*)haystack); + while (*haystack && len-- >= needle_len) + { + if (ft_strnequ(haystack, needle, needle_len)) + return ((char*)haystack); + haystack++; + } + return (NULL); +} diff --git a/src/str/ft_strrchr.c b/src/str/ft_strrchr.c new file mode 100644 index 0000000..56c8be5 --- /dev/null +++ b/src/str/ft_strrchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:26:24 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:36:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s) + 1; + while (s[--i] != (char)c) + if (i == 0) + return (NULL); + return ((char*)s + i); +} diff --git a/src/str/ft_strstr.c b/src/str/ft_strstr.c new file mode 100644 index 0000000..4d4d403 --- /dev/null +++ b/src/str/ft_strstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:15:29 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:58:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *haystack, const char *needle) +{ + size_t needle_len; + + needle_len = ft_strlen(needle); + if (needle_len == 0) + return ((char*)haystack); + while (*haystack) + { + if (ft_strnequ(haystack, needle, needle_len)) + return ((char*)haystack); + haystack++; + } + return (NULL); +} diff --git a/src/str/ft_strtrim.c b/src/str/ft_strtrim.c new file mode 100644 index 0000000..aa48826 --- /dev/null +++ b/src/str/ft_strtrim.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:24:16 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:52:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t start; + size_t len; + + if (s1 == NULL || set == NULL) + return (NULL); + start = 0; + while (s1[start] && ft_strchr(set, s1[start]) != NULL) + start++; + len = ft_strlen(&s1[start]); + if (len != 0) + while (s1[start + len - 1] + && ft_strchr(set, s1[start + len - 1]) != NULL) + len--; + return (ft_substr(s1, start, len)); +} diff --git a/src/str/ft_substr.c b/src/str/ft_substr.c new file mode 100644 index 0000000..84d6c58 --- /dev/null +++ b/src/str/ft_substr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ +/* Updated: 2019/11/20 03:57:58 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 = ft_strnew(len)) == NULL) + return (NULL); + if (start > ft_strlen(s)) + return (sub); + return (ft_strncpy(sub, s + start, len)); +} |
