Click here to Skip to main content
15,881,248 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.3K   3.4K   67  
A small, Unicode-aware regular expression engine based on Henry Spencer's early work
#include "regexp_int.h"
#include "regexp_custom.h"
#include <stdlib.h>
#include <ctype.h>

/* default unicode routines.  They use the normal C runtime. */
/* Also contains an isblank() implementation if one isn't available */
#ifdef REGEXP_UNICODE
CHAR_TYPE* re_ansi_to_unicode(const char* s)
{
    size_t sz;
    CHAR_TYPE* data;
    
    sz = mbstowcs(NULL, s, 0);
    if(sz == (size_t)-1)
        return NULL;
    
    data = re_malloc((sz + 1) * sizeof(CHAR_TYPE));
    if(!data)
        return NULL;
    mbstowcs(data, s, sz + 1);

    return data;
}

char* re_unicode_to_ansi(const CHAR_TYPE* s)
{
    size_t sz;
    char* data;

    sz = wcstombs(NULL, s, 0);
    if(sz == (size_t)-1)
        return NULL;

    data = re_malloc((sz + 1) * sizeof(char));
    if(!data)
        return NULL;
    wcstombs(data, s, sz + 1);
    
    return data;
}

#ifndef isblank
int iswblank(wint_t wc)
{
    /* cheap implementation */
    return wc == L'\t' || wc == L' ';
}
#endif

#else

#ifndef isblank
int isblank(int c)
{
    return c == '\t' || c == ' ';
}
#endif

#endif

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