diff options
Diffstat (limited to 'src/str')
| -rw-r--r-- | src/str/ft_split.c | 14 | ||||
| -rw-r--r-- | src/str/ft_strcat.c | 1 | ||||
| -rw-r--r-- | src/str/ft_strcmp.c | 7 | ||||
| -rw-r--r-- | src/str/ft_strdup.c | 7 | ||||
| -rw-r--r-- | src/str/ft_strlen.c | 78 | ||||
| -rw-r--r-- | src/str/ft_strncat.c | 18 | ||||
| -rw-r--r-- | src/str/ft_strncmp.c | 15 | ||||
| -rw-r--r-- | src/str/ft_strstr.c | 9 | ||||
| -rw-r--r-- | src/str/ft_strsub.c | 4 |
9 files changed, 82 insertions, 71 deletions
diff --git a/src/str/ft_split.c b/src/str/ft_split.c index 6fb5964..0cb08e4 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/08 13:39:31 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_split_destroy(strs)); j += i - 1; } strs[tab_counter] = NULL; 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_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_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_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 d810e8c..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_def.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_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_strsub.c b/src/str/ft_strsub.c index 77bffb2..c7121bd 100644 --- a/src/str/ft_strsub.c +++ b/src/str/ft_strsub.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2020/04/05 13:47:55 by charles ### ########.fr */ +/* Updated: 2020/05/09 12:30:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,5 +35,7 @@ char *ft_strsub(char const *s, size_t start, size_t len) 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)); } |
