C: create my own include file
This commit is contained in:
@@ -11,6 +11,7 @@ CFLAGS += -Wextra
|
|||||||
CFLAGS += -pedantic
|
CFLAGS += -pedantic
|
||||||
CFLAGS += -Werror
|
CFLAGS += -Werror
|
||||||
CFLAGS += -Wmissing-declarations
|
CFLAGS += -Wmissing-declarations
|
||||||
|
CFLAGS += -Wimplicit-fallthrough
|
||||||
CFLAGS += -DUNITY_SUPPORT_64
|
CFLAGS += -DUNITY_SUPPORT_64
|
||||||
|
|
||||||
ASANFLAGS = -fsanitize=address
|
ASANFLAGS = -fsanitize=address
|
||||||
@@ -34,4 +35,4 @@ clean:
|
|||||||
|
|
||||||
tests.out: test/*.c src/*.c src/*.h
|
tests.out: test/*.c src/*.c src/*.h
|
||||||
@echo Compiling $@
|
@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)
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -10,15 +9,15 @@
|
|||||||
#define LPAREN '('
|
#define LPAREN '('
|
||||||
|
|
||||||
/* this version is likely not very stable, due to poor scanf() capabilities
|
/* 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.
|
* parsing.
|
||||||
*/
|
*/
|
||||||
char *phone_number_clean(const char *input)
|
char *phone_number_clean(const char *input)
|
||||||
{
|
{
|
||||||
char *scan="%m[+(0-9]%*[()-. ]%m[0-9]%*[()-. ]%m[0-9]%*[-. ]%m[0-9]";
|
char *scan="%m[+(0-9]%*[()-. ]%m[0-9]%*[()-. ]%m[0-9]%*[-. ]%m[0-9]";
|
||||||
char *sn[4];
|
char *sn[4];
|
||||||
int64_t num[4];
|
uint64_t num[4];
|
||||||
int64_t *p = &num[0];
|
uint64_t *p = &num[0];
|
||||||
int nmatch;
|
int nmatch;
|
||||||
char *res;
|
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]);
|
nmatch = sscanf(input, scan, &sn[0], &sn[1], &sn[2], &sn[3]);
|
||||||
|
|
||||||
for (int i=0; i<nmatch; ++i) {
|
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]);
|
free(sn[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,19 +38,21 @@ char *phone_number_clean(const char *input)
|
|||||||
case 0:
|
case 0:
|
||||||
return res;
|
return res;
|
||||||
case 1:
|
case 1:
|
||||||
if (num[0] > 10000000000)
|
if (*p > 10000000000) /* 1 000 000 0000 */
|
||||||
num[0] -= 10000000000;
|
*p -= 10000000000;
|
||||||
if (num[0] > 9999999999 || num[0] < 2000000000)
|
if (*p > 9999999999 || /* 999 999 9999 */
|
||||||
|
*p < 2000000000) /* 200 000 0000 */
|
||||||
return res;
|
return res;
|
||||||
break;
|
break;
|
||||||
case 4: /* area */
|
case 4: /* area */
|
||||||
if (num[0] != 1)
|
if (*p != 1)
|
||||||
return res;
|
return res;
|
||||||
p++;
|
p++;
|
||||||
fallthrough;
|
fallthrough; /* only gcc>=7 & clang>=12 */
|
||||||
case 3: /* last 3 numbers */
|
case 3: /* last 3 numbers */
|
||||||
if (p[0] < 200 || p[0] > 999 || p[1] < 200 || p[1] > 999 ||
|
if (*p < 200 || *p > 999 ||
|
||||||
p[2] < 0 || p[2] > 9999)
|
*(p+1) < 200 || *(p+1) > 999 ||
|
||||||
|
*(p+2) > 9999)
|
||||||
return res;
|
return res;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@@ -1,32 +1,10 @@
|
|||||||
#ifndef PHONE_NUMBER_H
|
#ifndef PHONE_NUMBER_H
|
||||||
#define PHONE_NUMBER_H
|
#define PHONE_NUMBER_H
|
||||||
|
|
||||||
|
#include "br-common.h"
|
||||||
|
|
||||||
#define NUMBER_LENGTH 10
|
#define NUMBER_LENGTH 10
|
||||||
|
|
||||||
char *phone_number_clean(const char *input);
|
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
|
#endif
|
||||||
|
@@ -2,11 +2,11 @@
|
|||||||
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||||
*/
|
*/
|
||||||
#if defined UNIT_TEST || defined DEBUG
|
#if defined UNIT_TEST || defined DEBUG
|
||||||
#include <stdio.h>
|
# include <stdio.h>
|
||||||
#include <stdlib.h>
|
# include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef TESTALL
|
#ifdef TESTALL
|
||||||
#undef TEST_IGNORE
|
# undef TEST_IGNORE
|
||||||
#define TEST_IGNORE() {}
|
# define TEST_IGNORE() {}
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user