Browse Source

[enh] new f() to free str when asigning on same var.
- libft: new f().

Moul 3 years ago
parent
commit
e5eafe5c5c
3 changed files with 30 additions and 0 deletions
  1. 1 0
      Makefile
  2. 1 0
      libft.h
  3. 28 0
      src/ft_assign_free_str.c

+ 1 - 0
Makefile

@@ -48,6 +48,7 @@ ft_memalloc.c \
 ft_memdel.c \
 ft_strnew.c \
 ft_strdel.c \
+ft_assign_free_str.c \
 ft_strclr.c \
 ft_striter.c \
 ft_striteri.c \

+ 1 - 0
libft.h

@@ -63,6 +63,7 @@ void				*ft_memalloc(size_t size);
 void				ft_memdel(void **ap);
 char				*ft_strnew(size_t size);
 void				ft_strdel(char **as);
+char				*ft_assign_free_str(char *str, char *(*f)(const char*));
 void				ft_strclr(char *s);
 void				ft_striter(char *s, void (*f)(char *));
 void				ft_striteri(char *s, void (*f)(unsigned int, char *));

+ 28 - 0
src/ft_assign_free_str.c

@@ -0,0 +1,28 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_assign_free_str.c                               :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: mazimi <marvin@42.fr>                      +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2016/11/29 14:17:48 by mazimi            #+#    #+#             */
+/*   Updated: 2016/11/29 16:20:14 by mazimi           ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../libft.h"
+
+/*
+** pass str trought f()
+** free previous string
+** return new value
+*/
+
+char	*ft_assign_free_str(char *str, char *(*f)(const char*))
+{
+	char	*tmp;
+
+	tmp = f(str);
+	ft_strdel(&str);
+	return (tmp);
+}