From 44cafa48111fb581dcf5556f4cea464d7efc3650 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 9 Dec 2016 19:20:56 +0100 Subject: [PATCH] printf reallocation for 0x and 0b prefixes --- libftasm/src/ft_printf/ft_conversion.c | 2 +- libftasm/src/ft_printf/ft_printf.c | 2 +- libftasm/src/ft_printf/ft_printf_color.c | 2 +- libftasm/src/ft_printf/ft_printf_parse.c | 2 +- libftasm/src/ft_printf/ft_transform.c | 4 +++- libftasm/src/ft_printf/lib_pad.c | 2 +- libftasm/src/ft_printf/lib_pad_sharp.c | 25 +++++++++++++----------- libftasm/src/mem/ft_realloc.c | 2 +- libftasm/src/sstr/ft_sstrdup.c | 4 +--- libftasm/src/str/ft_strcat.c | 21 +++++++++----------- libftasm/src/str/ft_strcatf.c | 2 +- libftasm/src/str/ft_strjoin.c | 5 +++-- 12 files changed, 37 insertions(+), 36 deletions(-) diff --git a/libftasm/src/ft_printf/ft_conversion.c b/libftasm/src/ft_printf/ft_conversion.c index f8844a94..95e7e357 100644 --- a/libftasm/src/ft_printf/ft_conversion.c +++ b/libftasm/src/ft_printf/ft_conversion.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:31:48 by jhalford #+# #+# */ -/* Updated: 2016/11/16 18:30:20 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 18:00:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/ft_printf/ft_printf.c b/libftasm/src/ft_printf/ft_printf.c index 4247f327..2c49114a 100644 --- a/libftasm/src/ft_printf/ft_printf.c +++ b/libftasm/src/ft_printf/ft_printf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */ -/* Updated: 2016/12/07 17:14:27 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:14:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/ft_printf/ft_printf_color.c b/libftasm/src/ft_printf/ft_printf_color.c index b4cace7a..00cfb6ec 100644 --- a/libftasm/src/ft_printf/ft_printf_color.c +++ b/libftasm/src/ft_printf/ft_printf_color.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:07:39 by jhalford #+# #+# */ -/* Updated: 2016/12/07 13:48:34 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 18:03:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/ft_printf/ft_printf_parse.c b/libftasm/src/ft_printf/ft_printf_parse.c index fe3d2674..62de8046 100644 --- a/libftasm/src/ft_printf/ft_printf_parse.c +++ b/libftasm/src/ft_printf/ft_printf_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:24 by jhalford #+# #+# */ -/* Updated: 2016/11/10 12:59:50 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 15:48:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/ft_printf/ft_transform.c b/libftasm/src/ft_printf/ft_transform.c index edadbed2..8aea5e1e 100644 --- a/libftasm/src/ft_printf/ft_transform.c +++ b/libftasm/src/ft_printf/ft_transform.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:32 by jhalford #+# #+# */ -/* Updated: 2016/11/16 17:56:37 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:13:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,8 @@ char *ft_transform(t_fmt *fmt, va_list ap) ret = (*fmt->conv.converter)(fmt, ap); if (fmt->width > (int)ft_strlen(ret)) ret = ft_realloc(ret, fmt->width + 5); + else + ret = ft_realloc(ret, ft_strlen(ret) + 3); if (ft_strchr(fmt->flags, '-')) ft_pad_right(ret, fmt); else diff --git a/libftasm/src/ft_printf/lib_pad.c b/libftasm/src/ft_printf/lib_pad.c index 20919955..decdb9ac 100644 --- a/libftasm/src/ft_printf/lib_pad.c +++ b/libftasm/src/ft_printf/lib_pad.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:45 by jhalford #+# #+# */ -/* Updated: 2016/11/16 18:13:08 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:14:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/ft_printf/lib_pad_sharp.c b/libftasm/src/ft_printf/lib_pad_sharp.c index bca9add2..526fcd1e 100644 --- a/libftasm/src/ft_printf/lib_pad_sharp.c +++ b/libftasm/src/ft_printf/lib_pad_sharp.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:48 by jhalford #+# #+# */ -/* Updated: 2016/11/16 17:56:42 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:07:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,17 +24,20 @@ void ft_pad_sharp_o(char *str, t_fmt *fmt) void ft_pad_sharp_xb(char *str, t_fmt *fmt) { - char buf[100]; + char start[3] = {'0',fmt->conversion, 0}; int i; i = 0; - ft_bzero(buf, 100); - ft_strcpy(buf, "0"); - ft_strcat(buf, &fmt->conversion); - if (*str == '0') - i++; - if (*str == '0') - i++; - ft_strcat(buf, str + i); - ft_strcpy(str, buf); + if (str[0] == '0') + { + if (str[1] == '0') + str[1] = fmt->conversion; + else + { + str[0] = fmt->conversion; + ft_strcatf(str, "0"); + } + } + else + ft_strcatf(str, start); } diff --git a/libftasm/src/mem/ft_realloc.c b/libftasm/src/mem/ft_realloc.c index 1f57254d..5297a2b6 100644 --- a/libftasm/src/mem/ft_realloc.c +++ b/libftasm/src/mem/ft_realloc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:37:53 by jhalford #+# #+# */ -/* Updated: 2016/11/16 17:56:17 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 18:31:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/sstr/ft_sstrdup.c b/libftasm/src/sstr/ft_sstrdup.c index 4704b22d..ac34dae4 100644 --- a/libftasm/src/sstr/ft_sstrdup.c +++ b/libftasm/src/sstr/ft_sstrdup.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 14:25:45 by jhalford #+# #+# */ -/* Updated: 2016/12/07 16:29:08 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:14:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ char **ft_sstrdup(char **list) i = 0; size = 0; - DG("sstrdup call now"); while (list[size]) size++; cpy = (char **)malloc(sizeof(char *) * (size + 1)); @@ -29,7 +28,6 @@ char **ft_sstrdup(char **list) cpy[i++] = ft_strdup(*list); list++; } - DG("sstrdup i=%i, size=%i (should be equal)", i, size); cpy[i] = NULL; return (cpy); } diff --git a/libftasm/src/str/ft_strcat.c b/libftasm/src/str/ft_strcat.c index f376d850..d0d028ef 100644 --- a/libftasm/src/str/ft_strcat.c +++ b/libftasm/src/str/ft_strcat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/08/07 10:56:53 by jhalford #+# #+# */ -/* Updated: 2016/11/10 12:18:00 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:11:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,13 @@ char *ft_strcat(char *s1, const char *s2) { - size_t size; - size_t j; + char *start; - size = ft_strlen(s1); - j = 0; - while (s2 && s2[j]) - { - s1[size + j] = s2[j]; - j++; - } - s1[size + j] = '\0'; - return (s1); + start = s1; + s1 += ft_strlen(s1); + if (s2) + while (*s2) + *s1++ = *s2++; + *s1 = 0; + return (start); } diff --git a/libftasm/src/str/ft_strcatf.c b/libftasm/src/str/ft_strcatf.c index c4520d64..0656d6b0 100644 --- a/libftasm/src/str/ft_strcatf.c +++ b/libftasm/src/str/ft_strcatf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 15:46:03 by jhalford #+# #+# */ -/* Updated: 2016/11/16 17:58:02 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:02:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/str/ft_strjoin.c b/libftasm/src/str/ft_strjoin.c index 1bd59d22..17e18d35 100644 --- a/libftasm/src/str/ft_strjoin.c +++ b/libftasm/src/str/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:18 by jhalford #+# #+# */ -/* Updated: 2016/12/05 14:24:31 by jhalford ### ########.fr */ +/* Updated: 2016/12/09 19:11:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,8 @@ char *ft_strjoin(char const *s1, char const *s2) { char *join; - join = ft_strnew(ft_strlen(s1) + ft_strlen(s2) + 1); + if (!(join = ft_strnew(ft_strlen(s1) + ft_strlen(s2) + 1))) + return (NULL); if (s1) ft_strcpy(join, s1); if (s2)