Click here to Skip to main content
15,881,709 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 "stdregexp.hpp"
#include <algorithm>

#ifdef REGEXP_UNICODE 

#define re_comp_t re_comp_w
#define re_exec_t re_exec_w

#else

#define re_comp_t re_comp
#define re_exec_t re_exec

#endif

////////////////////////////////////////////////////////////////
// regular_expression_error
//

regular_expression_error::regular_expression_error(int error_code, regexp* re) :
    std::runtime_error("regular_expression_error"),
	code_(error_code)
{
	std::vector<char> buffer(128);
	re_error(code_, re, &buffer[0], buffer.size());
	message_.assign(&buffer[0]);
}

int regular_expression_error::code() const
{
	return code_;
}

const char* regular_expression_error::message() const
{
	return message_.c_str();
}

////////////////////////////////////////////////////////////////
// regular_expression
//

// regular_expression::regexp_wrapper
regular_expression::regexp_wrapper::regexp_wrapper() : wrapped_(NULL)
{}

regular_expression::regexp_wrapper::~regexp_wrapper()
{
	if(wrapped_)
		re_free(wrapped_);
}

regexp* regular_expression::regexp_wrapper::get()
{
	return wrapped_;
}

regexp** regular_expression::regexp_wrapper::operator& ()
{
	return &wrapped_;
}

// internal utility
namespace
{
	void convert_error(int error_code, regexp* re)
	{
		if(error_code < 0)
			throw regular_expression_error(error_code, re);
	}
}

regular_expression::regular_expression(const CharT* pattern)
{
	common_init(pattern);
}

regular_expression::regular_expression(const string_type& pattern)
{
	common_init(pattern.c_str());
}

void regular_expression::common_init(const CharT* pattern)
{
	int error_code = re_comp_t(&compiled_, pattern);
	convert_error(error_code, compiled_.get());

	error_code = re_nsubexp(compiled_.get());
	convert_error(error_code, compiled_.get());

	matches_.resize(error_code);
	reset();
}

void regular_expression::reset()
{
	regmatch dummy;
	dummy.begin = dummy.end = -1;

	std::fill(matches_.begin(), matches_.end(), dummy);
}

regular_expression::~regular_expression()
{
	// handled by regex_wrapper
}

bool regular_expression::exec(const CharT* match)
{
	return exec(string_type(match));
}

bool regular_expression::exec(const string_type& match)
{
	match_ = match;
	reset();
	int error_code = re_exec_t(compiled_.get(),
							   match_.c_str(),
							   matches_.size(),
							   &matches_[0]);
	convert_error(error_code, compiled_.get());
	return error_code > 0;
}

bool regular_expression::matched(size_type sub_exp) const
{
	return (sub_exp < matches_.size() &&
			matches_[sub_exp].begin != -1);
}

regular_expression::const_iterator
regular_expression::begin(size_type sub_exp) const
{
	return (sub_exp >= matches_.size() || matches_[sub_exp].begin == -1) ?
		match_.end() :
		(match_.begin() + matches_[sub_exp].begin);
}

regular_expression::const_iterator
regular_expression::end(size_type sub_exp) const
{
	return (sub_exp >= matches_.size() || matches_[sub_exp].end == -1) ?
		match_.end() :
		(match_.begin() + matches_[sub_exp].end);
}

regular_expression::string_type
regular_expression::operator[] (size_type sub_exp) const
{
	return string_type(begin(sub_exp), end(sub_exp));
}

regular_expression::size_type regular_expression::size() const
{
	return matches_.size();
}

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