substr alloue memoire en cas de nul

This commit is contained in:
Hugo LAMY
2019-11-28 16:35:13 +01:00
parent 365e8abb42
commit 07a2d393ef
3 changed files with 49 additions and 20 deletions

View File

@@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */
/* Updated: 2019/11/27 19:54:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/* /*
** create a copy of a portion of s, begining at start and of length len ** create a copy of a portion of s, begining at start and of length len
*/ */
@@ -19,14 +7,55 @@
** **
** char *ft_substr(char const *s, unsigned int start, size_t len); ** char *ft_substr(char const *s, unsigned int start, size_t len);
** **
** char *ft_strdup(const char *src)
** {
** int i;
** char *str;
**
** i = 0;
** while (src[i] != '\0')
** i++;
** if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
** return (NULL);
** while (i-- >= 0)
** str[i + 1] = src[i + 1];
** return (str);
** }
**
** int ft_strncmp(const char *s1, const char *s2, size_t n)
** {
** size_t i;
** int res;
**
** i = 0;
** res = 0;
** while (s1[i] && s1[i] == s2[i] && i < n - 1)
** i++;
** if (n != 0)
** res = (unsigned char)s1[i] - (unsigned char)s2[i];
** return (res);
** }
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** int main(int ac, char **av) ** int main(int ac, char **av)
** { ** {
** char *str; ** char *str;
** **
** if (ac != 4) ** str = "";
** return (0); ** size_t size = 0;
** str = ft_substr(av[1], atoi(av[2]), atoi(av[3])); ** char *ret = ft_substr(str, 5, size);
** printf("%s\n",str); ** if (!ft_strncmp(ret, str + 5, size))
** printf("gloups\n");
** free(ret);
** return (0); ** return (0);
** } ** }
*/ */
@@ -41,7 +70,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
if (!s) if (!s)
return (NULL); return (NULL);
if (ft_strlen(s) < start) if (ft_strlen(s) < start)
return (""); return (ft_strdup(""));
if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL); return (NULL);
i = 0; i = 0;

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:54 by hulamy #+# #+# */ /* Created: 2019/11/25 13:56:54 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:55 by hulamy ### ########.fr */ /* Updated: 2019/11/28 16:24:25 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */ /* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */
/* Updated: 2019/11/27 19:54:09 by hulamy ### ########.fr */ /* Updated: 2019/11/28 16:32:34 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -41,7 +41,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
if (!s) if (!s)
return (NULL); return (NULL);
if (ft_strlen(s) < start) if (ft_strlen(s) < start)
return (""); return (ft_strdup(""));
if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL); return (NULL);
i = 0; i = 0;