aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-12 21:40:41 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-12 21:40:41 +0200
commitd4aeabeac0a8bd1665b14aacf487df17198c1ab9 (patch)
treeef25802fc5272bd6698aa7ea33770d66c49d9911 /src
parent966eb29634a84496e0851ef2b5a7d64f413d33ed (diff)
downloadlibft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.tar.gz
libft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.tar.bz2
libft-d4aeabeac0a8bd1665b14aacf487df17198c1ab9.zip
Fixing/refactoring ft_getline, fixed/normed ft_memchr, added ft_strmove
Diffstat (limited to 'src')
-rw-r--r--src/io/ft_getline.c77
-rw-r--r--src/mem/ft_memchr.c23
-rw-r--r--src/mem/ft_memcpy.c3
-rw-r--r--src/str/ft_strmove.c25
4 files changed, 58 insertions, 70 deletions
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));
+}