#ifndef RE_H
#define RE_H

#include "sgx/core/stl/stl.hpp"
#include "sgx/core/regex/pcre/pcre.h"

class Regexp
{
public:

	class exception : public std::runtime_error
	{
	public:
		exception(const std::string & msg) : runtime_error(msg) { }
	};

	enum // bits de opciones
	{
		anchored =	PCRE_ANCHORED,
		caseless =	PCRE_CASELESS,
		dollarend =	PCRE_DOLLAR_ENDONLY,
		dotall =	PCRE_DOTALL,
		extended =	PCRE_EXTENDED,
		multiline =	PCRE_MULTILINE,
		ungreedy =	PCRE_UNGREEDY
	};

	Regexp(unsigned opts = 0);
	Regexp(const std::string & s, unsigned opts = 0);
	const Regexp & operator = (const std::string & s);
	Regexp(const char * s, unsigned opts = 0);
	const Regexp & operator = (const char * s);
	Regexp(const Regexp & r);
	const Regexp & operator = (const Regexp & r);
	~Regexp();

	typedef std::pair<int, int> markers;

	unsigned				options();
	void					options(unsigned opts);
	std::vector<markers>			match(const std::string & s, unsigned offset = 0) const;
	std::vector<markers>			gmatch(const std::string & s) const;
	static std::string				substr(const std::string & s, const std::vector<markers> & marks, unsigned index);
	static std::vector<std::string>	substr(const std::string & s, const std::vector<markers> & marks);
	std::vector<std::string>		split(const std::string & s, bool emptyfields = true, unsigned int maxfields = 0) const;
	std::vector<std::string>	 	split(const std::string & s, unsigned int maxfields, bool emptyfields = true) const;

private:
	pcre * re;
	unsigned _opts;

	void compile(const std::string & s);
	static pcre * clone_re(pcre * re);

	friend class sgxRegex;
};

#endif
