From 215629a19cadf594fcaaebdbc01d9e3c607b2494 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Fri, 3 Sep 2021 15:17:18 +0200 Subject: [PATCH] C: create my own include file --- c/phone-number/makefile | 3 ++- c/phone-number/src/phone_number.c | 25 +++++++++++++------------ c/phone-number/src/phone_number.h | 26 ++------------------------ c/templates/ex.h | 8 ++++---- 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/c/phone-number/makefile b/c/phone-number/makefile index f34535a..23c3c87 100644 --- a/c/phone-number/makefile +++ b/c/phone-number/makefile @@ -11,6 +11,7 @@ CFLAGS += -Wextra CFLAGS += -pedantic CFLAGS += -Werror CFLAGS += -Wmissing-declarations +CFLAGS += -Wimplicit-fallthrough CFLAGS += -DUNITY_SUPPORT_64 ASANFLAGS = -fsanitize=address @@ -34,4 +35,4 @@ clean: tests.out: test/*.c src/*.c src/*.h @echo Compiling $@ - @$(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS) + $(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS) diff --git a/c/phone-number/src/phone_number.c b/c/phone-number/src/phone_number.c index 6551089..e71f357 100644 --- a/c/phone-number/src/phone_number.c +++ b/c/phone-number/src/phone_number.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -10,15 +9,15 @@ #define LPAREN '(' /* this version is likely not very stable, due to poor scanf() capabilities - * I made it to offer an option to traditional strtok() of manual string + * I made it to offer an option to traditional strtok() or manual string * parsing. */ char *phone_number_clean(const char *input) { char *scan="%m[+(0-9]%*[()-. ]%m[0-9]%*[()-. ]%m[0-9]%*[-. ]%m[0-9]"; char *sn[4]; - int64_t num[4]; - int64_t *p = &num[0]; + uint64_t num[4]; + uint64_t *p = &num[0]; int nmatch; char *res; @@ -30,7 +29,7 @@ char *phone_number_clean(const char *input) nmatch = sscanf(input, scan, &sn[0], &sn[1], &sn[2], &sn[3]); for (int i=0; i 10000000000) - num[0] -= 10000000000; - if (num[0] > 9999999999 || num[0] < 2000000000) + if (*p > 10000000000) /* 1 000 000 0000 */ + *p -= 10000000000; + if (*p > 9999999999 || /* 999 999 9999 */ + *p < 2000000000) /* 200 000 0000 */ return res; break; case 4: /* area */ - if (num[0] != 1) + if (*p != 1) return res; p++; - fallthrough; + fallthrough; /* only gcc>=7 & clang>=12 */ case 3: /* last 3 numbers */ - if (p[0] < 200 || p[0] > 999 || p[1] < 200 || p[1] > 999 || - p[2] < 0 || p[2] > 9999) + if (*p < 200 || *p > 999 || + *(p+1) < 200 || *(p+1) > 999 || + *(p+2) > 9999) return res; break; } diff --git a/c/phone-number/src/phone_number.h b/c/phone-number/src/phone_number.h index 9cae0f5..a24b3b6 100644 --- a/c/phone-number/src/phone_number.h +++ b/c/phone-number/src/phone_number.h @@ -1,32 +1,10 @@ #ifndef PHONE_NUMBER_H #define PHONE_NUMBER_H +#include "br-common.h" + #define NUMBER_LENGTH 10 char *phone_number_clean(const char *input); -/* from : - * https://github.com/torvalds/linux/blob/master/include/linux/compiler_attributes.h - * around line 200 - * should work with recent gcc/clang - */ -#if __has_attribute(__fallthrough__) -# define fallthrough __attribute__((__fallthrough__)) -#else -# define fallthrough do {} while (0) /* fallthrough */ -#endif - -/* See GNUmakefile below for explanation - * https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile - */ -#if defined UNIT_TEST || defined DEBUG -#include -#include -#endif - -#ifdef TESTALL -#undef TEST_IGNORE -#define TEST_IGNORE() {} -#endif - #endif diff --git a/c/templates/ex.h b/c/templates/ex.h index 7517664..fcf854e 100644 --- a/c/templates/ex.h +++ b/c/templates/ex.h @@ -2,11 +2,11 @@ * https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile */ #if defined UNIT_TEST || defined DEBUG -#include -#include +# include +# include #endif #ifdef TESTALL -#undef TEST_IGNORE -#define TEST_IGNORE() {} +# undef TEST_IGNORE +# define TEST_IGNORE() {} #endif