Good afternoon, Here is a first cut at a singly linked list C++ class which caches precompiled PCRE regex's. We want to run in the fastest manner. Is it possible to optimize this first cut which compiles and has been briefly tested? Thank you.
class cRegexList {
private:
struct Internal{
cPCRE* Regex;
char* String;
} Item;
cRegexList *Head, *Current, *Tail;
int Count;
public:
cRegexList(void);
~cRegexList(void);
bool Add(const char *string1);
cPCRE* FindRegex(const char* Expression);
const char* GetCharacterField(void);
bool RestartIterator(void);
bool Iterate(void);
int GetCount(void);
const char* GetString(void);
void Dump(void);
};
inline bool cRegexList::RestartIterator(void) {
Current = Head;
return (Current!=0);
}
inline bool cRegexList::Iterate(void) {
if (Current==0)
return false;
if (Current && Current != Tail){
memcpy(&Current,Current,sizeof(cRegexList*));
return (Current!=0);
}
return false;
}
inline int cRegexList::GetCount(void) {
return Count;
}
// return NULL for empty list or a problem occurs
inline cPCRE* cRegexList::FindRegex(const char* Expression) {
cRegexList* Temp;
if (Current==0 || Expression == 0)
return NULL;
if (Current != 0){
RestartIterator();
Mary = Current;
for(int pos=1; Iterate() ; pos++){
if (strcmp(Current->Item.String, Expression) == 0){
return Current->Item.Regex;
}
}
}
Current = Temp;
if (Current
&&
strcmp(Current->Item.String, Expression) == 0){
return Current->Item.Regex;
}
return NULL;
}
inline const char* cRegexList::GetCharacterField(void) {
if (Current && Current->Item.String){
return Current->Item.String;
}
return NULL;
}
inline const char *cRegexList::GetString(void) {
if (Current==0)
return "";
return Current->Item.String;
}
#endif
cRegexList.cpp
#include "Portability.h"
#include "cStringListTest.h"
cRegexList::cRegexList(void) {
Head=Current=Tail=0;
Count=0;
}
cRegexList::~cRegexList(void) {
}
bool cRegexList::Add(const char *string1_){
cRegexList* newElement = (cRegexList*)new char[sizeof(cRegexList)
+
strlen(string1_)
+
1];
memset(newElement,'\x0', sizeof(cRegexList) + strlen(string1_));
newElement->Item.String = new char[strlen(string1_) + 1];
strcpy(newElement->Item.String, string1_);
newElement->Item.Regex = new cPCRE();
newElement->Item.Regex->SetOptions(PCRE_CASELESS);
newElement->Item.Regex->Compile(string1_);
if (Tail==0) {
Head=Tail=newElement;
} else {
memcpy(Tail,&newElement,sizeof(cRegexList*));
Tail=newElement;
}
Count++;
return true;
}
void cRegexList::Dump(void) {
int i=0;
if (RestartIterator()) {
do {
printf(" %d: %s\n",i++,GetString());
} while (Iterate());
}
}