|
|
@@ -7,17 +7,7 @@ |
|
|
|
#include <assert.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
int in; |
|
|
|
char input; |
|
|
|
int to; |
|
|
|
} delta_t; |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
int in; |
|
|
|
int to; |
|
|
|
} offshoot_t; |
|
|
|
|
|
|
|
// ### Char tests ### |
|
|
|
static bool is_quantifier(const char c) { |
|
|
|
for (const char * s = "+*?"; *s != '\00'; s++) { |
|
|
|
if (*s == c) { |
|
|
@@ -40,6 +30,22 @@ bool is_magic(const char c) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ### Internal Types ### |
|
|
|
typedef struct { |
|
|
|
int in; |
|
|
|
char input; |
|
|
|
int to; |
|
|
|
} delta_t; |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
int in; |
|
|
|
int to; |
|
|
|
} offshoot_t; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ### Regex creation/destruction ### |
|
|
|
static int escape_1_to_1(const char c, char * whitelist) { |
|
|
|
switch(c) { |
|
|
|
case 't': { |
|
|
@@ -209,6 +215,19 @@ static int compile_range(const char * const range, |
|
|
|
return ((s - range) + 1); |
|
|
|
} |
|
|
|
|
|
|
|
static bool catch_(const regex_t * const regex, |
|
|
|
int * const state) { |
|
|
|
|
|
|
|
for (int i = 0; i < regex->catch_table.element_size; i++){ |
|
|
|
const offshoot_t * const offshoot = (vector_get(®ex->catch_table, i)); |
|
|
|
if (offshoot->in == *state) { |
|
|
|
*state = offshoot->to; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
#define HALT_AND_CATCH_FIRE -1 |
|
|
|
|
|
|
|
#define HOOK_ALL(from, str, to) do { \ |
|
|
@@ -270,7 +289,7 @@ regex_t * regex_compile(const char * const pattern) { |
|
|
|
|
|
|
|
EAT(1); |
|
|
|
|
|
|
|
// Quantifier |
|
|
|
// Get quantifier |
|
|
|
switch (*s) { |
|
|
|
case '?': { |
|
|
|
HOOK_ALL(0, whitelist, +1); |
|
|
@@ -306,22 +325,13 @@ int regex_free(regex_t * const regex) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
bool catch_(const regex_t * const regex, |
|
|
|
int * const state) { |
|
|
|
|
|
|
|
for (int i = 0; i < regex->catch_table.element_size; i++){ |
|
|
|
const offshoot_t * const offshoot = (vector_get(®ex->catch_table, i)); |
|
|
|
if (offshoot->in == *state) { |
|
|
|
*state = offshoot->to; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
bool regex_assert(const regex_t * const regex, |
|
|
|
const char * const string, |
|
|
|
int state) { |
|
|
|
// ### Searching ### |
|
|
|
|
|
|
|
static bool regex_assert(const regex_t * const regex, |
|
|
|
const char * const string, |
|
|
|
int state) { |
|
|
|
|
|
|
|
for (const char * s = string; *s != '\00'; s++) { |
|
|
|
// delta |
|
|
|