diff --git a/re.c b/re.c index 20d1474..760e41a 100644 --- a/re.c +++ b/re.c @@ -35,21 +35,17 @@ /* Definitions: */ +#ifndef MAX_REGEXP_OBJECTS #define MAX_REGEXP_OBJECTS 30 /* Max number of regex symbols in expression. */ +#endif + +#ifndef MAX_CHAR_CLASS_LEN #define MAX_CHAR_CLASS_LEN 40 /* Max length of character-class buffer in. */ +#endif enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ }; -typedef struct regex_t -{ - unsigned char type; /* CHAR, STAR, etc. */ - union - { - unsigned char ch; /* the character itself */ - unsigned char* ccl; /* OR a pointer to characters in class */ - } u; -} regex_t; @@ -111,8 +107,13 @@ 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. */ - static regex_t re_compiled[MAX_REGEXP_OBJECTS]; - static unsigned char ccl_buf[MAX_CHAR_CLASS_LEN]; + static regex_t static_objects[MAX_REGEXP_OBJECTS]; + static unsigned char static_ccl_buf[MAX_CHAR_CLASS_LEN]; + return re_compile_to(pattern, static_objects, static_ccl_buf); +} + +re_t re_compile_to(const char* pattern, regex_t *re_compiled, unsigned char *ccl_buf) +{ int ccl_bufidx = 1; char c; /* current char in pattern */ diff --git a/re.h b/re.h index 69facc6..8dd5a4a 100644 --- a/re.h +++ b/re.h @@ -41,14 +41,25 @@ extern "C"{ #endif +typedef struct regex_t +{ + unsigned char type; /* CHAR, STAR, etc. */ + union + { + unsigned char ch; /* the character itself */ + unsigned char* ccl; /* OR a pointer to characters in class */ + } u; +} regex_t; /* Typedef'd pointer to get abstract datatype. */ typedef struct regex_t* re_t; -/* Compile regex string pattern to a regex_t-array. */ +/* Compile regex string pattern to a static regex_t-array. */ re_t re_compile(const char* pattern); +/* Compile regex string pattern to the given regex_t-array. */ +re_t re_compile_to(const char* pattern, re_t objects, unsigned char *ccl_buf); /* Find matches of the compiled pattern inside text. */ int re_matchp(re_t pattern, const char* text, int* matchlength);