From bd48a5c34dd440bfbd1a6ec55f4b3ae32a6ba9a5 Mon Sep 17 00:00:00 2001 From: Francesco Carli <62562180+fcarli3@users.noreply.github.com> Date: Sat, 19 Dec 2020 12:28:52 +0100 Subject: [PATCH] readability code clean-up (#546) --- include/n2n_regex.h | 8 +- src/n2n_regex.c | 337 +++++++++++++++++++------------------------- 2 files changed, 148 insertions(+), 197 deletions(-) diff --git a/include/n2n_regex.h b/include/n2n_regex.h index e7597d1..a66b6c8 100644 --- a/include/n2n_regex.h +++ b/include/n2n_regex.h @@ -52,21 +52,21 @@ #define _N2N_REGEX_ #ifdef __cplusplus -extern "C"{ +extern "C" { #endif #include /* 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. */ -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). */ -int re_match(const char* pattern, const char* text, int* matchlenght); +int re_match (const char* pattern, const char* text, int* matchlenght); #ifdef __cplusplus diff --git a/src/n2n_regex.c b/src/n2n_regex.c index 916f6a9..be6b6bf 100644 --- a/src/n2n_regex.c +++ b/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 */ }; -typedef struct regex_t -{ - unsigned char type; /* CHAR_TYPE, STAR, etc. */ - union - { +typedef struct regex_t { + unsigned char type; /* CHAR_TYPE, STAR, etc. */ + union { unsigned char ch; /* the character itself */ unsigned char* ccl; /* OR a pointer to characters in class */ }; @@ -73,24 +71,24 @@ typedef struct regex_t /* Private function declarations: */ -static int matchpattern(regex_t* pattern, const char* text, int* matchlength); -static int matchcharclass(char c, const char* str); -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 matchone(regex_t p, char c); -static int matchdigit(char c); -static int matchalpha(char c); -static int matchwhitespace(char c); -static int matchmetachar(char c, const char* str); -static int matchrange(char c, const char* str); -static int matchdot(char c); -static int ismetachar(char c); +static int matchpattern (regex_t* pattern, const char* text, int* matchlength); +static int matchcharclass (char c, const char* str); +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 matchone (regex_t p, char c); +static int matchdigit (char c); +static int matchalpha (char c); +static int matchwhitespace (char c); +static int matchmetachar (char c, const char* str); +static int matchrange (char c, const char* str); +static int matchdot (char c); +static int ismetachar (char c); /* 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 */ int ret = -1; @@ -101,39 +99,34 @@ int re_match(const char* pattern, const char* text, int* matchlength) 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; - if (pattern != 0) - { - if (pattern[0].type == BEGIN) - { + + if(pattern != 0) { + if(pattern[0].type == BEGIN) { return ((matchpattern(&pattern[1], text, matchlength)) ? 0 : -1); - } - else - { + } else { int idx = -1; - do - { + do { idx += 1; - if (matchpattern(pattern, text, matchlength)) - { - if (text[0] == '\0') + if(matchpattern(pattern, text, matchlength)) { + if(text[0] == '\0') { return -1; - + } return idx; } - } - while (*text++ != '\0'); + } while(*text++ != '\0'); } } + 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. 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. */ @@ -147,12 +140,10 @@ re_t re_compile(const char* pattern) int i = 0; /* index into pattern */ 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]; - switch (c) - { + switch(c) { /* Meta-characters: */ // case '^': { re_compiled[j].type = BEGIN; } 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 */ /* Escaped character-classes (\s \w ...): */ - case '\\': - { - if (pattern[i+1] != '\0') - { + case '\\': { + if(pattern[i+1] != '\0') { /* Skip the escape-char '\\' */ i += 1; /* ... and check the next */ - switch (pattern[i]) - { + switch(pattern[i]) { /* Meta-character: */ case 'd': { re_compiled[j].type = 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; /* Escaped character, e.g. '.' */ - default: - { + default: { re_compiled[j].type = CHAR_TYPE; re_compiled[j].ch = pattern[i]; } break; @@ -199,44 +186,35 @@ re_t re_compile(const char* pattern) } break; /* Character class: */ - case '[': - { + case '[': { /* Remember where the char-buffer starts. */ int buf_begin = ccl_bufidx; /* Look-ahead to determine if negated */ - if (pattern[i+1] == '^') - { + if(pattern[i+1] == '^') { re_compiled[j].type = INV_CHAR_CLASS; i += 1; /* Increment i to avoid including '^' in the char-buffer */ - } - else - { + } else { re_compiled[j].type = CHAR_CLASS; } /* Copy characters inside [..] to buffer */ - while ( (pattern[++i] != ']') + while( (pattern[++i] != ']') && (pattern[i] != '\0')) /* Missing ] */ { - if (pattern[i] == '\\') - { - if (ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) - { + if(pattern[i] == '\\') { + if(ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) { //fputs("exceeded internal buffer!\n", stderr); return 0; } 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); return 0; } 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][ */ //fputs("exceeded internal buffer!\n", stderr); return 0; @@ -247,8 +225,7 @@ re_t re_compile(const char* pattern) } break; /* Other characters: */ - default: - { + default: { re_compiled[j].type = CHAR_TYPE; re_compiled[j].ch = c; } break; @@ -261,40 +238,34 @@ re_t re_compile(const char* pattern) re_p = (re_t)calloc(1, sizeof(re_compiled)); memcpy (re_p, re_compiled, sizeof(re_compiled)); + return (re_t) re_p; } -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" */ }; +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" */ }; int i; int j; char c; - for (i = 0; i < MAX_REGEXP_OBJECTS; ++i) - { - if (pattern[i].type == UNUSED) - { + + for(i = 0; i < MAX_REGEXP_OBJECTS; ++i) { + if(pattern[i].type == UNUSED) { break; } 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(" ["); - for (j = 0; j < MAX_CHAR_CLASS_LEN; ++j) - { + for(j = 0; j < MAX_CHAR_CLASS_LEN; ++j) { c = pattern[i].ccl[j]; - if ((c == '\0') || (c == ']')) - { + if((c == '\0') || (c == ']')) { break; } printf("%c", c); } printf("]"); - } - else if (pattern[i].type == CHAR_TYPE) - { + } else if(pattern[i].type == CHAR_TYPE) { printf(" '%c'", pattern[i].ch); } printf("\n"); @@ -304,41 +275,46 @@ void re_print(regex_t* pattern) /* Private functions: */ -static int matchdigit(char c) -{ +static int matchdigit (char c) { + return ((c >= '0') && (c <= '9')); } -static int matchalpha(char c) -{ + +static int matchalpha (char c) { + 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')); } -static int matchalphanum(char c) -{ + +static int matchalphanum (char 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] != '-') && - (str[1] == '-') && (str[1] != '\0') && - (str[2] != '\0') && ((c >= str[0]) && (c <= str[2]))); + (str[1] == '-') && (str[1] != '\0') && + (str[2] != '\0') && ((c >= str[0]) && (c <= str[2]))); } -static int matchdot(char c) -{ - return c != '\n' && c != '\r'; + +static int matchdot (char c) { + + 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')); } -static int matchmetachar(char c, const char* str) -{ - switch (str[0]) - { +static int matchmetachar (char c, const char* str) { + + switch(str[0]) { case 'd': return matchdigit(c); case 'D': return !matchdigit(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) -{ - do - { - if (matchrange(c, str)) - { +static int matchcharclass (char c, const char* str) { + + do { + if(matchrange(c, str)) { return 1; - } - else if (str[0] == '\\') - { + } else if(str[0] == '\\') { /* Escape-char: increment str-ptr and match on next char */ str += 1; - if (matchmetachar(c, str)) - { + if(matchmetachar(c, str)) { return 1; - } - else if ((c == str[0]) && !ismetachar(c)) - { + } else if((c == str[0]) && !ismetachar(c)) { return 1; } - } - else if (c == str[0]) - { - if (c == '-') - { + } else if(c == str[0]) { + if(c == '-') { return ((str[-1] == '\0') || (str[1] == '\0')); - } - else - { + } else { return 1; } } - } - while (*str++ != '\0'); + } while(*str++ != '\0'); return 0; } -static int matchone(regex_t p, char c) -{ - switch (p.type) - { +static int matchone (regex_t p, char c) { + + switch(p.type) { case DOT: return matchdot(c); case 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; const char* prepoint = text; - while ((text[0] != '\0') && matchone(p, *text)) - { + + while((text[0] != '\0') && matchone(p, *text)) { text++; (*matchlength)++; } - while (text >= prepoint) - { - if (matchpattern(pattern, text--, matchlength)) + + while(text >= prepoint) { + if(matchpattern(pattern, text--, matchlength)) { return 1; + } (*matchlength)--; } *matchlength = prelen; + 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; - while ((text[0] != '\0') && matchone(p, *text)) - { + + while((text[0] != '\0') && matchone(p, *text)) { text++; (*matchlength)++; } - while (text > prepoint) - { - if (matchpattern(pattern, text--, matchlength)) + + while(text > prepoint) { + if(matchpattern(pattern, text--, matchlength)) { return 1; + } (*matchlength)--; } return 0; } -static int matchquestion(regex_t p, regex_t* pattern, const char* text, int* matchlength) -{ - if (p.type == UNUSED) +static int matchquestion (regex_t p, regex_t* pattern, const char* text, int* matchlength) { + + if(p.type == UNUSED) { return 1; - if (matchpattern(pattern, text, matchlength)) - return 1; - if (*text && matchone(p, *text++)) - { - if (matchpattern(pattern, text, matchlength)) - { + } + + if(matchpattern(pattern, text, matchlength)) { + return 1; + } + + if(*text && matchone(p, *text++)) { + if(matchpattern(pattern, text, matchlength)) { (*matchlength)++; return 1; } } + return 0; } @@ -463,32 +431,22 @@ static int matchquestion(regex_t p, regex_t* pattern, const char* text, int* mat #if 0 /* 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; - 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); - } - else if (pattern[1].type == STAR) - { + } else if(pattern[1].type == STAR) { 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); - } - else if ((pattern[0].type == END) && pattern[1].type == UNUSED) - { + } else if((pattern[0].type == END) && pattern[1].type == UNUSED) { 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)++; return matchpattern(&pattern[1], text+1); - } - else - { + } else { *matchlength = pre; return 0; } @@ -497,25 +455,18 @@ static int matchpattern(regex_t* pattern, const char* text, int *matchlength) #else /* 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; - do - { - if ((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) - { + + do { + if((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) { 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); - } - else if (pattern[1].type == PLUS) - { + } else if(pattern[1].type == PLUS) { 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'); } /* Branching is not working properly @@ -525,10 +476,10 @@ static int matchpattern(regex_t* pattern, const char* text, int* matchlength) } */ (*matchlength)++; - } - while ((text[0] != '\0') && matchone(*pattern++, *text++)); + } while((text[0] != '\0') && matchone(*pattern++, *text++)); *matchlength = pre; + return 0; }