This commit is contained in:
anon 2023-08-24 03:16:55 +02:00
parent ebc97b1fce
commit 0ea16eb5c8

View File

@ -1,6 +1,7 @@
#include "regex.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include "vector.h"
typedef struct { typedef struct {
int in; int in;
@ -13,23 +14,16 @@ typedef struct {
int to; int to;
} offshoot_t; } offshoot_t;
typedef struct {
char * str;
vector_t delta_table; // <delta_t>
vector_t catch_table; // <offshoot_t>
int accepting_state;
} regex_t;
#define HALT_AND_CATCH_FIRE -1 #define HALT_AND_CATCH_FIRE -1
#define HOOK_ALL(from, str, to) do { \ #define HOOK_ALL(from, str, to) do { \
for (char * s = str; *s != '\00'; s++) { \ for (char * s = str; *s != '\00'; s++) { \
vector_push(regex->delta_table \ vector_push(&regex->delta_table, \
(delta_t *){state + from, *s, state + to} \ (delta_t *){state + from, *s, state + to} \
); \ ); \
} \ } \
if (do_catch) { \ if (do_catch) { \
vector_push(regex->catch_table \ vector_push(&regex->catch_table, \
(offshoot_t *){state + from, state + to} \ (offshoot_t *){state + from, state + to} \
); \ ); \
} \ } \
@ -113,13 +107,13 @@ int escape_1_to_N(const char c, char * whitelist) {
}; };
case 'f': { case 'f': {
const char filename_chars[] = "@0123456789/.-_+,#$%~="; const char filename_chars[] = "@0123456789/.-_+,#$%~=";
strcpy(whitelist, keyword_chars); strcpy(whitelist, filename_chars);
return sizeof(keyword_chars)-1; return sizeof(filename_chars)-1;
}; };
case 'F': { case 'F': {
const char filename_chars[] = "@/.-_+,#$%~="; const char filename_chars[] = "@/.-_+,#$%~=";
strcpy(whitelist, keyword_chars); strcpy(whitelist, filename_chars);
return sizeof(keyword_chars)-1; return sizeof(filename_chars)-1;
}; };
case 'p': { case 'p': {
const char printable_chars[] = "@\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"; const char printable_chars[] = "@\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337";
@ -212,10 +206,10 @@ int compile_range(const char * const range,
} }
regex_t * regex_compile(const char * const pattern) { regex_t * regex_compile(const char * const pattern) {
regex_t * r = new regex_t; regex_t * regex = (regex_t *)malloc(sizeof(regex_t));
regex->str = strdup(pattern); regex->str = strdup(pattern);
vector_init(regex->delta_table, sizeof(delta_t), 32); vector_init(&regex->delta_table, sizeof(delta_t), 32);
vector_init(regex->catch_table, sizeof(offshoot_t), 16); vector_init(&regex->catch_table, sizeof(offshoot_t), 16);
int state = 0; int state = 0;
@ -275,23 +269,24 @@ regex_t * regex_compile(const char * const pattern) {
regex->accepting_state = state; regex->accepting_state = state;
return r; return regex;
} }
int regex_free(regex_t * const regex) { int regex_free(regex_t * const regex) {
free(regex->str); free(regex->str);
vector_free(regex->delta_table); vector_free(&regex->delta_table);
vector_free(regex->catch_table); vector_free(&regex->catch_table);
free(regex);
return 0; return 0;
} }
inline bool catch_(const regex_t * const regex, bool catch_(const regex_t * const regex,
int & state) { int * const state) {
for (int i = 0; i < regex->catch_table->element_size; i++){ for (int i = 0; i < regex->catch_table.element_size; i++){
const offshoot_t * const offshoot = (offshoot *)(vector_get(reg.catch_table, i)); const offshoot_t * const offshoot = (vector_get(&regex->catch_table, i));
if (offshoot->in == state) { if (offshoot->in == *state) {
state = offshoot->to; *state = offshoot->to;
return true; return true;
} }
} }
@ -305,8 +300,8 @@ bool regex_assert(const regex_t * const regex,
for (const char * s = string; *s != '\00'; s++) { for (const char * s = string; *s != '\00'; s++) {
// delta // delta
for (int i = 0; i < regex->delta_table.element_count; i++) { for (int i = 0; i < regex->delta_table.element_count; i++) {
const delta_t * const delta = (delta_t *)(vector_get(reg.delta_table, i); const delta_t * const delta = (delta_t *)(vector_get(&regex->delta_table, i));
if (delta->in == state) if ((delta->in == state)
&& (delta->input == *s)) { && (delta->input == *s)) {
if(regex_assert(regex, s+1, delta->to)){ if(regex_assert(regex, s+1, delta->to)){
return true; return true;
@ -314,7 +309,7 @@ bool regex_assert(const regex_t * const regex,
} }
} }
if (catch_(regex, state)) { if (catch_(regex, &state)) {
continue; continue;
} }