diff options
Diffstat (limited to 'src/str')
| -rw-r--r-- | src/str/ft_atof.c | 18 | ||||
| -rw-r--r-- | src/str/ft_atoi.c | 9 | ||||
| -rw-r--r-- | src/str/ft_fnmatch.c | 37 | ||||
| -rw-r--r-- | src/str/ft_split.c | 14 | ||||
| -rw-r--r-- | src/str/ft_strcasecmp.c | 2 | ||||
| -rw-r--r-- | src/str/ft_strcat.c | 1 | ||||
| -rw-r--r-- | src/str/ft_strcat3.c | 26 | ||||
| -rw-r--r-- | src/str/ft_strcmp.c | 7 | ||||
| -rw-r--r-- | src/str/ft_strdup.c | 7 | ||||
| -rw-r--r-- | src/str/ft_strjoin3.c | 36 | ||||
| -rw-r--r-- | src/str/ft_strjoinf.c | 4 | ||||
| -rw-r--r-- | src/str/ft_strlen.c | 78 | ||||
| -rw-r--r-- | src/str/ft_strmove.c | 25 | ||||
| -rw-r--r-- | src/str/ft_strncasecmp.c | 2 | ||||
| -rw-r--r-- | src/str/ft_strncat.c | 18 | ||||
| -rw-r--r-- | src/str/ft_strncmp.c | 15 | ||||
| -rw-r--r-- | src/str/ft_strnew.c | 19 | ||||
| -rw-r--r-- | src/str/ft_strsdestroy.c | 30 | ||||
| -rw-r--r-- | src/str/ft_strsep.c | 15 | ||||
| -rw-r--r-- | src/str/ft_strsjoin.c | 50 | ||||
| -rw-r--r-- | src/str/ft_strsjoinf.c | 29 | ||||
| -rw-r--r-- | src/str/ft_strslen.c | 23 | ||||
| -rw-r--r-- | src/str/ft_strstr.c | 9 | ||||
| -rw-r--r-- | src/str/ft_strsub.c (renamed from src/str/ft_substr.c) | 25 | ||||
| -rw-r--r-- | src/str/ft_strsubf.c | 30 | ||||
| -rw-r--r-- | src/str/ft_strtof.c | 44 | ||||
| -rw-r--r-- | src/str/ft_strtrim.c | 4 |
27 files changed, 486 insertions, 91 deletions
diff --git a/src/str/ft_atof.c b/src/str/ft_atof.c new file mode 100644 index 0000000..74d5a42 --- /dev/null +++ b/src/str/ft_atof.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 21:03:50 by charles #+# #+# */ +/* Updated: 2020/05/10 21:04:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +float ft_atof(const char *nptr) +{ + return (ft_strtof(nptr, NULL)); +} diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index fa31407..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,12 +6,19 @@ /* 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" +/* +** \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) { return ((int)ft_strtol(str, (char**)NULL, 10)); 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_split.c b/src/str/ft_split.c index 6fb5964..5d164d4 100644 --- a/src/str/ft_split.c +++ b/src/str/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:54:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,16 +33,6 @@ static size_t count_segment(char const *s, char c) 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; @@ -65,7 +55,7 @@ char **ft_split(char const *s, char c) while (s[j + i] && s[j + i] != c) i++; if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) - return (destroy_strs(strs)); + return (ft_strsdestroy(strs)); j += i - 1; } strs[tab_counter] = NULL; 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_strcat.c b/src/str/ft_strcat.c index d5bc7e0..faed515 100644 --- a/src/str/ft_strcat.c +++ b/src/str/ft_strcat.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" char *ft_strcat(char *dest, const char *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 <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_strcmp.c b/src/str/ft_strcmp.c index aced711..25d2972 100644 --- a/src/str/ft_strcmp.c +++ b/src/str/ft_strcmp.c @@ -14,10 +14,5 @@ int ft_strcmp(const char *s1, const char *s2) { - while (*s1 && *s2 && *s1 == *s2) - { - s1++; - s2++; - } - return (*s1 - *s2); + return (ft_memcmp(s1, s2, ft_strlen(s1) + 1)); } diff --git a/src/str/ft_strdup.c b/src/str/ft_strdup.c index b248272..9493d82 100644 --- a/src/str/ft_strdup.c +++ b/src/str/ft_strdup.c @@ -11,12 +11,15 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_str.h" char *ft_strdup(const char *s) { char *clone; + size_t size; - if ((clone = (char*)malloc(sizeof(char) * (ft_strlen(s) + 1))) == NULL) + size = ft_strlen(s) + 1; + if ((clone = (char*)malloc(sizeof(char) * size)) == NULL) return (NULL); - return (ft_strcpy(clone, s)); + return (ft_memcpy(clone, s, size)); } diff --git a/src/str/ft_strjoin3.c b/src/str/ft_strjoin3.c new file mode 100644 index 0000000..e5e5530 --- /dev/null +++ b/src/str/ft_strjoin3.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:00:49 by charles #+# #+# */ +/* Updated: 2020/04/01 18:01:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief Join 3 strings in a new malloc'd one +** \param s1 String 1 +** \param s2 String 2 +** \param s3 String 3 +** \return The joined string +*/ + +char *ft_strjoin3(char const *s1, char const *s2, char const *s3) +{ + char *joined; + + if (s1 == NULL || s2 == NULL || s3 == NULL) + return (NULL); + if ((joined = (char*)malloc(sizeof(char) + * (ft_strlen(s1) + ft_strlen(s2) + ft_strlen(s3) + 1))) == NULL) + return (NULL); + ft_strcpy(joined, s1); + ft_strcat(joined, s2); + ft_strcat(joined, s3); + return (joined); +} diff --git a/src/str/ft_strjoinf.c b/src/str/ft_strjoinf.c index 228a963..adf9825 100644 --- a/src/str/ft_strjoinf.c +++ b/src/str/ft_strjoinf.c @@ -6,12 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 03:41:07 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:41:25 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:20:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdlib.h> -#include "libft.h" #include "libft_str.h" char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoinf_tag tag) diff --git a/src/str/ft_strlen.c b/src/str/ft_strlen.c index 0d593e1..999763e 100644 --- a/src/str/ft_strlen.c +++ b/src/str/ft_strlen.c @@ -6,36 +6,68 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ -/* Updated: 2020/01/17 11:13:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:45:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include <stdint.h> + +/* +** Determining if one byte of a long word is 0 +** +** ~((((lw & 0x7F7F7F7F) + 0x7F7F7F7F) | lw) | 0x7F7F7F7F) +** +** where `lw` is a long word +** +** 0x7F -> 0b 0111 1111 +** +** null_high = lw & 0x7F7F7F7F // will set the high bit of each byte to 0 +** overflow = null_high + 0x7F7F7F7F // addition will overflow the high bit +** is one of the other bits was 1. +** +** oring = overflow | lw // the high bit of a byte is set +** iff any bit in the byte was set +** ones = oring | 0x7F7F7F7F // the high bits and ones everywhere else +** has_no_zero_byte = ~ones // the ones become zeros, if no high bit +** was set, there was no zero +** +** (lw - 0x01010101) & ~lw & 0x80808080 +** +** overflow = lw - 0x01010101 // overflow the high bit if one was +** 0 or > 0x80 (0b 1000 0000) 0 || >0x80 +** no_high_bit = ~lw & 0x80808080 // high bit set if the high bit was +** 0 (i.e < 0x80) 0 || <0x80 +** has_zero = overflow & no_high_bit // (0 || >0x80) && (0 || <0x80) -> 0 && 0 +** +** +** libc's strlen only filter out < 0x80 bytes by omitting the ~lw & 0x80808080 +** part because most string only contain ascii characters. +** +** sources: +** - https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord +** - https://stackoverflow.com/questions/20021066 +*/ + +#define HIMAGIC 0x8080808080808080L +#define LOMAGIC 0x0101010101010101L size_t ft_strlen(const char *s) { - unsigned long int *ptr; - const char *cpy; + uint64_t *ptr; + const char *cpy; + const char *found; - ptr = (unsigned long int*)s; + cpy = s; + while (((uint64_t)cpy & 0b111) != 0) + if (*cpy++ == '\0') + return (cpy - s - 1); + ptr = (uint64_t*)cpy; 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); - } + if (((*ptr++ - LOMAGIC) & HIMAGIC) != 0) + { + cpy = (const char*)(ptr - 1); + if ((found = ft_memchr(cpy, '\0', sizeof(uint64_t))) != NULL) + return (found - s); + } } diff --git a/src/str/ft_strmove.c b/src/str/ft_strmove.c new file mode 100644 index 0000000..1e297ea --- /dev/null +++ b/src/str/ft_strmove.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 20:46:30 by charles #+# #+# */ +/* Updated: 2020/05/12 20:49:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Same as ft_strcpy but the strings can overlap +** \param dest Where src will be copied +** \param src Source string +** \return Returns dest +*/ + +char *ft_strmove(char *dest, const char *src) +{ + return (ft_memmove(dest, src, ft_strlen(src) + 1)); +} 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_strncat.c b/src/str/ft_strncat.c index d68db0a..4686d59 100644 --- a/src/str/ft_strncat.c +++ b/src/str/ft_strncat.c @@ -14,16 +14,14 @@ char *ft_strncat(char *dest, const char *src, size_t n) { - size_t i; - size_t j; + size_t dest_len; + size_t src_len; - i = ft_strlen(dest); - j = 0; - while (j < n && src[j]) - { - dest[i + j] = src[j]; - j++; - } - dest[i + j] = '\0'; + dest_len = ft_strlen(dest); + src_len = ft_strlen(src); + if (n < src_len) + src_len = n; + ft_memcpy(dest + dest_len, src, src_len); + dest[dest_len + src_len] = '\0'; return (dest); } diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c index caa052b..3e01708 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -6,21 +6,16 @@ /* 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_types.h" +#include "libft_str.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { - size_t i; + size_t len; - if (n == 0) - return (0); - i = 0; - while (i + 1 < n && s1[i] == s2[i] && s1[i]) - i++; - return ((t_ftuchar)s1[i] - (t_ftuchar)s2[i]); + len = ft_strlen(s1); + return (ft_memcmp(s1, s2, n < len ? n : len + 1)); } diff --git a/src/str/ft_strnew.c b/src/str/ft_strnew.c index 1bca6d5..f0d2221 100644 --- a/src/str/ft_strnew.c +++ b/src/str/ft_strnew.c @@ -6,13 +6,26 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:17:34 by cacharle #+# #+# */ -/* Updated: 2019/11/20 03:16:14 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:28:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strnew(size_t size) +/* +** \brief Create a new null-terminated string +** \param len String length +** \return Allocated string or NULL is allocation failed +** \note This implementation doesn't follow the subject +** because zeroing every byte is too inefficient +*/ + +char *ft_strnew(size_t len) { - return ((char*)ft_calloc(size + 1, sizeof(char))); + char *s; + + if ((s = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + s[len] = '\0'; + return (s); } diff --git a/src/str/ft_strsdestroy.c b/src/str/ft_strsdestroy.c new file mode 100644 index 0000000..bb2204c --- /dev/null +++ b/src/str/ft_strsdestroy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ +/* Updated: 2020/05/11 15:53:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Destroy a NULL-terminated array of malloc'd string +** \param strs Strings to destroy +** \return NULL (so that it can be used in return statement) +*/ + +void *ft_strsdestroy(char **strs) +{ + int i; + + i = -1; + while (strs[++i] != NULL) + free(strs[i]); + free(strs); + return (NULL); +} 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..c1a0623 --- /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/05/11 15:54:33 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_strsdestroy(strs); + return (ret); +} diff --git a/src/str/ft_strslen.c b/src/str/ft_strslen.c new file mode 100644 index 0000000..0268033 --- /dev/null +++ b/src/str/ft_strslen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strslen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 20:58:46 by charles #+# #+# */ +/* Updated: 2020/05/11 15:49:40 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +size_t ft_strslen(char **strs) +{ + size_t count; + + count = 0; + while (strs[count] != NULL) + count++; + return (count); +} diff --git a/src/str/ft_strstr.c b/src/str/ft_strstr.c index 4d4d403..893ae1e 100644 --- a/src/str/ft_strstr.c +++ b/src/str/ft_strstr.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_str.h" char *ft_strstr(const char *haystack, const char *needle) { @@ -19,11 +20,5 @@ char *ft_strstr(const char *haystack, const char *needle) 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); + return (ft_memmem(haystack, ft_strlen(haystack), needle, needle_len)); } diff --git a/src/str/ft_substr.c b/src/str/ft_strsub.c index ad9c706..c7121bd 100644 --- a/src/str/ft_substr.c +++ b/src/str/ft_strsub.c @@ -1,25 +1,40 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 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'; if (start > ft_strlen(s)) return (sub); 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_strtof.c b/src/str/ft_strtof.c new file mode 100644 index 0000000..f147394 --- /dev/null +++ b/src/str/ft_strtof.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/09 12:09:13 by charles #+# #+# */ +/* Updated: 2020/05/09 12:23:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Extract a float from a string +** \param nptr String to extract from +** \param endptr If not NULL pointer to last character +** of the extraction is placed in *endptr +** \return Extracted float value +** \note This function doesn't try mimic the original perfectly +** (no hexadecimal, exponent, NaN, inf handling) +*/ + +float ft_strtof(const char *nptr, char **endptr) +{ + float n; + bool is_neg; + const char *tmp; + + while (ft_isspace(*nptr)) + nptr++; + is_neg = *nptr == '-'; + if (*nptr == '-' || *nptr == '+') + nptr++; + n = (float)ft_strtol(nptr, (char**)&nptr, 10); + if (*nptr == '.') + nptr++; + tmp = nptr; + n += (float)ft_strtol(nptr, (char**)&nptr, 10) / (10 * (nptr - tmp)); + if (endptr != NULL) + *endptr = (char*)nptr; + return (is_neg ? -n : n); +} 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)); } |
