C: create my own include file

This commit is contained in:
2021-09-03 15:17:18 +02:00
parent 8ebb596c9f
commit 215629a19c
4 changed files with 21 additions and 41 deletions

View File

@@ -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)

View File

@@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
@@ -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<nmatch; ++i) {
num[i] = atol(*sn[i] == LPAREN? sn[i]+1: sn[i]);
*(p+i) = atol(*sn[i] == LPAREN? sn[i]+1: sn[i]);
free(sn[i]);
}
@@ -39,19 +38,21 @@ char *phone_number_clean(const char *input)
case 0:
return res;
case 1:
if (num[0] > 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;
}

View File

@@ -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 <stdio.h>
#include <stdlib.h>
#endif
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif

View File

@@ -2,11 +2,11 @@
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
# include <stdio.h>
# include <stdlib.h>
#endif
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
# undef TEST_IGNORE
# define TEST_IGNORE() {}
#endif