Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WTL

Henry Spencer's Regexp Engine Revisited

Rate me:
Please Sign up or sign in to vote.
4.88/5 (19 votes)
2 Jul 200317 min read 176.5K   3.4K   67  
A small, Unicode-aware regular expression engine based on Henry Spencer's early work
#include "stdafx.h"

#include <vector>
// include the C regexp code
#ifdef _UNICODE

#define REGEXP_UNICODE
#define re_comp_t re_comp_w
#define re_exec_t re_exec_w

#else

#undef REGEXP_UNICODE
#define re_comp_t re_comp
#define re_exec_t re_exec

#endif
#include "regexp.h"

#include "CRegExp.h"


CRegExpException::CRegExpException(int nError) :
    m_nError(nError)
{}

int CRegExpException::GetError() const
{
    return m_nError;
}

CString CRegExpException::GetErrorString() const
{
    char arTemp[128];
    re_error(m_nError, NULL, arTemp, sizeof(arTemp));
    CString retval(arTemp);
    return retval;
}

CRegExp::CRegExp(LPCTSTR pszPattern) :
    m_preCompiled(NULL)
{
    int nErrCode = re_comp_t(&m_preCompiled, pszPattern);
    ConvertError(nErrCode);

    try
    {
        nErrCode = re_nsubexp(m_preCompiled);
        ConvertError(nErrCode);
        
        m_arMatches.resize(nErrCode);
        ResetMatches();
    }
    catch(...)
    {
        re_free(m_preCompiled);
        throw;
    }
}

CRegExp::~CRegExp()
{
    re_free(m_preCompiled);
}

BOOL CRegExp::Exec(const CString& sMatch)
{
    m_sMatch = sMatch;
    ResetMatches();
    int nErrCode = re_exec_t(m_preCompiled,
                             (LPCTSTR)m_sMatch,
                             m_arMatches.size(),
                             &m_arMatches[0]);
    ConvertError(nErrCode);
    return nErrCode > 0 ? TRUE : FALSE;
}

BOOL CRegExp::IsMatched(int nSubExp) const
{
    return (nSubExp < m_arMatches.size() &&
            m_arMatches[nSubExp].begin != -1) ?
        TRUE : FALSE;
}

int CRegExp::GetMatchStart(int nSubExp) const
{
    return nSubExp >= m_arMatches.size() ?
        -1 :
        m_arMatches[nSubExp].begin;
}

int CRegExp::GetMatchEnd(int nSubExp) const
{
    return nSubExp >= m_arMatches.size() ?
        -1 :
        m_arMatches[nSubExp].end;
}

CString CRegExp::GetMatch(int nSubExp) const
{
    if(nSubExp >= m_arMatches.size())
        return CString();

    regmatch rmMatch = m_arMatches[nSubExp];
    
    if(rmMatch.begin == -1 || rmMatch.end == -1)
        return CString();

    return m_sMatch.Mid(rmMatch.begin,
                        rmMatch.end - rmMatch.begin);
}

int CRegExp::GetNumberOfMatches() const
{
	return m_arMatches.size();
}

void CRegExp::ResetMatches()
{
    regmatch rmDummy;
    rmDummy.begin = rmDummy.end = -1;
    int nSize = m_arMatches.size();

    for(int nIndex = 0; nIndex < nSize; ++nIndex)
        m_arMatches[nIndex] = rmDummy;
}

void CRegExp::ConvertError(int nErrorCode) const
{
    if(nErrorCode < 0)
        throw CRegExpException(nErrorCode);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
I'm a senior software developer, working at Silanis Technology (http://www.silanis.com). I've acquired quite a bit of experience (usually the hard way!) in Win32 and raw COM programming on the job. In my spare time, I like to monkey around with POSIX code.

I'm mostly interested in portable C++ libraries. I'm happiest when I develop portable C++ code--C++ being such a powerful language as long as one keeps clear of the rather nasty subtleties of the language.

I hope the articles I contribute will be of some help to someone. If even one person gains a few hours through use of that code, I'll be very happy.

When not coding, I like to listen to Anime and try to learn Japanese. It's not working too well so far, unfortunately. :{)

Comments and Discussions