#define CPPCMS_SOURCE
#include "regex.h"
#include "config.h"
#ifdef CPPCMS_USE_EXTERNAL_BOOST
#   include <boost/regex.hpp>
#else // Internal Boost
#   include <cppcms_boost/regex.hpp>
    namespace boost = cppcms_boost;
#endif

namespace cppcms { namespace util {

	struct regex_result::data {
		std::string str;
		boost::cmatch match;
	};
	regex_result::regex_result() : d(new data)
	{
	}
	regex_result::~regex_result()
	{
	}
	std::string regex_result::operator[](int n)
	{
		return d->match[n];
	}

	struct regex::data {
		boost::regex r;
		data(std::string const &e) : r(e) {}
	};

	regex::regex(std::string const &e) : d(new data(e))
	{
	}
	regex::~regex()
	{
	}
	bool regex::match(std::string const &str,regex_result &res) const
	{
		//
		// Make sure that cmatch is valid, even if original string already is not
		// 
		res.d->str = str;
		return boost::regex_match(res.d->str.c_str(),res.d->match,d->r);
	}
	bool regex::match(std::string const &str) const
	{
		return boost::regex_match(str.c_str(),d->r);
	}


}} // cppcms::util
