1

下午好,这是一个单链表 C++ 类的第一个剪辑,它缓存了预编译的 PCRE 正则表达式。我们希望以最快的方式运行。是否可以优化编译并经过简要测试的第一个剪辑?谢谢你。

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

正则表达式列表.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());
    }
}
4

0 回答 0