// deelx.h
//
// DEELX Regular Expression Engine (v1.2)
//
// Copyright 2006 (c) RegExLab.com
// All Rights Reserved.
//
// http://www.regexlab.com/deelx/
//
// Author: 史寿伟 (sswater shi)
// sswater@gmail.com
//
// $Revision: 1.1.2.42 $
//
#ifndef __DEELX_REGEXP__H__
#define __DEELX_REGEXP__H__
#include <memory.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
extern "C" {
typedef int (*POSIX_FUNC)(int);
int isblank(int c);
}
//
// Data Reference
//
template <class ELT> class CBufferRefT
{
public:
CBufferRefT(const ELT * pcsz, int length);
CBufferRefT(const ELT * pcsz);
public:
int nCompare (const ELT * pcsz) const;
int nCompareNoCase(const ELT * pcsz) const;
int Compare (const ELT * pcsz) const;
int CompareNoCase(const ELT * pcsz) const;
int Compare (const CBufferRefT <ELT> &) const;
int CompareNoCase(const CBufferRefT <ELT> &) const;
ELT At (int nIndex, ELT def = 0) const;
ELT operator [] (int nIndex) const;
const ELT * GetBuffer() const;
int GetSize() const;
public:
virtual ~CBufferRefT();
// Content
protected:
const ELT * m_pRef;
int m_nSize;
};
//
// Implemenation
//
template <class ELT> CBufferRefT <ELT> :: CBufferRefT(const ELT * pcsz, int length)
{
m_pRef = pcsz;
m_nSize = length;
}
template <class ELT> CBufferRefT <ELT> :: CBufferRefT(const ELT * pcsz)
{
m_pRef = pcsz;
m_nSize = 0;
if(pcsz != 0) while(m_pRef[m_nSize] != 0) m_nSize ++;
}
template <class ELT> int CBufferRefT <ELT> :: nCompare(const ELT * pcsz) const
{
for(int i=0; i<m_nSize; i++)
{
if(m_pRef[i] != pcsz[i])
return m_pRef[i] - pcsz[i];
}
return 0;
}
template <class ELT> int CBufferRefT <ELT> :: nCompareNoCase(const ELT * pcsz) const
{
for(int i=0; i<m_nSize; i++)
{
if(m_pRef[i] != pcsz[i])
{
if(toupper((int)m_pRef[i]) != toupper((int)pcsz[i]))
return m_pRef[i] - pcsz[i];
}
}
return 0;
}
template <class ELT> inline int CBufferRefT <ELT> :: Compare(const ELT * pcsz) const
{
return nCompare(pcsz) ? 1 : (int)pcsz[m_nSize];
}
template <class ELT> inline int CBufferRefT <ELT> :: CompareNoCase(const ELT * pcsz) const
{
return nCompareNoCase(pcsz) ? 1 : (int)pcsz[m_nSize];
}
template <class ELT> inline int CBufferRefT <ELT> :: Compare(const CBufferRefT <ELT> & cref) const
{
return m_nSize == cref.m_nSize ? nCompare(cref.GetBuffer()) : 1;
}
template <class ELT> inline int CBufferRefT <ELT> :: CompareNoCase(const CBufferRefT <ELT> & cref) const
{
return m_nSize == cref.m_nSize ? nCompareNoCase(cref.GetBuffer()) : 1;
}
template <class ELT> inline ELT CBufferRefT <ELT> :: At(int nIndex, ELT def) const
{
return nIndex >= m_nSize ? def : m_pRef[nIndex];
}
template <class ELT> inline ELT CBufferRefT <ELT> :: operator [] (int nIndex) const
{
return nIndex >= m_nSize ? 0 : m_pRef[nIndex];
}
template <class ELT> const ELT * CBufferRefT <ELT> :: GetBuffer() const
{
static const ELT _def[] = {0}; return m_pRef ? m_pRef : _def;
}
template <class ELT> inline int CBufferRefT <ELT> :: GetSize() const
{
return m_nSize;
}
template <class ELT> CBufferRefT <ELT> :: ~CBufferRefT()
{
}
//
// Data Buffer
//
template <class ELT> class CBufferT : public CBufferRefT <ELT>
{
public:
CBufferT(const ELT * pcsz, int length);
CBufferT(const ELT * pcsz);
CBufferT();
public:
ELT & operator [] (int nIndex);
const ELT & operator [] (int nIndex) const;
void Append(const ELT * pcsz, int length, int eol = 0);
void Append(ELT el, int eol = 0);
public:
void Push(ELT el);
int Pop (ELT & el);
int Peek(ELT & el) const;
public:
const ELT * GetBuffer() const;
ELT * GetBuffer();
ELT * Detach();
void Release();
void Prepare(int index, int fill = 0);
void Restore(int size);
public:
virtual ~CBufferT();
// Content
protected:
ELT * m_pBuffer;
int m_nMaxLength;
};
//
// Implemenation
//
template <class ELT> CBufferT <ELT> :: CBufferT(const ELT * pcsz, int length) : CBufferRefT <ELT> (0, length)
{
m_nMaxLength = CBufferRefT <ELT> :: m_nSize + 1;
CBufferRefT <ELT> :: m_pRef = m_pBuffer = (ELT *) malloc(sizeof(ELT) * m_nMaxLength);
memcpy(m_pBuffer, pcsz, sizeof(ELT) * CBufferRefT <ELT> :: m_nSize);
m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> CBufferT <ELT> :: CBufferT(const ELT * pcsz) : CBufferRefT <ELT> (pcsz)
{
m_nMaxLength = CBufferRefT <ELT> :: m_nSize + 1;
CBufferRefT <ELT> :: m_pRef = m_pBuffer = (ELT *) malloc(sizeof(ELT) * m_nMaxLength);
memcpy(m_pBuffer, pcsz, sizeof(ELT) * CBufferRefT <ELT> :: m_nSize);
m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> CBufferT <ELT> :: CBufferT() : CBufferRefT <ELT> (0, 0)
{
m_nMaxLength = 0;
m_pBuffer = 0;
}
template <class ELT> inline ELT & CBufferT <ELT> :: operator [] (int nIndex)
{
return m_pBuffer[nIndex];
}
template <class ELT> inline const ELT & CBufferT <ELT> :: operator [] (int nIndex) const
{
return m_pBuffer[nIndex];
}
template <class ELT> void CBufferT <ELT> :: Append(const ELT * pcsz, int length, int eol)
{
int nNewLength = m_nMaxLength;
// Check length
if(nNewLength < 8)
nNewLength = 8;
if(CBufferRefT <ELT> :: m_nSize + length + eol > nNewLength)
nNewLength *= 2;
if(CBufferRefT <ELT> :: m_nSize + length + eol > nNewLength)
{
nNewLength = CBufferRefT <ELT> :: m_nSize + length + eol + 11;
nNewLength -= nNewLength % 8;
}
// Realloc
if(nNewLength > m_nMaxLength)
{
CBufferRefT <ELT> :: m_pRef = m_pBuffer = (ELT *) realloc(m_pBuffer, sizeof(ELT) * nNewLength);
m_nMaxLength = nNewLength;
}
// Append
memcpy(m_pBuffer + CBufferRefT <ELT> :: m_nSize, pcsz, sizeof(ELT) * length);
CBufferRefT <ELT> :: m_nSize += length;
if(eol > 0) m_pBuffer[CBufferRefT <ELT> :: m_nSize] = 0;
}
template <class ELT> inline void CBufferT <ELT> :: Append(ELT el, int eol)
{
Append(&el, 1, eol);
}
template <class ELT> void CBufferT <ELT> :: Push(ELT el)
{
// Realloc
if(CBufferRefT <ELT> :: m_nSize >= m_nMaxLength)
{
int nNewLength = m_nMaxLength * 2;
if( nNewLength < 8 ) nNewLength = 8;
CBufferRefT <ELT> :: m_pRef = m_pBuffer = (ELT *) realloc(m_pBuffer, sizeof(ELT) * nNewLength);
m_nMaxLength = nNewLength;
}
// Append
m_pBuffer[CBufferRefT <ELT> :: m_nSize++] = el;
}
template <class ELT> inline int CBufferT <ELT> :: Pop(ELT & el)
{
if(CBufferRefT <ELT> :: m_nSize > 0)
{
el = m_pBuffer[--CBufferRefT <ELT> :: m_nSize];
return 1;
}
else
{
return 0;
}
}
template <class ELT> inline int CBufferT <ELT> :: Peek(ELT & el) const
{
if(CBufferRefT <ELT> :: m_nSize > 0)
{
el = m_pBuffer[CBufferRefT <ELT> :: m_nSize - 1];
return 1;
}
else
{
return 0;
}
}
template <class ELT> const ELT * CBufferT <ELT> :: GetBuffer() const
{
static const ELT _def[] = {0}; return m_pBuffer ? m_pBuffer : _def;
}
template <class ELT> ELT * CBufferT <ELT> :: GetBuffer()
{
static const ELT _def[] = {0}; return m_pBuffer ? m_pBuffer : (ELT *)_def;
}
template <class ELT> ELT * CBufferT <ELT> :: Detach()
{
ELT * pBuffer = m_pBuffer;
CBufferRefT <ELT> :: m_pRef = m_pBuffer = 0;
CBufferRefT <ELT> :: m_nSize = m_nMaxLength = 0;
return pBuffer;
}
template <class ELT> void CBufferT <ELT> :: Release()
{
ELT * pBuffer = Detach();
if(pBuffer != 0) free(pBuffer);
}
template <class ELT> void CBufferT <ELT> :: Prepare(int index, int fill)
{
int nNewSize = index + 1;
// Realloc
if(nNewSize > m_nMaxLength)
{
int nNewLength = m_nMaxLength;
if( nNewLength < 8 )
nNewLength = 8;
if( nNewSize > nNewLength )
nNewLength *= 2;
if( nNewSize > nNewLength )
{
nNewLength = nNewSize + 11;
nNewLength -= nNewLength % 8;
}
CBufferRefT <ELT> :: m_pRef =
评论0