From 38021fb98f5ea1dd7f8d59e29204736f5eac4326 Mon Sep 17 00:00:00 2001 From: gbaconni Date: Fri, 29 Apr 2022 08:46:53 +0200 Subject: [PATCH] Reuse ft_isdigit and ft_strchr from libft --- Makefile | 9 ++++-- libftprintf/Makefile | 4 ++- libftprintf/ft_ischr.c | 53 ++++++++++++++++++++++++++++++++ libftprintf/ft_vprintf_decimal.c | 6 ++-- libftprintf/ft_vprintf_string.c | 6 ++-- libftprintf/libft/ft_isdigit.c | 18 +++++++++++ libftprintf/libft/ft_strchr.c | 24 +++++++++++++++ main.c | 4 ++- 8 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 libftprintf/ft_ischr.c create mode 100644 libftprintf/libft/ft_isdigit.c create mode 100644 libftprintf/libft/ft_strchr.c diff --git a/Makefile b/Makefile index cc07f81..297d53b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/29 08:10:10 by gbaconni ### lausanne.ch # +# Updated: 2022/04/29 08:44:41 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -116,9 +116,15 @@ test: bonus ifeq ($(KERNEL),Linux) test2: leak @$(MAKE) test LEAK=1 DEBUG=$(DEBUG) FAST=$(FAST) + +test21: leak + @$(MAKE) test42 LEAK=1 DEBUG=$(DEBUG) FAST=$(FAST) else test2: debug @$(MAKE) test DEBUG=1 LEAK=$(LEAKS) FAST=$(FAST) + +test21: debug + @$(MAKE) test42 DEBUG=1 LEAK=$(LEAKS) FAST=$(FAST) endif test3: fast @@ -131,7 +137,6 @@ test42: bonus @$(PREFIX) ./$(NAME) "Preceding with blanks: %10d \n" 1977 @$(PREFIX) ./$(NAME) "Preceding with zeros: %010d \n" 1977 @$(PREFIX) ./$(NAME) "Some different radices: %d %x %#x\n" 100 100 100 - @$(PREFIX) ./$(NAME) "Width trick: %*d \n" 5 10 watch: fclean @read -p "cmd: " cmd; while :; do clear; date "+%F %T (every 2.0s)"; echo; sh -c "$${cmd} 2>&1" | tail -n 10 || true; sleep 2; done diff --git a/libftprintf/Makefile b/libftprintf/Makefile index 70b4abd..1751b6c 100644 --- a/libftprintf/Makefile +++ b/libftprintf/Makefile @@ -6,7 +6,7 @@ # By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # -# Updated: 2022/04/26 19:15:34 by gbaconni ### lausanne.ch # +# Updated: 2022/04/29 08:39:34 by gbaconni ### lausanne.ch # # # # **************************************************************************** # # @@ -29,6 +29,8 @@ SRC = \ $(LIBFT)/ft_bzero.c \ $(LIBFT)/ft_calloc.c \ $(LIBFT)/ft_strlen.c \ + $(LIBFT)/ft_strchr.c \ + $(LIBFT)/ft_isdigit.c \ $(LIBFT)/ft_itoa.c \ ft_eoflags.c \ ft_skipchars.c \ diff --git a/libftprintf/ft_ischr.c b/libftprintf/ft_ischr.c new file mode 100644 index 0000000..bd0eb1d --- /dev/null +++ b/libftprintf/ft_ischr.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:56:37 by gbaconni #+# #+# */ +/* Updated: 2022/04/26 18:41:30 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_string(const char *format, va_list ap) +{ + int ret; + char *s; + + (void) format; + ret = 0; + s = va_arg(ap, char *); + if (s == NULL) + { + ret = ft_puts("(null)"); + return (ret); + } + + const char *f; + int precision; + int len; + int flags; + precision = 0; + flags = ft_vprintf_flags_bonus(format, ap); + (void) flags; + f = format; + while (*f != '\0') + { + if (*f >= '0' && *f <= '9') + precision = precision * 10 + *f - '0'; + if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + break ; + f++; + } + if (precision > 0) + { + len = precision - ft_strlen(s); + while (len --> 0) + ret += ft_putchar(' '); + } + ret += ft_puts(s); + return (ret); +} diff --git a/libftprintf/ft_vprintf_decimal.c b/libftprintf/ft_vprintf_decimal.c index 8d3758f..a2a6dd5 100644 --- a/libftprintf/ft_vprintf_decimal.c +++ b/libftprintf/ft_vprintf_decimal.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */ -/* Updated: 2022/04/26 17:48:37 by gbaconni ### ########.fr */ +/* Updated: 2022/04/29 08:42:35 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -42,9 +42,9 @@ int ft_vprintf_decimal(const char *format, va_list ap) f = format; while (*f != '\0') { - if (*f >= '0' && *f <= '9') + if (ft_isdigit(*f)) precision = precision * 10 + *f - '0'; - if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + if (ft_strchr("cspdiuxX%", *f) != NULL) break ; f++; } diff --git a/libftprintf/ft_vprintf_string.c b/libftprintf/ft_vprintf_string.c index bd0eb1d..c0d6ee0 100644 --- a/libftprintf/ft_vprintf_string.c +++ b/libftprintf/ft_vprintf_string.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 11:56:37 by gbaconni #+# #+# */ -/* Updated: 2022/04/26 18:41:30 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/29 08:42:02 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -36,9 +36,9 @@ int ft_vprintf_string(const char *format, va_list ap) f = format; while (*f != '\0') { - if (*f >= '0' && *f <= '9') + if (ft_isdigit(*f)) precision = precision * 10 + *f - '0'; - if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%') + if (ft_strchr("cspdiuxX%", *f) != NULL) break ; f++; } diff --git a/libftprintf/libft/ft_isdigit.c b/libftprintf/libft/ft_isdigit.c new file mode 100644 index 0000000..b458d17 --- /dev/null +++ b/libftprintf/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:33:30 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 16:22:32 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libftprintf/libft/ft_strchr.c b/libftprintf/libft/ft_strchr.c new file mode 100644 index 0000000..7fd5a17 --- /dev/null +++ b/libftprintf/libft/ft_strchr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:34:33 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 21:33:09 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != (const char)c) + { + if (*s == '\0') + return (NULL); + s++; + } + return ((char *)s); +} diff --git a/main.c b/main.c index e993f1e..7cbf63b 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */ -/* Updated: 2022/04/25 07:08:49 by gbaconni ### lausanne.ch */ +/* Updated: 2022/04/29 08:23:07 by gbaconni ### lausanne.ch */ /* */ /* ************************************************************************** */ @@ -53,6 +53,8 @@ int _printf(int (*f)(const char *restrict, ...), \ char c; ret = 0; + if (arg == NULL) + return (ret); c = *(format + 1 + ft_skipchars(format + 1, "0123456789# +")); if (c == 'c') ret = (*f)(format, *arg);