diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-12 21:40:41 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-12 21:40:41 +0200 |
| commit | d4aeabeac0a8bd1665b14aacf487df17198c1ab9 (patch) | |
| tree | ef25802fc5272bd6698aa7ea33770d66c49d9911 | |
| parent | 966eb29634a84496e0851ef2b5a7d64f413d33ed (diff) | |
| download | libft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.tar.gz libft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.tar.bz2 libft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.zip | |
Fixing/refactoring ft_getline, fixed/normed ft_memchr, added ft_strmove
| -rw-r--r-- | include/libft_io.h | 10 | ||||
| -rw-r--r-- | include/libft_str.h | 3 | ||||
| -rw-r--r-- | src/io/ft_getline.c | 77 | ||||
| -rw-r--r-- | src/mem/ft_memchr.c | 23 | ||||
| -rw-r--r-- | src/mem/ft_memcpy.c | 3 | ||||
| -rw-r--r-- | src/str/ft_strmove.c | 25 | ||||
| -rw-r--r-- | test/src/mem/test_ft_memchr.c | 5 |
7 files changed, 72 insertions, 74 deletions
diff --git a/include/libft_io.h b/include/libft_io.h index faeade4..ab40875 100644 --- a/include/libft_io.h +++ b/include/libft_io.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:35:43 by cacharle #+# #+# */ -/* Updated: 2020/05/12 18:00:55 by charles ### ########.fr */ +/* Updated: 2020/05/12 20:36:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,9 @@ char ft_getchar(void); # ifndef FT_GETFILE_BUFFER_SIZE # define FT_GETFILE_BUFFER_SIZE 64 # endif +# if FT_GETFILE_BUFFER_SIZE <= 0 +# error "FT_GETFILE_BUFFER_SIZE must be > 0" +# endif typedef struct s_ftmem { @@ -43,7 +46,10 @@ typedef struct s_ftmem int ft_getfile(int fd, t_ftmem *mem); # ifndef FT_GETLINE_BUFFER_SIZE -# define FT_GETLINE_BUFFER_SIZE 1 +# define FT_GETLINE_BUFFER_SIZE 64 +# endif +# if FT_GETLINE_BUFFER_SIZE <= 0 +# error "FT_GETLINE_BUFFER_SIZE must be > 0" # endif # define FT_LINE 1 diff --git a/include/libft_str.h b/include/libft_str.h index 383c393..41e81ac 100644 --- a/include/libft_str.h +++ b/include/libft_str.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:22 by cacharle #+# #+# */ -/* Updated: 2020/05/11 15:52:40 by charles ### ########.fr */ +/* Updated: 2020/05/12 20:47:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,6 +79,7 @@ int ft_strnequ(char const *s1, char const *s2, size_t n); char *ft_strtolower(char *s); char *ft_strtoupper(char *s); char *ft_strcat3(char *dest, const char *src1, const char *src2); +char *ft_strmove(char *dest, const char *src); /* ** glob diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c index d59aa63..067e66c 100644 --- a/src/io/ft_getline.c +++ b/src/io/ft_getline.c @@ -6,63 +6,34 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/05/12 17:56:59 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:32:50 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int st_find_newline(char *str) -{ - int i; - - i = -1; - while (str[++i]) - if (str[i] == '\n') - return (i); - return (-1); -} - -static int st_free_return(char **ptr, char **ptr2, int ret) -{ - if (ptr != NULL) - { - free(*ptr); - *ptr = NULL; - } - if (ptr2 != NULL) - { - free(*ptr2); - *ptr2 = NULL; - } - return (ret); -} - static int st_read_line(int fd, char **line, char *rest) { - int ret; - int split_at; - char *buf; + int ret; + char *cut; + static char buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'}; - if ((buf = malloc(sizeof(char) * (FT_GETLINE_BUFFER_SIZE + 1))) == NULL) - return (st_free_return(line, NULL, FT_ERROR)); while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - if ((split_at = st_find_newline(buf)) != -1) + if ((cut = ft_strchr(buf, '\n')) != NULL) { - ft_strcpy(rest, buf + split_at + 1); - buf[split_at] = '\0'; - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); - return (st_free_return(&buf, NULL, FT_LINE)); + ft_strcpy(rest, cut + 1); + *cut = '\0'; } if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); + return (FT_ERROR); + if (cut != NULL) + return (FT_LINE); } if (ret == -1) - return (st_free_return(&buf, line, FT_ERROR)); - return (st_free_return(&buf, NULL, ret)); + free(line); + return (ret); } /* @@ -85,29 +56,27 @@ static int st_read_line(int fd, char **line, char *rest) int ft_getline(int fd, char **line) { - int split_at; - static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; + char *cut; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{'\0'}}; if (fd < 0 || fd > OPEN_MAX || line == NULL) return (FT_ERROR); - if ((*line = ft_strdup("")) == NULL) - return (FT_ERROR); if (rest[fd][0] == '\0') + { + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); return (st_read_line(fd, line, rest[fd])); - if ((split_at = st_find_newline(rest[fd])) != -1) + } + if ((cut = ft_strchr(rest[fd], '\n')) != NULL) { - free(*line); - if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + *cut = '\0'; + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strncpy(*line, rest[fd], split_at); - (*line)[split_at] = '\0'; - ft_strcpy(rest[fd], rest[fd] + split_at + 1); + ft_strmove(rest[fd], cut + 1); return (FT_LINE); } - free(*line); - if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strcpy(*line, rest[fd]); rest[fd][0] = '\0'; return (st_read_line(fd, line, rest[fd])); } diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c index e0266af..848b436 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2020/04/01 21:50:49 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:39:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,16 +27,15 @@ void *ft_memchr(const void *s, int c, size_t n) uint64_t lw; c = (uint8_t)c; - while (((uint64_t)s & 0b111) != 0) + while (((uint64_t)s & 0b111) != 0 && n > 0) { - n--; if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); + return ((void*)s); + n--; s++; } - buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16 - | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40 - | (uint64_t)c << 48 | (uint64_t)c << 56; + buf = c | c << 8 | c << 16 | c << 24; + buf |= buf << 32; while (n >= 8) { lw = *(uint64_t*)s ^ buf; @@ -45,12 +44,8 @@ void *ft_memchr(const void *s, int c, size_t n) n -= 8; s += 8; } - while (n > 0) - { - if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); - n--; - s++; - } + while (n-- > 0) + if (*(uint8_t*)s++ == (uint8_t)c) + return ((void*)(s - 1)); return (NULL); } diff --git a/src/mem/ft_memcpy.c b/src/mem/ft_memcpy.c index 1f84bfd..699111f 100644 --- a/src/mem/ft_memcpy.c +++ b/src/mem/ft_memcpy.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:00:07 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:39:04 by cacharle ### ########.fr */ +/* Updated: 2020/05/12 20:49:35 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_mem.h" void *ft_memcpy(void *dest, const void *src, size_t n) 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/test/src/mem/test_ft_memchr.c b/test/src/mem/test_ft_memchr.c index 135edfb..6473e20 100644 --- a/test/src/mem/test_ft_memchr.c +++ b/test/src/mem/test_ft_memchr.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/13 03:33:14 by cacharle #+# #+# */ -/* Updated: 2020/02/13 19:38:17 by cacharle ### ########.fr */ +/* Updated: 2020/05/12 21:14:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,4 +32,7 @@ TEST(ft_memchr, basic) ptr = ft_memchr(a, '\0', sizeof(a)); TEST_ASSERT_EQUAL_PTR(a + 7, ptr); + + ptr = ft_memchr("v 1", '\n', 4); + TEST_ASSERT_NULL(ptr); } |
