diff options
| -rw-r--r-- | include/libft_str.h | 3 | ||||
| -rw-r--r-- | src/str/ft_itoa_cpy.c | 44 |
2 files changed, 46 insertions, 1 deletions
diff --git a/include/libft_str.h b/include/libft_str.h index 20545bc..816cb68 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/06/12 11:46:12 by charles ### ########.fr */ +/* Updated: 2020/09/14 15:57:44 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ char **ft_split(char const *s, char c); char **ft_splitf(char *s, char c); int ft_strcount(char *str, char c); char *ft_itoa(int n); +char *ft_itoa_cpy(char *dst, int n); int ft_atoi_strict(const char *s); long ft_strtol(const char *s, char **endptr, int base); int ft_strcasecmp(const char *s1, const char *s2); diff --git a/src/str/ft_itoa_cpy.c b/src/str/ft_itoa_cpy.c new file mode 100644 index 0000000..a66d016 --- /dev/null +++ b/src/str/ft_itoa_cpy.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_cpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */ +/* Updated: 2020/09/14 15:59:28 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief itoa but cpy number in a buffer instead of allocating memory +** \param dst Buffer where to put the string representation of the number +** \param n Number to convert +** \return Always dst +*/ + +char *ft_itoa_cpy(char *dst, int n) +{ + int len; + unsigned int u_nbr; + + len = n < 0 || n == 0 ? 1 : 0; + u_nbr = n < 0 ? -n : n; + while (u_nbr > 0) + { + u_nbr /= 10; + len++; + } + dst[len] = '\0'; + u_nbr = n < 0 ? -n : n; + if (n < 0) + dst[0] = '-'; + while (--len >= (n < 0 ? 1 : 0)) + { + dst[len] = (u_nbr % 10) | 0x30; + u_nbr /= 10; + } + return (dst); +} |
