Browse Source

readability code clean-up (#546)

pull/554/head
Francesco Carli 4 years ago
committed by GitHub
parent
commit
bd48a5c34d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      include/n2n_regex.h
  2. 329
      src/n2n_regex.c

8
include/n2n_regex.h

@ -52,21 +52,21 @@
#define _N2N_REGEX_ #define _N2N_REGEX_
#ifdef __cplusplus #ifdef __cplusplus
extern "C"{ extern "C" {
#endif #endif
#include <stdio.h> #include <stdio.h>
/* Compile regex string pattern to a regex_t-array. */ /* Compile regex string pattern to a regex_t-array. */
re_t re_compile(const char* pattern); re_t re_compile (const char* pattern);
/* Find matches of the compiled pattern inside text. */ /* Find matches of the compiled pattern inside text. */
int re_matchp(re_t pattern, const char* text, int* matchlenght); int re_matchp (re_t pattern, const char* text, int* matchlenght);
/* Find matches of the txt pattern inside text (will compile automatically first). */ /* Find matches of the txt pattern inside text (will compile automatically first). */
int re_match(const char* pattern, const char* text, int* matchlenght); int re_match (const char* pattern, const char* text, int* matchlenght);
#ifdef __cplusplus #ifdef __cplusplus

329
src/n2n_regex.c

@ -60,11 +60,9 @@
enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR_TYPE, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ }; enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR_TYPE, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ };
typedef struct regex_t typedef struct regex_t {
{
unsigned char type; /* CHAR_TYPE, STAR, etc. */ unsigned char type; /* CHAR_TYPE, STAR, etc. */
union union {
{
unsigned char ch; /* the character itself */ unsigned char ch; /* the character itself */
unsigned char* ccl; /* OR a pointer to characters in class */ unsigned char* ccl; /* OR a pointer to characters in class */
}; };
@ -73,24 +71,24 @@ typedef struct regex_t
/* Private function declarations: */ /* Private function declarations: */
static int matchpattern(regex_t* pattern, const char* text, int* matchlength); static int matchpattern (regex_t* pattern, const char* text, int* matchlength);
static int matchcharclass(char c, const char* str); static int matchcharclass (char c, const char* str);
static int matchstar(regex_t p, regex_t* pattern, const char* text, int* matchlength); static int matchstar (regex_t p, regex_t* pattern, const char* text, int* matchlength);
static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchlength); static int matchplus (regex_t p, regex_t* pattern, const char* text, int* matchlength);
static int matchone(regex_t p, char c); static int matchone (regex_t p, char c);
static int matchdigit(char c); static int matchdigit (char c);
static int matchalpha(char c); static int matchalpha (char c);
static int matchwhitespace(char c); static int matchwhitespace (char c);
static int matchmetachar(char c, const char* str); static int matchmetachar (char c, const char* str);
static int matchrange(char c, const char* str); static int matchrange (char c, const char* str);
static int matchdot(char c); static int matchdot (char c);
static int ismetachar(char c); static int ismetachar (char c);
/* Public functions: */ /* Public functions: */
int re_match(const char* pattern, const char* text, int* matchlength) int re_match (const char* pattern, const char* text, int* matchlength) {
{
re_t re_p; /* pointer to (to be created) copy of compiled regex */ re_t re_p; /* pointer to (to be created) copy of compiled regex */
int ret = -1; int ret = -1;
@ -101,39 +99,34 @@ int re_match(const char* pattern, const char* text, int* matchlength)
return(ret); return(ret);
} }
int re_matchp(re_t pattern, const char* text, int* matchlength) int re_matchp (re_t pattern, const char* text, int* matchlength) {
{
*matchlength = 0; *matchlength = 0;
if (pattern != 0)
{ if(pattern != 0) {
if (pattern[0].type == BEGIN) if(pattern[0].type == BEGIN) {
{
return ((matchpattern(&pattern[1], text, matchlength)) ? 0 : -1); return ((matchpattern(&pattern[1], text, matchlength)) ? 0 : -1);
} } else {
else
{
int idx = -1; int idx = -1;
do do {
{
idx += 1; idx += 1;
if (matchpattern(pattern, text, matchlength)) if(matchpattern(pattern, text, matchlength)) {
{ if(text[0] == '\0') {
if (text[0] == '\0')
return -1; return -1;
return idx;
} }
return idx;
} }
while (*text++ != '\0'); } while(*text++ != '\0');
} }
} }
return -1; return -1;
} }
re_t re_compile(const char* pattern) re_t re_compile (const char* pattern) {
{
/* The sizes of the two static arrays below substantiates the static RAM usage of this module. /* The sizes of the two static arrays below substantiates the static RAM usage of this module.
MAX_REGEXP_OBJECTS is the max number of symbols in the expression. MAX_REGEXP_OBJECTS is the max number of symbols in the expression.
MAX_CHAR_CLASS_LEN determines the size of buffer for chars in all char-classes in the expression. */ MAX_CHAR_CLASS_LEN determines the size of buffer for chars in all char-classes in the expression. */
@ -147,12 +140,10 @@ re_t re_compile(const char* pattern)
int i = 0; /* index into pattern */ int i = 0; /* index into pattern */
int j = 0; /* index into re_compiled */ int j = 0; /* index into re_compiled */
while (pattern[i] != '\0' && (j+1 < MAX_REGEXP_OBJECTS)) while(pattern[i] != '\0' && (j+1 < MAX_REGEXP_OBJECTS)) {
{
c = pattern[i]; c = pattern[i];
switch (c) switch(c) {
{
/* Meta-characters: */ /* Meta-characters: */
// case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches) // case '^': { re_compiled[j].type = BEGIN; } break; <-- disabled (always full matches)
// case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches) // case '$': { re_compiled[j].type = END; } break; <-- disabled (always full matches)
@ -163,15 +154,12 @@ re_t re_compile(const char* pattern)
/* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */ /* case '|': { re_compiled[j].type = BRANCH; } break; <-- not working properly */
/* Escaped character-classes (\s \w ...): */ /* Escaped character-classes (\s \w ...): */
case '\\': case '\\': {
{ if(pattern[i+1] != '\0') {
if (pattern[i+1] != '\0')
{
/* Skip the escape-char '\\' */ /* Skip the escape-char '\\' */
i += 1; i += 1;
/* ... and check the next */ /* ... and check the next */
switch (pattern[i]) switch(pattern[i]) {
{
/* Meta-character: */ /* Meta-character: */
case 'd': { re_compiled[j].type = DIGIT; } break; case 'd': { re_compiled[j].type = DIGIT; } break;
case 'D': { re_compiled[j].type = NOT_DIGIT; } break; case 'D': { re_compiled[j].type = NOT_DIGIT; } break;
@ -181,8 +169,7 @@ re_t re_compile(const char* pattern)
case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break; case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break;
/* Escaped character, e.g. '.' */ /* Escaped character, e.g. '.' */
default: default: {
{
re_compiled[j].type = CHAR_TYPE; re_compiled[j].type = CHAR_TYPE;
re_compiled[j].ch = pattern[i]; re_compiled[j].ch = pattern[i];
} break; } break;
@ -199,44 +186,35 @@ re_t re_compile(const char* pattern)
} break; } break;
/* Character class: */ /* Character class: */
case '[': case '[': {
{
/* Remember where the char-buffer starts. */ /* Remember where the char-buffer starts. */
int buf_begin = ccl_bufidx; int buf_begin = ccl_bufidx;
/* Look-ahead to determine if negated */ /* Look-ahead to determine if negated */
if (pattern[i+1] == '^') if(pattern[i+1] == '^') {
{
re_compiled[j].type = INV_CHAR_CLASS; re_compiled[j].type = INV_CHAR_CLASS;
i += 1; /* Increment i to avoid including '^' in the char-buffer */ i += 1; /* Increment i to avoid including '^' in the char-buffer */
} } else {
else
{
re_compiled[j].type = CHAR_CLASS; re_compiled[j].type = CHAR_CLASS;
} }
/* Copy characters inside [..] to buffer */ /* Copy characters inside [..] to buffer */
while ( (pattern[++i] != ']') while( (pattern[++i] != ']')
&& (pattern[i] != '\0')) /* Missing ] */ && (pattern[i] != '\0')) /* Missing ] */
{ {
if (pattern[i] == '\\') if(pattern[i] == '\\') {
{ if(ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) {
if (ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1)
{
//fputs("exceeded internal buffer!\n", stderr); //fputs("exceeded internal buffer!\n", stderr);
return 0; return 0;
} }
ccl_buf[ccl_bufidx++] = pattern[i++]; ccl_buf[ccl_bufidx++] = pattern[i++];
} } else if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
else if (ccl_bufidx >= MAX_CHAR_CLASS_LEN)
{
//fputs("exceeded internal buffer!\n", stderr); //fputs("exceeded internal buffer!\n", stderr);
return 0; return 0;
} }
ccl_buf[ccl_bufidx++] = pattern[i]; ccl_buf[ccl_bufidx++] = pattern[i];
} }
if (ccl_bufidx >= MAX_CHAR_CLASS_LEN) if(ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
{
/* Catches cases such as [00000000000000000000000000000000000000][ */ /* Catches cases such as [00000000000000000000000000000000000000][ */
//fputs("exceeded internal buffer!\n", stderr); //fputs("exceeded internal buffer!\n", stderr);
return 0; return 0;
@ -247,8 +225,7 @@ re_t re_compile(const char* pattern)
} break; } break;
/* Other characters: */ /* Other characters: */
default: default: {
{
re_compiled[j].type = CHAR_TYPE; re_compiled[j].type = CHAR_TYPE;
re_compiled[j].ch = c; re_compiled[j].ch = c;
} break; } break;
@ -261,40 +238,34 @@ re_t re_compile(const char* pattern)
re_p = (re_t)calloc(1, sizeof(re_compiled)); re_p = (re_t)calloc(1, sizeof(re_compiled));
memcpy (re_p, re_compiled, sizeof(re_compiled)); memcpy (re_p, re_compiled, sizeof(re_compiled));
return (re_t) re_p; return (re_t) re_p;
} }
void re_print(regex_t* pattern) void re_print (regex_t* pattern) {
{
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR_TYPE", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE" , "NOT_WHITESPACE", /* "BRANCH" */ };
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR_TYPE", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE" , "NOT_WHITESPACE", /* "BRANCH" */ };
int i; int i;
int j; int j;
char c; char c;
for (i = 0; i < MAX_REGEXP_OBJECTS; ++i)
{ for(i = 0; i < MAX_REGEXP_OBJECTS; ++i) {
if (pattern[i].type == UNUSED) if(pattern[i].type == UNUSED) {
{
break; break;
} }
printf("type: %s", types[pattern[i].type]); printf("type: %s", types[pattern[i].type]);
if (pattern[i].type == CHAR_CLASS || pattern[i].type == INV_CHAR_CLASS) if((pattern[i].type == CHAR_CLASS) || (pattern[i].type == INV_CHAR_CLASS)) {
{
printf(" ["); printf(" [");
for (j = 0; j < MAX_CHAR_CLASS_LEN; ++j) for(j = 0; j < MAX_CHAR_CLASS_LEN; ++j) {
{
c = pattern[i].ccl[j]; c = pattern[i].ccl[j];
if ((c == '\0') || (c == ']')) if((c == '\0') || (c == ']')) {
{
break; break;
} }
printf("%c", c); printf("%c", c);
} }
printf("]"); printf("]");
} } else if(pattern[i].type == CHAR_TYPE) {
else if (pattern[i].type == CHAR_TYPE)
{
printf(" '%c'", pattern[i].ch); printf(" '%c'", pattern[i].ch);
} }
printf("\n"); printf("\n");
@ -304,41 +275,46 @@ void re_print(regex_t* pattern)
/* Private functions: */ /* Private functions: */
static int matchdigit(char c) static int matchdigit (char c) {
{
return ((c >= '0') && (c <= '9')); return ((c >= '0') && (c <= '9'));
} }
static int matchalpha(char c)
{ static int matchalpha (char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
} }
static int matchwhitespace(char c)
{ static int matchwhitespace (char c) {
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v')); return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
} }
static int matchalphanum(char c)
{ static int matchalphanum (char c) {
return ((c == '_') || matchalpha(c) || matchdigit(c)); return ((c == '_') || matchalpha(c) || matchdigit(c));
} }
static int matchrange(char c, const char* str)
{ static int matchrange (char c, const char* str) {
return ((c != '-') && (str[0] != '\0') && (str[0] != '-') && return ((c != '-') && (str[0] != '\0') && (str[0] != '-') &&
(str[1] == '-') && (str[1] != '\0') && (str[1] == '-') && (str[1] != '\0') &&
(str[2] != '\0') && ((c >= str[0]) && (c <= str[2]))); (str[2] != '\0') && ((c >= str[0]) && (c <= str[2])));
} }
static int matchdot(char c)
{ static int matchdot (char c) {
return c != '\n' && c != '\r';
return ((c != '\n') && (c != '\r'));
} }
static int ismetachar(char c)
{ static int ismetachar (char c) {
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D')); return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D'));
} }
static int matchmetachar(char c, const char* str) static int matchmetachar (char c, const char* str) {
{
switch (str[0]) switch(str[0]) {
{
case 'd': return matchdigit(c); case 'd': return matchdigit(c);
case 'D': return !matchdigit(c); case 'D': return !matchdigit(c);
case 'w': return matchalphanum(c); case 'w': return matchalphanum(c);
@ -349,48 +325,34 @@ static int matchmetachar(char c, const char* str)
} }
} }
static int matchcharclass(char c, const char* str) static int matchcharclass (char c, const char* str) {
{
do do {
{ if(matchrange(c, str)) {
if (matchrange(c, str))
{
return 1; return 1;
} } else if(str[0] == '\\') {
else if (str[0] == '\\')
{
/* Escape-char: increment str-ptr and match on next char */ /* Escape-char: increment str-ptr and match on next char */
str += 1; str += 1;
if (matchmetachar(c, str)) if(matchmetachar(c, str)) {
{
return 1; return 1;
} } else if((c == str[0]) && !ismetachar(c)) {
else if ((c == str[0]) && !ismetachar(c))
{
return 1; return 1;
} }
} } else if(c == str[0]) {
else if (c == str[0]) if(c == '-') {
{
if (c == '-')
{
return ((str[-1] == '\0') || (str[1] == '\0')); return ((str[-1] == '\0') || (str[1] == '\0'));
} } else {
else
{
return 1; return 1;
} }
} }
} } while(*str++ != '\0');
while (*str++ != '\0');
return 0; return 0;
} }
static int matchone(regex_t p, char c) static int matchone (regex_t p, char c) {
{
switch (p.type) switch(p.type) {
{
case DOT: return matchdot(c); case DOT: return matchdot(c);
case CHAR_CLASS: return matchcharclass(c, (const char*)p.ccl); case CHAR_CLASS: return matchcharclass(c, (const char*)p.ccl);
case INV_CHAR_CLASS: return !matchcharclass(c, (const char*)p.ccl); case INV_CHAR_CLASS: return !matchcharclass(c, (const char*)p.ccl);
@ -404,58 +366,64 @@ static int matchone(regex_t p, char c)
} }
} }
static int matchstar(regex_t p, regex_t* pattern, const char* text, int* matchlength) static int matchstar (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
{
int prelen = *matchlength; int prelen = *matchlength;
const char* prepoint = text; const char* prepoint = text;
while ((text[0] != '\0') && matchone(p, *text))
{ while((text[0] != '\0') && matchone(p, *text)) {
text++; text++;
(*matchlength)++; (*matchlength)++;
} }
while (text >= prepoint)
{ while(text >= prepoint) {
if (matchpattern(pattern, text--, matchlength)) if(matchpattern(pattern, text--, matchlength)) {
return 1; return 1;
}
(*matchlength)--; (*matchlength)--;
} }
*matchlength = prelen; *matchlength = prelen;
return 0; return 0;
} }
static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchlength) static int matchplus (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
{
const char* prepoint = text; const char* prepoint = text;
while ((text[0] != '\0') && matchone(p, *text))
{ while((text[0] != '\0') && matchone(p, *text)) {
text++; text++;
(*matchlength)++; (*matchlength)++;
} }
while (text > prepoint)
{ while(text > prepoint) {
if (matchpattern(pattern, text--, matchlength)) if(matchpattern(pattern, text--, matchlength)) {
return 1; return 1;
}
(*matchlength)--; (*matchlength)--;
} }
return 0; return 0;
} }
static int matchquestion(regex_t p, regex_t* pattern, const char* text, int* matchlength) static int matchquestion (regex_t p, regex_t* pattern, const char* text, int* matchlength) {
{
if (p.type == UNUSED) if(p.type == UNUSED) {
return 1; return 1;
if (matchpattern(pattern, text, matchlength)) }
if(matchpattern(pattern, text, matchlength)) {
return 1; return 1;
if (*text && matchone(p, *text++)) }
{
if (matchpattern(pattern, text, matchlength)) if(*text && matchone(p, *text++)) {
{ if(matchpattern(pattern, text, matchlength)) {
(*matchlength)++; (*matchlength)++;
return 1; return 1;
} }
} }
return 0; return 0;
} }
@ -463,32 +431,22 @@ static int matchquestion(regex_t p, regex_t* pattern, const char* text, int* mat
#if 0 #if 0
/* Recursive matching */ /* Recursive matching */
static int matchpattern(regex_t* pattern, const char* text, int *matchlength) static int matchpattern (regex_t* pattern, const char* text, int *matchlength) {
{
int pre = *matchlength; int pre = *matchlength;
if ((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK))
{ if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
return matchquestion(pattern[1], &pattern[2], text, matchlength); return matchquestion(pattern[1], &pattern[2], text, matchlength);
} } else if(pattern[1].type == STAR) {
else if (pattern[1].type == STAR)
{
return matchstar(pattern[0], &pattern[2], text, matchlength); return matchstar(pattern[0], &pattern[2], text, matchlength);
} } else if(pattern[1].type == PLUS) {
else if (pattern[1].type == PLUS)
{
return matchplus(pattern[0], &pattern[2], text, matchlength); return matchplus(pattern[0], &pattern[2], text, matchlength);
} } else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
else if ((pattern[0].type == END) && pattern[1].type == UNUSED)
{
return text[0] == '\0'; return text[0] == '\0';
} } else if((text[0] != '\0') && matchone(pattern[0], text[0])) {
else if ((text[0] != '\0') && matchone(pattern[0], text[0]))
{
(*matchlength)++; (*matchlength)++;
return matchpattern(&pattern[1], text+1); return matchpattern(&pattern[1], text+1);
} } else {
else
{
*matchlength = pre; *matchlength = pre;
return 0; return 0;
} }
@ -497,25 +455,18 @@ static int matchpattern(regex_t* pattern, const char* text, int *matchlength)
#else #else
/* Iterative matching */ /* Iterative matching */
static int matchpattern(regex_t* pattern, const char* text, int* matchlength) static int matchpattern (regex_t* pattern, const char* text, int* matchlength) {
{
int pre = *matchlength; int pre = *matchlength;
do
{ do {
if ((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
{
return matchquestion(pattern[0], &pattern[2], text, matchlength); return matchquestion(pattern[0], &pattern[2], text, matchlength);
} } else if(pattern[1].type == STAR) {
else if (pattern[1].type == STAR)
{
return matchstar(pattern[0], &pattern[2], text, matchlength); return matchstar(pattern[0], &pattern[2], text, matchlength);
} } else if(pattern[1].type == PLUS) {
else if (pattern[1].type == PLUS)
{
return matchplus(pattern[0], &pattern[2], text, matchlength); return matchplus(pattern[0], &pattern[2], text, matchlength);
} } else if((pattern[0].type == END) && pattern[1].type == UNUSED) {
else if ((pattern[0].type == END) && pattern[1].type == UNUSED)
{
return (text[0] == '\0'); return (text[0] == '\0');
} }
/* Branching is not working properly /* Branching is not working properly
@ -525,10 +476,10 @@ static int matchpattern(regex_t* pattern, const char* text, int* matchlength)
} }
*/ */
(*matchlength)++; (*matchlength)++;
} } while((text[0] != '\0') && matchone(*pattern++, *text++));
while ((text[0] != '\0') && matchone(*pattern++, *text++));
*matchlength = pre; *matchlength = pre;
return 0; return 0;
} }

Loading…
Cancel
Save