libhl/source/regex2.hpp
2023-08-23 18:38:12 +02:00

231 lines
4.6 KiB
C++

#include <vector>
#include <assert.h>
#include <string.h>
typedef struct {
int in;
char input;
int to;
} delta_t;
typedef struct {
int in;
int to;
} offshoot_t;
typedef struct {
char * str;
std::vector<delta_t> delta_table;
std::vector<offshoot_t> catch_table;
int accepting_state;
} regex_t;
#define HALT_AND_CATCH_FIRE -1
#define HOOK_ALL(from, str, to) do { \
for (char * s = str; *s != '\00'; s++) { \
reg.delta_table.push_back( \
delta_t{state + from, *s, state + to} \
); \
} \
if (do_catch) { \
reg.catch_table.push_back( \
{state + from, state + to} \
); \
} \
} while (0)
#define EAT(n) do { \
s += n; \
} while (0)
bool is_quantifier(const char c){
for (const char * s = "+*?"; *s != '\00'; s++) {
if (*s == c) {
return true;
}
}
return false;
}
int escape_1_to_1(const char c, char * whitelist) {
switch(c) {
case 't': {
strcpy(whitelist, "\t");
} return 1;
case 'n': {
strcpy(whitelist, "\n");
} return 1;
}
return 0;
}
int escape_1_to_N(const char c, char * whitelist) {
switch(c) {
case 'd': {
const char digitchars[] = "0123456789";
strcpy(whitelist, digitchars);
return sizeof(digitchars)-1;
};
case 'w': {
const char wordchars[] = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";
strcpy(whitelist, wordchars);
return sizeof(wordchars)-1;
};
case 's': {
const char blankchars[] = " \t\v\n";
strcpy(whitelist, blankchars);
return sizeof(blankchars)-1;
};
}
return 0;
}
int compile_range(const char * const range,
char * whitelist) {
assert(range[0] == '[' && "Not a range.");
int r = 0;
const char * s;
for (s = range+1; *s != ']'; s++) {
assert(*s != '\00' && "Unclosed range.");
char c = *s;
if (*(s+1) == '-') {
char end = *(s+2);
assert(c < end && "Endless range.");
for (char cc = c; cc < end+1; cc++) {
strncat(whitelist, &cc, 1);
strncat(whitelist, "\00", 1);
}
s += 2;
} else {
++r;
strncat(whitelist, &c, 1);
strncat(whitelist, "\00", 1);
}
}
return ((s - range) + 1);
}
regex_t * regex_compile(const char * const pattern) {
regex_t * r = new regex_t;
regex_t &reg = *r;
reg.str = strdup(pattern);
int state = 0;
char whitelist[64];
bool do_catch;
for (const char * s = pattern; *s != '\00';) {
// Get token
assert(!is_quantifier(*pattern) && "Pattern starts with quantifier.");
whitelist[0] = '\00';
do_catch = false;
switch (*s) {
case '.': {
do_catch = true;
} break;
case '\\': {
EAT(1);
if(escape_1_to_1(*s, whitelist)
|| escape_1_to_N(*s, whitelist)){
;
} else {
assert(!"Unknown escape.");
}
} break;
case '[': {
EAT(compile_range(s, whitelist)-1);
} break;
default: {
whitelist[0] = *s;
whitelist[1] = '\00';
} break;
}
EAT(1);
// Quantifier
switch (*s) {
case '?': {
HOOK_ALL(0, whitelist, +1);
EAT(1);
} break;
case '*': {
HOOK_ALL(0, whitelist, 0);
EAT(1);
} break;
case '+': {
HOOK_ALL(0, whitelist, +1);
state += 1;
HOOK_ALL(0, whitelist, 0);
EAT(1);
} break;
default: { // Literal
HOOK_ALL(0, whitelist, +1);
state += 1;
} break;
}
}
reg.accepting_state = state;
return r;
}
inline bool catch_(const regex_t * regex,
int & state) {
const regex_t &reg = *regex;
for (int i = 0; i < reg.catch_table.size(); i++){
if (reg.catch_table[i].in == state) {
state = reg.catch_table[i].to;
return true;
}
}
return false;
}
bool regex_assert(const regex_t * const regex,
const char * const string,
int state) {
const regex_t &reg = *regex;
for (const char * s = string; *s != '\00'; s++) {
// delta
for (int i = 0; i < reg.delta_table.size(); i++) {
if ((reg.delta_table[i].in == state)
&& (reg.delta_table[i].input == *s)) {
if(regex_assert(regex, s+1, reg.delta_table[i].to)){
return true;
}
}
}
if (catch_(regex, state)) {
continue;
}
return false;
}
return (state == regex->accepting_state);
}
bool regex_search( regex_t * regex,
const char * const string) {
if (regex == NULL) {
return false;
}
if (string == NULL) {
return true;
}
return regex_assert(regex, string, 0);
}